1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/dir.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * nfs directory handling functions 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * 10 Apr 1996 Added silly rename for unlink --okir 101da177e4SLinus Torvalds * 28 Sep 1996 Improved directory cache --okir 111da177e4SLinus Torvalds * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de 121da177e4SLinus Torvalds * Re-implemented silly rename for unlink, newly implemented 131da177e4SLinus Torvalds * silly rename for nfs_rename() following the suggestions 141da177e4SLinus Torvalds * of Olaf Kirch (okir) found in this file. 151da177e4SLinus Torvalds * Following Linus comments on my original hack, this version 161da177e4SLinus Torvalds * depends only on the dcache stuff and doesn't touch the inode 171da177e4SLinus Torvalds * layer (iput() and friends). 181da177e4SLinus Torvalds * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 21ddda8e0aSBryan Schumaker #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/errno.h> 241da177e4SLinus Torvalds #include <linux/stat.h> 251da177e4SLinus Torvalds #include <linux/fcntl.h> 261da177e4SLinus Torvalds #include <linux/string.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/slab.h> 291da177e4SLinus Torvalds #include <linux/mm.h> 301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 311da177e4SLinus Torvalds #include <linux/nfs_fs.h> 321da177e4SLinus Torvalds #include <linux/nfs_mount.h> 331da177e4SLinus Torvalds #include <linux/pagemap.h> 34873101b3SChuck Lever #include <linux/pagevec.h> 351da177e4SLinus Torvalds #include <linux/namei.h> 3654ceac45SDavid Howells #include <linux/mount.h> 37a0b8cab3SMel Gorman #include <linux/swap.h> 38e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include "delegation.h" 4391d5b470SChuck Lever #include "iostat.h" 444c30d56eSAdrian Bunk #include "internal.h" 45cd9a1c0eSTrond Myklebust #include "fscache.h" 461da177e4SLinus Torvalds 47f4ce1299STrond Myklebust #include "nfstrace.h" 48f4ce1299STrond Myklebust 491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 571da177e4SLinus Torvalds 584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 59f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 601da177e4SLinus Torvalds .read = generic_read_dir, 6193a6ab7bSTrond Myklebust .iterate_shared = nfs_readdir, 621da177e4SLinus Torvalds .open = nfs_opendir, 63480c2006SBryan Schumaker .release = nfs_closedir, 641da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 651da177e4SLinus Torvalds }; 661da177e4SLinus Torvalds 6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6811de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 69d1bacf9eSBryan Schumaker }; 70d1bacf9eSBryan Schumaker 71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred) 72480c2006SBryan Schumaker { 73311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 74480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 75480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 76480c2006SBryan Schumaker if (ctx != NULL) { 778ef2ce3eSBryan Schumaker ctx->duped = 0; 78311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 79480c2006SBryan Schumaker ctx->dir_cookie = 0; 808ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 81684f39b4SNeilBrown ctx->cred = get_cred(cred); 82311324adSTrond Myklebust spin_lock(&dir->i_lock); 831c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 841c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 851c341b77STrond Myklebust nfsi->cache_validity |= NFS_INO_INVALID_DATA | 861c341b77STrond Myklebust NFS_INO_REVAL_FORCED; 87311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 88311324adSTrond Myklebust spin_unlock(&dir->i_lock); 89480c2006SBryan Schumaker return ctx; 90480c2006SBryan Schumaker } 910c030806STrond Myklebust return ERR_PTR(-ENOMEM); 920c030806STrond Myklebust } 93480c2006SBryan Schumaker 94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 95480c2006SBryan Schumaker { 96311324adSTrond Myklebust spin_lock(&dir->i_lock); 97311324adSTrond Myklebust list_del(&ctx->list); 98311324adSTrond Myklebust spin_unlock(&dir->i_lock); 99684f39b4SNeilBrown put_cred(ctx->cred); 100480c2006SBryan Schumaker kfree(ctx); 101480c2006SBryan Schumaker } 102480c2006SBryan Schumaker 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * Open file 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds static int 1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1081da177e4SLinus Torvalds { 109480c2006SBryan Schumaker int res = 0; 110480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1111da177e4SLinus Torvalds 1126de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 113cc0dd2d1SChuck Lever 114cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1151e7cb3dcSChuck Lever 116684f39b4SNeilBrown ctx = alloc_nfs_open_dir_context(inode, current_cred()); 117480c2006SBryan Schumaker if (IS_ERR(ctx)) { 118480c2006SBryan Schumaker res = PTR_ERR(ctx); 119480c2006SBryan Schumaker goto out; 120480c2006SBryan Schumaker } 121480c2006SBryan Schumaker filp->private_data = ctx; 122480c2006SBryan Schumaker out: 1231da177e4SLinus Torvalds return res; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126480c2006SBryan Schumaker static int 127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 128480c2006SBryan Schumaker { 129a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 130480c2006SBryan Schumaker return 0; 131480c2006SBryan Schumaker } 132480c2006SBryan Schumaker 133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 134d1bacf9eSBryan Schumaker u64 cookie; 135d1bacf9eSBryan Schumaker u64 ino; 136d1bacf9eSBryan Schumaker struct qstr string; 1370b26a0bfSTrond Myklebust unsigned char d_type; 138d1bacf9eSBryan Schumaker }; 139d1bacf9eSBryan Schumaker 140d1bacf9eSBryan Schumaker struct nfs_cache_array { 14188b8e133SChuck Lever int size; 142d1bacf9eSBryan Schumaker int eof_index; 143d1bacf9eSBryan Schumaker u64 last_cookie; 144d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 145d1bacf9eSBryan Schumaker }; 146d1bacf9eSBryan Schumaker 147a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool); 1481da177e4SLinus Torvalds typedef struct { 1491da177e4SLinus Torvalds struct file *file; 1501da177e4SLinus Torvalds struct page *page; 15123db8620SAl Viro struct dir_context *ctx; 1521da177e4SLinus Torvalds unsigned long page_index; 153f0dd2136STrond Myklebust u64 *dir_cookie; 1540aded708STrond Myklebust u64 last_cookie; 155f0dd2136STrond Myklebust loff_t current_index; 1561da177e4SLinus Torvalds decode_dirent_t decode; 157d1bacf9eSBryan Schumaker 158*a1147b82STrond Myklebust unsigned long dir_verifier; 1591f4eab7eSNeil Brown unsigned long timestamp; 1604704f0e2STrond Myklebust unsigned long gencount; 161d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 162a7a3b1e9SBenjamin Coddington bool plus; 163a7a3b1e9SBenjamin Coddington bool eof; 1641da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1651da177e4SLinus Torvalds 1664b310319STrond Myklebust static 1674b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 1684b310319STrond Myklebust { 1694b310319STrond Myklebust struct nfs_cache_array *array; 1704b310319STrond Myklebust 1714b310319STrond Myklebust array = kmap_atomic(page); 1724b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1734b310319STrond Myklebust array->eof_index = -1; 1744b310319STrond Myklebust kunmap_atomic(array); 1754b310319STrond Myklebust } 1764b310319STrond Myklebust 177d1bacf9eSBryan Schumaker /* 178d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 179d1bacf9eSBryan Schumaker */ 180d1bacf9eSBryan Schumaker static 18111de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 182d1bacf9eSBryan Schumaker { 18311de3b11STrond Myklebust struct nfs_cache_array *array; 184d1bacf9eSBryan Schumaker int i; 1858cd51a0cSTrond Myklebust 1862b86ce2dSCong Wang array = kmap_atomic(page); 187d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 188d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1894b310319STrond Myklebust array->size = 0; 1902b86ce2dSCong Wang kunmap_atomic(array); 191d1bacf9eSBryan Schumaker } 192d1bacf9eSBryan Schumaker 193d1bacf9eSBryan Schumaker /* 194d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 195d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 196d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 197d1bacf9eSBryan Schumaker */ 198d1bacf9eSBryan Schumaker static 1994a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 200d1bacf9eSBryan Schumaker { 201d1bacf9eSBryan Schumaker string->len = len; 2023803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2034a201d6eSTrond Myklebust if (string->name == NULL) 2044a201d6eSTrond Myklebust return -ENOMEM; 20504e4bd1cSCatalin Marinas /* 20604e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 20704e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 20804e4bd1cSCatalin Marinas */ 20904e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2108387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2114a201d6eSTrond Myklebust return 0; 212d1bacf9eSBryan Schumaker } 213d1bacf9eSBryan Schumaker 214d1bacf9eSBryan Schumaker static 215d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 216d1bacf9eSBryan Schumaker { 2170795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2184a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2194a201d6eSTrond Myklebust int ret; 2204a201d6eSTrond Myklebust 2214a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2223020093fSTrond Myklebust 2233020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2243020093fSTrond Myklebust ret = -ENOSPC; 2253020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2263020093fSTrond Myklebust goto out; 2273020093fSTrond Myklebust 2284a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2294a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2300b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2314a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2324a201d6eSTrond Myklebust if (ret) 2334a201d6eSTrond Myklebust goto out; 234d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2358cd51a0cSTrond Myklebust array->size++; 23647c716cbSTrond Myklebust if (entry->eof != 0) 237d1bacf9eSBryan Schumaker array->eof_index = array->size; 2384a201d6eSTrond Myklebust out: 2390795bf83SFabian Frederick kunmap(page); 2404a201d6eSTrond Myklebust return ret; 241d1bacf9eSBryan Schumaker } 242d1bacf9eSBryan Schumaker 243d1bacf9eSBryan Schumaker static 244d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 245d1bacf9eSBryan Schumaker { 24623db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 247d1bacf9eSBryan Schumaker unsigned int index; 248d1bacf9eSBryan Schumaker 249d1bacf9eSBryan Schumaker if (diff < 0) 250d1bacf9eSBryan Schumaker goto out_eof; 251d1bacf9eSBryan Schumaker if (diff >= array->size) { 2528cd51a0cSTrond Myklebust if (array->eof_index >= 0) 253d1bacf9eSBryan Schumaker goto out_eof; 254d1bacf9eSBryan Schumaker return -EAGAIN; 255d1bacf9eSBryan Schumaker } 256d1bacf9eSBryan Schumaker 257d1bacf9eSBryan Schumaker index = (unsigned int)diff; 258d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 259d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 260d1bacf9eSBryan Schumaker return 0; 261d1bacf9eSBryan Schumaker out_eof: 2626089dd0dSThomas Meyer desc->eof = true; 263d1bacf9eSBryan Schumaker return -EBADCOOKIE; 264d1bacf9eSBryan Schumaker } 265d1bacf9eSBryan Schumaker 2664db72b40SJeff Layton static bool 2674db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2684db72b40SJeff Layton { 2694db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2704db72b40SJeff Layton return false; 2714db72b40SJeff Layton smp_rmb(); 2724db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2734db72b40SJeff Layton } 2744db72b40SJeff Layton 275d1bacf9eSBryan Schumaker static 276d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 277d1bacf9eSBryan Schumaker { 278d1bacf9eSBryan Schumaker int i; 2798ef2ce3eSBryan Schumaker loff_t new_pos; 280d1bacf9eSBryan Schumaker int status = -EAGAIN; 281d1bacf9eSBryan Schumaker 282d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2838cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 284496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2850c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2860c030806STrond Myklebust 2878ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2884db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 2894db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 2900c030806STrond Myklebust ctx->duped = 0; 2910c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 29223db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2930c030806STrond Myklebust if (ctx->duped > 0 2940c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2950c030806STrond Myklebust if (printk_ratelimit()) { 2966de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 2970c030806STrond Myklebust "Please contact your server vendor. " 2989581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 2999581a4aeSJeff Layton desc->file, array->array[i].string.len, 3009581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3010c030806STrond Myklebust } 3020c030806STrond Myklebust status = -ELOOP; 3030c030806STrond Myklebust goto out; 3040c030806STrond Myklebust } 3058ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3060c030806STrond Myklebust ctx->duped = -1; 3078ef2ce3eSBryan Schumaker } 30823db8620SAl Viro desc->ctx->pos = new_pos; 3098cd51a0cSTrond Myklebust desc->cache_entry_index = i; 31047c716cbSTrond Myklebust return 0; 3118cd51a0cSTrond Myklebust } 3128cd51a0cSTrond Myklebust } 31347c716cbSTrond Myklebust if (array->eof_index >= 0) { 314d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 31518fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 3166089dd0dSThomas Meyer desc->eof = true; 317d1bacf9eSBryan Schumaker } 3180c030806STrond Myklebust out: 319d1bacf9eSBryan Schumaker return status; 320d1bacf9eSBryan Schumaker } 321d1bacf9eSBryan Schumaker 322d1bacf9eSBryan Schumaker static 323d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 324d1bacf9eSBryan Schumaker { 325d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 32647c716cbSTrond Myklebust int status; 327d1bacf9eSBryan Schumaker 3280795bf83SFabian Frederick array = kmap(desc->page); 329d1bacf9eSBryan Schumaker 330d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 331d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 332d1bacf9eSBryan Schumaker else 333d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 334d1bacf9eSBryan Schumaker 33547c716cbSTrond Myklebust if (status == -EAGAIN) { 3360aded708STrond Myklebust desc->last_cookie = array->last_cookie; 337e47c085aSTrond Myklebust desc->current_index += array->size; 33847c716cbSTrond Myklebust desc->page_index++; 33947c716cbSTrond Myklebust } 3400795bf83SFabian Frederick kunmap(desc->page); 341d1bacf9eSBryan Schumaker return status; 342d1bacf9eSBryan Schumaker } 343d1bacf9eSBryan Schumaker 344d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 345d1bacf9eSBryan Schumaker static 34656e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 347d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 348d1bacf9eSBryan Schumaker { 349480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 350684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3514704f0e2STrond Myklebust unsigned long timestamp, gencount; 3521da177e4SLinus Torvalds int error; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds again: 3551da177e4SLinus Torvalds timestamp = jiffies; 3564704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 357*a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 358be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3591da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3601da177e4SLinus Torvalds if (error < 0) { 3611da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3621da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3631da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3643a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 365a7a3b1e9SBenjamin Coddington desc->plus = false; 3661da177e4SLinus Torvalds goto again; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds goto error; 3691da177e4SLinus Torvalds } 3701f4eab7eSNeil Brown desc->timestamp = timestamp; 3714704f0e2STrond Myklebust desc->gencount = gencount; 372d1bacf9eSBryan Schumaker error: 373d1bacf9eSBryan Schumaker return error; 374d1bacf9eSBryan Schumaker } 375d1bacf9eSBryan Schumaker 376573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 377573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 378d1bacf9eSBryan Schumaker { 379573c4e1eSChuck Lever int error; 380d1bacf9eSBryan Schumaker 381573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 382573c4e1eSChuck Lever if (error) 383573c4e1eSChuck Lever return error; 384d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 385d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 386d1bacf9eSBryan Schumaker return 0; 387d1bacf9eSBryan Schumaker } 388d1bacf9eSBryan Schumaker 389fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 390fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 391fa923369STrond Myklebust */ 392d39ab9deSBryan Schumaker static 393d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 394d39ab9deSBryan Schumaker { 395d8fdb47fSTrond Myklebust struct inode *inode; 396fa923369STrond Myklebust struct nfs_inode *nfsi; 397fa923369STrond Myklebust 3982b0143b5SDavid Howells if (d_really_is_negative(dentry)) 3992b0143b5SDavid Howells return 0; 400fa923369STrond Myklebust 401d8fdb47fSTrond Myklebust inode = d_inode(dentry); 402d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 403d8fdb47fSTrond Myklebust return 0; 404d8fdb47fSTrond Myklebust 405d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4067dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 407d39ab9deSBryan Schumaker return 0; 4087dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4097dc72d5fSTrond Myklebust return 0; 4107dc72d5fSTrond Myklebust return 1; 411d39ab9deSBryan Schumaker } 412d39ab9deSBryan Schumaker 413d39ab9deSBryan Schumaker static 41423db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 415d69ee9b8STrond Myklebust { 416d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 417d69ee9b8STrond Myklebust return false; 418d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 419d69ee9b8STrond Myklebust return true; 42023db8620SAl Viro if (ctx->pos == 0) 421d69ee9b8STrond Myklebust return true; 422d69ee9b8STrond Myklebust return false; 423d69ee9b8STrond Myklebust } 424d69ee9b8STrond Myklebust 425d69ee9b8STrond Myklebust /* 42663519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 42763519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 428d69ee9b8STrond Myklebust * directory. 429d69ee9b8STrond Myklebust */ 430d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 431d69ee9b8STrond Myklebust { 43263519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 43363519fbcSTrond Myklebust 43463519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 43563519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 43663519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 437d69ee9b8STrond Myklebust } 438d69ee9b8STrond Myklebust 439311324adSTrond Myklebust /* 440311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 441311324adSTrond Myklebust * 442311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 443311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 444311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 445311324adSTrond Myklebust * cache flush. 446311324adSTrond Myklebust */ 447311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 448311324adSTrond Myklebust { 44963519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 45063519fbcSTrond Myklebust 45163519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 45263519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 45363519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 454227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 455227823d2SDai Ngo nfsi->page_index + 1, -1); 456311324adSTrond Myklebust } 457311324adSTrond Myklebust } 458311324adSTrond Myklebust 459d69ee9b8STrond Myklebust static 460*a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 461*a1147b82STrond Myklebust unsigned long dir_verifier) 462d39ab9deSBryan Schumaker { 46326fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4649ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4654a201d6eSTrond Myklebust struct dentry *dentry; 4664a201d6eSTrond Myklebust struct dentry *alias; 467d39ab9deSBryan Schumaker struct inode *inode; 468aa9c2669SDavid Quigley int status; 469d39ab9deSBryan Schumaker 470fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 471fa923369STrond Myklebust return; 4726c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4736c441c25STrond Myklebust return; 47478d04af4STrond Myklebust if (filename.len == 0) 47578d04af4STrond Myklebust return; 47678d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 47778d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 47878d04af4STrond Myklebust return; 47978d04af4STrond Myklebust /* ...or '/' */ 48078d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 48178d04af4STrond Myklebust return; 4824a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4834a201d6eSTrond Myklebust if (filename.len == 1) 4844a201d6eSTrond Myklebust return; 4854a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4864a201d6eSTrond Myklebust return; 4874a201d6eSTrond Myklebust } 4888387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 489d39ab9deSBryan Schumaker 4904a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 4919ac3d3e8SAl Viro again: 4929ac3d3e8SAl Viro if (!dentry) { 4939ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 4949ac3d3e8SAl Viro if (IS_ERR(dentry)) 4959ac3d3e8SAl Viro return; 4969ac3d3e8SAl Viro } 4979ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 4986c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4996c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 5006c441c25STrond Myklebust &entry->fattr->fsid)) 5016c441c25STrond Myklebust goto out; 502d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5037dc72d5fSTrond Myklebust if (!entry->fh->size) 5047dc72d5fSTrond Myklebust goto out; 505*a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 5062b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 507aa9c2669SDavid Quigley if (!status) 5082b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 509d39ab9deSBryan Schumaker goto out; 510d39ab9deSBryan Schumaker } else { 5115542aa2fSEric W. Biederman d_invalidate(dentry); 512d39ab9deSBryan Schumaker dput(dentry); 5139ac3d3e8SAl Viro dentry = NULL; 5149ac3d3e8SAl Viro goto again; 515d39ab9deSBryan Schumaker } 516d39ab9deSBryan Schumaker } 5177dc72d5fSTrond Myklebust if (!entry->fh->size) { 5187dc72d5fSTrond Myklebust d_lookup_done(dentry); 5197dc72d5fSTrond Myklebust goto out; 5207dc72d5fSTrond Myklebust } 521d39ab9deSBryan Schumaker 5221775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 52341d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5249ac3d3e8SAl Viro d_lookup_done(dentry); 5259ac3d3e8SAl Viro if (alias) { 526d39ab9deSBryan Schumaker if (IS_ERR(alias)) 527d39ab9deSBryan Schumaker goto out; 5289ac3d3e8SAl Viro dput(dentry); 5299ac3d3e8SAl Viro dentry = alias; 5309ac3d3e8SAl Viro } 531*a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 532d39ab9deSBryan Schumaker out: 533d39ab9deSBryan Schumaker dput(dentry); 534d39ab9deSBryan Schumaker } 535d39ab9deSBryan Schumaker 536d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 537d1bacf9eSBryan Schumaker static 5388cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5396650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 540d1bacf9eSBryan Schumaker { 541babddc72SBryan Schumaker struct xdr_stream stream; 542f7da7a12SBenny Halevy struct xdr_buf buf; 5436650239aSTrond Myklebust struct page *scratch; 54499424380SBryan Schumaker struct nfs_cache_array *array; 5455c346854STrond Myklebust unsigned int count = 0; 5465c346854STrond Myklebust int status; 547babddc72SBryan Schumaker 5486650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5496650239aSTrond Myklebust if (scratch == NULL) 5506650239aSTrond Myklebust return -ENOMEM; 551babddc72SBryan Schumaker 552ce85cfbeSBenjamin Coddington if (buflen == 0) 553ce85cfbeSBenjamin Coddington goto out_nopages; 554ce85cfbeSBenjamin Coddington 555f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5566650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 55799424380SBryan Schumaker 55899424380SBryan Schumaker do { 55999424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5608cd51a0cSTrond Myklebust if (status != 0) { 5618cd51a0cSTrond Myklebust if (status == -EAGAIN) 5628cd51a0cSTrond Myklebust status = 0; 56399424380SBryan Schumaker break; 5648cd51a0cSTrond Myklebust } 56599424380SBryan Schumaker 5665c346854STrond Myklebust count++; 5675c346854STrond Myklebust 568a7a3b1e9SBenjamin Coddington if (desc->plus) 569*a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 570*a1147b82STrond Myklebust desc->dir_verifier); 5718cd51a0cSTrond Myklebust 572db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 5738cd51a0cSTrond Myklebust if (status != 0) 5748cd51a0cSTrond Myklebust break; 57599424380SBryan Schumaker } while (!entry->eof); 57699424380SBryan Schumaker 577ce85cfbeSBenjamin Coddington out_nopages: 57847c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 579db531db9SMax Kellermann array = kmap(page); 5808cd51a0cSTrond Myklebust array->eof_index = array->size; 58199424380SBryan Schumaker status = 0; 582db531db9SMax Kellermann kunmap(page); 58356e4ebf8SBryan Schumaker } 5846650239aSTrond Myklebust 5856650239aSTrond Myklebust put_page(scratch); 5868cd51a0cSTrond Myklebust return status; 5878cd51a0cSTrond Myklebust } 58856e4ebf8SBryan Schumaker 58956e4ebf8SBryan Schumaker static 590c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 59156e4ebf8SBryan Schumaker { 59256e4ebf8SBryan Schumaker unsigned int i; 59356e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 59456e4ebf8SBryan Schumaker put_page(pages[i]); 59556e4ebf8SBryan Schumaker } 59656e4ebf8SBryan Schumaker 59756e4ebf8SBryan Schumaker /* 598bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 599bf211ca1Szhangliguang * to nfs_readdir_free_pages() 60056e4ebf8SBryan Schumaker */ 60156e4ebf8SBryan Schumaker static 602c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 60356e4ebf8SBryan Schumaker { 60456e4ebf8SBryan Schumaker unsigned int i; 60556e4ebf8SBryan Schumaker 60656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 60756e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 60856e4ebf8SBryan Schumaker if (page == NULL) 60956e4ebf8SBryan Schumaker goto out_freepages; 61056e4ebf8SBryan Schumaker pages[i] = page; 61156e4ebf8SBryan Schumaker } 6126650239aSTrond Myklebust return 0; 61356e4ebf8SBryan Schumaker 61456e4ebf8SBryan Schumaker out_freepages: 615c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6166650239aSTrond Myklebust return -ENOMEM; 617d1bacf9eSBryan Schumaker } 618d1bacf9eSBryan Schumaker 619d1bacf9eSBryan Schumaker static 620d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 621d1bacf9eSBryan Schumaker { 62256e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 623d1bacf9eSBryan Schumaker struct nfs_entry entry; 624d1bacf9eSBryan Schumaker struct file *file = desc->file; 625d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6268cd51a0cSTrond Myklebust int status = -ENOMEM; 62756e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 628d1bacf9eSBryan Schumaker 6294b310319STrond Myklebust nfs_readdir_init_array(page); 6304b310319STrond Myklebust 631d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6320aded708STrond Myklebust entry.cookie = desc->last_cookie; 633d1bacf9eSBryan Schumaker entry.eof = 0; 634d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 635d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 636573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 637d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 638d1bacf9eSBryan Schumaker goto out; 639d1bacf9eSBryan Schumaker 64014c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 64114c43f76SDavid Quigley if (IS_ERR(entry.label)) { 64214c43f76SDavid Quigley status = PTR_ERR(entry.label); 64314c43f76SDavid Quigley goto out; 64414c43f76SDavid Quigley } 64514c43f76SDavid Quigley 6460795bf83SFabian Frederick array = kmap(page); 647d1bacf9eSBryan Schumaker 648c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6496650239aSTrond Myklebust if (status < 0) 650d1bacf9eSBryan Schumaker goto out_release_array; 651d1bacf9eSBryan Schumaker do { 652ac396128STrond Myklebust unsigned int pglen; 65356e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 654babddc72SBryan Schumaker 655d1bacf9eSBryan Schumaker if (status < 0) 656d1bacf9eSBryan Schumaker break; 657ac396128STrond Myklebust pglen = status; 6586650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6598cd51a0cSTrond Myklebust if (status < 0) { 6608cd51a0cSTrond Myklebust if (status == -ENOSPC) 6618cd51a0cSTrond Myklebust status = 0; 6628cd51a0cSTrond Myklebust break; 6638cd51a0cSTrond Myklebust } 6648cd51a0cSTrond Myklebust } while (array->eof_index < 0); 665d1bacf9eSBryan Schumaker 666c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 667d1bacf9eSBryan Schumaker out_release_array: 6680795bf83SFabian Frederick kunmap(page); 66914c43f76SDavid Quigley nfs4_label_free(entry.label); 670d1bacf9eSBryan Schumaker out: 671d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 672d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 673d1bacf9eSBryan Schumaker return status; 674d1bacf9eSBryan Schumaker } 675d1bacf9eSBryan Schumaker 676d1bacf9eSBryan Schumaker /* 677d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 678d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 679d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 680d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6811da177e4SLinus Torvalds */ 682d1bacf9eSBryan Schumaker static 683a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 684d1bacf9eSBryan Schumaker { 685a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 686496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6878cd51a0cSTrond Myklebust int ret; 688d1bacf9eSBryan Schumaker 6898cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6908cd51a0cSTrond Myklebust if (ret < 0) 691d1bacf9eSBryan Schumaker goto error; 692d1bacf9eSBryan Schumaker SetPageUptodate(page); 693d1bacf9eSBryan Schumaker 6942aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 695cd9ae2b6STrond Myklebust /* Should never happen */ 696cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 697cd9ae2b6STrond Myklebust } 6981da177e4SLinus Torvalds unlock_page(page); 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds error: 7014b310319STrond Myklebust nfs_readdir_clear_array(page); 7021da177e4SLinus Torvalds unlock_page(page); 7038cd51a0cSTrond Myklebust return ret; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 706d1bacf9eSBryan Schumaker static 707d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7081da177e4SLinus Torvalds { 70909cbfeafSKirill A. Shutemov put_page(desc->page); 7101da177e4SLinus Torvalds desc->page = NULL; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 713d1bacf9eSBryan Schumaker static 714d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7151da177e4SLinus Torvalds { 716a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 717a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* 721d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 722114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7231da177e4SLinus Torvalds */ 724d1bacf9eSBryan Schumaker static 725114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 726d1bacf9eSBryan Schumaker { 727227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 728227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 729d1bacf9eSBryan Schumaker int res; 730d1bacf9eSBryan Schumaker 731d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 732d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 733d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 734114de382STrond Myklebust res = lock_page_killable(desc->page); 73547c716cbSTrond Myklebust if (res != 0) 736114de382STrond Myklebust goto error; 737114de382STrond Myklebust res = -EAGAIN; 738114de382STrond Myklebust if (desc->page->mapping != NULL) { 739114de382STrond Myklebust res = nfs_readdir_search_array(desc); 740227823d2SDai Ngo if (res == 0) { 741227823d2SDai Ngo nfsi->page_index = desc->page_index; 742114de382STrond Myklebust return 0; 743114de382STrond Myklebust } 744227823d2SDai Ngo } 745114de382STrond Myklebust unlock_page(desc->page); 746114de382STrond Myklebust error: 747d1bacf9eSBryan Schumaker cache_page_release(desc); 748d1bacf9eSBryan Schumaker return res; 749d1bacf9eSBryan Schumaker } 750d1bacf9eSBryan Schumaker 751d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7521da177e4SLinus Torvalds static inline 7531da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7541da177e4SLinus Torvalds { 7558cd51a0cSTrond Myklebust int res; 756d1bacf9eSBryan Schumaker 7570aded708STrond Myklebust if (desc->page_index == 0) { 7588cd51a0cSTrond Myklebust desc->current_index = 0; 7590aded708STrond Myklebust desc->last_cookie = 0; 7600aded708STrond Myklebust } 76147c716cbSTrond Myklebust do { 762114de382STrond Myklebust res = find_and_lock_cache_page(desc); 76347c716cbSTrond Myklebust } while (res == -EAGAIN); 7641da177e4SLinus Torvalds return res; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* 7681da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7691da177e4SLinus Torvalds */ 7701da177e4SLinus Torvalds static 77123db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds struct file *file = desc->file; 774d1bacf9eSBryan Schumaker int i = 0; 775d1bacf9eSBryan Schumaker int res = 0; 776d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7778ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7788ef2ce3eSBryan Schumaker 7790795bf83SFabian Frederick array = kmap(desc->page); 780d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 781ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7821da177e4SLinus Torvalds 783ece0b423STrond Myklebust ent = &array->array[i]; 78423db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 78523db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 7866089dd0dSThomas Meyer desc->eof = true; 7871da177e4SLinus Torvalds break; 788ece0b423STrond Myklebust } 78923db8620SAl Viro desc->ctx->pos++; 790d1bacf9eSBryan Schumaker if (i < (array->size-1)) 791d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 792d1bacf9eSBryan Schumaker else 793d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7940c030806STrond Myklebust if (ctx->duped != 0) 7950c030806STrond Myklebust ctx->duped = 1; 7968cd51a0cSTrond Myklebust } 79747c716cbSTrond Myklebust if (array->eof_index >= 0) 7986089dd0dSThomas Meyer desc->eof = true; 799d1bacf9eSBryan Schumaker 8000795bf83SFabian Frederick kunmap(desc->page); 8011e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8021e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8031da177e4SLinus Torvalds return res; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* 8071da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8081da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8091da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8101da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8111da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8121da177e4SLinus Torvalds * 8131da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8141da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8151da177e4SLinus Torvalds * we should already have a complete representation of the 8161da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8171da177e4SLinus Torvalds */ 8181da177e4SLinus Torvalds static inline 81923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct page *page = NULL; 8221da177e4SLinus Torvalds int status; 823496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8240c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8251da177e4SLinus Torvalds 8261e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8271e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8301da177e4SLinus Torvalds if (!page) { 8311da177e4SLinus Torvalds status = -ENOMEM; 8321da177e4SLinus Torvalds goto out; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8357a8e1dc3STrond Myklebust desc->page_index = 0; 8360aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8377a8e1dc3STrond Myklebust desc->page = page; 8380c030806STrond Myklebust ctx->duped = 0; 8397a8e1dc3STrond Myklebust 84085f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 84185f8607eSTrond Myklebust if (status < 0) 842d1bacf9eSBryan Schumaker goto out_release; 843d1bacf9eSBryan Schumaker 84423db8620SAl Viro status = nfs_do_filldir(desc); 8451da177e4SLinus Torvalds 846114de382STrond Myklebust out_release: 847114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 848114de382STrond Myklebust cache_page_release(desc); 8491da177e4SLinus Torvalds out: 8501e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8513110ff80SHarvey Harrison __func__, status); 8521da177e4SLinus Torvalds return status; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 85500a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 85600a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 85700a92642SOlivier Galibert whole directory. 8581da177e4SLinus Torvalds */ 85923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8601da177e4SLinus Torvalds { 861be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8622b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8631da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8641da177e4SLinus Torvalds *desc = &my_desc; 86523db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 86607b5ce8eSScott Mayhew int res = 0; 8671da177e4SLinus Torvalds 8686de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8696de1472fSAl Viro file, (long long)ctx->pos); 87091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 87191d5b470SChuck Lever 8721da177e4SLinus Torvalds /* 87323db8620SAl Viro * ctx->pos points to the dirent entry number. 874f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 87500a92642SOlivier Galibert * to either find the entry with the appropriate number or 87600a92642SOlivier Galibert * revalidate the cookie. 8771da177e4SLinus Torvalds */ 8781da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8791da177e4SLinus Torvalds 88023db8620SAl Viro desc->file = file; 88123db8620SAl Viro desc->ctx = ctx; 882480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8831da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 884a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 8851da177e4SLinus Torvalds 88679f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 88723db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 888fccca7fcSTrond Myklebust if (res < 0) 889fccca7fcSTrond Myklebust goto out; 890fccca7fcSTrond Myklebust 89147c716cbSTrond Myklebust do { 8921da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 89300a92642SOlivier Galibert 8941da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 895ece0b423STrond Myklebust res = 0; 8961da177e4SLinus Torvalds /* This means either end of directory */ 8976089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 8981da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 89923db8620SAl Viro res = uncached_readdir(desc); 900ece0b423STrond Myklebust if (res == 0) 9011da177e4SLinus Torvalds continue; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds break; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9063a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9071da177e4SLinus Torvalds nfs_zap_caches(inode); 908baf57a09STrond Myklebust desc->page_index = 0; 909a7a3b1e9SBenjamin Coddington desc->plus = false; 910a7a3b1e9SBenjamin Coddington desc->eof = false; 9111da177e4SLinus Torvalds continue; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds if (res < 0) 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds 91623db8620SAl Viro res = nfs_do_filldir(desc); 917114de382STrond Myklebust unlock_page(desc->page); 918114de382STrond Myklebust cache_page_release(desc); 919ece0b423STrond Myklebust if (res < 0) 9201da177e4SLinus Torvalds break; 92147c716cbSTrond Myklebust } while (!desc->eof); 922fccca7fcSTrond Myklebust out: 9231e7cb3dcSChuck Lever if (res > 0) 9241e7cb3dcSChuck Lever res = 0; 9256de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9261da177e4SLinus Torvalds return res; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 929965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 930f0dd2136STrond Myklebust { 931b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 932480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 933b84e06c5SChuck Lever 9346de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9356de1472fSAl Viro filp, offset, whence); 936b84e06c5SChuck Lever 937965c8e59SAndrew Morton switch (whence) { 938f0dd2136STrond Myklebust default: 939b2b1ff3dSTrond Myklebust return -EINVAL; 940b2b1ff3dSTrond Myklebust case SEEK_SET: 941b2b1ff3dSTrond Myklebust if (offset < 0) 942b2b1ff3dSTrond Myklebust return -EINVAL; 943b2b1ff3dSTrond Myklebust inode_lock(inode); 944b2b1ff3dSTrond Myklebust break; 945b2b1ff3dSTrond Myklebust case SEEK_CUR: 946b2b1ff3dSTrond Myklebust if (offset == 0) 947b2b1ff3dSTrond Myklebust return filp->f_pos; 948b2b1ff3dSTrond Myklebust inode_lock(inode); 949b2b1ff3dSTrond Myklebust offset += filp->f_pos; 950b2b1ff3dSTrond Myklebust if (offset < 0) { 951b2b1ff3dSTrond Myklebust inode_unlock(inode); 952b2b1ff3dSTrond Myklebust return -EINVAL; 953b2b1ff3dSTrond Myklebust } 954f0dd2136STrond Myklebust } 955f0dd2136STrond Myklebust if (offset != filp->f_pos) { 956f0dd2136STrond Myklebust filp->f_pos = offset; 957480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9588ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 959f0dd2136STrond Myklebust } 960b044f645SBenjamin Coddington inode_unlock(inode); 961f0dd2136STrond Myklebust return offset; 962f0dd2136STrond Myklebust } 963f0dd2136STrond Myklebust 9641da177e4SLinus Torvalds /* 9651da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9661da177e4SLinus Torvalds * is a dummy operation. 9671da177e4SLinus Torvalds */ 96802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 96902c24a82SJosef Bacik int datasync) 9701da177e4SLinus Torvalds { 9716de1472fSAl Viro struct inode *inode = file_inode(filp); 9727ea80859SChristoph Hellwig 9736de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9741e7cb3dcSChuck Lever 9755955102cSAl Viro inode_lock(inode); 9766de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9775955102cSAl Viro inode_unlock(inode); 9781da177e4SLinus Torvalds return 0; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 981bfc69a45STrond Myklebust /** 982bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 983302fad7bSTrond Myklebust * @dir: pointer to directory inode 984bfc69a45STrond Myklebust * 985bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 986bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 987bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 988bfc69a45STrond Myklebust * 989bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 990bfc69a45STrond Myklebust */ 991bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 992bfc69a45STrond Myklebust { 993011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 994bfc69a45STrond Myklebust } 99589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 996bfc69a45STrond Myklebust 9971da177e4SLinus Torvalds /* 9981da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9991da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10001da177e4SLinus Torvalds * and may need to be looked up again. 1001912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 10021da177e4SLinus Torvalds */ 1003912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1004912a108dSNeilBrown int rcu_walk) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10071da177e4SLinus Torvalds return 1; 10084eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10094eec952eSTrond Myklebust return 0; 1010f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10116ecc5e8fSTrond Myklebust return 0; 1012f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 10131cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1014912a108dSNeilBrown if (rcu_walk) 1015f2c77f4eSTrond Myklebust return 0; 10161cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 10171cd9cb05STrond Myklebust return 0; 10181cd9cb05STrond Myklebust } 1019f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1020f2c77f4eSTrond Myklebust return 0; 1021f2c77f4eSTrond Myklebust return 1; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* 1025a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1026a12802caSTrond Myklebust * an O_EXCL create using this path component. 1027a12802caSTrond Myklebust */ 1028fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1029a12802caSTrond Myklebust { 1030a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1031a12802caSTrond Myklebust return 0; 1032fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1033a12802caSTrond Myklebust } 1034a12802caSTrond Myklebust 1035a12802caSTrond Myklebust /* 10361d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10371d6757fbSTrond Myklebust * 10381d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10391d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10401d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10411d6757fbSTrond Myklebust * 10421d6757fbSTrond Myklebust */ 104365a0c149STrond Myklebust static 1044fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10451da177e4SLinus Torvalds { 10461da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 104765a0c149STrond Myklebust int ret; 10481da177e4SLinus Torvalds 104936d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10504e99a1ffSTrond Myklebust return 0; 105147921921STrond Myklebust 105247921921STrond Myklebust if (flags & LOOKUP_OPEN) { 105347921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 105447921921STrond Myklebust case S_IFREG: 105547921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 105647921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 105747921921STrond Myklebust goto out; 105847921921STrond Myklebust /* Fallthrough */ 105947921921STrond Myklebust case S_IFDIR: 106047921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 106147921921STrond Myklebust break; 106247921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 106347921921STrond Myklebust goto out_force; 106447921921STrond Myklebust } 106547921921STrond Myklebust } 106647921921STrond Myklebust 10671da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1068fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10691da177e4SLinus Torvalds goto out_force; 107065a0c149STrond Myklebust out: 1071a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 10721da177e4SLinus Torvalds out_force: 10731fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10741fa1e384SNeilBrown return -ECHILD; 107565a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 107665a0c149STrond Myklebust if (ret != 0) 107765a0c149STrond Myklebust return ret; 107865a0c149STrond Myklebust goto out; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds /* 10821da177e4SLinus Torvalds * We judge how long we want to trust negative 10831da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10841da177e4SLinus Torvalds * 10851da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10861da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1087912a108dSNeilBrown * 1088912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1089912a108dSNeilBrown * suggesting a reval is needed. 10909f6d44d4STrond Myklebust * 10919f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 10929f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 10931da177e4SLinus Torvalds */ 10941da177e4SLinus Torvalds static inline 10951da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1096fa3c56bbSAl Viro unsigned int flags) 10971da177e4SLinus Torvalds { 10989f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 10991da177e4SLinus Torvalds return 0; 11004eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 11014eec952eSTrond Myklebust return 1; 1102912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11055ceb9d7fSTrond Myklebust static int 11065ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 11075ceb9d7fSTrond Myklebust struct inode *inode, int error) 11081da177e4SLinus Torvalds { 11095ceb9d7fSTrond Myklebust switch (error) { 11105ceb9d7fSTrond Myklebust case 1: 11116de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 11126de1472fSAl Viro __func__, dentry); 11131da177e4SLinus Torvalds return 1; 11145ceb9d7fSTrond Myklebust case 0: 1115a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11161da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11171da177e4SLinus Torvalds /* Purge readdir caches. */ 11181da177e4SLinus Torvalds nfs_zap_caches(inode); 1119a3f432bfSJ. Bruce Fields /* 1120a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1121a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1122a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1123a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1124a3f432bfSJ. Bruce Fields */ 1125a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 11265ceb9d7fSTrond Myklebust return 1; 11271da177e4SLinus Torvalds } 11286de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 11296de1472fSAl Viro __func__, dentry); 11301da177e4SLinus Torvalds return 0; 11315ceb9d7fSTrond Myklebust } 11326de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 11336de1472fSAl Viro __func__, dentry, error); 1134e1fb4d05STrond Myklebust return error; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11375ceb9d7fSTrond Myklebust static int 11385ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 11395ceb9d7fSTrond Myklebust unsigned int flags) 11405ceb9d7fSTrond Myklebust { 11415ceb9d7fSTrond Myklebust int ret = 1; 11425ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 11435ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 11445ceb9d7fSTrond Myklebust return -ECHILD; 11455ceb9d7fSTrond Myklebust ret = 0; 11465ceb9d7fSTrond Myklebust } 11475ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 11485ceb9d7fSTrond Myklebust } 11495ceb9d7fSTrond Myklebust 11505ceb9d7fSTrond Myklebust static int 11515ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 11525ceb9d7fSTrond Myklebust struct inode *inode) 11535ceb9d7fSTrond Myklebust { 11545ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11555ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 11565ceb9d7fSTrond Myklebust } 11575ceb9d7fSTrond Myklebust 11585ceb9d7fSTrond Myklebust static int 11595ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 11605ceb9d7fSTrond Myklebust struct inode *inode) 11615ceb9d7fSTrond Myklebust { 11625ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 11635ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 11645ceb9d7fSTrond Myklebust struct nfs4_label *label; 1165*a1147b82STrond Myklebust unsigned long dir_verifier; 11665ceb9d7fSTrond Myklebust int ret; 11675ceb9d7fSTrond Myklebust 11685ceb9d7fSTrond Myklebust ret = -ENOMEM; 11695ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 11705ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 11715ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 11725ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 11735ceb9d7fSTrond Myklebust goto out; 11745ceb9d7fSTrond Myklebust 1175*a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1176f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 11775ceb9d7fSTrond Myklebust if (ret < 0) { 1178f7b37b8bSTrond Myklebust switch (ret) { 1179f7b37b8bSTrond Myklebust case -ESTALE: 1180f7b37b8bSTrond Myklebust case -ENOENT: 11815ceb9d7fSTrond Myklebust ret = 0; 1182f7b37b8bSTrond Myklebust break; 1183f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1184f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1185f7b37b8bSTrond Myklebust ret = 1; 1186f7b37b8bSTrond Myklebust } 11875ceb9d7fSTrond Myklebust goto out; 11885ceb9d7fSTrond Myklebust } 11895ceb9d7fSTrond Myklebust ret = 0; 11905ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11915ceb9d7fSTrond Myklebust goto out; 11925ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 11935ceb9d7fSTrond Myklebust goto out; 11945ceb9d7fSTrond Myklebust 11955ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1196*a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 11975ceb9d7fSTrond Myklebust 11985ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 11995ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 12005ceb9d7fSTrond Myklebust ret = 1; 12015ceb9d7fSTrond Myklebust out: 12025ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 12035ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 12045ceb9d7fSTrond Myklebust nfs4_label_free(label); 12055ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 12065ceb9d7fSTrond Myklebust } 12075ceb9d7fSTrond Myklebust 12085ceb9d7fSTrond Myklebust /* 12095ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 12105ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 12115ceb9d7fSTrond Myklebust * lookup. 12125ceb9d7fSTrond Myklebust * 12135ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 12145ceb9d7fSTrond Myklebust * we have an inode! 12155ceb9d7fSTrond Myklebust * 12165ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 12175ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 12185ceb9d7fSTrond Myklebust */ 12195ceb9d7fSTrond Myklebust static int 12205ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 12215ceb9d7fSTrond Myklebust unsigned int flags) 12225ceb9d7fSTrond Myklebust { 12235ceb9d7fSTrond Myklebust struct inode *inode; 12245ceb9d7fSTrond Myklebust int error; 12255ceb9d7fSTrond Myklebust 12265ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 12275ceb9d7fSTrond Myklebust inode = d_inode(dentry); 12285ceb9d7fSTrond Myklebust 12295ceb9d7fSTrond Myklebust if (!inode) 12305ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 12315ceb9d7fSTrond Myklebust 12325ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 12335ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12345ceb9d7fSTrond Myklebust __func__, dentry); 12355ceb9d7fSTrond Myklebust goto out_bad; 12365ceb9d7fSTrond Myklebust } 12375ceb9d7fSTrond Myklebust 12385ceb9d7fSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 12395ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 12405ceb9d7fSTrond Myklebust 12415ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 12425ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 12435ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 12445ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 12455ceb9d7fSTrond Myklebust if (error) { 12465ceb9d7fSTrond Myklebust if (error == -ESTALE) 12475ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 12485ceb9d7fSTrond Myklebust goto out_bad; 12495ceb9d7fSTrond Myklebust } 12505ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 12515ceb9d7fSTrond Myklebust goto out_valid; 12525ceb9d7fSTrond Myklebust } 12535ceb9d7fSTrond Myklebust 12545ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12555ceb9d7fSTrond Myklebust return -ECHILD; 12565ceb9d7fSTrond Myklebust 12575ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 12585ceb9d7fSTrond Myklebust goto out_bad; 12595ceb9d7fSTrond Myklebust 12605ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 12615ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 12625ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 12635ceb9d7fSTrond Myklebust return error; 12645ceb9d7fSTrond Myklebust out_valid: 12655ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12665ceb9d7fSTrond Myklebust out_bad: 12675ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12685ceb9d7fSTrond Myklebust return -ECHILD; 12695ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 12705ceb9d7fSTrond Myklebust } 12715ceb9d7fSTrond Myklebust 12725ceb9d7fSTrond Myklebust static int 1273c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1274c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 12755ceb9d7fSTrond Myklebust { 12765ceb9d7fSTrond Myklebust struct dentry *parent; 12775ceb9d7fSTrond Myklebust struct inode *dir; 12785ceb9d7fSTrond Myklebust int ret; 12795ceb9d7fSTrond Myklebust 12805ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 12815ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 12825ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 12835ceb9d7fSTrond Myklebust if (!dir) 12845ceb9d7fSTrond Myklebust return -ECHILD; 1285c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 12865ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 12875ceb9d7fSTrond Myklebust return -ECHILD; 12885ceb9d7fSTrond Myklebust } else { 12895ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1290c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 12915ceb9d7fSTrond Myklebust dput(parent); 12925ceb9d7fSTrond Myklebust } 12935ceb9d7fSTrond Myklebust return ret; 12945ceb9d7fSTrond Myklebust } 12955ceb9d7fSTrond Myklebust 1296c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1297c7944ebbSTrond Myklebust { 1298c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1299c7944ebbSTrond Myklebust } 1300c7944ebbSTrond Myklebust 13011da177e4SLinus Torvalds /* 13022b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1303ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1304ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1305ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1306ecf3d1f1SJeff Layton * 1307ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1308ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1309ecf3d1f1SJeff Layton */ 1310ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1311ecf3d1f1SJeff Layton { 13122b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 13139cdd1d3fSTrond Myklebust int error = 0; 1314ecf3d1f1SJeff Layton 1315ecf3d1f1SJeff Layton /* 1316ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1317ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1318ecf3d1f1SJeff Layton * eventually need to do something more here. 1319ecf3d1f1SJeff Layton */ 1320ecf3d1f1SJeff Layton if (!inode) { 13216de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 13226de1472fSAl Viro __func__, dentry); 1323ecf3d1f1SJeff Layton return 1; 1324ecf3d1f1SJeff Layton } 1325ecf3d1f1SJeff Layton 1326ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 13276de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13286de1472fSAl Viro __func__, dentry); 1329ecf3d1f1SJeff Layton return 0; 1330ecf3d1f1SJeff Layton } 1331ecf3d1f1SJeff Layton 1332b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1333ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1334ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1335ecf3d1f1SJeff Layton return !error; 1336ecf3d1f1SJeff Layton } 1337ecf3d1f1SJeff Layton 1338ecf3d1f1SJeff Layton /* 13391da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 13401da177e4SLinus Torvalds */ 1341fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 13421da177e4SLinus Torvalds { 13436de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13446de1472fSAl Viro dentry, dentry->d_flags); 13451da177e4SLinus Torvalds 134677f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13472b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 134877f11192STrond Myklebust return 1; 134977f11192STrond Myklebust 13501da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13511da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13521da177e4SLinus Torvalds return 1; 13531da177e4SLinus Torvalds } 13541751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 13551da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13561da177e4SLinus Torvalds * files will be cleaned up during umount */ 13571da177e4SLinus Torvalds return 1; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds return 0; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13631f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13641b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13651b83d707STrond Myklebust { 13661b83d707STrond Myklebust spin_lock(&inode->i_lock); 13671f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 136859a707b0STrond Myklebust if (inode->i_nlink > 0) 136959a707b0STrond Myklebust drop_nlink(inode); 137059a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 137116e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 137216e14375STrond Myklebust | NFS_INO_INVALID_CTIME 137359a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 137459a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 13751b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13761b83d707STrond Myklebust } 13771b83d707STrond Myklebust 13781da177e4SLinus Torvalds /* 13791da177e4SLinus Torvalds * Called when the dentry loses inode. 13801da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13811da177e4SLinus Torvalds */ 13821da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13831da177e4SLinus Torvalds { 138483672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 138583672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 138683672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 138783672d39SNeil Brown 13881da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1389e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13901f018458STrond Myklebust nfs_drop_nlink(inode); 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds iput(inode); 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 1395b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1396b1942c5fSAl Viro { 1397b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1398b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1399b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1400b1942c5fSAl Viro WARN_ON(1); 1401b1942c5fSAl Viro else 1402b1942c5fSAl Viro kfree(dentry->d_fsdata); 1403b1942c5fSAl Viro } 1404b1942c5fSAl Viro } 1405b1942c5fSAl Viro 1406f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 14071da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1408ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 14091da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14101da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 141136d43a43SDavid Howells .d_automount = nfs_d_automount, 1412b1942c5fSAl Viro .d_release = nfs_d_release, 14131da177e4SLinus Torvalds }; 1414ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 14151da177e4SLinus Torvalds 1416597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 14171da177e4SLinus Torvalds { 14181da177e4SLinus Torvalds struct dentry *res; 14191da177e4SLinus Torvalds struct inode *inode = NULL; 1420e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1421e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 14221775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1423*a1147b82STrond Myklebust unsigned long dir_verifier; 14241da177e4SLinus Torvalds int error; 14251da177e4SLinus Torvalds 14266de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 142791d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 14281da177e4SLinus Torvalds 1429130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1430130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 14311da177e4SLinus Torvalds 1432fd684071STrond Myklebust /* 1433fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1434fd684071STrond Myklebust * but don't hash the dentry. 1435fd684071STrond Myklebust */ 14369f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1437130f9ab7SAl Viro return NULL; 14381da177e4SLinus Torvalds 1439e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1440e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1441e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1442e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1443e1fb4d05STrond Myklebust goto out; 1444e1fb4d05STrond Myklebust 144514c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 144614c43f76SDavid Quigley if (IS_ERR(label)) 144714c43f76SDavid Quigley goto out; 144814c43f76SDavid Quigley 1449*a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 14506e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1451f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 14521da177e4SLinus Torvalds if (error == -ENOENT) 14531da177e4SLinus Torvalds goto no_entry; 14541da177e4SLinus Torvalds if (error < 0) { 14551da177e4SLinus Torvalds res = ERR_PTR(error); 1456bf130914SAl Viro goto out_label; 14571da177e4SLinus Torvalds } 14581775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1459bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 146003f28e3aSTrond Myklebust if (IS_ERR(res)) 1461bf130914SAl Viro goto out_label; 146254ceac45SDavid Howells 146363519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 146463519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1465d69ee9b8STrond Myklebust 14661da177e4SLinus Torvalds no_entry: 146741d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14689eaef27bSTrond Myklebust if (res != NULL) { 14699eaef27bSTrond Myklebust if (IS_ERR(res)) 1470bf130914SAl Viro goto out_label; 14711da177e4SLinus Torvalds dentry = res; 14729eaef27bSTrond Myklebust } 1473*a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1474bf130914SAl Viro out_label: 14756e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 147614c43f76SDavid Quigley nfs4_label_free(label); 14771da177e4SLinus Torvalds out: 1478e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1479e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14801da177e4SLinus Torvalds return res; 14811da177e4SLinus Torvalds } 1482ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14831da177e4SLinus Torvalds 148489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14850b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14861da177e4SLinus Torvalds 1487f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14880ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1489b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 14901da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14911da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 149236d43a43SDavid Howells .d_automount = nfs_d_automount, 1493b1942c5fSAl Viro .d_release = nfs_d_release, 14941da177e4SLinus Torvalds }; 149589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14961da177e4SLinus Torvalds 14978a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14988a5e929dSAl Viro { 14998a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 15008a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 15018a5e929dSAl Viro res |= FMODE_READ; 15028a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 15038a5e929dSAl Viro res |= FMODE_WRITE; 15048a5e929dSAl Viro return res; 15058a5e929dSAl Viro } 15068a5e929dSAl Viro 1507532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1508cd9a1c0eSTrond Myklebust { 1509532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1510cd9a1c0eSTrond Myklebust } 1511cd9a1c0eSTrond Myklebust 1512cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1513cd9a1c0eSTrond Myklebust { 1514f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1515cd9a1c0eSTrond Myklebust return 0; 1516cd9a1c0eSTrond Myklebust } 1517cd9a1c0eSTrond Myklebust 1518d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 15190dd2b474SMiklos Szeredi struct dentry *dentry, 1520b452a458SAl Viro struct file *file, unsigned open_flags) 1521cd9a1c0eSTrond Myklebust { 15220dd2b474SMiklos Szeredi int err; 15230dd2b474SMiklos Szeredi 1524be12af3eSAl Viro err = finish_open(file, dentry, do_open); 152530d90494SAl Viro if (err) 1526d9585277SAl Viro goto out; 1527eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 152830d90494SAl Viro nfs_file_set_open_context(file, ctx); 1529eaa2b82cSNeilBrown else 15309821421aSTrond Myklebust err = -EOPENSTALE; 1531cd9a1c0eSTrond Myklebust out: 1532d9585277SAl Viro return err; 1533cd9a1c0eSTrond Myklebust } 1534cd9a1c0eSTrond Myklebust 153573a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 153630d90494SAl Viro struct file *file, unsigned open_flags, 153744907d79SAl Viro umode_t mode) 15381da177e4SLinus Torvalds { 1539c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1540cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 15410dd2b474SMiklos Szeredi struct dentry *res; 15420dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1543f46e0bd3STrond Myklebust struct inode *inode; 15441472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1545c94c0953SAl Viro bool switched = false; 154673a09dd9SAl Viro int created = 0; 1547898f635cSTrond Myklebust int err; 15481da177e4SLinus Torvalds 15490dd2b474SMiklos Szeredi /* Expect a negative dentry */ 15502b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 15510dd2b474SMiklos Szeredi 15521e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 15536de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15541e7cb3dcSChuck Lever 15559597c13bSJeff Layton err = nfs_check_flags(open_flags); 15569597c13bSJeff Layton if (err) 15579597c13bSJeff Layton return err; 15589597c13bSJeff Layton 15590dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15600dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 156100699ad8SAl Viro if (!d_in_lookup(dentry)) { 15620dd2b474SMiklos Szeredi /* 15630dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15640dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15650dd2b474SMiklos Szeredi * again 15660dd2b474SMiklos Szeredi */ 1567d9585277SAl Viro return -ENOENT; 15680dd2b474SMiklos Szeredi } 15691472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15701da177e4SLinus Torvalds goto no_open; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 15730dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1574d9585277SAl Viro return -ENAMETOOLONG; 15751da177e4SLinus Torvalds 15760dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1577dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1578dff25ddbSAndreas Gruenbacher 1579dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1580dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1581dff25ddbSAndreas Gruenbacher 1582536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1583dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 15840dd2b474SMiklos Szeredi } 1585536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1586536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1587536e43d1STrond Myklebust attr.ia_size = 0; 1588cd9a1c0eSTrond Myklebust } 1589cd9a1c0eSTrond Myklebust 1590c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1591c94c0953SAl Viro d_drop(dentry); 1592c94c0953SAl Viro switched = true; 1593c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1594c94c0953SAl Viro &dentry->d_name, &wq); 1595c94c0953SAl Viro if (IS_ERR(dentry)) 1596c94c0953SAl Viro return PTR_ERR(dentry); 1597c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1598c94c0953SAl Viro return finish_no_open(file, dentry); 1599c94c0953SAl Viro } 1600c94c0953SAl Viro 1601532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 16020dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 16030dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1604d9585277SAl Viro goto out; 16050dd2b474SMiklos Szeredi 16066e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 160773a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 160873a09dd9SAl Viro if (created) 160973a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1610275bb307STrond Myklebust if (IS_ERR(inode)) { 16110dd2b474SMiklos Szeredi err = PTR_ERR(inode); 16126e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16132d9db750STrond Myklebust put_nfs_open_context(ctx); 1614d20cb71dSAl Viro d_drop(dentry); 16150dd2b474SMiklos Szeredi switch (err) { 16161da177e4SLinus Torvalds case -ENOENT: 1617774d9513SPeng Tao d_splice_alias(NULL, dentry); 1618809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16190dd2b474SMiklos Szeredi break; 16201788ea6eSJeff Layton case -EISDIR: 16216f926b5bSTrond Myklebust case -ENOTDIR: 16226f926b5bSTrond Myklebust goto no_open; 16231da177e4SLinus Torvalds case -ELOOP: 16240dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 16251da177e4SLinus Torvalds goto no_open; 16260dd2b474SMiklos Szeredi break; 16271da177e4SLinus Torvalds /* case -EINVAL: */ 16281da177e4SLinus Torvalds default: 16290dd2b474SMiklos Szeredi break; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds goto out; 16321da177e4SLinus Torvalds } 16330dd2b474SMiklos Szeredi 1634b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 16356e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16362d9db750STrond Myklebust put_nfs_open_context(ctx); 1637d9585277SAl Viro out: 1638c94c0953SAl Viro if (unlikely(switched)) { 1639c94c0953SAl Viro d_lookup_done(dentry); 1640c94c0953SAl Viro dput(dentry); 1641c94c0953SAl Viro } 1642d9585277SAl Viro return err; 16430dd2b474SMiklos Szeredi 16441da177e4SLinus Torvalds no_open: 16451472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1646c94c0953SAl Viro if (switched) { 1647c94c0953SAl Viro d_lookup_done(dentry); 1648c94c0953SAl Viro if (!res) 1649c94c0953SAl Viro res = dentry; 1650c94c0953SAl Viro else 1651c94c0953SAl Viro dput(dentry); 1652c94c0953SAl Viro } 16530dd2b474SMiklos Szeredi if (IS_ERR(res)) 1654c94c0953SAl Viro return PTR_ERR(res); 1655e45198a6SAl Viro return finish_no_open(file, res); 16561da177e4SLinus Torvalds } 165789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 16581da177e4SLinus Torvalds 1659c7944ebbSTrond Myklebust static int 1660c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1661c7944ebbSTrond Myklebust unsigned int flags) 16621da177e4SLinus Torvalds { 1663657e94b6SNick Piggin struct inode *inode; 16641da177e4SLinus Torvalds 1665fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1666c7944ebbSTrond Myklebust goto full_reval; 1667eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1668c7944ebbSTrond Myklebust goto full_reval; 16692b484297STrond Myklebust 16702b0143b5SDavid Howells inode = d_inode(dentry); 16712b484297STrond Myklebust 16721da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 16731da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 16741da177e4SLinus Torvalds */ 1675c7944ebbSTrond Myklebust if (inode == NULL) 1676c7944ebbSTrond Myklebust goto full_reval; 167749317a7fSNeilBrown 1678c7944ebbSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 1679c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1680216d5d06STrond Myklebust 16811da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16821da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1683c7944ebbSTrond Myklebust goto full_reval; 1684c7944ebbSTrond Myklebust 16851da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1686c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1687c7944ebbSTrond Myklebust goto reval_dentry; 1688c7944ebbSTrond Myklebust 1689c7944ebbSTrond Myklebust /* Check if the directory changed */ 1690c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1691c7944ebbSTrond Myklebust goto reval_dentry; 16921da177e4SLinus Torvalds 16930ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1694c7944ebbSTrond Myklebust return 1; 1695c7944ebbSTrond Myklebust reval_dentry: 1696c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1697c7944ebbSTrond Myklebust return -ECHILD; 169842f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 16990ef97dcfSMiklos Szeredi 1700c7944ebbSTrond Myklebust full_reval: 1701c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1702c7944ebbSTrond Myklebust } 1703535918f1STrond Myklebust 1704c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1705c7944ebbSTrond Myklebust { 1706c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1707c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1708c0204fd2STrond Myklebust } 1709c0204fd2STrond Myklebust 17101da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 17111da177e4SLinus Torvalds 1712406cd915SBenjamin Coddington struct dentry * 1713406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 17141775fd3eSDavid Quigley struct nfs_fattr *fattr, 17151775fd3eSDavid Quigley struct nfs4_label *label) 17161da177e4SLinus Torvalds { 1717fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 17182b0143b5SDavid Howells struct inode *dir = d_inode(parent); 17191da177e4SLinus Torvalds struct inode *inode; 1720b0c6108eSAl Viro struct dentry *d; 1721406cd915SBenjamin Coddington int error; 17221da177e4SLinus Torvalds 1723fab728e1STrond Myklebust d_drop(dentry); 1724fab728e1STrond Myklebust 17251da177e4SLinus Torvalds if (fhandle->size == 0) { 1726f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 17271da177e4SLinus Torvalds if (error) 1728fab728e1STrond Myklebust goto out_error; 17291da177e4SLinus Torvalds } 17305724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17311da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 17321da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1733a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1734a841b54dSTrond Myklebust fattr, NULL, NULL); 17351da177e4SLinus Torvalds if (error < 0) 1736fab728e1STrond Myklebust goto out_error; 17371da177e4SLinus Torvalds } 17381775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1739b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1740fab728e1STrond Myklebust out: 1741fab728e1STrond Myklebust dput(parent); 1742406cd915SBenjamin Coddington return d; 1743fab728e1STrond Myklebust out_error: 1744fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1745406cd915SBenjamin Coddington d = ERR_PTR(error); 1746406cd915SBenjamin Coddington goto out; 1747406cd915SBenjamin Coddington } 1748406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1749406cd915SBenjamin Coddington 1750406cd915SBenjamin Coddington /* 1751406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1752406cd915SBenjamin Coddington */ 1753406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1754406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1755406cd915SBenjamin Coddington struct nfs4_label *label) 1756406cd915SBenjamin Coddington { 1757406cd915SBenjamin Coddington struct dentry *d; 1758406cd915SBenjamin Coddington 1759406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1760406cd915SBenjamin Coddington if (IS_ERR(d)) 1761406cd915SBenjamin Coddington return PTR_ERR(d); 1762406cd915SBenjamin Coddington 1763406cd915SBenjamin Coddington /* Callers don't care */ 1764406cd915SBenjamin Coddington dput(d); 1765406cd915SBenjamin Coddington return 0; 17661da177e4SLinus Torvalds } 1767ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds /* 17701da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17711da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17721da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17731da177e4SLinus Torvalds * reply path made it appear to have failed. 17741da177e4SLinus Torvalds */ 1775597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1776ebfc3b49SAl Viro umode_t mode, bool excl) 17771da177e4SLinus Torvalds { 17781da177e4SLinus Torvalds struct iattr attr; 1779ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 17801da177e4SLinus Torvalds int error; 17811da177e4SLinus Torvalds 17821e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 17836de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds attr.ia_mode = mode; 17861da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17871da177e4SLinus Torvalds 17888b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17898867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17908b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17911da177e4SLinus Torvalds if (error != 0) 17921da177e4SLinus Torvalds goto out_err; 17931da177e4SLinus Torvalds return 0; 17941da177e4SLinus Torvalds out_err: 17951da177e4SLinus Torvalds d_drop(dentry); 17961da177e4SLinus Torvalds return error; 17971da177e4SLinus Torvalds } 1798ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /* 18011da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18021da177e4SLinus Torvalds */ 1803597d9289SBryan Schumaker int 18041a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 18051da177e4SLinus Torvalds { 18061da177e4SLinus Torvalds struct iattr attr; 18071da177e4SLinus Torvalds int status; 18081da177e4SLinus Torvalds 18091e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 18106de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds attr.ia_mode = mode; 18131da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18141da177e4SLinus Torvalds 18151ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 18161da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 18171ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 18181da177e4SLinus Torvalds if (status != 0) 18191da177e4SLinus Torvalds goto out_err; 18201da177e4SLinus Torvalds return 0; 18211da177e4SLinus Torvalds out_err: 18221da177e4SLinus Torvalds d_drop(dentry); 18231da177e4SLinus Torvalds return status; 18241da177e4SLinus Torvalds } 1825ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds /* 18281da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18291da177e4SLinus Torvalds */ 1830597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds struct iattr attr; 18331da177e4SLinus Torvalds int error; 18341da177e4SLinus Torvalds 18351e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 18366de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18391da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 18401da177e4SLinus Torvalds 18411ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 18421da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 18431ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 18441da177e4SLinus Torvalds if (error != 0) 18451da177e4SLinus Torvalds goto out_err; 18461da177e4SLinus Torvalds return 0; 18471da177e4SLinus Torvalds out_err: 18481da177e4SLinus Torvalds d_drop(dentry); 18491da177e4SLinus Torvalds return error; 18501da177e4SLinus Torvalds } 1851ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 18521da177e4SLinus Torvalds 1853d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1854d45b9d8bSTrond Myklebust { 1855dc3f4198SAl Viro if (simple_positive(dentry)) 1856d45b9d8bSTrond Myklebust d_delete(dentry); 1857d45b9d8bSTrond Myklebust } 1858d45b9d8bSTrond Myklebust 1859597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 18601da177e4SLinus Torvalds { 18611da177e4SLinus Torvalds int error; 18621da177e4SLinus Torvalds 18631e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 18646de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18651da177e4SLinus Torvalds 18661ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18672b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1868884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18691da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18701da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1871ba6c0592STrond Myklebust switch (error) { 1872ba6c0592STrond Myklebust case 0: 18732b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1874ba6c0592STrond Myklebust break; 1875ba6c0592STrond Myklebust case -ENOENT: 1876d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1877ba6c0592STrond Myklebust } 1878884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1879ba6c0592STrond Myklebust } else 1880ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18811ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds return error; 18841da177e4SLinus Torvalds } 1885ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds /* 18881da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18891da177e4SLinus Torvalds * and after checking that the file has only one user. 18901da177e4SLinus Torvalds * 18911da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18921da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18931da177e4SLinus Torvalds */ 18941da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18951da177e4SLinus Torvalds { 18962b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18972b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18981da177e4SLinus Torvalds int error = -EBUSY; 18991da177e4SLinus Torvalds 19006de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 19031da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 19041da177e4SLinus Torvalds error = 0; 19051da177e4SLinus Torvalds goto out; 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds 19081ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 19091da177e4SLinus Torvalds if (inode != NULL) { 1910912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 19111da177e4SLinus Torvalds if (error == 0) 19121b83d707STrond Myklebust nfs_drop_nlink(inode); 19131da177e4SLinus Torvalds } else 1914912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 1915d45b9d8bSTrond Myklebust if (error == -ENOENT) 1916d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 19171ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 19181da177e4SLinus Torvalds out: 19191da177e4SLinus Torvalds return error; 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 19231da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 19241da177e4SLinus Torvalds * 19251da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 19261da177e4SLinus Torvalds */ 1927597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 19281da177e4SLinus Torvalds { 19291da177e4SLinus Torvalds int error; 19301da177e4SLinus Torvalds int need_rehash = 0; 19311da177e4SLinus Torvalds 19321e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 19336de1472fSAl Viro dir->i_ino, dentry); 19341da177e4SLinus Torvalds 19351ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 19361da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 193784d08fa8SAl Viro if (d_count(dentry) > 1) { 19381da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1939ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 19402b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 19411da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 19421ca42382STrond Myklebust goto out; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 19451da177e4SLinus Torvalds __d_drop(dentry); 19461da177e4SLinus Torvalds need_rehash = 1; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 19491da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1950d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 19511da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19521da177e4SLinus Torvalds } else if (need_rehash) 19531da177e4SLinus Torvalds d_rehash(dentry); 19541ca42382STrond Myklebust out: 19551ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 19561da177e4SLinus Torvalds return error; 19571da177e4SLinus Torvalds } 1958ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 19591da177e4SLinus Torvalds 1960873101b3SChuck Lever /* 1961873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1962873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1963873101b3SChuck Lever * using prepare_write/commit_write. 1964873101b3SChuck Lever * 1965873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1966873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1967873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1968873101b3SChuck Lever * symlink request has completed on the server. 1969873101b3SChuck Lever * 1970873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1971873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1972873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1973873101b3SChuck Lever * and move the raw page into its mapping. 1974873101b3SChuck Lever */ 1975597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19761da177e4SLinus Torvalds { 1977873101b3SChuck Lever struct page *page; 1978873101b3SChuck Lever char *kaddr; 19791da177e4SLinus Torvalds struct iattr attr; 1980873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19811da177e4SLinus Torvalds int error; 19821da177e4SLinus Torvalds 19831e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 19846de1472fSAl Viro dir->i_ino, dentry, symname); 19851da177e4SLinus Torvalds 1986873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1987873101b3SChuck Lever return -ENAMETOOLONG; 19881da177e4SLinus Torvalds 1989873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1990873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19911da177e4SLinus Torvalds 1992e8ecde25SAl Viro page = alloc_page(GFP_USER); 199376566991STrond Myklebust if (!page) 1994873101b3SChuck Lever return -ENOMEM; 1995873101b3SChuck Lever 1996e8ecde25SAl Viro kaddr = page_address(page); 1997873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1998873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1999873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2000873101b3SChuck Lever 20011ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 200294a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 20031ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2004873101b3SChuck Lever if (error != 0) { 20051e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2006873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 20076de1472fSAl Viro dentry, symname, error); 20081da177e4SLinus Torvalds d_drop(dentry); 2009873101b3SChuck Lever __free_page(page); 20101da177e4SLinus Torvalds return error; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 2013873101b3SChuck Lever /* 2014873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2015873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2016873101b3SChuck Lever */ 20172b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2018873101b3SChuck Lever GFP_KERNEL)) { 2019873101b3SChuck Lever SetPageUptodate(page); 2020873101b3SChuck Lever unlock_page(page); 2021a0b54addSRafael Aquini /* 2022a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2023a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2024a0b54addSRafael Aquini */ 202509cbfeafSKirill A. Shutemov put_page(page); 2026873101b3SChuck Lever } else 2027873101b3SChuck Lever __free_page(page); 2028873101b3SChuck Lever 2029873101b3SChuck Lever return 0; 2030873101b3SChuck Lever } 2031ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2032873101b3SChuck Lever 2033597d9289SBryan Schumaker int 20341da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20351da177e4SLinus Torvalds { 20362b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 20371da177e4SLinus Torvalds int error; 20381da177e4SLinus Torvalds 20396de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 20406de1472fSAl Viro old_dentry, dentry); 20411da177e4SLinus Torvalds 20421fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 20439697d234STrond Myklebust d_drop(dentry); 20441da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2045cf809556STrond Myklebust if (error == 0) { 20467de9c6eeSAl Viro ihold(inode); 20479697d234STrond Myklebust d_add(dentry, inode); 2048cf809556STrond Myklebust } 20491fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 20501da177e4SLinus Torvalds return error; 20511da177e4SLinus Torvalds } 2052ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds /* 20551da177e4SLinus Torvalds * RENAME 20561da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20571da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20581da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20591da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20601da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20611da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20621da177e4SLinus Torvalds * 20631da177e4SLinus Torvalds * FIXED. 20641da177e4SLinus Torvalds * 20651da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20661da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20671da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20681da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20691da177e4SLinus Torvalds * using the inode layer 20701da177e4SLinus Torvalds * 20711da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20721da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20731da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20741da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20751da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20761da177e4SLinus Torvalds * the rename. 20771da177e4SLinus Torvalds */ 2078597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 20791cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 20801cd66c93SMiklos Szeredi unsigned int flags) 20811da177e4SLinus Torvalds { 20822b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 20832b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2084d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 208580a491fdSJeff Layton struct rpc_task *task; 20861da177e4SLinus Torvalds int error = -EBUSY; 20871da177e4SLinus Torvalds 20881cd66c93SMiklos Szeredi if (flags) 20891cd66c93SMiklos Szeredi return -EINVAL; 20901cd66c93SMiklos Szeredi 20916de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20926de1472fSAl Viro old_dentry, new_dentry, 209384d08fa8SAl Viro d_count(new_dentry)); 20941da177e4SLinus Torvalds 209570ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20961da177e4SLinus Torvalds /* 209728f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 209828f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 209928f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 210028f79a1aSMiklos Szeredi * the new target. 21011da177e4SLinus Torvalds */ 210227226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 210327226104SMiklos Szeredi /* 210427226104SMiklos Szeredi * To prevent any new references to the target during the 210527226104SMiklos Szeredi * rename, we unhash the dentry in advance. 210627226104SMiklos Szeredi */ 2107d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 210827226104SMiklos Szeredi d_drop(new_dentry); 2109d9f29500SBenjamin Coddington rehash = new_dentry; 2110d9f29500SBenjamin Coddington } 211127226104SMiklos Szeredi 211284d08fa8SAl Viro if (d_count(new_dentry) > 2) { 21131da177e4SLinus Torvalds int err; 211427226104SMiklos Szeredi 21151da177e4SLinus Torvalds /* copy the target dentry's name */ 21161da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 21171da177e4SLinus Torvalds &new_dentry->d_name); 21181da177e4SLinus Torvalds if (!dentry) 21191da177e4SLinus Torvalds goto out; 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* silly-rename the existing target ... */ 21221da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 212324e93025SMiklos Szeredi if (err) 21241da177e4SLinus Torvalds goto out; 212524e93025SMiklos Szeredi 212624e93025SMiklos Szeredi new_dentry = dentry; 2127d9f29500SBenjamin Coddington rehash = NULL; 212824e93025SMiklos Szeredi new_inode = NULL; 2129b1e4adf4STrond Myklebust } 213027226104SMiklos Szeredi } 21311da177e4SLinus Torvalds 2132d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 213380a491fdSJeff Layton if (IS_ERR(task)) { 213480a491fdSJeff Layton error = PTR_ERR(task); 213580a491fdSJeff Layton goto out; 213680a491fdSJeff Layton } 213780a491fdSJeff Layton 213880a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2139818a8dbeSBenjamin Coddington if (error != 0) { 2140818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2141818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2142818a8dbeSBenjamin Coddington smp_wmb(); 2143818a8dbeSBenjamin Coddington } else 214480a491fdSJeff Layton error = task->tk_status; 214580a491fdSJeff Layton rpc_put_task(task); 214659a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 214759a707b0STrond Myklebust if (error == 0) { 214859a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 214959a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 215059a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 215159a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 215259a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 215359a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 215459a707b0STrond Myklebust } 21551da177e4SLinus Torvalds out: 2156d9f29500SBenjamin Coddington if (rehash) 2157d9f29500SBenjamin Coddington d_rehash(rehash); 215870ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 215970ded201STrond Myklebust new_dir, new_dentry, error); 2160d9f29500SBenjamin Coddington if (!error) { 2161d9f29500SBenjamin Coddington if (new_inode != NULL) 2162d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2163d9f29500SBenjamin Coddington /* 2164d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2165d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2166d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2167d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2168d9f29500SBenjamin Coddington */ 2169d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2170d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2171d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2172d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2173d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2174d9f29500SBenjamin Coddington 21751da177e4SLinus Torvalds /* new dentry created? */ 21761da177e4SLinus Torvalds if (dentry) 21771da177e4SLinus Torvalds dput(dentry); 21781da177e4SLinus Torvalds return error; 21791da177e4SLinus Torvalds } 2180ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 21811da177e4SLinus Torvalds 2182cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2183cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2184cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2185cfcea3e8STrond Myklebust 21863a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 21873a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 21883a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 21893a505845STrond Myklebust 21901c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 21911c3c07e9STrond Myklebust { 2192b68572e0SNeilBrown put_cred(entry->cred); 2193f682a398SNeilBrown kfree_rcu(entry, rcu_head); 21944e857c58SPeter Zijlstra smp_mb__before_atomic(); 2195cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 21964e857c58SPeter Zijlstra smp_mb__after_atomic(); 21971c3c07e9STrond Myklebust } 21981c3c07e9STrond Myklebust 21991a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 22001a81bb8aSTrond Myklebust { 22011a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 22021a81bb8aSTrond Myklebust 22031a81bb8aSTrond Myklebust while (!list_empty(head)) { 22041a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 22051a81bb8aSTrond Myklebust list_del(&cache->lru); 22061a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 22071a81bb8aSTrond Myklebust } 22081a81bb8aSTrond Myklebust } 22091a81bb8aSTrond Myklebust 22103a505845STrond Myklebust static unsigned long 22113a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2212979df72eSTrond Myklebust { 2213979df72eSTrond Myklebust LIST_HEAD(head); 2214aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2215979df72eSTrond Myklebust struct nfs_access_entry *cache; 22161ab6c499SDave Chinner long freed = 0; 2217979df72eSTrond Myklebust 2218a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2219aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2220979df72eSTrond Myklebust struct inode *inode; 2221979df72eSTrond Myklebust 2222979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2223979df72eSTrond Myklebust break; 22249c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2225979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2226979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2227979df72eSTrond Myklebust goto remove_lru_entry; 2228979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2229979df72eSTrond Myklebust struct nfs_access_entry, lru); 2230979df72eSTrond Myklebust list_move(&cache->lru, &head); 2231979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 22321ab6c499SDave Chinner freed++; 2233979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2234979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2235979df72eSTrond Myklebust &nfs_access_lru_list); 2236979df72eSTrond Myklebust else { 2237979df72eSTrond Myklebust remove_lru_entry: 2238979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 22394e857c58SPeter Zijlstra smp_mb__before_atomic(); 2240979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 22414e857c58SPeter Zijlstra smp_mb__after_atomic(); 2242979df72eSTrond Myklebust } 224359844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2244979df72eSTrond Myklebust } 2245979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22461a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22471ab6c499SDave Chinner return freed; 22481ab6c499SDave Chinner } 22491ab6c499SDave Chinner 22501ab6c499SDave Chinner unsigned long 22513a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 22523a505845STrond Myklebust { 22533a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 22543a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 22553a505845STrond Myklebust 22563a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 22573a505845STrond Myklebust return SHRINK_STOP; 22583a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 22593a505845STrond Myklebust } 22603a505845STrond Myklebust 22613a505845STrond Myklebust 22623a505845STrond Myklebust unsigned long 22631ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 22641ab6c499SDave Chinner { 226555f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2266979df72eSTrond Myklebust } 2267979df72eSTrond Myklebust 22683a505845STrond Myklebust static void 22693a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22703a505845STrond Myklebust { 22713a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22723a505845STrond Myklebust unsigned long diff; 22733a505845STrond Myklebust unsigned int nr_to_scan; 22743a505845STrond Myklebust 22753a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22763a505845STrond Myklebust return; 22773a505845STrond Myklebust nr_to_scan = 100; 22783a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 22793a505845STrond Myklebust if (diff < nr_to_scan) 22803a505845STrond Myklebust nr_to_scan = diff; 22813a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 22823a505845STrond Myklebust } 22833a505845STrond Myklebust 22841a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 22851c3c07e9STrond Myklebust { 22861c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22871a81bb8aSTrond Myklebust struct rb_node *n; 22881c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22891c3c07e9STrond Myklebust 22901c3c07e9STrond Myklebust /* Unhook entries from the cache */ 22911c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 22921c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22931c3c07e9STrond Myklebust rb_erase(n, root_node); 22941a81bb8aSTrond Myklebust list_move(&entry->lru, head); 22951c3c07e9STrond Myklebust } 22961c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 22971c3c07e9STrond Myklebust } 22981c3c07e9STrond Myklebust 22991c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 23001c3c07e9STrond Myklebust { 23011a81bb8aSTrond Myklebust LIST_HEAD(head); 23021a81bb8aSTrond Myklebust 23031a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 23041a81bb8aSTrond Myklebust return; 2305cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2306cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 23071a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2308cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2309cfcea3e8STrond Myklebust 23101c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23111a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 23121a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23131a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23141a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23151c3c07e9STrond Myklebust } 23161c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 23171c3c07e9STrond Myklebust 2318b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 23191c3c07e9STrond Myklebust { 23201c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 23211c3c07e9STrond Myklebust 23221c3c07e9STrond Myklebust while (n != NULL) { 2323b68572e0SNeilBrown struct nfs_access_entry *entry = 2324b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2325b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 23261c3c07e9STrond Myklebust 2327b68572e0SNeilBrown if (cmp < 0) 23281c3c07e9STrond Myklebust n = n->rb_left; 2329b68572e0SNeilBrown else if (cmp > 0) 23301c3c07e9STrond Myklebust n = n->rb_right; 23311c3c07e9STrond Myklebust else 23321c3c07e9STrond Myklebust return entry; 23331c3c07e9STrond Myklebust } 23341c3c07e9STrond Myklebust return NULL; 23351c3c07e9STrond Myklebust } 23361c3c07e9STrond Myklebust 2337b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 23381da177e4SLinus Torvalds { 233955296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 23401c3c07e9STrond Myklebust struct nfs_access_entry *cache; 234157b69181STrond Myklebust bool retry = true; 234257b69181STrond Myklebust int err; 23431da177e4SLinus Torvalds 23441c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 234557b69181STrond Myklebust for(;;) { 23461c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 23471c3c07e9STrond Myklebust goto out_zap; 23481c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 234957b69181STrond Myklebust err = -ENOENT; 23501c3c07e9STrond Myklebust if (cache == NULL) 23511c3c07e9STrond Myklebust goto out; 235257b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 235321c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 235457b69181STrond Myklebust break; 23555c965db8STrond Myklebust if (!retry) 23565c965db8STrond Myklebust break; 235757b69181STrond Myklebust err = -ECHILD; 235857b69181STrond Myklebust if (!may_block) 235957b69181STrond Myklebust goto out; 236057b69181STrond Myklebust spin_unlock(&inode->i_lock); 236157b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 236257b69181STrond Myklebust if (err) 236357b69181STrond Myklebust return err; 236457b69181STrond Myklebust spin_lock(&inode->i_lock); 236557b69181STrond Myklebust retry = false; 236657b69181STrond Myklebust } 23671c3c07e9STrond Myklebust res->cred = cache->cred; 23681c3c07e9STrond Myklebust res->mask = cache->mask; 2369cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23701c3c07e9STrond Myklebust err = 0; 23711c3c07e9STrond Myklebust out: 23721c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23731c3c07e9STrond Myklebust return err; 23741c3c07e9STrond Myklebust out_zap: 23751a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23761a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 23771c3c07e9STrond Myklebust return -ENOENT; 23781c3c07e9STrond Myklebust } 23791c3c07e9STrond Myklebust 2380b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2381f682a398SNeilBrown { 2382f682a398SNeilBrown /* Only check the most recently returned cache entry, 2383f682a398SNeilBrown * but do it without locking. 2384f682a398SNeilBrown */ 2385f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2386f682a398SNeilBrown struct nfs_access_entry *cache; 2387f682a398SNeilBrown int err = -ECHILD; 2388f682a398SNeilBrown struct list_head *lh; 2389f682a398SNeilBrown 2390f682a398SNeilBrown rcu_read_lock(); 2391f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2392f682a398SNeilBrown goto out; 2393f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2394f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2395f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 23969a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2397f682a398SNeilBrown cache = NULL; 2398f682a398SNeilBrown if (cache == NULL) 2399f682a398SNeilBrown goto out; 240021c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2401f682a398SNeilBrown goto out; 2402f682a398SNeilBrown res->cred = cache->cred; 2403f682a398SNeilBrown res->mask = cache->mask; 240421c3ba7eSTrond Myklebust err = 0; 2405f682a398SNeilBrown out: 2406f682a398SNeilBrown rcu_read_unlock(); 2407f682a398SNeilBrown return err; 2408f682a398SNeilBrown } 2409f682a398SNeilBrown 24101c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 24111c3c07e9STrond Myklebust { 2412cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2413cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24141c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 24151c3c07e9STrond Myklebust struct rb_node *parent = NULL; 24161c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2417b68572e0SNeilBrown int cmp; 24181c3c07e9STrond Myklebust 24191c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24201c3c07e9STrond Myklebust while (*p != NULL) { 24211c3c07e9STrond Myklebust parent = *p; 24221c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2423b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 24241c3c07e9STrond Myklebust 2425b68572e0SNeilBrown if (cmp < 0) 24261c3c07e9STrond Myklebust p = &parent->rb_left; 2427b68572e0SNeilBrown else if (cmp > 0) 24281c3c07e9STrond Myklebust p = &parent->rb_right; 24291c3c07e9STrond Myklebust else 24301c3c07e9STrond Myklebust goto found; 24311c3c07e9STrond Myklebust } 24321c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 24331c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2434cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 24351c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24361c3c07e9STrond Myklebust return; 24371c3c07e9STrond Myklebust found: 24381c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2439cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2440cfcea3e8STrond Myklebust list_del(&entry->lru); 24411c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24421c3c07e9STrond Myklebust nfs_access_free_entry(entry); 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds 24456168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 24461da177e4SLinus Torvalds { 24471c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 24481c3c07e9STrond Myklebust if (cache == NULL) 24491c3c07e9STrond Myklebust return; 24501c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2451b68572e0SNeilBrown cache->cred = get_cred(set->cred); 24521da177e4SLinus Torvalds cache->mask = set->mask; 24531c3c07e9STrond Myklebust 2454f682a398SNeilBrown /* The above field assignments must be visible 2455f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2456f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2457f682a398SNeilBrown */ 2458f682a398SNeilBrown smp_wmb(); 24591c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2460cfcea3e8STrond Myklebust 2461cfcea3e8STrond Myklebust /* Update accounting */ 24624e857c58SPeter Zijlstra smp_mb__before_atomic(); 2463cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 24644e857c58SPeter Zijlstra smp_mb__after_atomic(); 2465cfcea3e8STrond Myklebust 2466cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24671a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2468cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24691a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24701a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24711a81bb8aSTrond Myklebust &nfs_access_lru_list); 2472cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2473cfcea3e8STrond Myklebust } 24743a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24751da177e4SLinus Torvalds } 24766168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 24776168f62cSWeston Andros Adamson 24783c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 24793c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24803c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 24813c181827SAnna Schumaker NFS_ACCESS_DELETE) 24823c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24833c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2484ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 24853c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 24863c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 248715d4b73aSTrond Myklebust static int 2488ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 248915d4b73aSTrond Myklebust { 249015d4b73aSTrond Myklebust int mask = 0; 249115d4b73aSTrond Myklebust 249215d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 249315d4b73aSTrond Myklebust mask |= MAY_READ; 2494ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2495ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 249615d4b73aSTrond Myklebust mask |= MAY_WRITE; 2497ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 249815d4b73aSTrond Myklebust mask |= MAY_EXEC; 2499ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2500ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2501ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2502ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 250315d4b73aSTrond Myklebust mask |= MAY_EXEC; 2504ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2505ecbb903cSTrond Myklebust mask |= MAY_WRITE; 250615d4b73aSTrond Myklebust return mask; 250715d4b73aSTrond Myklebust } 250815d4b73aSTrond Myklebust 25096168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 25106168f62cSWeston Andros Adamson { 2511bd8b2441STrond Myklebust entry->mask = access_result; 25126168f62cSWeston Andros Adamson } 25136168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 25141da177e4SLinus Torvalds 2515b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 25161da177e4SLinus Torvalds { 25171da177e4SLinus Torvalds struct nfs_access_entry cache; 251857b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2519e8194b7dSTrond Myklebust int cache_mask = -1; 25201da177e4SLinus Torvalds int status; 25211da177e4SLinus Torvalds 2522f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2523f4ce1299STrond Myklebust 2524f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2525f682a398SNeilBrown if (status != 0) 252657b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 25271da177e4SLinus Torvalds if (status == 0) 2528f4ce1299STrond Myklebust goto out_cached; 25291da177e4SLinus Torvalds 2530f3324a2aSNeilBrown status = -ECHILD; 253157b69181STrond Myklebust if (!may_block) 2532f3324a2aSNeilBrown goto out; 2533f3324a2aSNeilBrown 25341750d929SAnna Schumaker /* 25351750d929SAnna Schumaker * Determine which access bits we want to ask for... 25361750d929SAnna Schumaker */ 25371750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 25381750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 25391750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 25401750d929SAnna Schumaker else 25411750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 25421da177e4SLinus Torvalds cache.cred = cred; 25431da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2544a71ee337SSuresh Jayaraman if (status != 0) { 2545a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2546a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2547a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2548a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2549a71ee337SSuresh Jayaraman } 2550f4ce1299STrond Myklebust goto out; 2551a71ee337SSuresh Jayaraman } 25521da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2553f4ce1299STrond Myklebust out_cached: 2554ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2555bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2556f4ce1299STrond Myklebust status = -EACCES; 25571da177e4SLinus Torvalds out: 2558e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2559f4ce1299STrond Myklebust return status; 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 2562af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2563af22f94aSTrond Myklebust { 2564af22f94aSTrond Myklebust int mask = 0; 2565af22f94aSTrond Myklebust 2566f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2567f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2568f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2569f8d9a897SWeston Andros Adamson } else { 25708a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2571af22f94aSTrond Myklebust mask |= MAY_READ; 25728a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2573af22f94aSTrond Myklebust mask |= MAY_WRITE; 2574f8d9a897SWeston Andros Adamson } 2575f8d9a897SWeston Andros Adamson 2576af22f94aSTrond Myklebust return mask; 2577af22f94aSTrond Myklebust } 2578af22f94aSTrond Myklebust 2579b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2580af22f94aSTrond Myklebust { 2581af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2582af22f94aSTrond Myklebust } 258389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2584af22f94aSTrond Myklebust 25855c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 25865c5fc09aSTrond Myklebust { 25875c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 258821c3ba7eSTrond Myklebust int ret = 0; 25895c5fc09aSTrond Myklebust 25903825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 25913825827eSTrond Myklebust return 0; 2592cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 25935c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 259421c3ba7eSTrond Myklebust return -ECHILD; 259521c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 259621c3ba7eSTrond Myklebust } 25975c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 25985c5fc09aSTrond Myklebust ret = -EACCES; 25995c5fc09aSTrond Myklebust return ret; 26005c5fc09aSTrond Myklebust } 26015c5fc09aSTrond Myklebust 260210556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 26031da177e4SLinus Torvalds { 2604b68572e0SNeilBrown const struct cred *cred = current_cred(); 26051da177e4SLinus Torvalds int res = 0; 26061da177e4SLinus Torvalds 260791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 260891d5b470SChuck Lever 2609e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 26101da177e4SLinus Torvalds goto out; 26111da177e4SLinus Torvalds /* Is this sys_access() ? */ 26129cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 26131da177e4SLinus Torvalds goto force_lookup; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 26161da177e4SLinus Torvalds case S_IFLNK: 26171da177e4SLinus Torvalds goto out; 26181da177e4SLinus Torvalds case S_IFREG: 2619762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2620762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2621762674f8STrond Myklebust return 0; 26221da177e4SLinus Torvalds break; 26231da177e4SLinus Torvalds case S_IFDIR: 26241da177e4SLinus Torvalds /* 26251da177e4SLinus Torvalds * Optimize away all write operations, since the server 26261da177e4SLinus Torvalds * will check permissions when we perform the op. 26271da177e4SLinus Torvalds */ 26281da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 26291da177e4SLinus Torvalds goto out; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds force_lookup: 26331da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 26341da177e4SLinus Torvalds goto out_notsup; 26351da177e4SLinus Torvalds 2636f3324a2aSNeilBrown /* Always try fast lookups first */ 2637f3324a2aSNeilBrown rcu_read_lock(); 2638f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2639f3324a2aSNeilBrown rcu_read_unlock(); 2640f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2641f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 26421da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2643f3324a2aSNeilBrown } 26441da177e4SLinus Torvalds out: 26455c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 26465c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2647f696a365SMiklos Szeredi 26481e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 26491e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 26501da177e4SLinus Torvalds return res; 26511da177e4SLinus Torvalds out_notsup: 2652d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2653d51ac1a8SNeilBrown return -ECHILD; 2654d51ac1a8SNeilBrown 26551da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 26561da177e4SLinus Torvalds if (res == 0) 26572830ba7fSAl Viro res = generic_permission(inode, mask); 26581e7cb3dcSChuck Lever goto out; 26591da177e4SLinus Torvalds } 2660ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 26611da177e4SLinus Torvalds 26621da177e4SLinus Torvalds /* 26631da177e4SLinus Torvalds * Local variables: 26641da177e4SLinus Torvalds * version-control: t 26651da177e4SLinus Torvalds * kept-new-versions: 5 26661da177e4SLinus Torvalds * End: 26671da177e4SLinus Torvalds */ 2668