11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/dir.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * nfs directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 10 Apr 1996 Added silly rename for unlink --okir 91da177e4SLinus Torvalds * 28 Sep 1996 Improved directory cache --okir 101da177e4SLinus Torvalds * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de 111da177e4SLinus Torvalds * Re-implemented silly rename for unlink, newly implemented 121da177e4SLinus Torvalds * silly rename for nfs_rename() following the suggestions 131da177e4SLinus Torvalds * of Olaf Kirch (okir) found in this file. 141da177e4SLinus Torvalds * Following Linus comments on my original hack, this version 151da177e4SLinus Torvalds * depends only on the dcache stuff and doesn't touch the inode 161da177e4SLinus Torvalds * layer (iput() and friends). 171da177e4SLinus Torvalds * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 20ddda8e0aSBryan Schumaker #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/time.h> 221da177e4SLinus Torvalds #include <linux/errno.h> 231da177e4SLinus Torvalds #include <linux/stat.h> 241da177e4SLinus Torvalds #include <linux/fcntl.h> 251da177e4SLinus Torvalds #include <linux/string.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/mm.h> 291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 301da177e4SLinus Torvalds #include <linux/nfs_fs.h> 311da177e4SLinus Torvalds #include <linux/nfs_mount.h> 321da177e4SLinus Torvalds #include <linux/pagemap.h> 33873101b3SChuck Lever #include <linux/pagevec.h> 341da177e4SLinus Torvalds #include <linux/namei.h> 3554ceac45SDavid Howells #include <linux/mount.h> 36a0b8cab3SMel Gorman #include <linux/swap.h> 37e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include "delegation.h" 4291d5b470SChuck Lever #include "iostat.h" 434c30d56eSAdrian Bunk #include "internal.h" 44cd9a1c0eSTrond Myklebust #include "fscache.h" 451da177e4SLinus Torvalds 46f4ce1299STrond Myklebust #include "nfstrace.h" 47f4ce1299STrond Myklebust 481da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 51480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5223db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 54f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5511de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 561da177e4SLinus Torvalds 574b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 58f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 591da177e4SLinus Torvalds .read = generic_read_dir, 609ac3d3e8SAl Viro .iterate_shared = nfs_readdir, 611da177e4SLinus Torvalds .open = nfs_opendir, 62480c2006SBryan Schumaker .release = nfs_closedir, 631da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 6611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6711de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 68d1bacf9eSBryan Schumaker }; 69d1bacf9eSBryan Schumaker 700c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred) 71480c2006SBryan Schumaker { 72311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 73480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 74480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 75480c2006SBryan Schumaker if (ctx != NULL) { 768ef2ce3eSBryan Schumaker ctx->duped = 0; 77311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 78480c2006SBryan Schumaker ctx->dir_cookie = 0; 798ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 80480c2006SBryan Schumaker ctx->cred = get_rpccred(cred); 81311324adSTrond Myklebust spin_lock(&dir->i_lock); 82311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 83311324adSTrond Myklebust spin_unlock(&dir->i_lock); 84480c2006SBryan Schumaker return ctx; 85480c2006SBryan Schumaker } 860c030806STrond Myklebust return ERR_PTR(-ENOMEM); 870c030806STrond Myklebust } 88480c2006SBryan Schumaker 89311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 90480c2006SBryan Schumaker { 91311324adSTrond Myklebust spin_lock(&dir->i_lock); 92311324adSTrond Myklebust list_del(&ctx->list); 93311324adSTrond Myklebust spin_unlock(&dir->i_lock); 94480c2006SBryan Schumaker put_rpccred(ctx->cred); 95480c2006SBryan Schumaker kfree(ctx); 96480c2006SBryan Schumaker } 97480c2006SBryan Schumaker 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * Open file 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds static int 1021da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1031da177e4SLinus Torvalds { 104480c2006SBryan Schumaker int res = 0; 105480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 106480c2006SBryan Schumaker struct rpc_cred *cred; 1071da177e4SLinus Torvalds 1086de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 109cc0dd2d1SChuck Lever 110cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1111e7cb3dcSChuck Lever 112480c2006SBryan Schumaker cred = rpc_lookup_cred(); 113480c2006SBryan Schumaker if (IS_ERR(cred)) 114480c2006SBryan Schumaker return PTR_ERR(cred); 1150c030806STrond Myklebust ctx = alloc_nfs_open_dir_context(inode, cred); 116480c2006SBryan Schumaker if (IS_ERR(ctx)) { 117480c2006SBryan Schumaker res = PTR_ERR(ctx); 118480c2006SBryan Schumaker goto out; 119480c2006SBryan Schumaker } 120480c2006SBryan Schumaker filp->private_data = ctx; 121f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 122f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 123f5a73672SNeil Brown * have been called, so we need to refresh the 124f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 125f5a73672SNeil Brown */ 126f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 127f5a73672SNeil Brown } 128480c2006SBryan Schumaker out: 129480c2006SBryan Schumaker put_rpccred(cred); 1301da177e4SLinus Torvalds return res; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133480c2006SBryan Schumaker static int 134480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 135480c2006SBryan Schumaker { 136a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 137480c2006SBryan Schumaker return 0; 138480c2006SBryan Schumaker } 139480c2006SBryan Schumaker 140d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 141d1bacf9eSBryan Schumaker u64 cookie; 142d1bacf9eSBryan Schumaker u64 ino; 143d1bacf9eSBryan Schumaker struct qstr string; 1440b26a0bfSTrond Myklebust unsigned char d_type; 145d1bacf9eSBryan Schumaker }; 146d1bacf9eSBryan Schumaker 147d1bacf9eSBryan Schumaker struct nfs_cache_array { 1489ac3d3e8SAl Viro atomic_t refcount; 14988b8e133SChuck Lever int size; 150d1bacf9eSBryan Schumaker int eof_index; 151d1bacf9eSBryan Schumaker u64 last_cookie; 152d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 153d1bacf9eSBryan Schumaker }; 154d1bacf9eSBryan Schumaker 155573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int); 1561da177e4SLinus Torvalds typedef struct { 1571da177e4SLinus Torvalds struct file *file; 1581da177e4SLinus Torvalds struct page *page; 15923db8620SAl Viro struct dir_context *ctx; 1601da177e4SLinus Torvalds unsigned long page_index; 161f0dd2136STrond Myklebust u64 *dir_cookie; 1620aded708STrond Myklebust u64 last_cookie; 163f0dd2136STrond Myklebust loff_t current_index; 1641da177e4SLinus Torvalds decode_dirent_t decode; 165d1bacf9eSBryan Schumaker 1661f4eab7eSNeil Brown unsigned long timestamp; 1674704f0e2STrond Myklebust unsigned long gencount; 168d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 169d1bacf9eSBryan Schumaker unsigned int plus:1; 170d1bacf9eSBryan Schumaker unsigned int eof:1; 1711da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1721da177e4SLinus Torvalds 173d1bacf9eSBryan Schumaker /* 174d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds static 177d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 1781da177e4SLinus Torvalds { 1798cd51a0cSTrond Myklebust void *ptr; 180d1bacf9eSBryan Schumaker if (page == NULL) 181d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 1828cd51a0cSTrond Myklebust ptr = kmap(page); 1838cd51a0cSTrond Myklebust if (ptr == NULL) 1848cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 1858cd51a0cSTrond Myklebust return ptr; 186d1bacf9eSBryan Schumaker } 187d1bacf9eSBryan Schumaker 188d1bacf9eSBryan Schumaker static 189d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 190d1bacf9eSBryan Schumaker { 191d1bacf9eSBryan Schumaker kunmap(page); 192d1bacf9eSBryan Schumaker } 193d1bacf9eSBryan Schumaker 194d1bacf9eSBryan Schumaker /* 195d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 196d1bacf9eSBryan Schumaker */ 197d1bacf9eSBryan Schumaker static 19811de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 199d1bacf9eSBryan Schumaker { 20011de3b11STrond Myklebust struct nfs_cache_array *array; 201d1bacf9eSBryan Schumaker int i; 2028cd51a0cSTrond Myklebust 2032b86ce2dSCong Wang array = kmap_atomic(page); 2049ac3d3e8SAl Viro if (atomic_dec_and_test(&array->refcount)) 205d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 206d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 2072b86ce2dSCong Wang kunmap_atomic(array); 208d1bacf9eSBryan Schumaker } 209d1bacf9eSBryan Schumaker 2109ac3d3e8SAl Viro static bool grab_page(struct page *page) 2119ac3d3e8SAl Viro { 2129ac3d3e8SAl Viro struct nfs_cache_array *array = kmap_atomic(page); 2139ac3d3e8SAl Viro bool res = atomic_inc_not_zero(&array->refcount); 2149ac3d3e8SAl Viro kunmap_atomic(array); 2159ac3d3e8SAl Viro return res; 2169ac3d3e8SAl Viro } 2179ac3d3e8SAl Viro 218d1bacf9eSBryan Schumaker /* 219d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 220d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 221d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 222d1bacf9eSBryan Schumaker */ 223d1bacf9eSBryan Schumaker static 2244a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 225d1bacf9eSBryan Schumaker { 226d1bacf9eSBryan Schumaker string->len = len; 227d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2284a201d6eSTrond Myklebust if (string->name == NULL) 2294a201d6eSTrond Myklebust return -ENOMEM; 23004e4bd1cSCatalin Marinas /* 23104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 23204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 23304e4bd1cSCatalin Marinas */ 23404e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2354a201d6eSTrond Myklebust string->hash = full_name_hash(name, len); 2364a201d6eSTrond Myklebust return 0; 237d1bacf9eSBryan Schumaker } 238d1bacf9eSBryan Schumaker 239d1bacf9eSBryan Schumaker static 240d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 241d1bacf9eSBryan Schumaker { 242d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2434a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2444a201d6eSTrond Myklebust int ret; 2454a201d6eSTrond Myklebust 246d1bacf9eSBryan Schumaker if (IS_ERR(array)) 247d1bacf9eSBryan Schumaker return PTR_ERR(array); 248d1bacf9eSBryan Schumaker 2494a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2503020093fSTrond Myklebust 2513020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2523020093fSTrond Myklebust ret = -ENOSPC; 2533020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2543020093fSTrond Myklebust goto out; 2553020093fSTrond Myklebust 2564a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2574a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2580b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2594a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2604a201d6eSTrond Myklebust if (ret) 2614a201d6eSTrond Myklebust goto out; 262d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2638cd51a0cSTrond Myklebust array->size++; 26447c716cbSTrond Myklebust if (entry->eof != 0) 265d1bacf9eSBryan Schumaker array->eof_index = array->size; 2664a201d6eSTrond Myklebust out: 267d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 2684a201d6eSTrond Myklebust return ret; 269d1bacf9eSBryan Schumaker } 270d1bacf9eSBryan Schumaker 271d1bacf9eSBryan Schumaker static 272d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 273d1bacf9eSBryan Schumaker { 27423db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 275d1bacf9eSBryan Schumaker unsigned int index; 276d1bacf9eSBryan Schumaker 277d1bacf9eSBryan Schumaker if (diff < 0) 278d1bacf9eSBryan Schumaker goto out_eof; 279d1bacf9eSBryan Schumaker if (diff >= array->size) { 2808cd51a0cSTrond Myklebust if (array->eof_index >= 0) 281d1bacf9eSBryan Schumaker goto out_eof; 282d1bacf9eSBryan Schumaker return -EAGAIN; 283d1bacf9eSBryan Schumaker } 284d1bacf9eSBryan Schumaker 285d1bacf9eSBryan Schumaker index = (unsigned int)diff; 286d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 287d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 288d1bacf9eSBryan Schumaker return 0; 289d1bacf9eSBryan Schumaker out_eof: 290d1bacf9eSBryan Schumaker desc->eof = 1; 291d1bacf9eSBryan Schumaker return -EBADCOOKIE; 292d1bacf9eSBryan Schumaker } 293d1bacf9eSBryan Schumaker 2944db72b40SJeff Layton static bool 2954db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2964db72b40SJeff Layton { 2974db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2984db72b40SJeff Layton return false; 2994db72b40SJeff Layton smp_rmb(); 3004db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 3014db72b40SJeff Layton } 3024db72b40SJeff Layton 303d1bacf9eSBryan Schumaker static 304d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 305d1bacf9eSBryan Schumaker { 306d1bacf9eSBryan Schumaker int i; 3078ef2ce3eSBryan Schumaker loff_t new_pos; 308d1bacf9eSBryan Schumaker int status = -EAGAIN; 309d1bacf9eSBryan Schumaker 310d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3118cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 312496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 3130c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 3140c030806STrond Myklebust 3158ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 3164db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 3174db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 3180c030806STrond Myklebust ctx->duped = 0; 3190c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 32023db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 3210c030806STrond Myklebust if (ctx->duped > 0 3220c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 3230c030806STrond Myklebust if (printk_ratelimit()) { 3246de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 3250c030806STrond Myklebust "Please contact your server vendor. " 3269581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 3279581a4aeSJeff Layton desc->file, array->array[i].string.len, 3289581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3290c030806STrond Myklebust } 3300c030806STrond Myklebust status = -ELOOP; 3310c030806STrond Myklebust goto out; 3320c030806STrond Myklebust } 3338ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3340c030806STrond Myklebust ctx->duped = -1; 3358ef2ce3eSBryan Schumaker } 33623db8620SAl Viro desc->ctx->pos = new_pos; 3378cd51a0cSTrond Myklebust desc->cache_entry_index = i; 33847c716cbSTrond Myklebust return 0; 3398cd51a0cSTrond Myklebust } 3408cd51a0cSTrond Myklebust } 34147c716cbSTrond Myklebust if (array->eof_index >= 0) { 342d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 34318fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 34418fb5fe4STrond Myklebust desc->eof = 1; 345d1bacf9eSBryan Schumaker } 3460c030806STrond Myklebust out: 347d1bacf9eSBryan Schumaker return status; 348d1bacf9eSBryan Schumaker } 349d1bacf9eSBryan Schumaker 350d1bacf9eSBryan Schumaker static 351d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 352d1bacf9eSBryan Schumaker { 353d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 35447c716cbSTrond Myklebust int status; 355d1bacf9eSBryan Schumaker 356d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 357d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 358d1bacf9eSBryan Schumaker status = PTR_ERR(array); 359d1bacf9eSBryan Schumaker goto out; 360d1bacf9eSBryan Schumaker } 361d1bacf9eSBryan Schumaker 362d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 363d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 364d1bacf9eSBryan Schumaker else 365d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 366d1bacf9eSBryan Schumaker 36747c716cbSTrond Myklebust if (status == -EAGAIN) { 3680aded708STrond Myklebust desc->last_cookie = array->last_cookie; 369e47c085aSTrond Myklebust desc->current_index += array->size; 37047c716cbSTrond Myklebust desc->page_index++; 37147c716cbSTrond Myklebust } 372d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 373d1bacf9eSBryan Schumaker out: 374d1bacf9eSBryan Schumaker return status; 375d1bacf9eSBryan Schumaker } 376d1bacf9eSBryan Schumaker 377d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 378d1bacf9eSBryan Schumaker static 37956e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 380d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 381d1bacf9eSBryan Schumaker { 382480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 383480c2006SBryan Schumaker struct rpc_cred *cred = ctx->cred; 3844704f0e2STrond Myklebust unsigned long timestamp, gencount; 3851da177e4SLinus Torvalds int error; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds again: 3881da177e4SLinus Torvalds timestamp = jiffies; 3894704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 390be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3911da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3921da177e4SLinus Torvalds if (error < 0) { 3931da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3941da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3951da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3963a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 3971da177e4SLinus Torvalds desc->plus = 0; 3981da177e4SLinus Torvalds goto again; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds goto error; 4011da177e4SLinus Torvalds } 4021f4eab7eSNeil Brown desc->timestamp = timestamp; 4034704f0e2STrond Myklebust desc->gencount = gencount; 404d1bacf9eSBryan Schumaker error: 405d1bacf9eSBryan Schumaker return error; 406d1bacf9eSBryan Schumaker } 407d1bacf9eSBryan Schumaker 408573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 409573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 410d1bacf9eSBryan Schumaker { 411573c4e1eSChuck Lever int error; 412d1bacf9eSBryan Schumaker 413573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 414573c4e1eSChuck Lever if (error) 415573c4e1eSChuck Lever return error; 416d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 417d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 418d1bacf9eSBryan Schumaker return 0; 419d1bacf9eSBryan Schumaker } 420d1bacf9eSBryan Schumaker 421fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 422fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 423fa923369STrond Myklebust */ 424d39ab9deSBryan Schumaker static 425d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 426d39ab9deSBryan Schumaker { 427d8fdb47fSTrond Myklebust struct inode *inode; 428fa923369STrond Myklebust struct nfs_inode *nfsi; 429fa923369STrond Myklebust 4302b0143b5SDavid Howells if (d_really_is_negative(dentry)) 4312b0143b5SDavid Howells return 0; 432fa923369STrond Myklebust 433d8fdb47fSTrond Myklebust inode = d_inode(dentry); 434d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 435d8fdb47fSTrond Myklebust return 0; 436d8fdb47fSTrond Myklebust 437d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 438fa923369STrond Myklebust if (entry->fattr->fileid == nfsi->fileid) 439fa923369STrond Myklebust return 1; 440fa923369STrond Myklebust if (nfs_compare_fh(entry->fh, &nfsi->fh) == 0) 441d39ab9deSBryan Schumaker return 1; 442d39ab9deSBryan Schumaker return 0; 443d39ab9deSBryan Schumaker } 444d39ab9deSBryan Schumaker 445d39ab9deSBryan Schumaker static 44623db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 447d69ee9b8STrond Myklebust { 448d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 449d69ee9b8STrond Myklebust return false; 450d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 451d69ee9b8STrond Myklebust return true; 45223db8620SAl Viro if (ctx->pos == 0) 453d69ee9b8STrond Myklebust return true; 454d69ee9b8STrond Myklebust return false; 455d69ee9b8STrond Myklebust } 456d69ee9b8STrond Myklebust 457d69ee9b8STrond Myklebust /* 458d69ee9b8STrond Myklebust * This function is called by the lookup code to request the use of 459d69ee9b8STrond Myklebust * readdirplus to accelerate any future lookups in the same 460d69ee9b8STrond Myklebust * directory. 461d69ee9b8STrond Myklebust */ 462d69ee9b8STrond Myklebust static 463d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 464d69ee9b8STrond Myklebust { 465d69ee9b8STrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags); 466d69ee9b8STrond Myklebust } 467d69ee9b8STrond Myklebust 468311324adSTrond Myklebust /* 469311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 470311324adSTrond Myklebust * 471311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 472311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 473311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 474311324adSTrond Myklebust * cache flush. 475311324adSTrond Myklebust */ 476311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 477311324adSTrond Myklebust { 478311324adSTrond Myklebust if (!list_empty(&NFS_I(dir)->open_files)) { 479311324adSTrond Myklebust nfs_advise_use_readdirplus(dir); 480311324adSTrond Myklebust nfs_zap_mapping(dir, dir->i_mapping); 481311324adSTrond Myklebust } 482311324adSTrond Myklebust } 483311324adSTrond Myklebust 484d69ee9b8STrond Myklebust static 485d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 486d39ab9deSBryan Schumaker { 48726fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4889ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4894a201d6eSTrond Myklebust struct dentry *dentry; 4904a201d6eSTrond Myklebust struct dentry *alias; 4912b0143b5SDavid Howells struct inode *dir = d_inode(parent); 492d39ab9deSBryan Schumaker struct inode *inode; 493aa9c2669SDavid Quigley int status; 494d39ab9deSBryan Schumaker 495fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 496fa923369STrond Myklebust return; 4976c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4986c441c25STrond Myklebust return; 4994a201d6eSTrond Myklebust if (filename.name[0] == '.') { 5004a201d6eSTrond Myklebust if (filename.len == 1) 5014a201d6eSTrond Myklebust return; 5024a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 5034a201d6eSTrond Myklebust return; 5044a201d6eSTrond Myklebust } 5054a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 506d39ab9deSBryan Schumaker 5074a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 5089ac3d3e8SAl Viro again: 5099ac3d3e8SAl Viro if (!dentry) { 5109ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 5119ac3d3e8SAl Viro if (IS_ERR(dentry)) 5129ac3d3e8SAl Viro return; 5139ac3d3e8SAl Viro } 5149ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 5156c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 5166c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 5176c441c25STrond Myklebust &entry->fattr->fsid)) 5186c441c25STrond Myklebust goto out; 519d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 520cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 5212b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 522aa9c2669SDavid Quigley if (!status) 5232b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 524d39ab9deSBryan Schumaker goto out; 525d39ab9deSBryan Schumaker } else { 5265542aa2fSEric W. Biederman d_invalidate(dentry); 527d39ab9deSBryan Schumaker dput(dentry); 5289ac3d3e8SAl Viro dentry = NULL; 5299ac3d3e8SAl Viro goto again; 530d39ab9deSBryan Schumaker } 531d39ab9deSBryan Schumaker } 532d39ab9deSBryan Schumaker 5331775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 53441d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5359ac3d3e8SAl Viro d_lookup_done(dentry); 5369ac3d3e8SAl Viro if (alias) { 537d39ab9deSBryan Schumaker if (IS_ERR(alias)) 538d39ab9deSBryan Schumaker goto out; 5399ac3d3e8SAl Viro dput(dentry); 5409ac3d3e8SAl Viro dentry = alias; 5419ac3d3e8SAl Viro } 542d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 543d39ab9deSBryan Schumaker out: 544d39ab9deSBryan Schumaker dput(dentry); 545d39ab9deSBryan Schumaker } 546d39ab9deSBryan Schumaker 547d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 548d1bacf9eSBryan Schumaker static 5498cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5506650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 551d1bacf9eSBryan Schumaker { 552babddc72SBryan Schumaker struct xdr_stream stream; 553f7da7a12SBenny Halevy struct xdr_buf buf; 5546650239aSTrond Myklebust struct page *scratch; 55599424380SBryan Schumaker struct nfs_cache_array *array; 5565c346854STrond Myklebust unsigned int count = 0; 5575c346854STrond Myklebust int status; 558babddc72SBryan Schumaker 5596650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5606650239aSTrond Myklebust if (scratch == NULL) 5616650239aSTrond Myklebust return -ENOMEM; 562babddc72SBryan Schumaker 563ce85cfbeSBenjamin Coddington if (buflen == 0) 564ce85cfbeSBenjamin Coddington goto out_nopages; 565ce85cfbeSBenjamin Coddington 566f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5676650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 56899424380SBryan Schumaker 56999424380SBryan Schumaker do { 57099424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5718cd51a0cSTrond Myklebust if (status != 0) { 5728cd51a0cSTrond Myklebust if (status == -EAGAIN) 5738cd51a0cSTrond Myklebust status = 0; 57499424380SBryan Schumaker break; 5758cd51a0cSTrond Myklebust } 57699424380SBryan Schumaker 5775c346854STrond Myklebust count++; 5785c346854STrond Myklebust 57947c716cbSTrond Myklebust if (desc->plus != 0) 580be62a1a8SMiklos Szeredi nfs_prime_dcache(file_dentry(desc->file), entry); 5818cd51a0cSTrond Myklebust 5828cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5838cd51a0cSTrond Myklebust if (status != 0) 5848cd51a0cSTrond Myklebust break; 58599424380SBryan Schumaker } while (!entry->eof); 58699424380SBryan Schumaker 587ce85cfbeSBenjamin Coddington out_nopages: 58847c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 58999424380SBryan Schumaker array = nfs_readdir_get_array(page); 5908cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 5918cd51a0cSTrond Myklebust array->eof_index = array->size; 59299424380SBryan Schumaker status = 0; 59399424380SBryan Schumaker nfs_readdir_release_array(page); 5945c346854STrond Myklebust } else 5955c346854STrond Myklebust status = PTR_ERR(array); 59656e4ebf8SBryan Schumaker } 5976650239aSTrond Myklebust 5986650239aSTrond Myklebust put_page(scratch); 5998cd51a0cSTrond Myklebust return status; 6008cd51a0cSTrond Myklebust } 60156e4ebf8SBryan Schumaker 60256e4ebf8SBryan Schumaker static 603c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 60456e4ebf8SBryan Schumaker { 60556e4ebf8SBryan Schumaker unsigned int i; 60656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 60756e4ebf8SBryan Schumaker put_page(pages[i]); 60856e4ebf8SBryan Schumaker } 60956e4ebf8SBryan Schumaker 61056e4ebf8SBryan Schumaker /* 61156e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 6120b936e37SAnna Schumaker * to nfs_readdir_free_pagearray 61356e4ebf8SBryan Schumaker */ 61456e4ebf8SBryan Schumaker static 615c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 61656e4ebf8SBryan Schumaker { 61756e4ebf8SBryan Schumaker unsigned int i; 61856e4ebf8SBryan Schumaker 61956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 62056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 62156e4ebf8SBryan Schumaker if (page == NULL) 62256e4ebf8SBryan Schumaker goto out_freepages; 62356e4ebf8SBryan Schumaker pages[i] = page; 62456e4ebf8SBryan Schumaker } 6256650239aSTrond Myklebust return 0; 62656e4ebf8SBryan Schumaker 62756e4ebf8SBryan Schumaker out_freepages: 628c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6296650239aSTrond Myklebust return -ENOMEM; 630d1bacf9eSBryan Schumaker } 631d1bacf9eSBryan Schumaker 632d1bacf9eSBryan Schumaker static 633d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 634d1bacf9eSBryan Schumaker { 63556e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 636d1bacf9eSBryan Schumaker struct nfs_entry entry; 637d1bacf9eSBryan Schumaker struct file *file = desc->file; 638d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6398cd51a0cSTrond Myklebust int status = -ENOMEM; 64056e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 641d1bacf9eSBryan Schumaker 642d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6430aded708STrond Myklebust entry.cookie = desc->last_cookie; 644d1bacf9eSBryan Schumaker entry.eof = 0; 645d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 646d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 647573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 648d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 649d1bacf9eSBryan Schumaker goto out; 650d1bacf9eSBryan Schumaker 65114c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 65214c43f76SDavid Quigley if (IS_ERR(entry.label)) { 65314c43f76SDavid Quigley status = PTR_ERR(entry.label); 65414c43f76SDavid Quigley goto out; 65514c43f76SDavid Quigley } 65614c43f76SDavid Quigley 657d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 6588cd51a0cSTrond Myklebust if (IS_ERR(array)) { 6598cd51a0cSTrond Myklebust status = PTR_ERR(array); 66014c43f76SDavid Quigley goto out_label_free; 6618cd51a0cSTrond Myklebust } 662d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 6639ac3d3e8SAl Viro atomic_set(&array->refcount, 1); 664d1bacf9eSBryan Schumaker array->eof_index = -1; 665d1bacf9eSBryan Schumaker 666c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6676650239aSTrond Myklebust if (status < 0) 668d1bacf9eSBryan Schumaker goto out_release_array; 669d1bacf9eSBryan Schumaker do { 670ac396128STrond Myklebust unsigned int pglen; 67156e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 672babddc72SBryan Schumaker 673d1bacf9eSBryan Schumaker if (status < 0) 674d1bacf9eSBryan Schumaker break; 675ac396128STrond Myklebust pglen = status; 6766650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6778cd51a0cSTrond Myklebust if (status < 0) { 6788cd51a0cSTrond Myklebust if (status == -ENOSPC) 6798cd51a0cSTrond Myklebust status = 0; 6808cd51a0cSTrond Myklebust break; 6818cd51a0cSTrond Myklebust } 6828cd51a0cSTrond Myklebust } while (array->eof_index < 0); 683d1bacf9eSBryan Schumaker 684c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 685d1bacf9eSBryan Schumaker out_release_array: 686d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 68714c43f76SDavid Quigley out_label_free: 68814c43f76SDavid Quigley nfs4_label_free(entry.label); 689d1bacf9eSBryan Schumaker out: 690d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 691d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 692d1bacf9eSBryan Schumaker return status; 693d1bacf9eSBryan Schumaker } 694d1bacf9eSBryan Schumaker 695d1bacf9eSBryan Schumaker /* 696d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 697d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 698d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 699d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7001da177e4SLinus Torvalds */ 701d1bacf9eSBryan Schumaker static 702d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 703d1bacf9eSBryan Schumaker { 704496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7058cd51a0cSTrond Myklebust int ret; 706d1bacf9eSBryan Schumaker 7078cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7088cd51a0cSTrond Myklebust if (ret < 0) 709d1bacf9eSBryan Schumaker goto error; 710d1bacf9eSBryan Schumaker SetPageUptodate(page); 711d1bacf9eSBryan Schumaker 7122aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 713cd9ae2b6STrond Myklebust /* Should never happen */ 714cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 715cd9ae2b6STrond Myklebust } 7161da177e4SLinus Torvalds unlock_page(page); 7171da177e4SLinus Torvalds return 0; 7181da177e4SLinus Torvalds error: 7191da177e4SLinus Torvalds unlock_page(page); 7208cd51a0cSTrond Myklebust return ret; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 723d1bacf9eSBryan Schumaker static 724d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7251da177e4SLinus Torvalds { 72611de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 72709cbfeafSKirill A. Shutemov put_page(desc->page); 7281da177e4SLinus Torvalds desc->page = NULL; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 731d1bacf9eSBryan Schumaker static 732d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7331da177e4SLinus Torvalds { 7349ac3d3e8SAl Viro struct page *page; 7359ac3d3e8SAl Viro 7369ac3d3e8SAl Viro for (;;) { 7379ac3d3e8SAl Viro page = read_cache_page(file_inode(desc->file)->i_mapping, 738d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 7399ac3d3e8SAl Viro if (IS_ERR(page) || grab_page(page)) 7409ac3d3e8SAl Viro break; 7419ac3d3e8SAl Viro put_page(page); 7429ac3d3e8SAl Viro } 7439ac3d3e8SAl Viro return page; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* 747d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7481da177e4SLinus Torvalds */ 749d1bacf9eSBryan Schumaker static 750d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 751d1bacf9eSBryan Schumaker { 752d1bacf9eSBryan Schumaker int res; 753d1bacf9eSBryan Schumaker 754d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 755d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 756d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 757d1bacf9eSBryan Schumaker 758d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 75947c716cbSTrond Myklebust if (res != 0) 760d1bacf9eSBryan Schumaker cache_page_release(desc); 761d1bacf9eSBryan Schumaker return res; 762d1bacf9eSBryan Schumaker } 763d1bacf9eSBryan Schumaker 764d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7651da177e4SLinus Torvalds static inline 7661da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7671da177e4SLinus Torvalds { 7688cd51a0cSTrond Myklebust int res; 769d1bacf9eSBryan Schumaker 7700aded708STrond Myklebust if (desc->page_index == 0) { 7718cd51a0cSTrond Myklebust desc->current_index = 0; 7720aded708STrond Myklebust desc->last_cookie = 0; 7730aded708STrond Myklebust } 77447c716cbSTrond Myklebust do { 775d1bacf9eSBryan Schumaker res = find_cache_page(desc); 77647c716cbSTrond Myklebust } while (res == -EAGAIN); 7771da177e4SLinus Torvalds return res; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7821da177e4SLinus Torvalds */ 7831da177e4SLinus Torvalds static 78423db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7851da177e4SLinus Torvalds { 7861da177e4SLinus Torvalds struct file *file = desc->file; 787d1bacf9eSBryan Schumaker int i = 0; 788d1bacf9eSBryan Schumaker int res = 0; 789d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7908ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7918ef2ce3eSBryan Schumaker 792d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 793e7c58e97STrond Myklebust if (IS_ERR(array)) { 794e7c58e97STrond Myklebust res = PTR_ERR(array); 795e7c58e97STrond Myklebust goto out; 796e7c58e97STrond Myklebust } 7971da177e4SLinus Torvalds 798d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 799ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8001da177e4SLinus Torvalds 801ece0b423STrond Myklebust ent = &array->array[i]; 80223db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 80323db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 804ece0b423STrond Myklebust desc->eof = 1; 8051da177e4SLinus Torvalds break; 806ece0b423STrond Myklebust } 80723db8620SAl Viro desc->ctx->pos++; 808d1bacf9eSBryan Schumaker if (i < (array->size-1)) 809d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 810d1bacf9eSBryan Schumaker else 811d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 8120c030806STrond Myklebust if (ctx->duped != 0) 8130c030806STrond Myklebust ctx->duped = 1; 8148cd51a0cSTrond Myklebust } 81547c716cbSTrond Myklebust if (array->eof_index >= 0) 816d1bacf9eSBryan Schumaker desc->eof = 1; 817d1bacf9eSBryan Schumaker 818d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 819e7c58e97STrond Myklebust out: 820d1bacf9eSBryan Schumaker cache_page_release(desc); 8211e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8221e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8231da177e4SLinus Torvalds return res; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* 8271da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8281da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8291da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8301da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8311da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8321da177e4SLinus Torvalds * 8331da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8341da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8351da177e4SLinus Torvalds * we should already have a complete representation of the 8361da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8371da177e4SLinus Torvalds */ 8381da177e4SLinus Torvalds static inline 83923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds struct page *page = NULL; 8421da177e4SLinus Torvalds int status; 843496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8440c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8451da177e4SLinus Torvalds 8461e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8471e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8501da177e4SLinus Torvalds if (!page) { 8511da177e4SLinus Torvalds status = -ENOMEM; 8521da177e4SLinus Torvalds goto out; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 8557a8e1dc3STrond Myklebust desc->page_index = 0; 8560aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8577a8e1dc3STrond Myklebust desc->page = page; 8580c030806STrond Myklebust ctx->duped = 0; 8597a8e1dc3STrond Myklebust 86085f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 86185f8607eSTrond Myklebust if (status < 0) 862d1bacf9eSBryan Schumaker goto out_release; 863d1bacf9eSBryan Schumaker 86423db8620SAl Viro status = nfs_do_filldir(desc); 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds out: 8671e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8683110ff80SHarvey Harrison __func__, status); 8691da177e4SLinus Torvalds return status; 8701da177e4SLinus Torvalds out_release: 871d1bacf9eSBryan Schumaker cache_page_release(desc); 8721da177e4SLinus Torvalds goto out; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 875311324adSTrond Myklebust static bool nfs_dir_mapping_need_revalidate(struct inode *dir) 876311324adSTrond Myklebust { 877311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 878311324adSTrond Myklebust 879311324adSTrond Myklebust if (nfs_attribute_cache_expired(dir)) 880311324adSTrond Myklebust return true; 881311324adSTrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 882311324adSTrond Myklebust return true; 883311324adSTrond Myklebust return false; 884311324adSTrond Myklebust } 885311324adSTrond Myklebust 88600a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 88700a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 88800a92642SOlivier Galibert whole directory. 8891da177e4SLinus Torvalds */ 89023db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8911da177e4SLinus Torvalds { 892be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8932b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8941da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8951da177e4SLinus Torvalds *desc = &my_desc; 89623db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 89707b5ce8eSScott Mayhew int res = 0; 8981da177e4SLinus Torvalds 8996de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9006de1472fSAl Viro file, (long long)ctx->pos); 90191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 90291d5b470SChuck Lever 9031da177e4SLinus Torvalds /* 90423db8620SAl Viro * ctx->pos points to the dirent entry number. 905f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 90600a92642SOlivier Galibert * to either find the entry with the appropriate number or 90700a92642SOlivier Galibert * revalidate the cookie. 9081da177e4SLinus Torvalds */ 9091da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 9101da177e4SLinus Torvalds 91123db8620SAl Viro desc->file = file; 91223db8620SAl Viro desc->ctx = ctx; 913480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 9141da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 91523db8620SAl Viro desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0; 9161da177e4SLinus Torvalds 917311324adSTrond Myklebust if (ctx->pos == 0 || nfs_dir_mapping_need_revalidate(inode)) 91823db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 919fccca7fcSTrond Myklebust if (res < 0) 920fccca7fcSTrond Myklebust goto out; 921fccca7fcSTrond Myklebust 92247c716cbSTrond Myklebust do { 9231da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 92400a92642SOlivier Galibert 9251da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 926ece0b423STrond Myklebust res = 0; 9271da177e4SLinus Torvalds /* This means either end of directory */ 928d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 9291da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 93023db8620SAl Viro res = uncached_readdir(desc); 931ece0b423STrond Myklebust if (res == 0) 9321da177e4SLinus Torvalds continue; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds break; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9373a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9381da177e4SLinus Torvalds nfs_zap_caches(inode); 939baf57a09STrond Myklebust desc->page_index = 0; 9401da177e4SLinus Torvalds desc->plus = 0; 941d1bacf9eSBryan Schumaker desc->eof = 0; 9421da177e4SLinus Torvalds continue; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds if (res < 0) 9451da177e4SLinus Torvalds break; 9461da177e4SLinus Torvalds 94723db8620SAl Viro res = nfs_do_filldir(desc); 948ece0b423STrond Myklebust if (res < 0) 9491da177e4SLinus Torvalds break; 95047c716cbSTrond Myklebust } while (!desc->eof); 951fccca7fcSTrond Myklebust out: 9521e7cb3dcSChuck Lever if (res > 0) 9531e7cb3dcSChuck Lever res = 0; 9546de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9551da177e4SLinus Torvalds return res; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 958965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 959f0dd2136STrond Myklebust { 960480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 961b84e06c5SChuck Lever 9626de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9636de1472fSAl Viro filp, offset, whence); 964b84e06c5SChuck Lever 965965c8e59SAndrew Morton switch (whence) { 966f0dd2136STrond Myklebust case 1: 967f0dd2136STrond Myklebust offset += filp->f_pos; 968f0dd2136STrond Myklebust case 0: 969f0dd2136STrond Myklebust if (offset >= 0) 970f0dd2136STrond Myklebust break; 971f0dd2136STrond Myklebust default: 9729ac3d3e8SAl Viro return -EINVAL; 973f0dd2136STrond Myklebust } 974f0dd2136STrond Myklebust if (offset != filp->f_pos) { 975f0dd2136STrond Myklebust filp->f_pos = offset; 976480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9778ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 978f0dd2136STrond Myklebust } 979f0dd2136STrond Myklebust return offset; 980f0dd2136STrond Myklebust } 981f0dd2136STrond Myklebust 9821da177e4SLinus Torvalds /* 9831da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9841da177e4SLinus Torvalds * is a dummy operation. 9851da177e4SLinus Torvalds */ 98602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 98702c24a82SJosef Bacik int datasync) 9881da177e4SLinus Torvalds { 9896de1472fSAl Viro struct inode *inode = file_inode(filp); 9907ea80859SChristoph Hellwig 9916de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9921e7cb3dcSChuck Lever 9935955102cSAl Viro inode_lock(inode); 9946de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9955955102cSAl Viro inode_unlock(inode); 9961da177e4SLinus Torvalds return 0; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 999bfc69a45STrond Myklebust /** 1000bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1001bfc69a45STrond Myklebust * @dir - pointer to directory inode 1002bfc69a45STrond Myklebust * 1003bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1004bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1005bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1006bfc69a45STrond Myklebust * 1007bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1008bfc69a45STrond Myklebust */ 1009bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1010bfc69a45STrond Myklebust { 1011011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 1012bfc69a45STrond Myklebust } 101389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1014bfc69a45STrond Myklebust 10151da177e4SLinus Torvalds /* 10161da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 10171da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10181da177e4SLinus Torvalds * and may need to be looked up again. 1019912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 10201da177e4SLinus Torvalds */ 1021912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1022912a108dSNeilBrown int rcu_walk) 10231da177e4SLinus Torvalds { 1024912a108dSNeilBrown int ret; 1025912a108dSNeilBrown 10261da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10271da177e4SLinus Torvalds return 1; 10284eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10294eec952eSTrond Myklebust return 0; 1030f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10316ecc5e8fSTrond Myklebust return 0; 1032f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 1033912a108dSNeilBrown if (rcu_walk) 1034912a108dSNeilBrown ret = nfs_revalidate_inode_rcu(NFS_SERVER(dir), dir); 1035912a108dSNeilBrown else 1036912a108dSNeilBrown ret = nfs_revalidate_inode(NFS_SERVER(dir), dir); 1037912a108dSNeilBrown if (ret < 0) 1038f2c77f4eSTrond Myklebust return 0; 1039f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1040f2c77f4eSTrond Myklebust return 0; 1041f2c77f4eSTrond Myklebust return 1; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /* 1045a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1046a12802caSTrond Myklebust * an O_EXCL create using this path component. 1047a12802caSTrond Myklebust */ 1048fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1049a12802caSTrond Myklebust { 1050a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1051a12802caSTrond Myklebust return 0; 1052fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1053a12802caSTrond Myklebust } 1054a12802caSTrond Myklebust 1055a12802caSTrond Myklebust /* 10561d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10571d6757fbSTrond Myklebust * 10581d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10591d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10601d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10611d6757fbSTrond Myklebust * 10621d6757fbSTrond Myklebust */ 106365a0c149STrond Myklebust static 1064fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10651da177e4SLinus Torvalds { 10661da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 106765a0c149STrond Myklebust int ret; 10681da177e4SLinus Torvalds 106936d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10704e99a1ffSTrond Myklebust return 0; 10711da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1072fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10731da177e4SLinus Torvalds goto out_force; 10741da177e4SLinus Torvalds /* This is an open(2) */ 1075fa3c56bbSAl Viro if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) && 1076fa3c56bbSAl Viro (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) 10771da177e4SLinus Torvalds goto out_force; 107865a0c149STrond Myklebust out: 107965a0c149STrond Myklebust return (inode->i_nlink == 0) ? -ENOENT : 0; 10801da177e4SLinus Torvalds out_force: 10811fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10821fa1e384SNeilBrown return -ECHILD; 108365a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 108465a0c149STrond Myklebust if (ret != 0) 108565a0c149STrond Myklebust return ret; 108665a0c149STrond Myklebust goto out; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * We judge how long we want to trust negative 10911da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10921da177e4SLinus Torvalds * 10931da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10941da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1095912a108dSNeilBrown * 1096912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1097912a108dSNeilBrown * suggesting a reval is needed. 10981da177e4SLinus Torvalds */ 10991da177e4SLinus Torvalds static inline 11001da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1101fa3c56bbSAl Viro unsigned int flags) 11021da177e4SLinus Torvalds { 11031da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 1104fa3c56bbSAl Viro if (flags & LOOKUP_CREATE) 11051da177e4SLinus Torvalds return 0; 11064eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 11074eec952eSTrond Myklebust return 1; 1108912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds /* 11121da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 11131da177e4SLinus Torvalds * and we should check whether we can really trust that 11141da177e4SLinus Torvalds * lookup. 11151da177e4SLinus Torvalds * 11161da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 11171da177e4SLinus Torvalds * we have an inode! 11181da177e4SLinus Torvalds * 11191da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 11201da177e4SLinus Torvalds * cached dentry and do a new lookup. 11211da177e4SLinus Torvalds */ 11220b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds struct inode *dir; 11251da177e4SLinus Torvalds struct inode *inode; 11261da177e4SLinus Torvalds struct dentry *parent; 1127e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1128e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 11291775fd3eSDavid Quigley struct nfs4_label *label = NULL; 11301da177e4SLinus Torvalds int error; 11311da177e4SLinus Torvalds 1132d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) { 113350d77739SNeilBrown parent = ACCESS_ONCE(dentry->d_parent); 11342b0143b5SDavid Howells dir = d_inode_rcu(parent); 1135d51ac1a8SNeilBrown if (!dir) 113634286d66SNick Piggin return -ECHILD; 1137d51ac1a8SNeilBrown } else { 11381da177e4SLinus Torvalds parent = dget_parent(dentry); 11392b0143b5SDavid Howells dir = d_inode(parent); 1140d51ac1a8SNeilBrown } 114191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 11422b0143b5SDavid Howells inode = d_inode(dentry); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds if (!inode) { 1145912a108dSNeilBrown if (nfs_neg_need_reval(dir, dentry, flags)) { 1146d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1147d51ac1a8SNeilBrown return -ECHILD; 11481da177e4SLinus Torvalds goto out_bad; 1149912a108dSNeilBrown } 1150d69ee9b8STrond Myklebust goto out_valid_noent; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds if (is_bad_inode(inode)) { 1154d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1155d51ac1a8SNeilBrown return -ECHILD; 11566de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 11576de1472fSAl Viro __func__, dentry); 11581da177e4SLinus Torvalds goto out_bad; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 1161011e2a7fSBryan Schumaker if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 116215860ab1STrond Myklebust goto out_set_verifier; 116315860ab1STrond Myklebust 1164912a108dSNeilBrown /* Force a full look up iff the parent directory has changed */ 1165912a108dSNeilBrown if (!nfs_is_exclusive_create(dir, flags) && 1166912a108dSNeilBrown nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 1167912a108dSNeilBrown 11681fa1e384SNeilBrown if (nfs_lookup_verify_inode(inode, flags)) { 1169d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1170d51ac1a8SNeilBrown return -ECHILD; 11711da177e4SLinus Torvalds goto out_zap_parent; 11721fa1e384SNeilBrown } 11731da177e4SLinus Torvalds goto out_valid; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 1176912a108dSNeilBrown if (flags & LOOKUP_RCU) 1177912a108dSNeilBrown return -ECHILD; 1178912a108dSNeilBrown 11791da177e4SLinus Torvalds if (NFS_STALE(inode)) 11801da177e4SLinus Torvalds goto out_bad; 11811da177e4SLinus Torvalds 1182e1fb4d05STrond Myklebust error = -ENOMEM; 1183e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1184e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1185e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1186e1fb4d05STrond Myklebust goto out_error; 1187e1fb4d05STrond Myklebust 118814c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 118914c43f76SDavid Quigley if (IS_ERR(label)) 119014c43f76SDavid Quigley goto out_error; 119114c43f76SDavid Quigley 11926e0d0be7STrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 11931775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 11946e0d0be7STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 11951da177e4SLinus Torvalds if (error) 11961da177e4SLinus Torvalds goto out_bad; 1197e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11981da177e4SLinus Torvalds goto out_bad; 1199e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 12001da177e4SLinus Torvalds goto out_bad; 12011da177e4SLinus Torvalds 1202aa9c2669SDavid Quigley nfs_setsecurity(inode, fattr, label); 1203aa9c2669SDavid Quigley 1204e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1205e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 120614c43f76SDavid Quigley nfs4_label_free(label); 120714c43f76SDavid Quigley 120815860ab1STrond Myklebust out_set_verifier: 1209cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12101da177e4SLinus Torvalds out_valid: 1211d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1212d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1213d69ee9b8STrond Myklebust out_valid_noent: 1214d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) { 121550d77739SNeilBrown if (parent != ACCESS_ONCE(dentry->d_parent)) 1216d51ac1a8SNeilBrown return -ECHILD; 1217d51ac1a8SNeilBrown } else 12181da177e4SLinus Torvalds dput(parent); 12196de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12206de1472fSAl Viro __func__, dentry); 12211da177e4SLinus Torvalds return 1; 12221da177e4SLinus Torvalds out_zap_parent: 12231da177e4SLinus Torvalds nfs_zap_caches(dir); 12241da177e4SLinus Torvalds out_bad: 1225d51ac1a8SNeilBrown WARN_ON(flags & LOOKUP_RCU); 1226c44600c9SAl Viro nfs_free_fattr(fattr); 1227c44600c9SAl Viro nfs_free_fhandle(fhandle); 122814c43f76SDavid Quigley nfs4_label_free(label); 1229a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12301da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12311da177e4SLinus Torvalds /* Purge readdir caches. */ 12321da177e4SLinus Torvalds nfs_zap_caches(inode); 1233a3f432bfSJ. Bruce Fields /* 1234a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1235a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1236a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1237a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1238a3f432bfSJ. Bruce Fields */ 1239a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 1240d9e80b7dSAl Viro goto out_valid; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds dput(parent); 12436de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12446de1472fSAl Viro __func__, dentry); 12451da177e4SLinus Torvalds return 0; 1246e1fb4d05STrond Myklebust out_error: 1247d51ac1a8SNeilBrown WARN_ON(flags & LOOKUP_RCU); 1248e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1249e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 125014c43f76SDavid Quigley nfs4_label_free(label); 1251e1fb4d05STrond Myklebust dput(parent); 12526de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12536de1472fSAl Viro __func__, dentry, error); 1254e1fb4d05STrond Myklebust return error; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* 12582b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1259ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1260ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1261ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1262ecf3d1f1SJeff Layton * 1263ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1264ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1265ecf3d1f1SJeff Layton */ 1266ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1267ecf3d1f1SJeff Layton { 1268ecf3d1f1SJeff Layton int error; 12692b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 1270ecf3d1f1SJeff Layton 1271ecf3d1f1SJeff Layton /* 1272ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1273ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1274ecf3d1f1SJeff Layton * eventually need to do something more here. 1275ecf3d1f1SJeff Layton */ 1276ecf3d1f1SJeff Layton if (!inode) { 12776de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 12786de1472fSAl Viro __func__, dentry); 1279ecf3d1f1SJeff Layton return 1; 1280ecf3d1f1SJeff Layton } 1281ecf3d1f1SJeff Layton 1282ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 12836de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12846de1472fSAl Viro __func__, dentry); 1285ecf3d1f1SJeff Layton return 0; 1286ecf3d1f1SJeff Layton } 1287ecf3d1f1SJeff Layton 1288ecf3d1f1SJeff Layton error = nfs_revalidate_inode(NFS_SERVER(inode), inode); 1289ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1290ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1291ecf3d1f1SJeff Layton return !error; 1292ecf3d1f1SJeff Layton } 1293ecf3d1f1SJeff Layton 1294ecf3d1f1SJeff Layton /* 12951da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 12961da177e4SLinus Torvalds */ 1297fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 12981da177e4SLinus Torvalds { 12996de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13006de1472fSAl Viro dentry, dentry->d_flags); 13011da177e4SLinus Torvalds 130277f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13032b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 130477f11192STrond Myklebust return 1; 130577f11192STrond Myklebust 13061da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13071da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13081da177e4SLinus Torvalds return 1; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 13111da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13121da177e4SLinus Torvalds * files will be cleaned up during umount */ 13131da177e4SLinus Torvalds return 1; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds return 0; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13201b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13211b83d707STrond Myklebust { 13221b83d707STrond Myklebust spin_lock(&inode->i_lock); 13231f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 13241f018458STrond Myklebust if (inode->i_nlink == 1) 13251f018458STrond Myklebust clear_nlink(inode); 13261f018458STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; 13271b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13281b83d707STrond Myklebust } 13291b83d707STrond Myklebust 13301da177e4SLinus Torvalds /* 13311da177e4SLinus Torvalds * Called when the dentry loses inode. 13321da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13331da177e4SLinus Torvalds */ 13341da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13351da177e4SLinus Torvalds { 133683672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 133783672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 133883672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 133983672d39SNeil Brown 13401da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1341e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13421f018458STrond Myklebust nfs_drop_nlink(inode); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds iput(inode); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 1347b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1348b1942c5fSAl Viro { 1349b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1350b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1351b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1352b1942c5fSAl Viro WARN_ON(1); 1353b1942c5fSAl Viro else 1354b1942c5fSAl Viro kfree(dentry->d_fsdata); 1355b1942c5fSAl Viro } 1356b1942c5fSAl Viro } 1357b1942c5fSAl Viro 1358f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 13591da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1360ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 13611da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13621da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 136336d43a43SDavid Howells .d_automount = nfs_d_automount, 1364b1942c5fSAl Viro .d_release = nfs_d_release, 13651da177e4SLinus Torvalds }; 1366ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 13671da177e4SLinus Torvalds 1368597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct dentry *res; 13711da177e4SLinus Torvalds struct inode *inode = NULL; 1372e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1373e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 13741775fd3eSDavid Quigley struct nfs4_label *label = NULL; 13751da177e4SLinus Torvalds int error; 13761da177e4SLinus Torvalds 13776de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 137891d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 13791da177e4SLinus Torvalds 1380130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1381130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 13821da177e4SLinus Torvalds 1383fd684071STrond Myklebust /* 1384fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1385fd684071STrond Myklebust * but don't hash the dentry. 1386fd684071STrond Myklebust */ 1387130f9ab7SAl Viro if (nfs_is_exclusive_create(dir, flags)) 1388130f9ab7SAl Viro return NULL; 13891da177e4SLinus Torvalds 1390e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1391e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1392e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1393e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1394e1fb4d05STrond Myklebust goto out; 1395e1fb4d05STrond Myklebust 139614c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 139714c43f76SDavid Quigley if (IS_ERR(label)) 139814c43f76SDavid Quigley goto out; 139914c43f76SDavid Quigley 1400565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 14016e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 14021775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 14031da177e4SLinus Torvalds if (error == -ENOENT) 14041da177e4SLinus Torvalds goto no_entry; 14051da177e4SLinus Torvalds if (error < 0) { 14061da177e4SLinus Torvalds res = ERR_PTR(error); 1407565277f6STrond Myklebust goto out_unblock_sillyrename; 14081da177e4SLinus Torvalds } 14091775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1410bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 141103f28e3aSTrond Myklebust if (IS_ERR(res)) 1412565277f6STrond Myklebust goto out_unblock_sillyrename; 141354ceac45SDavid Howells 1414d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1415d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1416d69ee9b8STrond Myklebust 14171da177e4SLinus Torvalds no_entry: 141841d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14199eaef27bSTrond Myklebust if (res != NULL) { 14209eaef27bSTrond Myklebust if (IS_ERR(res)) 1421565277f6STrond Myklebust goto out_unblock_sillyrename; 14221da177e4SLinus Torvalds dentry = res; 14239eaef27bSTrond Myklebust } 14241da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1425565277f6STrond Myklebust out_unblock_sillyrename: 14266e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 142714c43f76SDavid Quigley nfs4_label_free(label); 14281da177e4SLinus Torvalds out: 1429e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1430e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14311da177e4SLinus Torvalds return res; 14321da177e4SLinus Torvalds } 1433ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14341da177e4SLinus Torvalds 143589d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14360b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14371da177e4SLinus Torvalds 1438f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14390ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 14401da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14411da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 144236d43a43SDavid Howells .d_automount = nfs_d_automount, 1443b1942c5fSAl Viro .d_release = nfs_d_release, 14441da177e4SLinus Torvalds }; 144589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14461da177e4SLinus Torvalds 14478a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14488a5e929dSAl Viro { 14498a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14508a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14518a5e929dSAl Viro res |= FMODE_READ; 14528a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14538a5e929dSAl Viro res |= FMODE_WRITE; 14548a5e929dSAl Viro return res; 14558a5e929dSAl Viro } 14568a5e929dSAl Viro 145751141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags) 1458cd9a1c0eSTrond Myklebust { 14595ede7b1cSAl Viro return alloc_nfs_open_context(dentry, flags_to_mode(open_flags)); 1460cd9a1c0eSTrond Myklebust } 1461cd9a1c0eSTrond Myklebust 1462cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1463cd9a1c0eSTrond Myklebust { 1464f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1465cd9a1c0eSTrond Myklebust return 0; 1466cd9a1c0eSTrond Myklebust } 1467cd9a1c0eSTrond Myklebust 1468d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 14690dd2b474SMiklos Szeredi struct dentry *dentry, 147030d90494SAl Viro struct file *file, unsigned open_flags, 147147237687SAl Viro int *opened) 1472cd9a1c0eSTrond Myklebust { 14730dd2b474SMiklos Szeredi int err; 14740dd2b474SMiklos Szeredi 147530d90494SAl Viro err = finish_open(file, dentry, do_open, opened); 147630d90494SAl Viro if (err) 1477d9585277SAl Viro goto out; 147830d90494SAl Viro nfs_file_set_open_context(file, ctx); 14790dd2b474SMiklos Szeredi 1480cd9a1c0eSTrond Myklebust out: 1481d9585277SAl Viro return err; 1482cd9a1c0eSTrond Myklebust } 1483cd9a1c0eSTrond Myklebust 148473a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 148530d90494SAl Viro struct file *file, unsigned open_flags, 148647237687SAl Viro umode_t mode, int *opened) 14871da177e4SLinus Torvalds { 1488cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 14890dd2b474SMiklos Szeredi struct dentry *res; 14900dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1491f46e0bd3STrond Myklebust struct inode *inode; 14921472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1493898f635cSTrond Myklebust int err; 14941da177e4SLinus Torvalds 14950dd2b474SMiklos Szeredi /* Expect a negative dentry */ 14962b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 14970dd2b474SMiklos Szeredi 14981e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 14996de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15001e7cb3dcSChuck Lever 15019597c13bSJeff Layton err = nfs_check_flags(open_flags); 15029597c13bSJeff Layton if (err) 15039597c13bSJeff Layton return err; 15049597c13bSJeff Layton 15050dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15060dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 1507*00699ad8SAl Viro if (!d_in_lookup(dentry)) { 15080dd2b474SMiklos Szeredi /* 15090dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15100dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15110dd2b474SMiklos Szeredi * again 15120dd2b474SMiklos Szeredi */ 1513d9585277SAl Viro return -ENOENT; 15140dd2b474SMiklos Szeredi } 15151472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15161da177e4SLinus Torvalds goto no_open; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15190dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1520d9585277SAl Viro return -ENAMETOOLONG; 15211da177e4SLinus Torvalds 15220dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1523536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 15240dd2b474SMiklos Szeredi attr.ia_mode = mode & ~current_umask(); 15250dd2b474SMiklos Szeredi } 1526536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1527536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1528536e43d1STrond Myklebust attr.ia_size = 0; 1529cd9a1c0eSTrond Myklebust } 1530cd9a1c0eSTrond Myklebust 15310dd2b474SMiklos Szeredi ctx = create_nfs_open_context(dentry, open_flags); 15320dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15330dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1534d9585277SAl Viro goto out; 15350dd2b474SMiklos Szeredi 15366e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 15375bc2afc2STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened); 1538275bb307STrond Myklebust if (IS_ERR(inode)) { 15390dd2b474SMiklos Szeredi err = PTR_ERR(inode); 15406e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15412d9db750STrond Myklebust put_nfs_open_context(ctx); 1542d20cb71dSAl Viro d_drop(dentry); 15430dd2b474SMiklos Szeredi switch (err) { 15441da177e4SLinus Torvalds case -ENOENT: 1545f46e0bd3STrond Myklebust d_add(dentry, NULL); 1546809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15470dd2b474SMiklos Szeredi break; 15481788ea6eSJeff Layton case -EISDIR: 15496f926b5bSTrond Myklebust case -ENOTDIR: 15506f926b5bSTrond Myklebust goto no_open; 15511da177e4SLinus Torvalds case -ELOOP: 15520dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 15531da177e4SLinus Torvalds goto no_open; 15540dd2b474SMiklos Szeredi break; 15551da177e4SLinus Torvalds /* case -EINVAL: */ 15561da177e4SLinus Torvalds default: 15570dd2b474SMiklos Szeredi break; 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds goto out; 15601da177e4SLinus Torvalds } 15610dd2b474SMiklos Szeredi 1562275bb307STrond Myklebust err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened); 15636e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15642d9db750STrond Myklebust put_nfs_open_context(ctx); 1565d9585277SAl Viro out: 1566d9585277SAl Viro return err; 15670dd2b474SMiklos Szeredi 15681da177e4SLinus Torvalds no_open: 15691472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 15700dd2b474SMiklos Szeredi err = PTR_ERR(res); 15710dd2b474SMiklos Szeredi if (IS_ERR(res)) 1572d9585277SAl Viro goto out; 15730dd2b474SMiklos Szeredi 1574e45198a6SAl Viro return finish_no_open(file, res); 15751da177e4SLinus Torvalds } 157689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 15771da177e4SLinus Torvalds 15780b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 15791da177e4SLinus Torvalds { 1580657e94b6SNick Piggin struct inode *inode; 158150de348cSMiklos Szeredi int ret = 0; 15821da177e4SLinus Torvalds 1583fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1584eda72afbSMiklos Szeredi goto no_open; 1585eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 15865584c306STrond Myklebust goto no_open; 158749f9a0faSTrond Myklebust if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1) 158849f9a0faSTrond Myklebust goto no_open; 15892b484297STrond Myklebust 15902b0143b5SDavid Howells inode = d_inode(dentry); 15912b484297STrond Myklebust 15921da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 15931da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 15941da177e4SLinus Torvalds */ 1595216d5d06STrond Myklebust if (inode == NULL) { 159649317a7fSNeilBrown struct dentry *parent; 159749317a7fSNeilBrown struct inode *dir; 159849317a7fSNeilBrown 1599912a108dSNeilBrown if (flags & LOOKUP_RCU) { 160050d77739SNeilBrown parent = ACCESS_ONCE(dentry->d_parent); 16012b0143b5SDavid Howells dir = d_inode_rcu(parent); 1602912a108dSNeilBrown if (!dir) 1603d51ac1a8SNeilBrown return -ECHILD; 1604912a108dSNeilBrown } else { 160549317a7fSNeilBrown parent = dget_parent(dentry); 16062b0143b5SDavid Howells dir = d_inode(parent); 1607912a108dSNeilBrown } 1608fa3c56bbSAl Viro if (!nfs_neg_need_reval(dir, dentry, flags)) 1609216d5d06STrond Myklebust ret = 1; 1610912a108dSNeilBrown else if (flags & LOOKUP_RCU) 1611912a108dSNeilBrown ret = -ECHILD; 1612912a108dSNeilBrown if (!(flags & LOOKUP_RCU)) 161349317a7fSNeilBrown dput(parent); 161450d77739SNeilBrown else if (parent != ACCESS_ONCE(dentry->d_parent)) 1615912a108dSNeilBrown return -ECHILD; 16161da177e4SLinus Torvalds goto out; 1617216d5d06STrond Myklebust } 1618216d5d06STrond Myklebust 16191da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16201da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 162149317a7fSNeilBrown goto no_open; 16221da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1623fa3c56bbSAl Viro if (flags & LOOKUP_EXCL) 162449317a7fSNeilBrown goto no_open; 16251da177e4SLinus Torvalds 16260ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1627898f635cSTrond Myklebust ret = 1; 16280ef97dcfSMiklos Szeredi 16291da177e4SLinus Torvalds out: 16301da177e4SLinus Torvalds return ret; 1631535918f1STrond Myklebust 16325584c306STrond Myklebust no_open: 16330b728e19SAl Viro return nfs_lookup_revalidate(dentry, flags); 1634c0204fd2STrond Myklebust } 1635c0204fd2STrond Myklebust 16361da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds /* 16391da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 16401da177e4SLinus Torvalds */ 16411da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 16421775fd3eSDavid Quigley struct nfs_fattr *fattr, 16431775fd3eSDavid Quigley struct nfs4_label *label) 16441da177e4SLinus Torvalds { 1645fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 16462b0143b5SDavid Howells struct inode *dir = d_inode(parent); 16471da177e4SLinus Torvalds struct inode *inode; 16481da177e4SLinus Torvalds int error = -EACCES; 16491da177e4SLinus Torvalds 1650fab728e1STrond Myklebust d_drop(dentry); 1651fab728e1STrond Myklebust 16521da177e4SLinus Torvalds /* We may have been initialized further down */ 16532b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1654fab728e1STrond Myklebust goto out; 16551da177e4SLinus Torvalds if (fhandle->size == 0) { 16561775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL); 16571da177e4SLinus Torvalds if (error) 1658fab728e1STrond Myklebust goto out_error; 16591da177e4SLinus Torvalds } 16605724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16611da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 16621da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 16631775fd3eSDavid Quigley error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL); 16641da177e4SLinus Torvalds if (error < 0) 1665fab728e1STrond Myklebust goto out_error; 16661da177e4SLinus Torvalds } 16671775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 166803f28e3aSTrond Myklebust error = PTR_ERR(inode); 166903f28e3aSTrond Myklebust if (IS_ERR(inode)) 1670fab728e1STrond Myklebust goto out_error; 1671fab728e1STrond Myklebust d_add(dentry, inode); 1672fab728e1STrond Myklebust out: 1673fab728e1STrond Myklebust dput(parent); 16741da177e4SLinus Torvalds return 0; 1675fab728e1STrond Myklebust out_error: 1676fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1677fab728e1STrond Myklebust dput(parent); 1678fab728e1STrond Myklebust return error; 16791da177e4SLinus Torvalds } 1680ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* 16831da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 16841da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 16851da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 16861da177e4SLinus Torvalds * reply path made it appear to have failed. 16871da177e4SLinus Torvalds */ 1688597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1689ebfc3b49SAl Viro umode_t mode, bool excl) 16901da177e4SLinus Torvalds { 16911da177e4SLinus Torvalds struct iattr attr; 1692ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 16931da177e4SLinus Torvalds int error; 16941da177e4SLinus Torvalds 16951e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 16966de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds attr.ia_mode = mode; 16991da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17001da177e4SLinus Torvalds 17018b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17028867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17038b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17041da177e4SLinus Torvalds if (error != 0) 17051da177e4SLinus Torvalds goto out_err; 17061da177e4SLinus Torvalds return 0; 17071da177e4SLinus Torvalds out_err: 17081da177e4SLinus Torvalds d_drop(dentry); 17091da177e4SLinus Torvalds return error; 17101da177e4SLinus Torvalds } 1711ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds /* 17141da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17151da177e4SLinus Torvalds */ 1716597d9289SBryan Schumaker int 17171a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17181da177e4SLinus Torvalds { 17191da177e4SLinus Torvalds struct iattr attr; 17201da177e4SLinus Torvalds int status; 17211da177e4SLinus Torvalds 17221e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 17236de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds attr.ia_mode = mode; 17261da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17271da177e4SLinus Torvalds 17281ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 17291da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 17301ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 17311da177e4SLinus Torvalds if (status != 0) 17321da177e4SLinus Torvalds goto out_err; 17331da177e4SLinus Torvalds return 0; 17341da177e4SLinus Torvalds out_err: 17351da177e4SLinus Torvalds d_drop(dentry); 17361da177e4SLinus Torvalds return status; 17371da177e4SLinus Torvalds } 1738ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds /* 17411da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17421da177e4SLinus Torvalds */ 1743597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 17441da177e4SLinus Torvalds { 17451da177e4SLinus Torvalds struct iattr attr; 17461da177e4SLinus Torvalds int error; 17471da177e4SLinus Torvalds 17481e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 17496de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17521da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 17531da177e4SLinus Torvalds 17541ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 17551da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 17561ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 17571da177e4SLinus Torvalds if (error != 0) 17581da177e4SLinus Torvalds goto out_err; 17591da177e4SLinus Torvalds return 0; 17601da177e4SLinus Torvalds out_err: 17611da177e4SLinus Torvalds d_drop(dentry); 17621da177e4SLinus Torvalds return error; 17631da177e4SLinus Torvalds } 1764ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 17651da177e4SLinus Torvalds 1766d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1767d45b9d8bSTrond Myklebust { 1768dc3f4198SAl Viro if (simple_positive(dentry)) 1769d45b9d8bSTrond Myklebust d_delete(dentry); 1770d45b9d8bSTrond Myklebust } 1771d45b9d8bSTrond Myklebust 1772597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 17731da177e4SLinus Torvalds { 17741da177e4SLinus Torvalds int error; 17751da177e4SLinus Torvalds 17761e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 17776de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17781da177e4SLinus Torvalds 17791ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 17802b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1781884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 17821da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 17831da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1784ba6c0592STrond Myklebust switch (error) { 1785ba6c0592STrond Myklebust case 0: 17862b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1787ba6c0592STrond Myklebust break; 1788ba6c0592STrond Myklebust case -ENOENT: 1789d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1790ba6c0592STrond Myklebust } 1791884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1792ba6c0592STrond Myklebust } else 1793ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 17941ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds return error; 17971da177e4SLinus Torvalds } 1798ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /* 18011da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18021da177e4SLinus Torvalds * and after checking that the file has only one user. 18031da177e4SLinus Torvalds * 18041da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18051da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18061da177e4SLinus Torvalds */ 18071da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18081da177e4SLinus Torvalds { 18092b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18102b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18111da177e4SLinus Torvalds int error = -EBUSY; 18121da177e4SLinus Torvalds 18136de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18161da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18171da177e4SLinus Torvalds error = 0; 18181da177e4SLinus Torvalds goto out; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 18221da177e4SLinus Torvalds if (inode != NULL) { 182357ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 18241da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 18251da177e4SLinus Torvalds if (error == 0) 18261b83d707STrond Myklebust nfs_drop_nlink(inode); 18271da177e4SLinus Torvalds } else 18281da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1829d45b9d8bSTrond Myklebust if (error == -ENOENT) 1830d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18311ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 18321da177e4SLinus Torvalds out: 18331da177e4SLinus Torvalds return error; 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 18371da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 18381da177e4SLinus Torvalds * 18391da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 18401da177e4SLinus Torvalds */ 1841597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 18421da177e4SLinus Torvalds { 18431da177e4SLinus Torvalds int error; 18441da177e4SLinus Torvalds int need_rehash = 0; 18451da177e4SLinus Torvalds 18461e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 18476de1472fSAl Viro dir->i_ino, dentry); 18481da177e4SLinus Torvalds 18491ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 18501da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 185184d08fa8SAl Viro if (d_count(dentry) > 1) { 18521da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1853ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 18542b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 18551da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 18561ca42382STrond Myklebust goto out; 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 18591da177e4SLinus Torvalds __d_drop(dentry); 18601da177e4SLinus Torvalds need_rehash = 1; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 18631da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1864d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 18651da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18661da177e4SLinus Torvalds } else if (need_rehash) 18671da177e4SLinus Torvalds d_rehash(dentry); 18681ca42382STrond Myklebust out: 18691ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 18701da177e4SLinus Torvalds return error; 18711da177e4SLinus Torvalds } 1872ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 18731da177e4SLinus Torvalds 1874873101b3SChuck Lever /* 1875873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1876873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1877873101b3SChuck Lever * using prepare_write/commit_write. 1878873101b3SChuck Lever * 1879873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1880873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1881873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1882873101b3SChuck Lever * symlink request has completed on the server. 1883873101b3SChuck Lever * 1884873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1885873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1886873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1887873101b3SChuck Lever * and move the raw page into its mapping. 1888873101b3SChuck Lever */ 1889597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 18901da177e4SLinus Torvalds { 1891873101b3SChuck Lever struct page *page; 1892873101b3SChuck Lever char *kaddr; 18931da177e4SLinus Torvalds struct iattr attr; 1894873101b3SChuck Lever unsigned int pathlen = strlen(symname); 18951da177e4SLinus Torvalds int error; 18961da177e4SLinus Torvalds 18971e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 18986de1472fSAl Viro dir->i_ino, dentry, symname); 18991da177e4SLinus Torvalds 1900873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1901873101b3SChuck Lever return -ENAMETOOLONG; 19021da177e4SLinus Torvalds 1903873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1904873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19051da177e4SLinus Torvalds 1906e8ecde25SAl Viro page = alloc_page(GFP_USER); 190776566991STrond Myklebust if (!page) 1908873101b3SChuck Lever return -ENOMEM; 1909873101b3SChuck Lever 1910e8ecde25SAl Viro kaddr = page_address(page); 1911873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1912873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1913873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1914873101b3SChuck Lever 19151ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 191694a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19171ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1918873101b3SChuck Lever if (error != 0) { 19191e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1920873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 19216de1472fSAl Viro dentry, symname, error); 19221da177e4SLinus Torvalds d_drop(dentry); 1923873101b3SChuck Lever __free_page(page); 19241da177e4SLinus Torvalds return error; 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds 1927873101b3SChuck Lever /* 1928873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1929873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1930873101b3SChuck Lever */ 19312b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 1932873101b3SChuck Lever GFP_KERNEL)) { 1933873101b3SChuck Lever SetPageUptodate(page); 1934873101b3SChuck Lever unlock_page(page); 1935a0b54addSRafael Aquini /* 1936a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 1937a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 1938a0b54addSRafael Aquini */ 193909cbfeafSKirill A. Shutemov put_page(page); 1940873101b3SChuck Lever } else 1941873101b3SChuck Lever __free_page(page); 1942873101b3SChuck Lever 1943873101b3SChuck Lever return 0; 1944873101b3SChuck Lever } 1945ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 1946873101b3SChuck Lever 1947597d9289SBryan Schumaker int 19481da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19491da177e4SLinus Torvalds { 19502b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 19511da177e4SLinus Torvalds int error; 19521da177e4SLinus Torvalds 19536de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 19546de1472fSAl Viro old_dentry, dentry); 19551da177e4SLinus Torvalds 19561fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 195757ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 19589a3936aaSTrond Myklebust 19599697d234STrond Myklebust d_drop(dentry); 19601da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1961cf809556STrond Myklebust if (error == 0) { 19627de9c6eeSAl Viro ihold(inode); 19639697d234STrond Myklebust d_add(dentry, inode); 1964cf809556STrond Myklebust } 19651fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 19661da177e4SLinus Torvalds return error; 19671da177e4SLinus Torvalds } 1968ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /* 19711da177e4SLinus Torvalds * RENAME 19721da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 19731da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 19741da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 19751da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 19761da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 19771da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 19781da177e4SLinus Torvalds * 19791da177e4SLinus Torvalds * FIXED. 19801da177e4SLinus Torvalds * 19811da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 19821da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 19831da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 19841da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 19851da177e4SLinus Torvalds * using the inode layer 19861da177e4SLinus Torvalds * 19871da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 19881da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 19891da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 19901da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 19911da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 19921da177e4SLinus Torvalds * the rename. 19931da177e4SLinus Torvalds */ 1994597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 19951da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 19961da177e4SLinus Torvalds { 19972b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 19982b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 19991da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 200080a491fdSJeff Layton struct rpc_task *task; 20011da177e4SLinus Torvalds int error = -EBUSY; 20021da177e4SLinus Torvalds 20036de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20046de1472fSAl Viro old_dentry, new_dentry, 200584d08fa8SAl Viro d_count(new_dentry)); 20061da177e4SLinus Torvalds 200770ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20081da177e4SLinus Torvalds /* 200928f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 201028f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 201128f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 201228f79a1aSMiklos Szeredi * the new target. 20131da177e4SLinus Torvalds */ 201427226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 201527226104SMiklos Szeredi /* 201627226104SMiklos Szeredi * To prevent any new references to the target during the 201727226104SMiklos Szeredi * rename, we unhash the dentry in advance. 201827226104SMiklos Szeredi */ 201927226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 202027226104SMiklos Szeredi d_drop(new_dentry); 202127226104SMiklos Szeredi rehash = new_dentry; 202227226104SMiklos Szeredi } 202327226104SMiklos Szeredi 202484d08fa8SAl Viro if (d_count(new_dentry) > 2) { 20251da177e4SLinus Torvalds int err; 202627226104SMiklos Szeredi 20271da177e4SLinus Torvalds /* copy the target dentry's name */ 20281da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 20291da177e4SLinus Torvalds &new_dentry->d_name); 20301da177e4SLinus Torvalds if (!dentry) 20311da177e4SLinus Torvalds goto out; 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds /* silly-rename the existing target ... */ 20341da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 203524e93025SMiklos Szeredi if (err) 20361da177e4SLinus Torvalds goto out; 203724e93025SMiklos Szeredi 203824e93025SMiklos Szeredi new_dentry = dentry; 203956335936SOGAWA Hirofumi rehash = NULL; 204024e93025SMiklos Szeredi new_inode = NULL; 2041b1e4adf4STrond Myklebust } 204227226104SMiklos Szeredi } 20431da177e4SLinus Torvalds 204457ec14c5SBryan Schumaker NFS_PROTO(old_inode)->return_delegation(old_inode); 2045b1e4adf4STrond Myklebust if (new_inode != NULL) 204657ec14c5SBryan Schumaker NFS_PROTO(new_inode)->return_delegation(new_inode); 20471da177e4SLinus Torvalds 204880a491fdSJeff Layton task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 204980a491fdSJeff Layton if (IS_ERR(task)) { 205080a491fdSJeff Layton error = PTR_ERR(task); 205180a491fdSJeff Layton goto out; 205280a491fdSJeff Layton } 205380a491fdSJeff Layton 205480a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 205580a491fdSJeff Layton if (error == 0) 205680a491fdSJeff Layton error = task->tk_status; 205780a491fdSJeff Layton rpc_put_task(task); 20585ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 20591da177e4SLinus Torvalds out: 20601da177e4SLinus Torvalds if (rehash) 20611da177e4SLinus Torvalds d_rehash(rehash); 206270ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 206370ded201STrond Myklebust new_dir, new_dentry, error); 20641da177e4SLinus Torvalds if (!error) { 2065b1e4adf4STrond Myklebust if (new_inode != NULL) 2066b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 20671da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 20688fb559f8SChuck Lever nfs_set_verifier(new_dentry, 20698fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 2070d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 2071d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds /* new dentry created? */ 20741da177e4SLinus Torvalds if (dentry) 20751da177e4SLinus Torvalds dput(dentry); 20761da177e4SLinus Torvalds return error; 20771da177e4SLinus Torvalds } 2078ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 20791da177e4SLinus Torvalds 2080cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2081cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2082cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2083cfcea3e8STrond Myklebust 20843a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 20853a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 20863a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 20873a505845STrond Myklebust 20881c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 20891c3c07e9STrond Myklebust { 20901c3c07e9STrond Myklebust put_rpccred(entry->cred); 2091f682a398SNeilBrown kfree_rcu(entry, rcu_head); 20924e857c58SPeter Zijlstra smp_mb__before_atomic(); 2093cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 20944e857c58SPeter Zijlstra smp_mb__after_atomic(); 20951c3c07e9STrond Myklebust } 20961c3c07e9STrond Myklebust 20971a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 20981a81bb8aSTrond Myklebust { 20991a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 21001a81bb8aSTrond Myklebust 21011a81bb8aSTrond Myklebust while (!list_empty(head)) { 21021a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 21031a81bb8aSTrond Myklebust list_del(&cache->lru); 21041a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 21051a81bb8aSTrond Myklebust } 21061a81bb8aSTrond Myklebust } 21071a81bb8aSTrond Myklebust 21083a505845STrond Myklebust static unsigned long 21093a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2110979df72eSTrond Myklebust { 2111979df72eSTrond Myklebust LIST_HEAD(head); 2112aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2113979df72eSTrond Myklebust struct nfs_access_entry *cache; 21141ab6c499SDave Chinner long freed = 0; 2115979df72eSTrond Myklebust 2116a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2117aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2118979df72eSTrond Myklebust struct inode *inode; 2119979df72eSTrond Myklebust 2120979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2121979df72eSTrond Myklebust break; 21229c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2123979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2124979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2125979df72eSTrond Myklebust goto remove_lru_entry; 2126979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2127979df72eSTrond Myklebust struct nfs_access_entry, lru); 2128979df72eSTrond Myklebust list_move(&cache->lru, &head); 2129979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 21301ab6c499SDave Chinner freed++; 2131979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2132979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2133979df72eSTrond Myklebust &nfs_access_lru_list); 2134979df72eSTrond Myklebust else { 2135979df72eSTrond Myklebust remove_lru_entry: 2136979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 21374e857c58SPeter Zijlstra smp_mb__before_atomic(); 2138979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 21394e857c58SPeter Zijlstra smp_mb__after_atomic(); 2140979df72eSTrond Myklebust } 214159844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2142979df72eSTrond Myklebust } 2143979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21441a81bb8aSTrond Myklebust nfs_access_free_list(&head); 21451ab6c499SDave Chinner return freed; 21461ab6c499SDave Chinner } 21471ab6c499SDave Chinner 21481ab6c499SDave Chinner unsigned long 21493a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 21503a505845STrond Myklebust { 21513a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 21523a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 21533a505845STrond Myklebust 21543a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 21553a505845STrond Myklebust return SHRINK_STOP; 21563a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 21573a505845STrond Myklebust } 21583a505845STrond Myklebust 21593a505845STrond Myklebust 21603a505845STrond Myklebust unsigned long 21611ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 21621ab6c499SDave Chinner { 216355f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2164979df72eSTrond Myklebust } 2165979df72eSTrond Myklebust 21663a505845STrond Myklebust static void 21673a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 21683a505845STrond Myklebust { 21693a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 21703a505845STrond Myklebust unsigned long diff; 21713a505845STrond Myklebust unsigned int nr_to_scan; 21723a505845STrond Myklebust 21733a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 21743a505845STrond Myklebust return; 21753a505845STrond Myklebust nr_to_scan = 100; 21763a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 21773a505845STrond Myklebust if (diff < nr_to_scan) 21783a505845STrond Myklebust nr_to_scan = diff; 21793a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 21803a505845STrond Myklebust } 21813a505845STrond Myklebust 21821a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 21831c3c07e9STrond Myklebust { 21841c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 21851a81bb8aSTrond Myklebust struct rb_node *n; 21861c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21871c3c07e9STrond Myklebust 21881c3c07e9STrond Myklebust /* Unhook entries from the cache */ 21891c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 21901c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 21911c3c07e9STrond Myklebust rb_erase(n, root_node); 21921a81bb8aSTrond Myklebust list_move(&entry->lru, head); 21931c3c07e9STrond Myklebust } 21941c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 21951c3c07e9STrond Myklebust } 21961c3c07e9STrond Myklebust 21971c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 21981c3c07e9STrond Myklebust { 21991a81bb8aSTrond Myklebust LIST_HEAD(head); 22001a81bb8aSTrond Myklebust 22011a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 22021a81bb8aSTrond Myklebust return; 2203cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2204cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22051a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2206cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2207cfcea3e8STrond Myklebust 22081c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22091a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 22101a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22111a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22121a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22131c3c07e9STrond Myklebust } 22141c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 22151c3c07e9STrond Myklebust 22161c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 22171c3c07e9STrond Myklebust { 22181c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 22191c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22201c3c07e9STrond Myklebust 22211c3c07e9STrond Myklebust while (n != NULL) { 22221c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22231c3c07e9STrond Myklebust 22241c3c07e9STrond Myklebust if (cred < entry->cred) 22251c3c07e9STrond Myklebust n = n->rb_left; 22261c3c07e9STrond Myklebust else if (cred > entry->cred) 22271c3c07e9STrond Myklebust n = n->rb_right; 22281c3c07e9STrond Myklebust else 22291c3c07e9STrond Myklebust return entry; 22301c3c07e9STrond Myklebust } 22311c3c07e9STrond Myklebust return NULL; 22321c3c07e9STrond Myklebust } 22331c3c07e9STrond Myklebust 2234af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 22351da177e4SLinus Torvalds { 223655296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 22371c3c07e9STrond Myklebust struct nfs_access_entry *cache; 22381c3c07e9STrond Myklebust int err = -ENOENT; 22391da177e4SLinus Torvalds 22401c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22411c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 22421c3c07e9STrond Myklebust goto out_zap; 22431c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 22441c3c07e9STrond Myklebust if (cache == NULL) 22451c3c07e9STrond Myklebust goto out; 2246b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 224764672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 22481c3c07e9STrond Myklebust goto out_stale; 22491c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 22501c3c07e9STrond Myklebust res->cred = cache->cred; 22511c3c07e9STrond Myklebust res->mask = cache->mask; 2252cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 22531c3c07e9STrond Myklebust err = 0; 22541c3c07e9STrond Myklebust out: 22551c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22561c3c07e9STrond Myklebust return err; 22571c3c07e9STrond Myklebust out_stale: 22581c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2259cfcea3e8STrond Myklebust list_del(&cache->lru); 22601c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22611c3c07e9STrond Myklebust nfs_access_free_entry(cache); 22621da177e4SLinus Torvalds return -ENOENT; 22631c3c07e9STrond Myklebust out_zap: 22641a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22651a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 22661c3c07e9STrond Myklebust return -ENOENT; 22671c3c07e9STrond Myklebust } 22681c3c07e9STrond Myklebust 2269f682a398SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 2270f682a398SNeilBrown { 2271f682a398SNeilBrown /* Only check the most recently returned cache entry, 2272f682a398SNeilBrown * but do it without locking. 2273f682a398SNeilBrown */ 2274f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2275f682a398SNeilBrown struct nfs_access_entry *cache; 2276f682a398SNeilBrown int err = -ECHILD; 2277f682a398SNeilBrown struct list_head *lh; 2278f682a398SNeilBrown 2279f682a398SNeilBrown rcu_read_lock(); 2280f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2281f682a398SNeilBrown goto out; 2282f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2283f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2284f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 2285f682a398SNeilBrown cred != cache->cred) 2286f682a398SNeilBrown cache = NULL; 2287f682a398SNeilBrown if (cache == NULL) 2288f682a398SNeilBrown goto out; 2289f682a398SNeilBrown if (!nfs_have_delegated_attributes(inode) && 2290f682a398SNeilBrown !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 2291f682a398SNeilBrown goto out; 2292f682a398SNeilBrown res->jiffies = cache->jiffies; 2293f682a398SNeilBrown res->cred = cache->cred; 2294f682a398SNeilBrown res->mask = cache->mask; 2295f682a398SNeilBrown err = 0; 2296f682a398SNeilBrown out: 2297f682a398SNeilBrown rcu_read_unlock(); 2298f682a398SNeilBrown return err; 2299f682a398SNeilBrown } 2300f682a398SNeilBrown 23011c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 23021c3c07e9STrond Myklebust { 2303cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2304cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23051c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 23061c3c07e9STrond Myklebust struct rb_node *parent = NULL; 23071c3c07e9STrond Myklebust struct nfs_access_entry *entry; 23081c3c07e9STrond Myklebust 23091c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23101c3c07e9STrond Myklebust while (*p != NULL) { 23111c3c07e9STrond Myklebust parent = *p; 23121c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 23131c3c07e9STrond Myklebust 23141c3c07e9STrond Myklebust if (set->cred < entry->cred) 23151c3c07e9STrond Myklebust p = &parent->rb_left; 23161c3c07e9STrond Myklebust else if (set->cred > entry->cred) 23171c3c07e9STrond Myklebust p = &parent->rb_right; 23181c3c07e9STrond Myklebust else 23191c3c07e9STrond Myklebust goto found; 23201c3c07e9STrond Myklebust } 23211c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 23221c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2323cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 23241c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23251c3c07e9STrond Myklebust return; 23261c3c07e9STrond Myklebust found: 23271c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2328cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2329cfcea3e8STrond Myklebust list_del(&entry->lru); 23301c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23311c3c07e9STrond Myklebust nfs_access_free_entry(entry); 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 23346168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 23351da177e4SLinus Torvalds { 23361c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 23371c3c07e9STrond Myklebust if (cache == NULL) 23381c3c07e9STrond Myklebust return; 23391c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 23401da177e4SLinus Torvalds cache->jiffies = set->jiffies; 23411c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 23421da177e4SLinus Torvalds cache->mask = set->mask; 23431c3c07e9STrond Myklebust 2344f682a398SNeilBrown /* The above field assignments must be visible 2345f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2346f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2347f682a398SNeilBrown */ 2348f682a398SNeilBrown smp_wmb(); 23491c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2350cfcea3e8STrond Myklebust 2351cfcea3e8STrond Myklebust /* Update accounting */ 23524e857c58SPeter Zijlstra smp_mb__before_atomic(); 2353cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 23544e857c58SPeter Zijlstra smp_mb__after_atomic(); 2355cfcea3e8STrond Myklebust 2356cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 23571a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2358cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 23591a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 23601a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 23611a81bb8aSTrond Myklebust &nfs_access_lru_list); 2362cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2363cfcea3e8STrond Myklebust } 23643a505845STrond Myklebust nfs_access_cache_enforce_limit(); 23651da177e4SLinus Torvalds } 23666168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 23676168f62cSWeston Andros Adamson 23686168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 23696168f62cSWeston Andros Adamson { 23706168f62cSWeston Andros Adamson entry->mask = 0; 23716168f62cSWeston Andros Adamson if (access_result & NFS4_ACCESS_READ) 23726168f62cSWeston Andros Adamson entry->mask |= MAY_READ; 23736168f62cSWeston Andros Adamson if (access_result & 23746168f62cSWeston Andros Adamson (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) 23756168f62cSWeston Andros Adamson entry->mask |= MAY_WRITE; 23766168f62cSWeston Andros Adamson if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) 23776168f62cSWeston Andros Adamson entry->mask |= MAY_EXEC; 23786168f62cSWeston Andros Adamson } 23796168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 23821da177e4SLinus Torvalds { 23831da177e4SLinus Torvalds struct nfs_access_entry cache; 23841da177e4SLinus Torvalds int status; 23851da177e4SLinus Torvalds 2386f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2387f4ce1299STrond Myklebust 2388f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2389f682a398SNeilBrown if (status != 0) 23901da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 23911da177e4SLinus Torvalds if (status == 0) 2392f4ce1299STrond Myklebust goto out_cached; 23931da177e4SLinus Torvalds 2394f3324a2aSNeilBrown status = -ECHILD; 2395f3324a2aSNeilBrown if (mask & MAY_NOT_BLOCK) 2396f3324a2aSNeilBrown goto out; 2397f3324a2aSNeilBrown 23981da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 23991da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 24001da177e4SLinus Torvalds cache.cred = cred; 24011da177e4SLinus Torvalds cache.jiffies = jiffies; 24021da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2403a71ee337SSuresh Jayaraman if (status != 0) { 2404a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2405a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2406a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2407a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2408a71ee337SSuresh Jayaraman } 2409f4ce1299STrond Myklebust goto out; 2410a71ee337SSuresh Jayaraman } 24111da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2412f4ce1299STrond Myklebust out_cached: 2413f4ce1299STrond Myklebust if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2414f4ce1299STrond Myklebust status = -EACCES; 24151da177e4SLinus Torvalds out: 2416f4ce1299STrond Myklebust trace_nfs_access_exit(inode, status); 2417f4ce1299STrond Myklebust return status; 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds 2420af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2421af22f94aSTrond Myklebust { 2422af22f94aSTrond Myklebust int mask = 0; 2423af22f94aSTrond Myklebust 2424f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2425f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2426f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2427f8d9a897SWeston Andros Adamson } else { 24288a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2429af22f94aSTrond Myklebust mask |= MAY_READ; 24308a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2431af22f94aSTrond Myklebust mask |= MAY_WRITE; 2432f8d9a897SWeston Andros Adamson } 2433f8d9a897SWeston Andros Adamson 2434af22f94aSTrond Myklebust return mask; 2435af22f94aSTrond Myklebust } 2436af22f94aSTrond Myklebust 2437af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2438af22f94aSTrond Myklebust { 2439af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2440af22f94aSTrond Myklebust } 244189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2442af22f94aSTrond Myklebust 24435c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 24445c5fc09aSTrond Myklebust { 24455c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 24465c5fc09aSTrond Myklebust int ret; 24475c5fc09aSTrond Myklebust 24485c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 24495c5fc09aSTrond Myklebust ret = nfs_revalidate_inode_rcu(server, inode); 24505c5fc09aSTrond Myklebust else 24515c5fc09aSTrond Myklebust ret = nfs_revalidate_inode(server, inode); 24525c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 24535c5fc09aSTrond Myklebust ret = -EACCES; 24545c5fc09aSTrond Myklebust return ret; 24555c5fc09aSTrond Myklebust } 24565c5fc09aSTrond Myklebust 245710556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 24581da177e4SLinus Torvalds { 24591da177e4SLinus Torvalds struct rpc_cred *cred; 24601da177e4SLinus Torvalds int res = 0; 24611da177e4SLinus Torvalds 246291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 246391d5b470SChuck Lever 2464e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 24651da177e4SLinus Torvalds goto out; 24661da177e4SLinus Torvalds /* Is this sys_access() ? */ 24679cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 24681da177e4SLinus Torvalds goto force_lookup; 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 24711da177e4SLinus Torvalds case S_IFLNK: 24721da177e4SLinus Torvalds goto out; 24731da177e4SLinus Torvalds case S_IFREG: 2474762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2475762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2476762674f8STrond Myklebust return 0; 24771da177e4SLinus Torvalds break; 24781da177e4SLinus Torvalds case S_IFDIR: 24791da177e4SLinus Torvalds /* 24801da177e4SLinus Torvalds * Optimize away all write operations, since the server 24811da177e4SLinus Torvalds * will check permissions when we perform the op. 24821da177e4SLinus Torvalds */ 24831da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 24841da177e4SLinus Torvalds goto out; 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds force_lookup: 24881da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 24891da177e4SLinus Torvalds goto out_notsup; 24901da177e4SLinus Torvalds 2491f3324a2aSNeilBrown /* Always try fast lookups first */ 2492f3324a2aSNeilBrown rcu_read_lock(); 2493f3324a2aSNeilBrown cred = rpc_lookup_cred_nonblock(); 2494f3324a2aSNeilBrown if (!IS_ERR(cred)) 2495f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2496f3324a2aSNeilBrown else 2497f3324a2aSNeilBrown res = PTR_ERR(cred); 2498f3324a2aSNeilBrown rcu_read_unlock(); 2499f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2500f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 250198a8e323STrond Myklebust cred = rpc_lookup_cred(); 25021da177e4SLinus Torvalds if (!IS_ERR(cred)) { 25031da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 25041da177e4SLinus Torvalds put_rpccred(cred); 25051da177e4SLinus Torvalds } else 25061da177e4SLinus Torvalds res = PTR_ERR(cred); 2507f3324a2aSNeilBrown } 25081da177e4SLinus Torvalds out: 25095c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 25105c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2511f696a365SMiklos Szeredi 25121e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 25131e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 25141da177e4SLinus Torvalds return res; 25151da177e4SLinus Torvalds out_notsup: 2516d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2517d51ac1a8SNeilBrown return -ECHILD; 2518d51ac1a8SNeilBrown 25191da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 25201da177e4SLinus Torvalds if (res == 0) 25212830ba7fSAl Viro res = generic_permission(inode, mask); 25221e7cb3dcSChuck Lever goto out; 25231da177e4SLinus Torvalds } 2524ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds /* 25271da177e4SLinus Torvalds * Local variables: 25281da177e4SLinus Torvalds * version-control: t 25291da177e4SLinus Torvalds * kept-new-versions: 5 25301da177e4SLinus Torvalds * End: 25311da177e4SLinus Torvalds */ 2532