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, 60*b044f645SBenjamin Coddington .iterate = 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 { 14888b8e133SChuck Lever int size; 149d1bacf9eSBryan Schumaker int eof_index; 150d1bacf9eSBryan Schumaker u64 last_cookie; 151d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 152d1bacf9eSBryan Schumaker }; 153d1bacf9eSBryan Schumaker 154573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int); 1551da177e4SLinus Torvalds typedef struct { 1561da177e4SLinus Torvalds struct file *file; 1571da177e4SLinus Torvalds struct page *page; 15823db8620SAl Viro struct dir_context *ctx; 1591da177e4SLinus Torvalds unsigned long page_index; 160f0dd2136STrond Myklebust u64 *dir_cookie; 1610aded708STrond Myklebust u64 last_cookie; 162f0dd2136STrond Myklebust loff_t current_index; 1631da177e4SLinus Torvalds decode_dirent_t decode; 164d1bacf9eSBryan Schumaker 1651f4eab7eSNeil Brown unsigned long timestamp; 1664704f0e2STrond Myklebust unsigned long gencount; 167d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 168d1bacf9eSBryan Schumaker unsigned int plus:1; 169d1bacf9eSBryan Schumaker unsigned int eof:1; 1701da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1711da177e4SLinus Torvalds 172d1bacf9eSBryan Schumaker /* 173d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 1741da177e4SLinus Torvalds */ 1751da177e4SLinus Torvalds static 176d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 1771da177e4SLinus Torvalds { 1788cd51a0cSTrond Myklebust void *ptr; 179d1bacf9eSBryan Schumaker if (page == NULL) 180d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 1818cd51a0cSTrond Myklebust ptr = kmap(page); 1828cd51a0cSTrond Myklebust if (ptr == NULL) 1838cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 1848cd51a0cSTrond Myklebust return ptr; 185d1bacf9eSBryan Schumaker } 186d1bacf9eSBryan Schumaker 187d1bacf9eSBryan Schumaker static 188d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 189d1bacf9eSBryan Schumaker { 190d1bacf9eSBryan Schumaker kunmap(page); 191d1bacf9eSBryan Schumaker } 192d1bacf9eSBryan Schumaker 193d1bacf9eSBryan Schumaker /* 194d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 195d1bacf9eSBryan Schumaker */ 196d1bacf9eSBryan Schumaker static 19711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 198d1bacf9eSBryan Schumaker { 19911de3b11STrond Myklebust struct nfs_cache_array *array; 200d1bacf9eSBryan Schumaker int i; 2018cd51a0cSTrond Myklebust 2022b86ce2dSCong Wang array = kmap_atomic(page); 203d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 204d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 2052b86ce2dSCong Wang kunmap_atomic(array); 206d1bacf9eSBryan Schumaker } 207d1bacf9eSBryan Schumaker 208d1bacf9eSBryan Schumaker /* 209d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 210d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 211d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 212d1bacf9eSBryan Schumaker */ 213d1bacf9eSBryan Schumaker static 2144a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 215d1bacf9eSBryan Schumaker { 216d1bacf9eSBryan Schumaker string->len = len; 217d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2184a201d6eSTrond Myklebust if (string->name == NULL) 2194a201d6eSTrond Myklebust return -ENOMEM; 22004e4bd1cSCatalin Marinas /* 22104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 22204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 22304e4bd1cSCatalin Marinas */ 22404e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2258387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2264a201d6eSTrond Myklebust return 0; 227d1bacf9eSBryan Schumaker } 228d1bacf9eSBryan Schumaker 229d1bacf9eSBryan Schumaker static 230d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 231d1bacf9eSBryan Schumaker { 232d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2334a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2344a201d6eSTrond Myklebust int ret; 2354a201d6eSTrond Myklebust 236d1bacf9eSBryan Schumaker if (IS_ERR(array)) 237d1bacf9eSBryan Schumaker return PTR_ERR(array); 238d1bacf9eSBryan Schumaker 2394a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2403020093fSTrond Myklebust 2413020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2423020093fSTrond Myklebust ret = -ENOSPC; 2433020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2443020093fSTrond Myklebust goto out; 2453020093fSTrond Myklebust 2464a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2474a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2480b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2494a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2504a201d6eSTrond Myklebust if (ret) 2514a201d6eSTrond Myklebust goto out; 252d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2538cd51a0cSTrond Myklebust array->size++; 25447c716cbSTrond Myklebust if (entry->eof != 0) 255d1bacf9eSBryan Schumaker array->eof_index = array->size; 2564a201d6eSTrond Myklebust out: 257d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 2584a201d6eSTrond Myklebust return ret; 259d1bacf9eSBryan Schumaker } 260d1bacf9eSBryan Schumaker 261d1bacf9eSBryan Schumaker static 262d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 263d1bacf9eSBryan Schumaker { 26423db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 265d1bacf9eSBryan Schumaker unsigned int index; 266d1bacf9eSBryan Schumaker 267d1bacf9eSBryan Schumaker if (diff < 0) 268d1bacf9eSBryan Schumaker goto out_eof; 269d1bacf9eSBryan Schumaker if (diff >= array->size) { 2708cd51a0cSTrond Myklebust if (array->eof_index >= 0) 271d1bacf9eSBryan Schumaker goto out_eof; 272d1bacf9eSBryan Schumaker return -EAGAIN; 273d1bacf9eSBryan Schumaker } 274d1bacf9eSBryan Schumaker 275d1bacf9eSBryan Schumaker index = (unsigned int)diff; 276d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 277d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 278d1bacf9eSBryan Schumaker return 0; 279d1bacf9eSBryan Schumaker out_eof: 280d1bacf9eSBryan Schumaker desc->eof = 1; 281d1bacf9eSBryan Schumaker return -EBADCOOKIE; 282d1bacf9eSBryan Schumaker } 283d1bacf9eSBryan Schumaker 2844db72b40SJeff Layton static bool 2854db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2864db72b40SJeff Layton { 2874db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2884db72b40SJeff Layton return false; 2894db72b40SJeff Layton smp_rmb(); 2904db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2914db72b40SJeff Layton } 2924db72b40SJeff Layton 293d1bacf9eSBryan Schumaker static 294d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 295d1bacf9eSBryan Schumaker { 296d1bacf9eSBryan Schumaker int i; 2978ef2ce3eSBryan Schumaker loff_t new_pos; 298d1bacf9eSBryan Schumaker int status = -EAGAIN; 299d1bacf9eSBryan Schumaker 300d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3018cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 302496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 3030c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 3040c030806STrond Myklebust 3058ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 3064db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 3074db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 3080c030806STrond Myklebust ctx->duped = 0; 3090c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 31023db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 3110c030806STrond Myklebust if (ctx->duped > 0 3120c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 3130c030806STrond Myklebust if (printk_ratelimit()) { 3146de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 3150c030806STrond Myklebust "Please contact your server vendor. " 3169581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 3179581a4aeSJeff Layton desc->file, array->array[i].string.len, 3189581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3190c030806STrond Myklebust } 3200c030806STrond Myklebust status = -ELOOP; 3210c030806STrond Myklebust goto out; 3220c030806STrond Myklebust } 3238ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3240c030806STrond Myklebust ctx->duped = -1; 3258ef2ce3eSBryan Schumaker } 32623db8620SAl Viro desc->ctx->pos = new_pos; 3278cd51a0cSTrond Myklebust desc->cache_entry_index = i; 32847c716cbSTrond Myklebust return 0; 3298cd51a0cSTrond Myklebust } 3308cd51a0cSTrond Myklebust } 33147c716cbSTrond Myklebust if (array->eof_index >= 0) { 332d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 33318fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 33418fb5fe4STrond Myklebust desc->eof = 1; 335d1bacf9eSBryan Schumaker } 3360c030806STrond Myklebust out: 337d1bacf9eSBryan Schumaker return status; 338d1bacf9eSBryan Schumaker } 339d1bacf9eSBryan Schumaker 340d1bacf9eSBryan Schumaker static 341d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 342d1bacf9eSBryan Schumaker { 343d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 34447c716cbSTrond Myklebust int status; 345d1bacf9eSBryan Schumaker 346d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 347d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 348d1bacf9eSBryan Schumaker status = PTR_ERR(array); 349d1bacf9eSBryan Schumaker goto out; 350d1bacf9eSBryan Schumaker } 351d1bacf9eSBryan Schumaker 352d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 353d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 354d1bacf9eSBryan Schumaker else 355d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 356d1bacf9eSBryan Schumaker 35747c716cbSTrond Myklebust if (status == -EAGAIN) { 3580aded708STrond Myklebust desc->last_cookie = array->last_cookie; 359e47c085aSTrond Myklebust desc->current_index += array->size; 36047c716cbSTrond Myklebust desc->page_index++; 36147c716cbSTrond Myklebust } 362d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 363d1bacf9eSBryan Schumaker out: 364d1bacf9eSBryan Schumaker return status; 365d1bacf9eSBryan Schumaker } 366d1bacf9eSBryan Schumaker 367d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 368d1bacf9eSBryan Schumaker static 36956e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 370d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 371d1bacf9eSBryan Schumaker { 372480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 373480c2006SBryan Schumaker struct rpc_cred *cred = ctx->cred; 3744704f0e2STrond Myklebust unsigned long timestamp, gencount; 3751da177e4SLinus Torvalds int error; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds again: 3781da177e4SLinus Torvalds timestamp = jiffies; 3794704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 380be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3811da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3821da177e4SLinus Torvalds if (error < 0) { 3831da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3841da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3851da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3863a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 3871da177e4SLinus Torvalds desc->plus = 0; 3881da177e4SLinus Torvalds goto again; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds goto error; 3911da177e4SLinus Torvalds } 3921f4eab7eSNeil Brown desc->timestamp = timestamp; 3934704f0e2STrond Myklebust desc->gencount = gencount; 394d1bacf9eSBryan Schumaker error: 395d1bacf9eSBryan Schumaker return error; 396d1bacf9eSBryan Schumaker } 397d1bacf9eSBryan Schumaker 398573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 399573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 400d1bacf9eSBryan Schumaker { 401573c4e1eSChuck Lever int error; 402d1bacf9eSBryan Schumaker 403573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 404573c4e1eSChuck Lever if (error) 405573c4e1eSChuck Lever return error; 406d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 407d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 408d1bacf9eSBryan Schumaker return 0; 409d1bacf9eSBryan Schumaker } 410d1bacf9eSBryan Schumaker 411fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 412fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 413fa923369STrond Myklebust */ 414d39ab9deSBryan Schumaker static 415d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 416d39ab9deSBryan Schumaker { 417d8fdb47fSTrond Myklebust struct inode *inode; 418fa923369STrond Myklebust struct nfs_inode *nfsi; 419fa923369STrond Myklebust 4202b0143b5SDavid Howells if (d_really_is_negative(dentry)) 4212b0143b5SDavid Howells return 0; 422fa923369STrond Myklebust 423d8fdb47fSTrond Myklebust inode = d_inode(dentry); 424d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 425d8fdb47fSTrond Myklebust return 0; 426d8fdb47fSTrond Myklebust 427d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4287dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 429d39ab9deSBryan Schumaker return 0; 4307dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4317dc72d5fSTrond Myklebust return 0; 4327dc72d5fSTrond Myklebust return 1; 433d39ab9deSBryan Schumaker } 434d39ab9deSBryan Schumaker 435d39ab9deSBryan Schumaker static 43623db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 437d69ee9b8STrond Myklebust { 438d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 439d69ee9b8STrond Myklebust return false; 440d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 441d69ee9b8STrond Myklebust return true; 44223db8620SAl Viro if (ctx->pos == 0) 443d69ee9b8STrond Myklebust return true; 444d69ee9b8STrond Myklebust return false; 445d69ee9b8STrond Myklebust } 446d69ee9b8STrond Myklebust 447d69ee9b8STrond Myklebust /* 44863519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 44963519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 450d69ee9b8STrond Myklebust * directory. 451d69ee9b8STrond Myklebust */ 452d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 453d69ee9b8STrond Myklebust { 45463519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 45563519fbcSTrond Myklebust 45663519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 45763519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 45863519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 459d69ee9b8STrond Myklebust } 460d69ee9b8STrond Myklebust 461311324adSTrond Myklebust /* 462311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 463311324adSTrond Myklebust * 464311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 465311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 466311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 467311324adSTrond Myklebust * cache flush. 468311324adSTrond Myklebust */ 469311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 470311324adSTrond Myklebust { 47163519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 47263519fbcSTrond Myklebust 47363519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 47463519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 47563519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 47679f687a3STrond Myklebust invalidate_mapping_pages(dir->i_mapping, 0, -1); 477311324adSTrond Myklebust } 478311324adSTrond Myklebust } 479311324adSTrond Myklebust 480d69ee9b8STrond Myklebust static 481d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 482d39ab9deSBryan Schumaker { 48326fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4849ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4854a201d6eSTrond Myklebust struct dentry *dentry; 4864a201d6eSTrond Myklebust struct dentry *alias; 4872b0143b5SDavid Howells struct inode *dir = d_inode(parent); 488d39ab9deSBryan Schumaker struct inode *inode; 489aa9c2669SDavid Quigley int status; 490d39ab9deSBryan Schumaker 491fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 492fa923369STrond Myklebust return; 4936c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4946c441c25STrond Myklebust return; 49578d04af4STrond Myklebust if (filename.len == 0) 49678d04af4STrond Myklebust return; 49778d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 49878d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 49978d04af4STrond Myklebust return; 50078d04af4STrond Myklebust /* ...or '/' */ 50178d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 50278d04af4STrond Myklebust return; 5034a201d6eSTrond Myklebust if (filename.name[0] == '.') { 5044a201d6eSTrond Myklebust if (filename.len == 1) 5054a201d6eSTrond Myklebust return; 5064a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 5074a201d6eSTrond Myklebust return; 5084a201d6eSTrond Myklebust } 5098387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 510d39ab9deSBryan Schumaker 5114a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 5129ac3d3e8SAl Viro again: 5139ac3d3e8SAl Viro if (!dentry) { 5149ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 5159ac3d3e8SAl Viro if (IS_ERR(dentry)) 5169ac3d3e8SAl Viro return; 5179ac3d3e8SAl Viro } 5189ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 5196c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 5206c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 5216c441c25STrond Myklebust &entry->fattr->fsid)) 5226c441c25STrond Myklebust goto out; 523d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5247dc72d5fSTrond Myklebust if (!entry->fh->size) 5257dc72d5fSTrond Myklebust goto out; 526cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 5272b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 528aa9c2669SDavid Quigley if (!status) 5292b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 530d39ab9deSBryan Schumaker goto out; 531d39ab9deSBryan Schumaker } else { 5325542aa2fSEric W. Biederman d_invalidate(dentry); 533d39ab9deSBryan Schumaker dput(dentry); 5349ac3d3e8SAl Viro dentry = NULL; 5359ac3d3e8SAl Viro goto again; 536d39ab9deSBryan Schumaker } 537d39ab9deSBryan Schumaker } 5387dc72d5fSTrond Myklebust if (!entry->fh->size) { 5397dc72d5fSTrond Myklebust d_lookup_done(dentry); 5407dc72d5fSTrond Myklebust goto out; 5417dc72d5fSTrond Myklebust } 542d39ab9deSBryan Schumaker 5431775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 54441d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5459ac3d3e8SAl Viro d_lookup_done(dentry); 5469ac3d3e8SAl Viro if (alias) { 547d39ab9deSBryan Schumaker if (IS_ERR(alias)) 548d39ab9deSBryan Schumaker goto out; 5499ac3d3e8SAl Viro dput(dentry); 5509ac3d3e8SAl Viro dentry = alias; 5519ac3d3e8SAl Viro } 552d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 553d39ab9deSBryan Schumaker out: 554d39ab9deSBryan Schumaker dput(dentry); 555d39ab9deSBryan Schumaker } 556d39ab9deSBryan Schumaker 557d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 558d1bacf9eSBryan Schumaker static 5598cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5606650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 561d1bacf9eSBryan Schumaker { 562babddc72SBryan Schumaker struct xdr_stream stream; 563f7da7a12SBenny Halevy struct xdr_buf buf; 5646650239aSTrond Myklebust struct page *scratch; 56599424380SBryan Schumaker struct nfs_cache_array *array; 5665c346854STrond Myklebust unsigned int count = 0; 5675c346854STrond Myklebust int status; 568babddc72SBryan Schumaker 5696650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5706650239aSTrond Myklebust if (scratch == NULL) 5716650239aSTrond Myklebust return -ENOMEM; 572babddc72SBryan Schumaker 573ce85cfbeSBenjamin Coddington if (buflen == 0) 574ce85cfbeSBenjamin Coddington goto out_nopages; 575ce85cfbeSBenjamin Coddington 576f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5776650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 57899424380SBryan Schumaker 57999424380SBryan Schumaker do { 58099424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5818cd51a0cSTrond Myklebust if (status != 0) { 5828cd51a0cSTrond Myklebust if (status == -EAGAIN) 5838cd51a0cSTrond Myklebust status = 0; 58499424380SBryan Schumaker break; 5858cd51a0cSTrond Myklebust } 58699424380SBryan Schumaker 5875c346854STrond Myklebust count++; 5885c346854STrond Myklebust 58947c716cbSTrond Myklebust if (desc->plus != 0) 590be62a1a8SMiklos Szeredi nfs_prime_dcache(file_dentry(desc->file), entry); 5918cd51a0cSTrond Myklebust 5928cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5938cd51a0cSTrond Myklebust if (status != 0) 5948cd51a0cSTrond Myklebust break; 59599424380SBryan Schumaker } while (!entry->eof); 59699424380SBryan Schumaker 597ce85cfbeSBenjamin Coddington out_nopages: 59847c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 59999424380SBryan Schumaker array = nfs_readdir_get_array(page); 6008cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 6018cd51a0cSTrond Myklebust array->eof_index = array->size; 60299424380SBryan Schumaker status = 0; 60399424380SBryan Schumaker nfs_readdir_release_array(page); 6045c346854STrond Myklebust } else 6055c346854STrond Myklebust status = PTR_ERR(array); 60656e4ebf8SBryan Schumaker } 6076650239aSTrond Myklebust 6086650239aSTrond Myklebust put_page(scratch); 6098cd51a0cSTrond Myklebust return status; 6108cd51a0cSTrond Myklebust } 61156e4ebf8SBryan Schumaker 61256e4ebf8SBryan Schumaker static 613c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 61456e4ebf8SBryan Schumaker { 61556e4ebf8SBryan Schumaker unsigned int i; 61656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 61756e4ebf8SBryan Schumaker put_page(pages[i]); 61856e4ebf8SBryan Schumaker } 61956e4ebf8SBryan Schumaker 62056e4ebf8SBryan Schumaker /* 62156e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 6220b936e37SAnna Schumaker * to nfs_readdir_free_pagearray 62356e4ebf8SBryan Schumaker */ 62456e4ebf8SBryan Schumaker static 625c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 62656e4ebf8SBryan Schumaker { 62756e4ebf8SBryan Schumaker unsigned int i; 62856e4ebf8SBryan Schumaker 62956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 63056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 63156e4ebf8SBryan Schumaker if (page == NULL) 63256e4ebf8SBryan Schumaker goto out_freepages; 63356e4ebf8SBryan Schumaker pages[i] = page; 63456e4ebf8SBryan Schumaker } 6356650239aSTrond Myklebust return 0; 63656e4ebf8SBryan Schumaker 63756e4ebf8SBryan Schumaker out_freepages: 638c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6396650239aSTrond Myklebust return -ENOMEM; 640d1bacf9eSBryan Schumaker } 641d1bacf9eSBryan Schumaker 642d1bacf9eSBryan Schumaker static 643d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 644d1bacf9eSBryan Schumaker { 64556e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 646d1bacf9eSBryan Schumaker struct nfs_entry entry; 647d1bacf9eSBryan Schumaker struct file *file = desc->file; 648d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6498cd51a0cSTrond Myklebust int status = -ENOMEM; 65056e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 651d1bacf9eSBryan Schumaker 652d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6530aded708STrond Myklebust entry.cookie = desc->last_cookie; 654d1bacf9eSBryan Schumaker entry.eof = 0; 655d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 656d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 657573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 658d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 659d1bacf9eSBryan Schumaker goto out; 660d1bacf9eSBryan Schumaker 66114c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 66214c43f76SDavid Quigley if (IS_ERR(entry.label)) { 66314c43f76SDavid Quigley status = PTR_ERR(entry.label); 66414c43f76SDavid Quigley goto out; 66514c43f76SDavid Quigley } 66614c43f76SDavid Quigley 667d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 6688cd51a0cSTrond Myklebust if (IS_ERR(array)) { 6698cd51a0cSTrond Myklebust status = PTR_ERR(array); 67014c43f76SDavid Quigley goto out_label_free; 6718cd51a0cSTrond Myklebust } 672d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 673d1bacf9eSBryan Schumaker array->eof_index = -1; 674d1bacf9eSBryan Schumaker 675c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6766650239aSTrond Myklebust if (status < 0) 677d1bacf9eSBryan Schumaker goto out_release_array; 678d1bacf9eSBryan Schumaker do { 679ac396128STrond Myklebust unsigned int pglen; 68056e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 681babddc72SBryan Schumaker 682d1bacf9eSBryan Schumaker if (status < 0) 683d1bacf9eSBryan Schumaker break; 684ac396128STrond Myklebust pglen = status; 6856650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6868cd51a0cSTrond Myklebust if (status < 0) { 6878cd51a0cSTrond Myklebust if (status == -ENOSPC) 6888cd51a0cSTrond Myklebust status = 0; 6898cd51a0cSTrond Myklebust break; 6908cd51a0cSTrond Myklebust } 6918cd51a0cSTrond Myklebust } while (array->eof_index < 0); 692d1bacf9eSBryan Schumaker 693c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 694d1bacf9eSBryan Schumaker out_release_array: 695d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 69614c43f76SDavid Quigley out_label_free: 69714c43f76SDavid Quigley nfs4_label_free(entry.label); 698d1bacf9eSBryan Schumaker out: 699d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 700d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 701d1bacf9eSBryan Schumaker return status; 702d1bacf9eSBryan Schumaker } 703d1bacf9eSBryan Schumaker 704d1bacf9eSBryan Schumaker /* 705d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 706d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 707d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 708d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7091da177e4SLinus Torvalds */ 710d1bacf9eSBryan Schumaker static 711d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 712d1bacf9eSBryan Schumaker { 713496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7148cd51a0cSTrond Myklebust int ret; 715d1bacf9eSBryan Schumaker 7168cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7178cd51a0cSTrond Myklebust if (ret < 0) 718d1bacf9eSBryan Schumaker goto error; 719d1bacf9eSBryan Schumaker SetPageUptodate(page); 720d1bacf9eSBryan Schumaker 7212aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 722cd9ae2b6STrond Myklebust /* Should never happen */ 723cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 724cd9ae2b6STrond Myklebust } 7251da177e4SLinus Torvalds unlock_page(page); 7261da177e4SLinus Torvalds return 0; 7271da177e4SLinus Torvalds error: 7281da177e4SLinus Torvalds unlock_page(page); 7298cd51a0cSTrond Myklebust return ret; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 732d1bacf9eSBryan Schumaker static 733d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7341da177e4SLinus Torvalds { 735*b044f645SBenjamin Coddington if (!desc->page->mapping) 73611de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 73709cbfeafSKirill A. Shutemov put_page(desc->page); 7381da177e4SLinus Torvalds desc->page = NULL; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741d1bacf9eSBryan Schumaker static 742d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7431da177e4SLinus Torvalds { 744*b044f645SBenjamin Coddington return read_cache_page(desc->file->f_mapping, 745d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /* 749d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7501da177e4SLinus Torvalds */ 751d1bacf9eSBryan Schumaker static 752d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 753d1bacf9eSBryan Schumaker { 754d1bacf9eSBryan Schumaker int res; 755d1bacf9eSBryan Schumaker 756d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 757d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 758d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 759d1bacf9eSBryan Schumaker 760d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 76147c716cbSTrond Myklebust if (res != 0) 762d1bacf9eSBryan Schumaker cache_page_release(desc); 763d1bacf9eSBryan Schumaker return res; 764d1bacf9eSBryan Schumaker } 765d1bacf9eSBryan Schumaker 766d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7671da177e4SLinus Torvalds static inline 7681da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7691da177e4SLinus Torvalds { 7708cd51a0cSTrond Myklebust int res; 771d1bacf9eSBryan Schumaker 7720aded708STrond Myklebust if (desc->page_index == 0) { 7738cd51a0cSTrond Myklebust desc->current_index = 0; 7740aded708STrond Myklebust desc->last_cookie = 0; 7750aded708STrond Myklebust } 77647c716cbSTrond Myklebust do { 777d1bacf9eSBryan Schumaker res = find_cache_page(desc); 77847c716cbSTrond Myklebust } while (res == -EAGAIN); 7791da177e4SLinus Torvalds return res; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7841da177e4SLinus Torvalds */ 7851da177e4SLinus Torvalds static 78623db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7871da177e4SLinus Torvalds { 7881da177e4SLinus Torvalds struct file *file = desc->file; 789d1bacf9eSBryan Schumaker int i = 0; 790d1bacf9eSBryan Schumaker int res = 0; 791d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7928ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7938ef2ce3eSBryan Schumaker 794d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 795e7c58e97STrond Myklebust if (IS_ERR(array)) { 796e7c58e97STrond Myklebust res = PTR_ERR(array); 797e7c58e97STrond Myklebust goto out; 798e7c58e97STrond Myklebust } 7991da177e4SLinus Torvalds 800d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 801ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8021da177e4SLinus Torvalds 803ece0b423STrond Myklebust ent = &array->array[i]; 80423db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 80523db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 806ece0b423STrond Myklebust desc->eof = 1; 8071da177e4SLinus Torvalds break; 808ece0b423STrond Myklebust } 80923db8620SAl Viro desc->ctx->pos++; 810d1bacf9eSBryan Schumaker if (i < (array->size-1)) 811d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 812d1bacf9eSBryan Schumaker else 813d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 8140c030806STrond Myklebust if (ctx->duped != 0) 8150c030806STrond Myklebust ctx->duped = 1; 8168cd51a0cSTrond Myklebust } 81747c716cbSTrond Myklebust if (array->eof_index >= 0) 818d1bacf9eSBryan Schumaker desc->eof = 1; 819d1bacf9eSBryan Schumaker 820d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 821e7c58e97STrond Myklebust out: 822d1bacf9eSBryan Schumaker cache_page_release(desc); 8231e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8241e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8251da177e4SLinus Torvalds return res; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* 8291da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8301da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8311da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8321da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8331da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8341da177e4SLinus Torvalds * 8351da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8361da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8371da177e4SLinus Torvalds * we should already have a complete representation of the 8381da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8391da177e4SLinus Torvalds */ 8401da177e4SLinus Torvalds static inline 84123db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8421da177e4SLinus Torvalds { 8431da177e4SLinus Torvalds struct page *page = NULL; 8441da177e4SLinus Torvalds int status; 845496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8460c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8471da177e4SLinus Torvalds 8481e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8491e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8521da177e4SLinus Torvalds if (!page) { 8531da177e4SLinus Torvalds status = -ENOMEM; 8541da177e4SLinus Torvalds goto out; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8577a8e1dc3STrond Myklebust desc->page_index = 0; 8580aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8597a8e1dc3STrond Myklebust desc->page = page; 8600c030806STrond Myklebust ctx->duped = 0; 8617a8e1dc3STrond Myklebust 86285f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 86385f8607eSTrond Myklebust if (status < 0) 864d1bacf9eSBryan Schumaker goto out_release; 865d1bacf9eSBryan Schumaker 86623db8620SAl Viro status = nfs_do_filldir(desc); 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds out: 8691e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8703110ff80SHarvey Harrison __func__, status); 8711da177e4SLinus Torvalds return status; 8721da177e4SLinus Torvalds out_release: 873d1bacf9eSBryan Schumaker cache_page_release(desc); 8741da177e4SLinus Torvalds goto out; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 87700a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 87800a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 87900a92642SOlivier Galibert whole directory. 8801da177e4SLinus Torvalds */ 88123db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8821da177e4SLinus Torvalds { 883be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8842b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8851da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8861da177e4SLinus Torvalds *desc = &my_desc; 88723db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 88807b5ce8eSScott Mayhew int res = 0; 8891da177e4SLinus Torvalds 8906de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8916de1472fSAl Viro file, (long long)ctx->pos); 89291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 89391d5b470SChuck Lever 8941da177e4SLinus Torvalds /* 89523db8620SAl Viro * ctx->pos points to the dirent entry number. 896f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 89700a92642SOlivier Galibert * to either find the entry with the appropriate number or 89800a92642SOlivier Galibert * revalidate the cookie. 8991da177e4SLinus Torvalds */ 9001da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 9011da177e4SLinus Torvalds 90223db8620SAl Viro desc->file = file; 90323db8620SAl Viro desc->ctx = ctx; 904480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 9051da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 90623db8620SAl Viro desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0; 9071da177e4SLinus Torvalds 90879f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 90923db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 910fccca7fcSTrond Myklebust if (res < 0) 911fccca7fcSTrond Myklebust goto out; 912fccca7fcSTrond Myklebust 91347c716cbSTrond Myklebust do { 9141da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 91500a92642SOlivier Galibert 9161da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 917ece0b423STrond Myklebust res = 0; 9181da177e4SLinus Torvalds /* This means either end of directory */ 919d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 9201da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 92123db8620SAl Viro res = uncached_readdir(desc); 922ece0b423STrond Myklebust if (res == 0) 9231da177e4SLinus Torvalds continue; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds break; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9283a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9291da177e4SLinus Torvalds nfs_zap_caches(inode); 930baf57a09STrond Myklebust desc->page_index = 0; 9311da177e4SLinus Torvalds desc->plus = 0; 932d1bacf9eSBryan Schumaker desc->eof = 0; 9331da177e4SLinus Torvalds continue; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds if (res < 0) 9361da177e4SLinus Torvalds break; 9371da177e4SLinus Torvalds 93823db8620SAl Viro res = nfs_do_filldir(desc); 939ece0b423STrond Myklebust if (res < 0) 9401da177e4SLinus Torvalds break; 94147c716cbSTrond Myklebust } while (!desc->eof); 942fccca7fcSTrond Myklebust out: 9431e7cb3dcSChuck Lever if (res > 0) 9441e7cb3dcSChuck Lever res = 0; 9456de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9461da177e4SLinus Torvalds return res; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 949965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 950f0dd2136STrond Myklebust { 951*b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 952480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 953b84e06c5SChuck Lever 9546de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9556de1472fSAl Viro filp, offset, whence); 956b84e06c5SChuck Lever 957*b044f645SBenjamin Coddington inode_lock(inode); 958965c8e59SAndrew Morton switch (whence) { 959f0dd2136STrond Myklebust case 1: 960f0dd2136STrond Myklebust offset += filp->f_pos; 961f0dd2136STrond Myklebust case 0: 962f0dd2136STrond Myklebust if (offset >= 0) 963f0dd2136STrond Myklebust break; 964f0dd2136STrond Myklebust default: 965*b044f645SBenjamin Coddington offset = -EINVAL; 966*b044f645SBenjamin Coddington goto out; 967f0dd2136STrond Myklebust } 968f0dd2136STrond Myklebust if (offset != filp->f_pos) { 969f0dd2136STrond Myklebust filp->f_pos = offset; 970480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9718ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 972f0dd2136STrond Myklebust } 973*b044f645SBenjamin Coddington out: 974*b044f645SBenjamin Coddington inode_unlock(inode); 975f0dd2136STrond Myklebust return offset; 976f0dd2136STrond Myklebust } 977f0dd2136STrond Myklebust 9781da177e4SLinus Torvalds /* 9791da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9801da177e4SLinus Torvalds * is a dummy operation. 9811da177e4SLinus Torvalds */ 98202c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 98302c24a82SJosef Bacik int datasync) 9841da177e4SLinus Torvalds { 9856de1472fSAl Viro struct inode *inode = file_inode(filp); 9867ea80859SChristoph Hellwig 9876de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9881e7cb3dcSChuck Lever 9895955102cSAl Viro inode_lock(inode); 9906de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9915955102cSAl Viro inode_unlock(inode); 9921da177e4SLinus Torvalds return 0; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 995bfc69a45STrond Myklebust /** 996bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 997bfc69a45STrond Myklebust * @dir - pointer to directory inode 998bfc69a45STrond Myklebust * 999bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1000bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1001bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1002bfc69a45STrond Myklebust * 1003bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1004bfc69a45STrond Myklebust */ 1005bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1006bfc69a45STrond Myklebust { 1007011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 1008bfc69a45STrond Myklebust } 100989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1010bfc69a45STrond Myklebust 10111da177e4SLinus Torvalds /* 10121da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 10131da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10141da177e4SLinus Torvalds * and may need to be looked up again. 1015912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 10161da177e4SLinus Torvalds */ 1017912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1018912a108dSNeilBrown int rcu_walk) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10211da177e4SLinus Torvalds return 1; 10224eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10234eec952eSTrond Myklebust return 0; 1024f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10256ecc5e8fSTrond Myklebust return 0; 1026f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 10271cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1028912a108dSNeilBrown if (rcu_walk) 1029f2c77f4eSTrond Myklebust return 0; 10301cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 10311cd9cb05STrond Myklebust return 0; 10321cd9cb05STrond Myklebust } 1033f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1034f2c77f4eSTrond Myklebust return 0; 1035f2c77f4eSTrond Myklebust return 1; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /* 1039a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1040a12802caSTrond Myklebust * an O_EXCL create using this path component. 1041a12802caSTrond Myklebust */ 1042fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1043a12802caSTrond Myklebust { 1044a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1045a12802caSTrond Myklebust return 0; 1046fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1047a12802caSTrond Myklebust } 1048a12802caSTrond Myklebust 1049a12802caSTrond Myklebust /* 10501d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10511d6757fbSTrond Myklebust * 10521d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10531d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10541d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10551d6757fbSTrond Myklebust * 10561d6757fbSTrond Myklebust */ 105765a0c149STrond Myklebust static 1058fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10591da177e4SLinus Torvalds { 10601da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 106165a0c149STrond Myklebust int ret; 10621da177e4SLinus Torvalds 106336d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10644e99a1ffSTrond Myklebust return 0; 10651da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1066fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10671da177e4SLinus Torvalds goto out_force; 10681da177e4SLinus Torvalds /* This is an open(2) */ 1069fa3c56bbSAl Viro if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) && 1070fa3c56bbSAl Viro (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) 10711da177e4SLinus Torvalds goto out_force; 107265a0c149STrond Myklebust out: 107365a0c149STrond Myklebust return (inode->i_nlink == 0) ? -ENOENT : 0; 10741da177e4SLinus Torvalds out_force: 10751fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10761fa1e384SNeilBrown return -ECHILD; 107765a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 107865a0c149STrond Myklebust if (ret != 0) 107965a0c149STrond Myklebust return ret; 108065a0c149STrond Myklebust goto out; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* 10841da177e4SLinus Torvalds * We judge how long we want to trust negative 10851da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10861da177e4SLinus Torvalds * 10871da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10881da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1089912a108dSNeilBrown * 1090912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1091912a108dSNeilBrown * suggesting a reval is needed. 10921da177e4SLinus Torvalds */ 10931da177e4SLinus Torvalds static inline 10941da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1095fa3c56bbSAl Viro unsigned int flags) 10961da177e4SLinus Torvalds { 10971da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 1098fa3c56bbSAl Viro if (flags & LOOKUP_CREATE) 10991da177e4SLinus Torvalds return 0; 11004eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 11014eec952eSTrond Myklebust return 1; 1102912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds /* 11061da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 11071da177e4SLinus Torvalds * and we should check whether we can really trust that 11081da177e4SLinus Torvalds * lookup. 11091da177e4SLinus Torvalds * 11101da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 11111da177e4SLinus Torvalds * we have an inode! 11121da177e4SLinus Torvalds * 11131da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 11141da177e4SLinus Torvalds * cached dentry and do a new lookup. 11151da177e4SLinus Torvalds */ 11160b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 11171da177e4SLinus Torvalds { 11181da177e4SLinus Torvalds struct inode *dir; 11191da177e4SLinus Torvalds struct inode *inode; 11201da177e4SLinus Torvalds struct dentry *parent; 1121e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1122e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 11231775fd3eSDavid Quigley struct nfs4_label *label = NULL; 11241da177e4SLinus Torvalds int error; 11251da177e4SLinus Torvalds 1126d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) { 112750d77739SNeilBrown parent = ACCESS_ONCE(dentry->d_parent); 11282b0143b5SDavid Howells dir = d_inode_rcu(parent); 1129d51ac1a8SNeilBrown if (!dir) 113034286d66SNick Piggin return -ECHILD; 1131d51ac1a8SNeilBrown } else { 11321da177e4SLinus Torvalds parent = dget_parent(dentry); 11332b0143b5SDavid Howells dir = d_inode(parent); 1134d51ac1a8SNeilBrown } 113591d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 11362b0143b5SDavid Howells inode = d_inode(dentry); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds if (!inode) { 1139912a108dSNeilBrown if (nfs_neg_need_reval(dir, dentry, flags)) { 1140d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1141d51ac1a8SNeilBrown return -ECHILD; 11421da177e4SLinus Torvalds goto out_bad; 1143912a108dSNeilBrown } 114463519fbcSTrond Myklebust goto out_valid; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds if (is_bad_inode(inode)) { 1148d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1149d51ac1a8SNeilBrown return -ECHILD; 11506de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 11516de1472fSAl Viro __func__, dentry); 11521da177e4SLinus Torvalds goto out_bad; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 1155011e2a7fSBryan Schumaker if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 115615860ab1STrond Myklebust goto out_set_verifier; 115715860ab1STrond Myklebust 1158912a108dSNeilBrown /* Force a full look up iff the parent directory has changed */ 1159912a108dSNeilBrown if (!nfs_is_exclusive_create(dir, flags) && 1160912a108dSNeilBrown nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 1161912a108dSNeilBrown 11621fa1e384SNeilBrown if (nfs_lookup_verify_inode(inode, flags)) { 1163d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1164d51ac1a8SNeilBrown return -ECHILD; 11651da177e4SLinus Torvalds goto out_zap_parent; 11661fa1e384SNeilBrown } 116763519fbcSTrond Myklebust nfs_advise_use_readdirplus(dir); 11681da177e4SLinus Torvalds goto out_valid; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 1171912a108dSNeilBrown if (flags & LOOKUP_RCU) 1172912a108dSNeilBrown return -ECHILD; 1173912a108dSNeilBrown 11741da177e4SLinus Torvalds if (NFS_STALE(inode)) 11751da177e4SLinus Torvalds goto out_bad; 11761da177e4SLinus Torvalds 1177e1fb4d05STrond Myklebust error = -ENOMEM; 1178e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1179e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1180e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1181e1fb4d05STrond Myklebust goto out_error; 1182e1fb4d05STrond Myklebust 118314c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 118414c43f76SDavid Quigley if (IS_ERR(label)) 118514c43f76SDavid Quigley goto out_error; 118614c43f76SDavid Quigley 11876e0d0be7STrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 11881775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 11896e0d0be7STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 11901da177e4SLinus Torvalds if (error) 11911da177e4SLinus Torvalds goto out_bad; 1192e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11931da177e4SLinus Torvalds goto out_bad; 1194e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 11951da177e4SLinus Torvalds goto out_bad; 11961da177e4SLinus Torvalds 1197aa9c2669SDavid Quigley nfs_setsecurity(inode, fattr, label); 1198aa9c2669SDavid Quigley 1199e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1200e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 120114c43f76SDavid Quigley nfs4_label_free(label); 120214c43f76SDavid Quigley 120363519fbcSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 120463519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 120563519fbcSTrond Myklebust 120615860ab1STrond Myklebust out_set_verifier: 1207cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12081da177e4SLinus Torvalds out_valid: 1209d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) { 121050d77739SNeilBrown if (parent != ACCESS_ONCE(dentry->d_parent)) 1211d51ac1a8SNeilBrown return -ECHILD; 1212d51ac1a8SNeilBrown } else 12131da177e4SLinus Torvalds dput(parent); 12146de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12156de1472fSAl Viro __func__, dentry); 12161da177e4SLinus Torvalds return 1; 12171da177e4SLinus Torvalds out_zap_parent: 12181da177e4SLinus Torvalds nfs_zap_caches(dir); 12191da177e4SLinus Torvalds out_bad: 1220d51ac1a8SNeilBrown WARN_ON(flags & LOOKUP_RCU); 1221c44600c9SAl Viro nfs_free_fattr(fattr); 1222c44600c9SAl Viro nfs_free_fhandle(fhandle); 122314c43f76SDavid Quigley nfs4_label_free(label); 1224a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12251da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12261da177e4SLinus Torvalds /* Purge readdir caches. */ 12271da177e4SLinus Torvalds nfs_zap_caches(inode); 1228a3f432bfSJ. Bruce Fields /* 1229a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1230a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1231a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1232a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1233a3f432bfSJ. Bruce Fields */ 1234a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 1235d9e80b7dSAl Viro goto out_valid; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds dput(parent); 12386de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12396de1472fSAl Viro __func__, dentry); 12401da177e4SLinus Torvalds return 0; 1241e1fb4d05STrond Myklebust out_error: 1242d51ac1a8SNeilBrown WARN_ON(flags & LOOKUP_RCU); 1243e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1244e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 124514c43f76SDavid Quigley nfs4_label_free(label); 1246e1fb4d05STrond Myklebust dput(parent); 12476de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12486de1472fSAl Viro __func__, dentry, error); 1249e1fb4d05STrond Myklebust return error; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* 12532b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1254ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1255ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1256ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1257ecf3d1f1SJeff Layton * 1258ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1259ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1260ecf3d1f1SJeff Layton */ 1261ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1262ecf3d1f1SJeff Layton { 12632b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 12649cdd1d3fSTrond Myklebust int error = 0; 1265ecf3d1f1SJeff Layton 1266ecf3d1f1SJeff Layton /* 1267ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1268ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1269ecf3d1f1SJeff Layton * eventually need to do something more here. 1270ecf3d1f1SJeff Layton */ 1271ecf3d1f1SJeff Layton if (!inode) { 12726de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 12736de1472fSAl Viro __func__, dentry); 1274ecf3d1f1SJeff Layton return 1; 1275ecf3d1f1SJeff Layton } 1276ecf3d1f1SJeff Layton 1277ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 12786de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12796de1472fSAl Viro __func__, dentry); 1280ecf3d1f1SJeff Layton return 0; 1281ecf3d1f1SJeff Layton } 1282ecf3d1f1SJeff Layton 12839cdd1d3fSTrond Myklebust if (nfs_mapping_need_revalidate_inode(inode)) 12849cdd1d3fSTrond Myklebust error = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 1285ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1286ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1287ecf3d1f1SJeff Layton return !error; 1288ecf3d1f1SJeff Layton } 1289ecf3d1f1SJeff Layton 1290ecf3d1f1SJeff Layton /* 12911da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 12921da177e4SLinus Torvalds */ 1293fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 12941da177e4SLinus Torvalds { 12956de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 12966de1472fSAl Viro dentry, dentry->d_flags); 12971da177e4SLinus Torvalds 129877f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 12992b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 130077f11192STrond Myklebust return 1; 130177f11192STrond Myklebust 13021da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13031da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13041da177e4SLinus Torvalds return 1; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 13071da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13081da177e4SLinus Torvalds * files will be cleaned up during umount */ 13091da177e4SLinus Torvalds return 1; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds return 0; 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13151f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13161b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13171b83d707STrond Myklebust { 13181b83d707STrond Myklebust spin_lock(&inode->i_lock); 13191f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 13201f018458STrond Myklebust if (inode->i_nlink == 1) 13211f018458STrond Myklebust clear_nlink(inode); 13221f018458STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; 13231b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13241b83d707STrond Myklebust } 13251b83d707STrond Myklebust 13261da177e4SLinus Torvalds /* 13271da177e4SLinus Torvalds * Called when the dentry loses inode. 13281da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13291da177e4SLinus Torvalds */ 13301da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13311da177e4SLinus Torvalds { 133283672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 133383672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 133483672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 133583672d39SNeil Brown 13361da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1337e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13381f018458STrond Myklebust nfs_drop_nlink(inode); 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds iput(inode); 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 1343b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1344b1942c5fSAl Viro { 1345b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1346b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1347b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1348b1942c5fSAl Viro WARN_ON(1); 1349b1942c5fSAl Viro else 1350b1942c5fSAl Viro kfree(dentry->d_fsdata); 1351b1942c5fSAl Viro } 1352b1942c5fSAl Viro } 1353b1942c5fSAl Viro 1354f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 13551da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1356ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 13571da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13581da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 135936d43a43SDavid Howells .d_automount = nfs_d_automount, 1360b1942c5fSAl Viro .d_release = nfs_d_release, 13611da177e4SLinus Torvalds }; 1362ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 13631da177e4SLinus Torvalds 1364597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 13651da177e4SLinus Torvalds { 13661da177e4SLinus Torvalds struct dentry *res; 13671da177e4SLinus Torvalds struct inode *inode = NULL; 1368e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1369e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 13701775fd3eSDavid Quigley struct nfs4_label *label = NULL; 13711da177e4SLinus Torvalds int error; 13721da177e4SLinus Torvalds 13736de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 137491d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 13751da177e4SLinus Torvalds 1376130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1377130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 13781da177e4SLinus Torvalds 1379fd684071STrond Myklebust /* 1380fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1381fd684071STrond Myklebust * but don't hash the dentry. 1382fd684071STrond Myklebust */ 1383130f9ab7SAl Viro if (nfs_is_exclusive_create(dir, flags)) 1384130f9ab7SAl Viro return NULL; 13851da177e4SLinus Torvalds 1386e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1387e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1388e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1389e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1390e1fb4d05STrond Myklebust goto out; 1391e1fb4d05STrond Myklebust 139214c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 139314c43f76SDavid Quigley if (IS_ERR(label)) 139414c43f76SDavid Quigley goto out; 139514c43f76SDavid Quigley 13966e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 13971775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 13981da177e4SLinus Torvalds if (error == -ENOENT) 13991da177e4SLinus Torvalds goto no_entry; 14001da177e4SLinus Torvalds if (error < 0) { 14011da177e4SLinus Torvalds res = ERR_PTR(error); 1402bf130914SAl Viro goto out_label; 14031da177e4SLinus Torvalds } 14041775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1405bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 140603f28e3aSTrond Myklebust if (IS_ERR(res)) 1407bf130914SAl Viro goto out_label; 140854ceac45SDavid Howells 140963519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 141063519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1411d69ee9b8STrond Myklebust 14121da177e4SLinus Torvalds no_entry: 141341d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14149eaef27bSTrond Myklebust if (res != NULL) { 14159eaef27bSTrond Myklebust if (IS_ERR(res)) 1416bf130914SAl Viro goto out_label; 14171da177e4SLinus Torvalds dentry = res; 14189eaef27bSTrond Myklebust } 14191da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1420bf130914SAl Viro out_label: 14216e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 142214c43f76SDavid Quigley nfs4_label_free(label); 14231da177e4SLinus Torvalds out: 1424e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1425e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14261da177e4SLinus Torvalds return res; 14271da177e4SLinus Torvalds } 1428ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14291da177e4SLinus Torvalds 143089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14310b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14321da177e4SLinus Torvalds 1433f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14340ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 14351da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14361da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 143736d43a43SDavid Howells .d_automount = nfs_d_automount, 1438b1942c5fSAl Viro .d_release = nfs_d_release, 14391da177e4SLinus Torvalds }; 144089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14411da177e4SLinus Torvalds 14428a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14438a5e929dSAl Viro { 14448a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14458a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14468a5e929dSAl Viro res |= FMODE_READ; 14478a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14488a5e929dSAl Viro res |= FMODE_WRITE; 14498a5e929dSAl Viro return res; 14508a5e929dSAl Viro } 14518a5e929dSAl Viro 1452532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1453cd9a1c0eSTrond Myklebust { 1454532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1455cd9a1c0eSTrond Myklebust } 1456cd9a1c0eSTrond Myklebust 1457cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1458cd9a1c0eSTrond Myklebust { 1459f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1460cd9a1c0eSTrond Myklebust return 0; 1461cd9a1c0eSTrond Myklebust } 1462cd9a1c0eSTrond Myklebust 1463d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 14640dd2b474SMiklos Szeredi struct dentry *dentry, 146530d90494SAl Viro struct file *file, unsigned open_flags, 146647237687SAl Viro int *opened) 1467cd9a1c0eSTrond Myklebust { 14680dd2b474SMiklos Szeredi int err; 14690dd2b474SMiklos Szeredi 147030d90494SAl Viro err = finish_open(file, dentry, do_open, opened); 147130d90494SAl Viro if (err) 1472d9585277SAl Viro goto out; 147330d90494SAl Viro nfs_file_set_open_context(file, ctx); 14740dd2b474SMiklos Szeredi 1475cd9a1c0eSTrond Myklebust out: 1476d9585277SAl Viro return err; 1477cd9a1c0eSTrond Myklebust } 1478cd9a1c0eSTrond Myklebust 147973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 148030d90494SAl Viro struct file *file, unsigned open_flags, 148147237687SAl Viro umode_t mode, int *opened) 14821da177e4SLinus Torvalds { 1483c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1484cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 14850dd2b474SMiklos Szeredi struct dentry *res; 14860dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1487f46e0bd3STrond Myklebust struct inode *inode; 14881472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1489c94c0953SAl Viro bool switched = false; 1490898f635cSTrond Myklebust int err; 14911da177e4SLinus Torvalds 14920dd2b474SMiklos Szeredi /* Expect a negative dentry */ 14932b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 14940dd2b474SMiklos Szeredi 14951e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 14966de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 14971e7cb3dcSChuck Lever 14989597c13bSJeff Layton err = nfs_check_flags(open_flags); 14999597c13bSJeff Layton if (err) 15009597c13bSJeff Layton return err; 15019597c13bSJeff Layton 15020dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15030dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 150400699ad8SAl Viro if (!d_in_lookup(dentry)) { 15050dd2b474SMiklos Szeredi /* 15060dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15070dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15080dd2b474SMiklos Szeredi * again 15090dd2b474SMiklos Szeredi */ 1510d9585277SAl Viro return -ENOENT; 15110dd2b474SMiklos Szeredi } 15121472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15131da177e4SLinus Torvalds goto no_open; 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds 15160dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1517d9585277SAl Viro return -ENAMETOOLONG; 15181da177e4SLinus Torvalds 15190dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1520dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1521dff25ddbSAndreas Gruenbacher 1522dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1523dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1524dff25ddbSAndreas Gruenbacher 1525536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1526dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 15270dd2b474SMiklos Szeredi } 1528536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1529536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1530536e43d1STrond Myklebust attr.ia_size = 0; 1531cd9a1c0eSTrond Myklebust } 1532cd9a1c0eSTrond Myklebust 1533c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1534c94c0953SAl Viro d_drop(dentry); 1535c94c0953SAl Viro switched = true; 1536c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1537c94c0953SAl Viro &dentry->d_name, &wq); 1538c94c0953SAl Viro if (IS_ERR(dentry)) 1539c94c0953SAl Viro return PTR_ERR(dentry); 1540c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1541c94c0953SAl Viro return finish_no_open(file, dentry); 1542c94c0953SAl Viro } 1543c94c0953SAl Viro 1544532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 15450dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15460dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1547d9585277SAl Viro goto out; 15480dd2b474SMiklos Szeredi 15496e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 15505bc2afc2STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened); 1551275bb307STrond Myklebust if (IS_ERR(inode)) { 15520dd2b474SMiklos Szeredi err = PTR_ERR(inode); 15536e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15542d9db750STrond Myklebust put_nfs_open_context(ctx); 1555d20cb71dSAl Viro d_drop(dentry); 15560dd2b474SMiklos Szeredi switch (err) { 15571da177e4SLinus Torvalds case -ENOENT: 1558f46e0bd3STrond Myklebust d_add(dentry, NULL); 1559809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15600dd2b474SMiklos Szeredi break; 15611788ea6eSJeff Layton case -EISDIR: 15626f926b5bSTrond Myklebust case -ENOTDIR: 15636f926b5bSTrond Myklebust goto no_open; 15641da177e4SLinus Torvalds case -ELOOP: 15650dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 15661da177e4SLinus Torvalds goto no_open; 15670dd2b474SMiklos Szeredi break; 15681da177e4SLinus Torvalds /* case -EINVAL: */ 15691da177e4SLinus Torvalds default: 15700dd2b474SMiklos Szeredi break; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds goto out; 15731da177e4SLinus Torvalds } 15740dd2b474SMiklos Szeredi 1575275bb307STrond Myklebust err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened); 15766e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15772d9db750STrond Myklebust put_nfs_open_context(ctx); 1578d9585277SAl Viro out: 1579c94c0953SAl Viro if (unlikely(switched)) { 1580c94c0953SAl Viro d_lookup_done(dentry); 1581c94c0953SAl Viro dput(dentry); 1582c94c0953SAl Viro } 1583d9585277SAl Viro return err; 15840dd2b474SMiklos Szeredi 15851da177e4SLinus Torvalds no_open: 15861472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1587c94c0953SAl Viro if (switched) { 1588c94c0953SAl Viro d_lookup_done(dentry); 1589c94c0953SAl Viro if (!res) 1590c94c0953SAl Viro res = dentry; 1591c94c0953SAl Viro else 1592c94c0953SAl Viro dput(dentry); 1593c94c0953SAl Viro } 15940dd2b474SMiklos Szeredi if (IS_ERR(res)) 1595c94c0953SAl Viro return PTR_ERR(res); 1596e45198a6SAl Viro return finish_no_open(file, res); 15971da177e4SLinus Torvalds } 159889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 15991da177e4SLinus Torvalds 16000b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 16011da177e4SLinus Torvalds { 1602657e94b6SNick Piggin struct inode *inode; 160350de348cSMiklos Szeredi int ret = 0; 16041da177e4SLinus Torvalds 1605fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1606eda72afbSMiklos Szeredi goto no_open; 1607eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 16085584c306STrond Myklebust goto no_open; 160949f9a0faSTrond Myklebust if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1) 161049f9a0faSTrond Myklebust goto no_open; 16112b484297STrond Myklebust 16122b0143b5SDavid Howells inode = d_inode(dentry); 16132b484297STrond Myklebust 16141da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 16151da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 16161da177e4SLinus Torvalds */ 1617216d5d06STrond Myklebust if (inode == NULL) { 161849317a7fSNeilBrown struct dentry *parent; 161949317a7fSNeilBrown struct inode *dir; 162049317a7fSNeilBrown 1621912a108dSNeilBrown if (flags & LOOKUP_RCU) { 162250d77739SNeilBrown parent = ACCESS_ONCE(dentry->d_parent); 16232b0143b5SDavid Howells dir = d_inode_rcu(parent); 1624912a108dSNeilBrown if (!dir) 1625d51ac1a8SNeilBrown return -ECHILD; 1626912a108dSNeilBrown } else { 162749317a7fSNeilBrown parent = dget_parent(dentry); 16282b0143b5SDavid Howells dir = d_inode(parent); 1629912a108dSNeilBrown } 1630fa3c56bbSAl Viro if (!nfs_neg_need_reval(dir, dentry, flags)) 1631216d5d06STrond Myklebust ret = 1; 1632912a108dSNeilBrown else if (flags & LOOKUP_RCU) 1633912a108dSNeilBrown ret = -ECHILD; 1634912a108dSNeilBrown if (!(flags & LOOKUP_RCU)) 163549317a7fSNeilBrown dput(parent); 163650d77739SNeilBrown else if (parent != ACCESS_ONCE(dentry->d_parent)) 1637912a108dSNeilBrown return -ECHILD; 16381da177e4SLinus Torvalds goto out; 1639216d5d06STrond Myklebust } 1640216d5d06STrond Myklebust 16411da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16421da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 164349317a7fSNeilBrown goto no_open; 16441da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1645fa3c56bbSAl Viro if (flags & LOOKUP_EXCL) 164649317a7fSNeilBrown goto no_open; 16471da177e4SLinus Torvalds 16480ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1649898f635cSTrond Myklebust ret = 1; 16500ef97dcfSMiklos Szeredi 16511da177e4SLinus Torvalds out: 16521da177e4SLinus Torvalds return ret; 1653535918f1STrond Myklebust 16545584c306STrond Myklebust no_open: 16550b728e19SAl Viro return nfs_lookup_revalidate(dentry, flags); 1656c0204fd2STrond Myklebust } 1657c0204fd2STrond Myklebust 16581da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds /* 16611da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 16621da177e4SLinus Torvalds */ 16631da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 16641775fd3eSDavid Quigley struct nfs_fattr *fattr, 16651775fd3eSDavid Quigley struct nfs4_label *label) 16661da177e4SLinus Torvalds { 1667fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 16682b0143b5SDavid Howells struct inode *dir = d_inode(parent); 16691da177e4SLinus Torvalds struct inode *inode; 16701da177e4SLinus Torvalds int error = -EACCES; 16711da177e4SLinus Torvalds 1672fab728e1STrond Myklebust d_drop(dentry); 1673fab728e1STrond Myklebust 16741da177e4SLinus Torvalds /* We may have been initialized further down */ 16752b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1676fab728e1STrond Myklebust goto out; 16771da177e4SLinus Torvalds if (fhandle->size == 0) { 16781775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL); 16791da177e4SLinus Torvalds if (error) 1680fab728e1STrond Myklebust goto out_error; 16811da177e4SLinus Torvalds } 16825724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16831da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 16841da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 16851775fd3eSDavid Quigley error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL); 16861da177e4SLinus Torvalds if (error < 0) 1687fab728e1STrond Myklebust goto out_error; 16881da177e4SLinus Torvalds } 16891775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 169003f28e3aSTrond Myklebust error = PTR_ERR(inode); 169103f28e3aSTrond Myklebust if (IS_ERR(inode)) 1692fab728e1STrond Myklebust goto out_error; 1693fab728e1STrond Myklebust d_add(dentry, inode); 1694fab728e1STrond Myklebust out: 1695fab728e1STrond Myklebust dput(parent); 16961da177e4SLinus Torvalds return 0; 1697fab728e1STrond Myklebust out_error: 1698fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1699fab728e1STrond Myklebust dput(parent); 1700fab728e1STrond Myklebust return error; 17011da177e4SLinus Torvalds } 1702ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds /* 17051da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17061da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17071da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17081da177e4SLinus Torvalds * reply path made it appear to have failed. 17091da177e4SLinus Torvalds */ 1710597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1711ebfc3b49SAl Viro umode_t mode, bool excl) 17121da177e4SLinus Torvalds { 17131da177e4SLinus Torvalds struct iattr attr; 1714ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 17151da177e4SLinus Torvalds int error; 17161da177e4SLinus Torvalds 17171e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 17186de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds attr.ia_mode = mode; 17211da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17221da177e4SLinus Torvalds 17238b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17248867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17258b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17261da177e4SLinus Torvalds if (error != 0) 17271da177e4SLinus Torvalds goto out_err; 17281da177e4SLinus Torvalds return 0; 17291da177e4SLinus Torvalds out_err: 17301da177e4SLinus Torvalds d_drop(dentry); 17311da177e4SLinus Torvalds return error; 17321da177e4SLinus Torvalds } 1733ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds /* 17361da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17371da177e4SLinus Torvalds */ 1738597d9289SBryan Schumaker int 17391a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17401da177e4SLinus Torvalds { 17411da177e4SLinus Torvalds struct iattr attr; 17421da177e4SLinus Torvalds int status; 17431da177e4SLinus Torvalds 17441e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 17456de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds attr.ia_mode = mode; 17481da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17491da177e4SLinus Torvalds 17501ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 17511da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 17521ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 17531da177e4SLinus Torvalds if (status != 0) 17541da177e4SLinus Torvalds goto out_err; 17551da177e4SLinus Torvalds return 0; 17561da177e4SLinus Torvalds out_err: 17571da177e4SLinus Torvalds d_drop(dentry); 17581da177e4SLinus Torvalds return status; 17591da177e4SLinus Torvalds } 1760ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds /* 17631da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17641da177e4SLinus Torvalds */ 1765597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds struct iattr attr; 17681da177e4SLinus Torvalds int error; 17691da177e4SLinus Torvalds 17701e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 17716de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17741da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 17751da177e4SLinus Torvalds 17761ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 17771da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 17781ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 17791da177e4SLinus Torvalds if (error != 0) 17801da177e4SLinus Torvalds goto out_err; 17811da177e4SLinus Torvalds return 0; 17821da177e4SLinus Torvalds out_err: 17831da177e4SLinus Torvalds d_drop(dentry); 17841da177e4SLinus Torvalds return error; 17851da177e4SLinus Torvalds } 1786ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 17871da177e4SLinus Torvalds 1788d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1789d45b9d8bSTrond Myklebust { 1790dc3f4198SAl Viro if (simple_positive(dentry)) 1791d45b9d8bSTrond Myklebust d_delete(dentry); 1792d45b9d8bSTrond Myklebust } 1793d45b9d8bSTrond Myklebust 1794597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 17951da177e4SLinus Torvalds { 17961da177e4SLinus Torvalds int error; 17971da177e4SLinus Torvalds 17981e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 17996de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18001da177e4SLinus Torvalds 18011ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18022b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1803884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18041da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18051da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1806ba6c0592STrond Myklebust switch (error) { 1807ba6c0592STrond Myklebust case 0: 18082b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1809ba6c0592STrond Myklebust break; 1810ba6c0592STrond Myklebust case -ENOENT: 1811d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1812ba6c0592STrond Myklebust } 1813884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1814ba6c0592STrond Myklebust } else 1815ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18161ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds return error; 18191da177e4SLinus Torvalds } 1820ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds /* 18231da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18241da177e4SLinus Torvalds * and after checking that the file has only one user. 18251da177e4SLinus Torvalds * 18261da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18271da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18281da177e4SLinus Torvalds */ 18291da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18301da177e4SLinus Torvalds { 18312b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18322b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18331da177e4SLinus Torvalds int error = -EBUSY; 18341da177e4SLinus Torvalds 18356de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18381da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18391da177e4SLinus Torvalds error = 0; 18401da177e4SLinus Torvalds goto out; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 18431ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 18441da177e4SLinus Torvalds if (inode != NULL) { 184557ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 18461da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 18471da177e4SLinus Torvalds if (error == 0) 18481b83d707STrond Myklebust nfs_drop_nlink(inode); 18491da177e4SLinus Torvalds } else 18501da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1851d45b9d8bSTrond Myklebust if (error == -ENOENT) 1852d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18531ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 18541da177e4SLinus Torvalds out: 18551da177e4SLinus Torvalds return error; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 18591da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 18601da177e4SLinus Torvalds * 18611da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 18621da177e4SLinus Torvalds */ 1863597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 18641da177e4SLinus Torvalds { 18651da177e4SLinus Torvalds int error; 18661da177e4SLinus Torvalds int need_rehash = 0; 18671da177e4SLinus Torvalds 18681e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 18696de1472fSAl Viro dir->i_ino, dentry); 18701da177e4SLinus Torvalds 18711ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 18721da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 187384d08fa8SAl Viro if (d_count(dentry) > 1) { 18741da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1875ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 18762b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 18771da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 18781ca42382STrond Myklebust goto out; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 18811da177e4SLinus Torvalds __d_drop(dentry); 18821da177e4SLinus Torvalds need_rehash = 1; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 18851da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1886d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 18871da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18881da177e4SLinus Torvalds } else if (need_rehash) 18891da177e4SLinus Torvalds d_rehash(dentry); 18901ca42382STrond Myklebust out: 18911ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 18921da177e4SLinus Torvalds return error; 18931da177e4SLinus Torvalds } 1894ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 18951da177e4SLinus Torvalds 1896873101b3SChuck Lever /* 1897873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1898873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1899873101b3SChuck Lever * using prepare_write/commit_write. 1900873101b3SChuck Lever * 1901873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1902873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1903873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1904873101b3SChuck Lever * symlink request has completed on the server. 1905873101b3SChuck Lever * 1906873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1907873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1908873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1909873101b3SChuck Lever * and move the raw page into its mapping. 1910873101b3SChuck Lever */ 1911597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19121da177e4SLinus Torvalds { 1913873101b3SChuck Lever struct page *page; 1914873101b3SChuck Lever char *kaddr; 19151da177e4SLinus Torvalds struct iattr attr; 1916873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19171da177e4SLinus Torvalds int error; 19181da177e4SLinus Torvalds 19191e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 19206de1472fSAl Viro dir->i_ino, dentry, symname); 19211da177e4SLinus Torvalds 1922873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1923873101b3SChuck Lever return -ENAMETOOLONG; 19241da177e4SLinus Torvalds 1925873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1926873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19271da177e4SLinus Torvalds 1928e8ecde25SAl Viro page = alloc_page(GFP_USER); 192976566991STrond Myklebust if (!page) 1930873101b3SChuck Lever return -ENOMEM; 1931873101b3SChuck Lever 1932e8ecde25SAl Viro kaddr = page_address(page); 1933873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1934873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1935873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1936873101b3SChuck Lever 19371ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 193894a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19391ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1940873101b3SChuck Lever if (error != 0) { 19411e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1942873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 19436de1472fSAl Viro dentry, symname, error); 19441da177e4SLinus Torvalds d_drop(dentry); 1945873101b3SChuck Lever __free_page(page); 19461da177e4SLinus Torvalds return error; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 1949873101b3SChuck Lever /* 1950873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1951873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1952873101b3SChuck Lever */ 19532b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 1954873101b3SChuck Lever GFP_KERNEL)) { 1955873101b3SChuck Lever SetPageUptodate(page); 1956873101b3SChuck Lever unlock_page(page); 1957a0b54addSRafael Aquini /* 1958a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 1959a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 1960a0b54addSRafael Aquini */ 196109cbfeafSKirill A. Shutemov put_page(page); 1962873101b3SChuck Lever } else 1963873101b3SChuck Lever __free_page(page); 1964873101b3SChuck Lever 1965873101b3SChuck Lever return 0; 1966873101b3SChuck Lever } 1967ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 1968873101b3SChuck Lever 1969597d9289SBryan Schumaker int 19701da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19711da177e4SLinus Torvalds { 19722b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 19731da177e4SLinus Torvalds int error; 19741da177e4SLinus Torvalds 19756de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 19766de1472fSAl Viro old_dentry, dentry); 19771da177e4SLinus Torvalds 19781fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 197957ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 19809a3936aaSTrond Myklebust 19819697d234STrond Myklebust d_drop(dentry); 19821da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1983cf809556STrond Myklebust if (error == 0) { 19847de9c6eeSAl Viro ihold(inode); 19859697d234STrond Myklebust d_add(dentry, inode); 1986cf809556STrond Myklebust } 19871fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 19881da177e4SLinus Torvalds return error; 19891da177e4SLinus Torvalds } 1990ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 19911da177e4SLinus Torvalds 1992920b4530SBenjamin Coddington static void 1993920b4530SBenjamin Coddington nfs_complete_rename(struct rpc_task *task, struct nfs_renamedata *data) 1994920b4530SBenjamin Coddington { 1995920b4530SBenjamin Coddington struct dentry *old_dentry = data->old_dentry; 1996920b4530SBenjamin Coddington struct dentry *new_dentry = data->new_dentry; 1997920b4530SBenjamin Coddington struct inode *old_inode = d_inode(old_dentry); 1998920b4530SBenjamin Coddington struct inode *new_inode = d_inode(new_dentry); 1999920b4530SBenjamin Coddington 2000920b4530SBenjamin Coddington nfs_mark_for_revalidate(old_inode); 2001920b4530SBenjamin Coddington 2002920b4530SBenjamin Coddington switch (task->tk_status) { 2003920b4530SBenjamin Coddington case 0: 2004920b4530SBenjamin Coddington if (new_inode != NULL) 2005920b4530SBenjamin Coddington nfs_drop_nlink(new_inode); 2006920b4530SBenjamin Coddington d_move(old_dentry, new_dentry); 2007920b4530SBenjamin Coddington nfs_set_verifier(new_dentry, 2008920b4530SBenjamin Coddington nfs_save_change_attribute(data->new_dir)); 2009920b4530SBenjamin Coddington break; 2010920b4530SBenjamin Coddington case -ENOENT: 2011920b4530SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2012920b4530SBenjamin Coddington } 2013920b4530SBenjamin Coddington } 2014920b4530SBenjamin Coddington 20151da177e4SLinus Torvalds /* 20161da177e4SLinus Torvalds * RENAME 20171da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20181da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20191da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20201da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20211da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20221da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20231da177e4SLinus Torvalds * 20241da177e4SLinus Torvalds * FIXED. 20251da177e4SLinus Torvalds * 20261da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20271da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20281da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20291da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20301da177e4SLinus Torvalds * using the inode layer 20311da177e4SLinus Torvalds * 20321da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20331da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20341da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20351da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20361da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20371da177e4SLinus Torvalds * the rename. 20381da177e4SLinus Torvalds */ 2039597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 20401cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 20411cd66c93SMiklos Szeredi unsigned int flags) 20421da177e4SLinus Torvalds { 20432b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 20442b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2045d4ea7e3cSBenjamin Coddington struct dentry *dentry = NULL; 204680a491fdSJeff Layton struct rpc_task *task; 20471da177e4SLinus Torvalds int error = -EBUSY; 20481da177e4SLinus Torvalds 20491cd66c93SMiklos Szeredi if (flags) 20501cd66c93SMiklos Szeredi return -EINVAL; 20511cd66c93SMiklos Szeredi 20526de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20536de1472fSAl Viro old_dentry, new_dentry, 205484d08fa8SAl Viro d_count(new_dentry)); 20551da177e4SLinus Torvalds 205670ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20571da177e4SLinus Torvalds /* 205828f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 205928f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 206028f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 206128f79a1aSMiklos Szeredi * the new target. 20621da177e4SLinus Torvalds */ 206327226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 206427226104SMiklos Szeredi /* 206527226104SMiklos Szeredi * To prevent any new references to the target during the 206627226104SMiklos Szeredi * rename, we unhash the dentry in advance. 206727226104SMiklos Szeredi */ 2068d4ea7e3cSBenjamin Coddington if (!d_unhashed(new_dentry)) 206927226104SMiklos Szeredi d_drop(new_dentry); 207027226104SMiklos Szeredi 207184d08fa8SAl Viro if (d_count(new_dentry) > 2) { 20721da177e4SLinus Torvalds int err; 207327226104SMiklos Szeredi 20741da177e4SLinus Torvalds /* copy the target dentry's name */ 20751da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 20761da177e4SLinus Torvalds &new_dentry->d_name); 20771da177e4SLinus Torvalds if (!dentry) 20781da177e4SLinus Torvalds goto out; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /* silly-rename the existing target ... */ 20811da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 208224e93025SMiklos Szeredi if (err) 20831da177e4SLinus Torvalds goto out; 208424e93025SMiklos Szeredi 208524e93025SMiklos Szeredi new_dentry = dentry; 208624e93025SMiklos Szeredi new_inode = NULL; 2087b1e4adf4STrond Myklebust } 208827226104SMiklos Szeredi } 20891da177e4SLinus Torvalds 209057ec14c5SBryan Schumaker NFS_PROTO(old_inode)->return_delegation(old_inode); 2091b1e4adf4STrond Myklebust if (new_inode != NULL) 209257ec14c5SBryan Schumaker NFS_PROTO(new_inode)->return_delegation(new_inode); 20931da177e4SLinus Torvalds 2094920b4530SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, 2095920b4530SBenjamin Coddington nfs_complete_rename); 209680a491fdSJeff Layton if (IS_ERR(task)) { 209780a491fdSJeff Layton error = PTR_ERR(task); 209880a491fdSJeff Layton goto out; 209980a491fdSJeff Layton } 210080a491fdSJeff Layton 210180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 210280a491fdSJeff Layton if (error == 0) 210380a491fdSJeff Layton error = task->tk_status; 210480a491fdSJeff Layton rpc_put_task(task); 21051da177e4SLinus Torvalds out: 210670ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 210770ded201STrond Myklebust new_dir, new_dentry, error); 21081da177e4SLinus Torvalds /* new dentry created? */ 21091da177e4SLinus Torvalds if (dentry) 21101da177e4SLinus Torvalds dput(dentry); 21111da177e4SLinus Torvalds return error; 21121da177e4SLinus Torvalds } 2113ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 21141da177e4SLinus Torvalds 2115cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2116cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2117cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2118cfcea3e8STrond Myklebust 21193a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 21203a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 21213a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 21223a505845STrond Myklebust 21231c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 21241c3c07e9STrond Myklebust { 21251c3c07e9STrond Myklebust put_rpccred(entry->cred); 2126f682a398SNeilBrown kfree_rcu(entry, rcu_head); 21274e857c58SPeter Zijlstra smp_mb__before_atomic(); 2128cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 21294e857c58SPeter Zijlstra smp_mb__after_atomic(); 21301c3c07e9STrond Myklebust } 21311c3c07e9STrond Myklebust 21321a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 21331a81bb8aSTrond Myklebust { 21341a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 21351a81bb8aSTrond Myklebust 21361a81bb8aSTrond Myklebust while (!list_empty(head)) { 21371a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 21381a81bb8aSTrond Myklebust list_del(&cache->lru); 21391a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 21401a81bb8aSTrond Myklebust } 21411a81bb8aSTrond Myklebust } 21421a81bb8aSTrond Myklebust 21433a505845STrond Myklebust static unsigned long 21443a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2145979df72eSTrond Myklebust { 2146979df72eSTrond Myklebust LIST_HEAD(head); 2147aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2148979df72eSTrond Myklebust struct nfs_access_entry *cache; 21491ab6c499SDave Chinner long freed = 0; 2150979df72eSTrond Myklebust 2151a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2152aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2153979df72eSTrond Myklebust struct inode *inode; 2154979df72eSTrond Myklebust 2155979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2156979df72eSTrond Myklebust break; 21579c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2158979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2159979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2160979df72eSTrond Myklebust goto remove_lru_entry; 2161979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2162979df72eSTrond Myklebust struct nfs_access_entry, lru); 2163979df72eSTrond Myklebust list_move(&cache->lru, &head); 2164979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 21651ab6c499SDave Chinner freed++; 2166979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2167979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2168979df72eSTrond Myklebust &nfs_access_lru_list); 2169979df72eSTrond Myklebust else { 2170979df72eSTrond Myklebust remove_lru_entry: 2171979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 21724e857c58SPeter Zijlstra smp_mb__before_atomic(); 2173979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 21744e857c58SPeter Zijlstra smp_mb__after_atomic(); 2175979df72eSTrond Myklebust } 217659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2177979df72eSTrond Myklebust } 2178979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21791a81bb8aSTrond Myklebust nfs_access_free_list(&head); 21801ab6c499SDave Chinner return freed; 21811ab6c499SDave Chinner } 21821ab6c499SDave Chinner 21831ab6c499SDave Chinner unsigned long 21843a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 21853a505845STrond Myklebust { 21863a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 21873a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 21883a505845STrond Myklebust 21893a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 21903a505845STrond Myklebust return SHRINK_STOP; 21913a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 21923a505845STrond Myklebust } 21933a505845STrond Myklebust 21943a505845STrond Myklebust 21953a505845STrond Myklebust unsigned long 21961ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 21971ab6c499SDave Chinner { 219855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2199979df72eSTrond Myklebust } 2200979df72eSTrond Myklebust 22013a505845STrond Myklebust static void 22023a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22033a505845STrond Myklebust { 22043a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22053a505845STrond Myklebust unsigned long diff; 22063a505845STrond Myklebust unsigned int nr_to_scan; 22073a505845STrond Myklebust 22083a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22093a505845STrond Myklebust return; 22103a505845STrond Myklebust nr_to_scan = 100; 22113a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 22123a505845STrond Myklebust if (diff < nr_to_scan) 22133a505845STrond Myklebust nr_to_scan = diff; 22143a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 22153a505845STrond Myklebust } 22163a505845STrond Myklebust 22171a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 22181c3c07e9STrond Myklebust { 22191c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22201a81bb8aSTrond Myklebust struct rb_node *n; 22211c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22221c3c07e9STrond Myklebust 22231c3c07e9STrond Myklebust /* Unhook entries from the cache */ 22241c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 22251c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22261c3c07e9STrond Myklebust rb_erase(n, root_node); 22271a81bb8aSTrond Myklebust list_move(&entry->lru, head); 22281c3c07e9STrond Myklebust } 22291c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 22301c3c07e9STrond Myklebust } 22311c3c07e9STrond Myklebust 22321c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 22331c3c07e9STrond Myklebust { 22341a81bb8aSTrond Myklebust LIST_HEAD(head); 22351a81bb8aSTrond Myklebust 22361a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 22371a81bb8aSTrond Myklebust return; 2238cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2239cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22401a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2241cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2242cfcea3e8STrond Myklebust 22431c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22441a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 22451a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22461a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22471a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22481c3c07e9STrond Myklebust } 22491c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 22501c3c07e9STrond Myklebust 22511c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 22521c3c07e9STrond Myklebust { 22531c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 22541c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22551c3c07e9STrond Myklebust 22561c3c07e9STrond Myklebust while (n != NULL) { 22571c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22581c3c07e9STrond Myklebust 22591c3c07e9STrond Myklebust if (cred < entry->cred) 22601c3c07e9STrond Myklebust n = n->rb_left; 22611c3c07e9STrond Myklebust else if (cred > entry->cred) 22621c3c07e9STrond Myklebust n = n->rb_right; 22631c3c07e9STrond Myklebust else 22641c3c07e9STrond Myklebust return entry; 22651c3c07e9STrond Myklebust } 22661c3c07e9STrond Myklebust return NULL; 22671c3c07e9STrond Myklebust } 22681c3c07e9STrond Myklebust 226957b69181STrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block) 22701da177e4SLinus Torvalds { 227155296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 22721c3c07e9STrond Myklebust struct nfs_access_entry *cache; 227357b69181STrond Myklebust bool retry = true; 227457b69181STrond Myklebust int err; 22751da177e4SLinus Torvalds 22761c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 227757b69181STrond Myklebust for(;;) { 22781c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 22791c3c07e9STrond Myklebust goto out_zap; 22801c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 228157b69181STrond Myklebust err = -ENOENT; 22821c3c07e9STrond Myklebust if (cache == NULL) 22831c3c07e9STrond Myklebust goto out; 228457b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 228521c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 228657b69181STrond Myklebust break; 228757b69181STrond Myklebust err = -ECHILD; 228857b69181STrond Myklebust if (!may_block) 228957b69181STrond Myklebust goto out; 229057b69181STrond Myklebust if (!retry) 229157b69181STrond Myklebust goto out_zap; 229257b69181STrond Myklebust spin_unlock(&inode->i_lock); 229357b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 229457b69181STrond Myklebust if (err) 229557b69181STrond Myklebust return err; 229657b69181STrond Myklebust spin_lock(&inode->i_lock); 229757b69181STrond Myklebust retry = false; 229857b69181STrond Myklebust } 22991c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 23001c3c07e9STrond Myklebust res->cred = cache->cred; 23011c3c07e9STrond Myklebust res->mask = cache->mask; 2302cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23031c3c07e9STrond Myklebust err = 0; 23041c3c07e9STrond Myklebust out: 23051c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23061c3c07e9STrond Myklebust return err; 23071c3c07e9STrond Myklebust out_zap: 23081a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23091a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 23101c3c07e9STrond Myklebust return -ENOENT; 23111c3c07e9STrond Myklebust } 23121c3c07e9STrond Myklebust 2313f682a398SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 2314f682a398SNeilBrown { 2315f682a398SNeilBrown /* Only check the most recently returned cache entry, 2316f682a398SNeilBrown * but do it without locking. 2317f682a398SNeilBrown */ 2318f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2319f682a398SNeilBrown struct nfs_access_entry *cache; 2320f682a398SNeilBrown int err = -ECHILD; 2321f682a398SNeilBrown struct list_head *lh; 2322f682a398SNeilBrown 2323f682a398SNeilBrown rcu_read_lock(); 2324f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2325f682a398SNeilBrown goto out; 2326f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2327f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2328f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 2329f682a398SNeilBrown cred != cache->cred) 2330f682a398SNeilBrown cache = NULL; 2331f682a398SNeilBrown if (cache == NULL) 2332f682a398SNeilBrown goto out; 233321c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2334f682a398SNeilBrown goto out; 2335f682a398SNeilBrown res->jiffies = cache->jiffies; 2336f682a398SNeilBrown res->cred = cache->cred; 2337f682a398SNeilBrown res->mask = cache->mask; 233821c3ba7eSTrond Myklebust err = 0; 2339f682a398SNeilBrown out: 2340f682a398SNeilBrown rcu_read_unlock(); 2341f682a398SNeilBrown return err; 2342f682a398SNeilBrown } 2343f682a398SNeilBrown 23441c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 23451c3c07e9STrond Myklebust { 2346cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2347cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23481c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 23491c3c07e9STrond Myklebust struct rb_node *parent = NULL; 23501c3c07e9STrond Myklebust struct nfs_access_entry *entry; 23511c3c07e9STrond Myklebust 23521c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23531c3c07e9STrond Myklebust while (*p != NULL) { 23541c3c07e9STrond Myklebust parent = *p; 23551c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 23561c3c07e9STrond Myklebust 23571c3c07e9STrond Myklebust if (set->cred < entry->cred) 23581c3c07e9STrond Myklebust p = &parent->rb_left; 23591c3c07e9STrond Myklebust else if (set->cred > entry->cred) 23601c3c07e9STrond Myklebust p = &parent->rb_right; 23611c3c07e9STrond Myklebust else 23621c3c07e9STrond Myklebust goto found; 23631c3c07e9STrond Myklebust } 23641c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 23651c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2366cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 23671c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23681c3c07e9STrond Myklebust return; 23691c3c07e9STrond Myklebust found: 23701c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2371cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2372cfcea3e8STrond Myklebust list_del(&entry->lru); 23731c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23741c3c07e9STrond Myklebust nfs_access_free_entry(entry); 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds 23776168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 23781da177e4SLinus Torvalds { 23791c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 23801c3c07e9STrond Myklebust if (cache == NULL) 23811c3c07e9STrond Myklebust return; 23821c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 23831da177e4SLinus Torvalds cache->jiffies = set->jiffies; 23841c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 23851da177e4SLinus Torvalds cache->mask = set->mask; 23861c3c07e9STrond Myklebust 2387f682a398SNeilBrown /* The above field assignments must be visible 2388f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2389f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2390f682a398SNeilBrown */ 2391f682a398SNeilBrown smp_wmb(); 23921c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2393cfcea3e8STrond Myklebust 2394cfcea3e8STrond Myklebust /* Update accounting */ 23954e857c58SPeter Zijlstra smp_mb__before_atomic(); 2396cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 23974e857c58SPeter Zijlstra smp_mb__after_atomic(); 2398cfcea3e8STrond Myklebust 2399cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24001a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2401cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24021a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24031a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24041a81bb8aSTrond Myklebust &nfs_access_lru_list); 2405cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2406cfcea3e8STrond Myklebust } 24073a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24081da177e4SLinus Torvalds } 24096168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 24106168f62cSWeston Andros Adamson 24116168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 24126168f62cSWeston Andros Adamson { 24136168f62cSWeston Andros Adamson entry->mask = 0; 24146168f62cSWeston Andros Adamson if (access_result & NFS4_ACCESS_READ) 24156168f62cSWeston Andros Adamson entry->mask |= MAY_READ; 24166168f62cSWeston Andros Adamson if (access_result & 24176168f62cSWeston Andros Adamson (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) 24186168f62cSWeston Andros Adamson entry->mask |= MAY_WRITE; 24196168f62cSWeston Andros Adamson if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) 24206168f62cSWeston Andros Adamson entry->mask |= MAY_EXEC; 24216168f62cSWeston Andros Adamson } 24226168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 24251da177e4SLinus Torvalds { 24261da177e4SLinus Torvalds struct nfs_access_entry cache; 242757b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 24281da177e4SLinus Torvalds int status; 24291da177e4SLinus Torvalds 2430f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2431f4ce1299STrond Myklebust 2432f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2433f682a398SNeilBrown if (status != 0) 243457b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 24351da177e4SLinus Torvalds if (status == 0) 2436f4ce1299STrond Myklebust goto out_cached; 24371da177e4SLinus Torvalds 2438f3324a2aSNeilBrown status = -ECHILD; 243957b69181STrond Myklebust if (!may_block) 2440f3324a2aSNeilBrown goto out; 2441f3324a2aSNeilBrown 24421da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 24431da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 24441da177e4SLinus Torvalds cache.cred = cred; 24451da177e4SLinus Torvalds cache.jiffies = jiffies; 24461da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2447a71ee337SSuresh Jayaraman if (status != 0) { 2448a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2449a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2450a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2451a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2452a71ee337SSuresh Jayaraman } 2453f4ce1299STrond Myklebust goto out; 2454a71ee337SSuresh Jayaraman } 24551da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2456f4ce1299STrond Myklebust out_cached: 2457f4ce1299STrond Myklebust if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2458f4ce1299STrond Myklebust status = -EACCES; 24591da177e4SLinus Torvalds out: 2460f4ce1299STrond Myklebust trace_nfs_access_exit(inode, status); 2461f4ce1299STrond Myklebust return status; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 2464af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2465af22f94aSTrond Myklebust { 2466af22f94aSTrond Myklebust int mask = 0; 2467af22f94aSTrond Myklebust 2468f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2469f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2470f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2471f8d9a897SWeston Andros Adamson } else { 24728a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2473af22f94aSTrond Myklebust mask |= MAY_READ; 24748a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2475af22f94aSTrond Myklebust mask |= MAY_WRITE; 2476f8d9a897SWeston Andros Adamson } 2477f8d9a897SWeston Andros Adamson 2478af22f94aSTrond Myklebust return mask; 2479af22f94aSTrond Myklebust } 2480af22f94aSTrond Myklebust 2481af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2482af22f94aSTrond Myklebust { 2483af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2484af22f94aSTrond Myklebust } 248589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2486af22f94aSTrond Myklebust 24875c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 24885c5fc09aSTrond Myklebust { 24895c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 249021c3ba7eSTrond Myklebust int ret = 0; 24915c5fc09aSTrond Myklebust 249221c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) { 24935c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 249421c3ba7eSTrond Myklebust return -ECHILD; 249521c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 249621c3ba7eSTrond Myklebust } 24975c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 24985c5fc09aSTrond Myklebust ret = -EACCES; 24995c5fc09aSTrond Myklebust return ret; 25005c5fc09aSTrond Myklebust } 25015c5fc09aSTrond Myklebust 250210556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 25031da177e4SLinus Torvalds { 25041da177e4SLinus Torvalds struct rpc_cred *cred; 25051da177e4SLinus Torvalds int res = 0; 25061da177e4SLinus Torvalds 250791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 250891d5b470SChuck Lever 2509e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 25101da177e4SLinus Torvalds goto out; 25111da177e4SLinus Torvalds /* Is this sys_access() ? */ 25129cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 25131da177e4SLinus Torvalds goto force_lookup; 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 25161da177e4SLinus Torvalds case S_IFLNK: 25171da177e4SLinus Torvalds goto out; 25181da177e4SLinus Torvalds case S_IFREG: 2519762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2520762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2521762674f8STrond Myklebust return 0; 25221da177e4SLinus Torvalds break; 25231da177e4SLinus Torvalds case S_IFDIR: 25241da177e4SLinus Torvalds /* 25251da177e4SLinus Torvalds * Optimize away all write operations, since the server 25261da177e4SLinus Torvalds * will check permissions when we perform the op. 25271da177e4SLinus Torvalds */ 25281da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 25291da177e4SLinus Torvalds goto out; 25301da177e4SLinus Torvalds } 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds force_lookup: 25331da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 25341da177e4SLinus Torvalds goto out_notsup; 25351da177e4SLinus Torvalds 2536f3324a2aSNeilBrown /* Always try fast lookups first */ 2537f3324a2aSNeilBrown rcu_read_lock(); 2538f3324a2aSNeilBrown cred = rpc_lookup_cred_nonblock(); 2539f3324a2aSNeilBrown if (!IS_ERR(cred)) 2540f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2541f3324a2aSNeilBrown else 2542f3324a2aSNeilBrown res = PTR_ERR(cred); 2543f3324a2aSNeilBrown rcu_read_unlock(); 2544f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2545f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 254698a8e323STrond Myklebust cred = rpc_lookup_cred(); 25471da177e4SLinus Torvalds if (!IS_ERR(cred)) { 25481da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 25491da177e4SLinus Torvalds put_rpccred(cred); 25501da177e4SLinus Torvalds } else 25511da177e4SLinus Torvalds res = PTR_ERR(cred); 2552f3324a2aSNeilBrown } 25531da177e4SLinus Torvalds out: 25545c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 25555c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2556f696a365SMiklos Szeredi 25571e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 25581e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 25591da177e4SLinus Torvalds return res; 25601da177e4SLinus Torvalds out_notsup: 2561d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2562d51ac1a8SNeilBrown return -ECHILD; 2563d51ac1a8SNeilBrown 25641da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 25651da177e4SLinus Torvalds if (res == 0) 25662830ba7fSAl Viro res = generic_permission(inode, mask); 25671e7cb3dcSChuck Lever goto out; 25681da177e4SLinus Torvalds } 2569ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds /* 25721da177e4SLinus Torvalds * Local variables: 25731da177e4SLinus Torvalds * version-control: t 25741da177e4SLinus Torvalds * kept-new-versions: 5 25751da177e4SLinus Torvalds * End: 25761da177e4SLinus Torvalds */ 2577