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, 6023db8620SAl Viro .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); 2254a201d6eSTrond Myklebust string->hash = full_name_hash(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(); 38056e4ebf8SBryan Schumaker error = NFS_PROTO(inode)->readdir(file->f_path.dentry, 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 { 417fa923369STrond Myklebust struct nfs_inode *nfsi; 418fa923369STrond Myklebust 4192b0143b5SDavid Howells if (d_really_is_negative(dentry)) 4202b0143b5SDavid Howells return 0; 421fa923369STrond Myklebust 4222b0143b5SDavid Howells nfsi = NFS_I(d_inode(dentry)); 423fa923369STrond Myklebust if (entry->fattr->fileid == nfsi->fileid) 424fa923369STrond Myklebust return 1; 425fa923369STrond Myklebust if (nfs_compare_fh(entry->fh, &nfsi->fh) == 0) 426d39ab9deSBryan Schumaker return 1; 427d39ab9deSBryan Schumaker return 0; 428d39ab9deSBryan Schumaker } 429d39ab9deSBryan Schumaker 430d39ab9deSBryan Schumaker static 43123db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 432d69ee9b8STrond Myklebust { 433d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 434d69ee9b8STrond Myklebust return false; 435d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 436d69ee9b8STrond Myklebust return true; 43723db8620SAl Viro if (ctx->pos == 0) 438d69ee9b8STrond Myklebust return true; 439d69ee9b8STrond Myklebust return false; 440d69ee9b8STrond Myklebust } 441d69ee9b8STrond Myklebust 442d69ee9b8STrond Myklebust /* 443d69ee9b8STrond Myklebust * This function is called by the lookup code to request the use of 444d69ee9b8STrond Myklebust * readdirplus to accelerate any future lookups in the same 445d69ee9b8STrond Myklebust * directory. 446d69ee9b8STrond Myklebust */ 447d69ee9b8STrond Myklebust static 448d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 449d69ee9b8STrond Myklebust { 450d69ee9b8STrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags); 451d69ee9b8STrond Myklebust } 452d69ee9b8STrond Myklebust 453311324adSTrond Myklebust /* 454311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 455311324adSTrond Myklebust * 456311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 457311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 458311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 459311324adSTrond Myklebust * cache flush. 460311324adSTrond Myklebust */ 461311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 462311324adSTrond Myklebust { 463311324adSTrond Myklebust if (!list_empty(&NFS_I(dir)->open_files)) { 464311324adSTrond Myklebust nfs_advise_use_readdirplus(dir); 465311324adSTrond Myklebust nfs_zap_mapping(dir, dir->i_mapping); 466311324adSTrond Myklebust } 467311324adSTrond Myklebust } 468311324adSTrond Myklebust 469d69ee9b8STrond Myklebust static 470d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 471d39ab9deSBryan Schumaker { 47226fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4734a201d6eSTrond Myklebust struct dentry *dentry; 4744a201d6eSTrond Myklebust struct dentry *alias; 4752b0143b5SDavid Howells struct inode *dir = d_inode(parent); 476d39ab9deSBryan Schumaker struct inode *inode; 477aa9c2669SDavid Quigley int status; 478d39ab9deSBryan Schumaker 479fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 480fa923369STrond Myklebust return; 4816c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4826c441c25STrond Myklebust return; 4834a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4844a201d6eSTrond Myklebust if (filename.len == 1) 4854a201d6eSTrond Myklebust return; 4864a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4874a201d6eSTrond Myklebust return; 4884a201d6eSTrond Myklebust } 4894a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 490d39ab9deSBryan Schumaker 4914a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 492d39ab9deSBryan Schumaker if (dentry != NULL) { 4936c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4946c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 4956c441c25STrond Myklebust &entry->fattr->fsid)) 4966c441c25STrond Myklebust goto out; 497d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 498cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 4992b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 500aa9c2669SDavid Quigley if (!status) 5012b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 502d39ab9deSBryan Schumaker goto out; 503d39ab9deSBryan Schumaker } else { 5045542aa2fSEric W. Biederman d_invalidate(dentry); 505d39ab9deSBryan Schumaker dput(dentry); 506d39ab9deSBryan Schumaker } 507d39ab9deSBryan Schumaker } 508d39ab9deSBryan Schumaker 509d39ab9deSBryan Schumaker dentry = d_alloc(parent, &filename); 5104a201d6eSTrond Myklebust if (dentry == NULL) 5114a201d6eSTrond Myklebust return; 5124a201d6eSTrond Myklebust 5131775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 514d39ab9deSBryan Schumaker if (IS_ERR(inode)) 515d39ab9deSBryan Schumaker goto out; 516d39ab9deSBryan Schumaker 51741d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 518d39ab9deSBryan Schumaker if (IS_ERR(alias)) 519d39ab9deSBryan Schumaker goto out; 520d39ab9deSBryan Schumaker else if (alias) { 521d39ab9deSBryan Schumaker nfs_set_verifier(alias, nfs_save_change_attribute(dir)); 522d39ab9deSBryan Schumaker dput(alias); 523d39ab9deSBryan Schumaker } else 524d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 525d39ab9deSBryan Schumaker 526d39ab9deSBryan Schumaker out: 527d39ab9deSBryan Schumaker dput(dentry); 528d39ab9deSBryan Schumaker } 529d39ab9deSBryan Schumaker 530d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 531d1bacf9eSBryan Schumaker static 5328cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5336650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 534d1bacf9eSBryan Schumaker { 535babddc72SBryan Schumaker struct xdr_stream stream; 536f7da7a12SBenny Halevy struct xdr_buf buf; 5376650239aSTrond Myklebust struct page *scratch; 53899424380SBryan Schumaker struct nfs_cache_array *array; 5395c346854STrond Myklebust unsigned int count = 0; 5405c346854STrond Myklebust int status; 541babddc72SBryan Schumaker 5426650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5436650239aSTrond Myklebust if (scratch == NULL) 5446650239aSTrond Myklebust return -ENOMEM; 545babddc72SBryan Schumaker 546ce85cfbeSBenjamin Coddington if (buflen == 0) 547ce85cfbeSBenjamin Coddington goto out_nopages; 548ce85cfbeSBenjamin Coddington 549f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5506650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 55199424380SBryan Schumaker 55299424380SBryan Schumaker do { 55399424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5548cd51a0cSTrond Myklebust if (status != 0) { 5558cd51a0cSTrond Myklebust if (status == -EAGAIN) 5568cd51a0cSTrond Myklebust status = 0; 55799424380SBryan Schumaker break; 5588cd51a0cSTrond Myklebust } 55999424380SBryan Schumaker 5605c346854STrond Myklebust count++; 5615c346854STrond Myklebust 56247c716cbSTrond Myklebust if (desc->plus != 0) 563d39ab9deSBryan Schumaker nfs_prime_dcache(desc->file->f_path.dentry, entry); 5648cd51a0cSTrond Myklebust 5658cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5668cd51a0cSTrond Myklebust if (status != 0) 5678cd51a0cSTrond Myklebust break; 56899424380SBryan Schumaker } while (!entry->eof); 56999424380SBryan Schumaker 570ce85cfbeSBenjamin Coddington out_nopages: 57147c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 57299424380SBryan Schumaker array = nfs_readdir_get_array(page); 5738cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 5748cd51a0cSTrond Myklebust array->eof_index = array->size; 57599424380SBryan Schumaker status = 0; 57699424380SBryan Schumaker nfs_readdir_release_array(page); 5775c346854STrond Myklebust } else 5785c346854STrond Myklebust status = PTR_ERR(array); 57956e4ebf8SBryan Schumaker } 5806650239aSTrond Myklebust 5816650239aSTrond Myklebust put_page(scratch); 5828cd51a0cSTrond Myklebust return status; 5838cd51a0cSTrond Myklebust } 58456e4ebf8SBryan Schumaker 58556e4ebf8SBryan Schumaker static 586*c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 58756e4ebf8SBryan Schumaker { 58856e4ebf8SBryan Schumaker unsigned int i; 58956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 59056e4ebf8SBryan Schumaker put_page(pages[i]); 59156e4ebf8SBryan Schumaker } 59256e4ebf8SBryan Schumaker 59356e4ebf8SBryan Schumaker /* 59456e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 5950b936e37SAnna Schumaker * to nfs_readdir_free_pagearray 59656e4ebf8SBryan Schumaker */ 59756e4ebf8SBryan Schumaker static 598*c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 59956e4ebf8SBryan Schumaker { 60056e4ebf8SBryan Schumaker unsigned int i; 60156e4ebf8SBryan Schumaker 60256e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 60356e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 60456e4ebf8SBryan Schumaker if (page == NULL) 60556e4ebf8SBryan Schumaker goto out_freepages; 60656e4ebf8SBryan Schumaker pages[i] = page; 60756e4ebf8SBryan Schumaker } 6086650239aSTrond Myklebust return 0; 60956e4ebf8SBryan Schumaker 61056e4ebf8SBryan Schumaker out_freepages: 611*c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6126650239aSTrond Myklebust return -ENOMEM; 613d1bacf9eSBryan Schumaker } 614d1bacf9eSBryan Schumaker 615d1bacf9eSBryan Schumaker static 616d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 617d1bacf9eSBryan Schumaker { 61856e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 619d1bacf9eSBryan Schumaker struct nfs_entry entry; 620d1bacf9eSBryan Schumaker struct file *file = desc->file; 621d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6228cd51a0cSTrond Myklebust int status = -ENOMEM; 62356e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 624d1bacf9eSBryan Schumaker 625d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6260aded708STrond Myklebust entry.cookie = desc->last_cookie; 627d1bacf9eSBryan Schumaker entry.eof = 0; 628d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 629d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 630573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 631d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 632d1bacf9eSBryan Schumaker goto out; 633d1bacf9eSBryan Schumaker 63414c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 63514c43f76SDavid Quigley if (IS_ERR(entry.label)) { 63614c43f76SDavid Quigley status = PTR_ERR(entry.label); 63714c43f76SDavid Quigley goto out; 63814c43f76SDavid Quigley } 63914c43f76SDavid Quigley 640d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 6418cd51a0cSTrond Myklebust if (IS_ERR(array)) { 6428cd51a0cSTrond Myklebust status = PTR_ERR(array); 64314c43f76SDavid Quigley goto out_label_free; 6448cd51a0cSTrond Myklebust } 645d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 646d1bacf9eSBryan Schumaker array->eof_index = -1; 647d1bacf9eSBryan Schumaker 648*c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6496650239aSTrond Myklebust if (status < 0) 650d1bacf9eSBryan Schumaker goto out_release_array; 651d1bacf9eSBryan Schumaker do { 652ac396128STrond Myklebust unsigned int pglen; 65356e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 654babddc72SBryan Schumaker 655d1bacf9eSBryan Schumaker if (status < 0) 656d1bacf9eSBryan Schumaker break; 657ac396128STrond Myklebust pglen = status; 6586650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6598cd51a0cSTrond Myklebust if (status < 0) { 6608cd51a0cSTrond Myklebust if (status == -ENOSPC) 6618cd51a0cSTrond Myklebust status = 0; 6628cd51a0cSTrond Myklebust break; 6638cd51a0cSTrond Myklebust } 6648cd51a0cSTrond Myklebust } while (array->eof_index < 0); 665d1bacf9eSBryan Schumaker 666*c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 667d1bacf9eSBryan Schumaker out_release_array: 668d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 66914c43f76SDavid Quigley out_label_free: 67014c43f76SDavid Quigley nfs4_label_free(entry.label); 671d1bacf9eSBryan Schumaker out: 672d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 673d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 674d1bacf9eSBryan Schumaker return status; 675d1bacf9eSBryan Schumaker } 676d1bacf9eSBryan Schumaker 677d1bacf9eSBryan Schumaker /* 678d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 679d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 680d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 681d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6821da177e4SLinus Torvalds */ 683d1bacf9eSBryan Schumaker static 684d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 685d1bacf9eSBryan Schumaker { 686496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6878cd51a0cSTrond Myklebust int ret; 688d1bacf9eSBryan Schumaker 6898cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6908cd51a0cSTrond Myklebust if (ret < 0) 691d1bacf9eSBryan Schumaker goto error; 692d1bacf9eSBryan Schumaker SetPageUptodate(page); 693d1bacf9eSBryan Schumaker 6942aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 695cd9ae2b6STrond Myklebust /* Should never happen */ 696cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 697cd9ae2b6STrond Myklebust } 6981da177e4SLinus Torvalds unlock_page(page); 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds error: 7011da177e4SLinus Torvalds unlock_page(page); 7028cd51a0cSTrond Myklebust return ret; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 705d1bacf9eSBryan Schumaker static 706d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7071da177e4SLinus Torvalds { 70811de3b11STrond Myklebust if (!desc->page->mapping) 70911de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 7101da177e4SLinus Torvalds page_cache_release(desc->page); 7111da177e4SLinus Torvalds desc->page = NULL; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 714d1bacf9eSBryan Schumaker static 715d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7161da177e4SLinus Torvalds { 717496ad9aaSAl Viro return read_cache_page(file_inode(desc->file)->i_mapping, 718d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* 722d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7231da177e4SLinus Torvalds */ 724d1bacf9eSBryan Schumaker static 725d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 726d1bacf9eSBryan Schumaker { 727d1bacf9eSBryan Schumaker int res; 728d1bacf9eSBryan Schumaker 729d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 730d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 731d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 732d1bacf9eSBryan Schumaker 733d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 73447c716cbSTrond Myklebust if (res != 0) 735d1bacf9eSBryan Schumaker cache_page_release(desc); 736d1bacf9eSBryan Schumaker return res; 737d1bacf9eSBryan Schumaker } 738d1bacf9eSBryan Schumaker 739d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7401da177e4SLinus Torvalds static inline 7411da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7421da177e4SLinus Torvalds { 7438cd51a0cSTrond Myklebust int res; 744d1bacf9eSBryan Schumaker 7450aded708STrond Myklebust if (desc->page_index == 0) { 7468cd51a0cSTrond Myklebust desc->current_index = 0; 7470aded708STrond Myklebust desc->last_cookie = 0; 7480aded708STrond Myklebust } 74947c716cbSTrond Myklebust do { 750d1bacf9eSBryan Schumaker res = find_cache_page(desc); 75147c716cbSTrond Myklebust } while (res == -EAGAIN); 7521da177e4SLinus Torvalds return res; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds /* 7561da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds static 75923db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds struct file *file = desc->file; 762d1bacf9eSBryan Schumaker int i = 0; 763d1bacf9eSBryan Schumaker int res = 0; 764d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7658ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7668ef2ce3eSBryan Schumaker 767d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 768e7c58e97STrond Myklebust if (IS_ERR(array)) { 769e7c58e97STrond Myklebust res = PTR_ERR(array); 770e7c58e97STrond Myklebust goto out; 771e7c58e97STrond Myklebust } 7721da177e4SLinus Torvalds 773d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 774ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7751da177e4SLinus Torvalds 776ece0b423STrond Myklebust ent = &array->array[i]; 77723db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 77823db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 779ece0b423STrond Myklebust desc->eof = 1; 7801da177e4SLinus Torvalds break; 781ece0b423STrond Myklebust } 78223db8620SAl Viro desc->ctx->pos++; 783d1bacf9eSBryan Schumaker if (i < (array->size-1)) 784d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 785d1bacf9eSBryan Schumaker else 786d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7870c030806STrond Myklebust if (ctx->duped != 0) 7880c030806STrond Myklebust ctx->duped = 1; 7898cd51a0cSTrond Myklebust } 79047c716cbSTrond Myklebust if (array->eof_index >= 0) 791d1bacf9eSBryan Schumaker desc->eof = 1; 792d1bacf9eSBryan Schumaker 793d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 794e7c58e97STrond Myklebust out: 795d1bacf9eSBryan Schumaker cache_page_release(desc); 7961e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7971e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7981da177e4SLinus Torvalds return res; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /* 8021da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8031da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8041da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8051da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8061da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8071da177e4SLinus Torvalds * 8081da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8091da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8101da177e4SLinus Torvalds * we should already have a complete representation of the 8111da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds static inline 81423db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds struct page *page = NULL; 8171da177e4SLinus Torvalds int status; 818496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8190c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8201da177e4SLinus Torvalds 8211e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8221e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8251da177e4SLinus Torvalds if (!page) { 8261da177e4SLinus Torvalds status = -ENOMEM; 8271da177e4SLinus Torvalds goto out; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 8307a8e1dc3STrond Myklebust desc->page_index = 0; 8310aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8327a8e1dc3STrond Myklebust desc->page = page; 8330c030806STrond Myklebust ctx->duped = 0; 8347a8e1dc3STrond Myklebust 83585f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 83685f8607eSTrond Myklebust if (status < 0) 837d1bacf9eSBryan Schumaker goto out_release; 838d1bacf9eSBryan Schumaker 83923db8620SAl Viro status = nfs_do_filldir(desc); 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds out: 8421e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8433110ff80SHarvey Harrison __func__, status); 8441da177e4SLinus Torvalds return status; 8451da177e4SLinus Torvalds out_release: 846d1bacf9eSBryan Schumaker cache_page_release(desc); 8471da177e4SLinus Torvalds goto out; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 850311324adSTrond Myklebust static bool nfs_dir_mapping_need_revalidate(struct inode *dir) 851311324adSTrond Myklebust { 852311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 853311324adSTrond Myklebust 854311324adSTrond Myklebust if (nfs_attribute_cache_expired(dir)) 855311324adSTrond Myklebust return true; 856311324adSTrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 857311324adSTrond Myklebust return true; 858311324adSTrond Myklebust return false; 859311324adSTrond Myklebust } 860311324adSTrond Myklebust 86100a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 86200a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 86300a92642SOlivier Galibert whole directory. 8641da177e4SLinus Torvalds */ 86523db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8661da177e4SLinus Torvalds { 86723db8620SAl Viro struct dentry *dentry = file->f_path.dentry; 8682b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8691da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8701da177e4SLinus Torvalds *desc = &my_desc; 87123db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 87207b5ce8eSScott Mayhew int res = 0; 8731da177e4SLinus Torvalds 8746de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8756de1472fSAl Viro file, (long long)ctx->pos); 87691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 87791d5b470SChuck Lever 8781da177e4SLinus Torvalds /* 87923db8620SAl Viro * ctx->pos points to the dirent entry number. 880f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 88100a92642SOlivier Galibert * to either find the entry with the appropriate number or 88200a92642SOlivier Galibert * revalidate the cookie. 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8851da177e4SLinus Torvalds 88623db8620SAl Viro desc->file = file; 88723db8620SAl Viro desc->ctx = ctx; 888480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8891da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 89023db8620SAl Viro desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0; 8911da177e4SLinus Torvalds 892565277f6STrond Myklebust nfs_block_sillyrename(dentry); 893311324adSTrond Myklebust if (ctx->pos == 0 || nfs_dir_mapping_need_revalidate(inode)) 89423db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 895fccca7fcSTrond Myklebust if (res < 0) 896fccca7fcSTrond Myklebust goto out; 897fccca7fcSTrond Myklebust 89847c716cbSTrond Myklebust do { 8991da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 90000a92642SOlivier Galibert 9011da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 902ece0b423STrond Myklebust res = 0; 9031da177e4SLinus Torvalds /* This means either end of directory */ 904d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 9051da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 90623db8620SAl Viro res = uncached_readdir(desc); 907ece0b423STrond Myklebust if (res == 0) 9081da177e4SLinus Torvalds continue; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds break; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9133a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9141da177e4SLinus Torvalds nfs_zap_caches(inode); 915baf57a09STrond Myklebust desc->page_index = 0; 9161da177e4SLinus Torvalds desc->plus = 0; 917d1bacf9eSBryan Schumaker desc->eof = 0; 9181da177e4SLinus Torvalds continue; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds if (res < 0) 9211da177e4SLinus Torvalds break; 9221da177e4SLinus Torvalds 92323db8620SAl Viro res = nfs_do_filldir(desc); 924ece0b423STrond Myklebust if (res < 0) 9251da177e4SLinus Torvalds break; 92647c716cbSTrond Myklebust } while (!desc->eof); 927fccca7fcSTrond Myklebust out: 928565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 9291e7cb3dcSChuck Lever if (res > 0) 9301e7cb3dcSChuck Lever res = 0; 9316de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9321da177e4SLinus Torvalds return res; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 936f0dd2136STrond Myklebust { 9376de1472fSAl Viro struct inode *inode = file_inode(filp); 938480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 939b84e06c5SChuck Lever 9406de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9416de1472fSAl Viro filp, offset, whence); 942b84e06c5SChuck Lever 943b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 944965c8e59SAndrew Morton switch (whence) { 945f0dd2136STrond Myklebust case 1: 946f0dd2136STrond Myklebust offset += filp->f_pos; 947f0dd2136STrond Myklebust case 0: 948f0dd2136STrond Myklebust if (offset >= 0) 949f0dd2136STrond Myklebust break; 950f0dd2136STrond Myklebust default: 951f0dd2136STrond Myklebust offset = -EINVAL; 952f0dd2136STrond Myklebust goto out; 953f0dd2136STrond Myklebust } 954f0dd2136STrond Myklebust if (offset != filp->f_pos) { 955f0dd2136STrond Myklebust filp->f_pos = offset; 956480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9578ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 958f0dd2136STrond Myklebust } 959f0dd2136STrond Myklebust out: 960b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 961f0dd2136STrond Myklebust return offset; 962f0dd2136STrond Myklebust } 963f0dd2136STrond Myklebust 9641da177e4SLinus Torvalds /* 9651da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9661da177e4SLinus Torvalds * is a dummy operation. 9671da177e4SLinus Torvalds */ 96802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 96902c24a82SJosef Bacik int datasync) 9701da177e4SLinus Torvalds { 9716de1472fSAl Viro struct inode *inode = file_inode(filp); 9727ea80859SChristoph Hellwig 9736de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9741e7cb3dcSChuck Lever 97502c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 9766de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 97702c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 9781da177e4SLinus Torvalds return 0; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 981bfc69a45STrond Myklebust /** 982bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 983bfc69a45STrond Myklebust * @dir - pointer to directory inode 984bfc69a45STrond Myklebust * 985bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 986bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 987bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 988bfc69a45STrond Myklebust * 989bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 990bfc69a45STrond Myklebust */ 991bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 992bfc69a45STrond Myklebust { 993011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 994bfc69a45STrond Myklebust } 99589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 996bfc69a45STrond Myklebust 9971da177e4SLinus Torvalds /* 9981da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9991da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10001da177e4SLinus Torvalds * and may need to be looked up again. 1001912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 10021da177e4SLinus Torvalds */ 1003912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1004912a108dSNeilBrown int rcu_walk) 10051da177e4SLinus Torvalds { 1006912a108dSNeilBrown int ret; 1007912a108dSNeilBrown 10081da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10091da177e4SLinus Torvalds return 1; 10104eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10114eec952eSTrond Myklebust return 0; 1012f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10136ecc5e8fSTrond Myklebust return 0; 1014f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 1015912a108dSNeilBrown if (rcu_walk) 1016912a108dSNeilBrown ret = nfs_revalidate_inode_rcu(NFS_SERVER(dir), dir); 1017912a108dSNeilBrown else 1018912a108dSNeilBrown ret = nfs_revalidate_inode(NFS_SERVER(dir), dir); 1019912a108dSNeilBrown if (ret < 0) 1020f2c77f4eSTrond Myklebust return 0; 1021f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1022f2c77f4eSTrond Myklebust return 0; 1023f2c77f4eSTrond Myklebust return 1; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 1027a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1028a12802caSTrond Myklebust * an O_EXCL create using this path component. 1029a12802caSTrond Myklebust */ 1030fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1031a12802caSTrond Myklebust { 1032a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1033a12802caSTrond Myklebust return 0; 1034fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1035a12802caSTrond Myklebust } 1036a12802caSTrond Myklebust 1037a12802caSTrond Myklebust /* 10381d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10391d6757fbSTrond Myklebust * 10401d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10411d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10421d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10431d6757fbSTrond Myklebust * 10441d6757fbSTrond Myklebust */ 104565a0c149STrond Myklebust static 1046fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10471da177e4SLinus Torvalds { 10481da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 104965a0c149STrond Myklebust int ret; 10501da177e4SLinus Torvalds 105136d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10524e99a1ffSTrond Myklebust return 0; 10531da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1054fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10551da177e4SLinus Torvalds goto out_force; 10561da177e4SLinus Torvalds /* This is an open(2) */ 1057fa3c56bbSAl Viro if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) && 1058fa3c56bbSAl Viro (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) 10591da177e4SLinus Torvalds goto out_force; 106065a0c149STrond Myklebust out: 106165a0c149STrond Myklebust return (inode->i_nlink == 0) ? -ENOENT : 0; 10621da177e4SLinus Torvalds out_force: 10631fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10641fa1e384SNeilBrown return -ECHILD; 106565a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 106665a0c149STrond Myklebust if (ret != 0) 106765a0c149STrond Myklebust return ret; 106865a0c149STrond Myklebust goto out; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * We judge how long we want to trust negative 10731da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10741da177e4SLinus Torvalds * 10751da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10761da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1077912a108dSNeilBrown * 1078912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1079912a108dSNeilBrown * suggesting a reval is needed. 10801da177e4SLinus Torvalds */ 10811da177e4SLinus Torvalds static inline 10821da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1083fa3c56bbSAl Viro unsigned int flags) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 1086fa3c56bbSAl Viro if (flags & LOOKUP_CREATE) 10871da177e4SLinus Torvalds return 0; 10884eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10894eec952eSTrond Myklebust return 1; 1090912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* 10941da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 10951da177e4SLinus Torvalds * and we should check whether we can really trust that 10961da177e4SLinus Torvalds * lookup. 10971da177e4SLinus Torvalds * 10981da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 10991da177e4SLinus Torvalds * we have an inode! 11001da177e4SLinus Torvalds * 11011da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 11021da177e4SLinus Torvalds * cached dentry and do a new lookup. 11031da177e4SLinus Torvalds */ 11040b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 11051da177e4SLinus Torvalds { 11061da177e4SLinus Torvalds struct inode *dir; 11071da177e4SLinus Torvalds struct inode *inode; 11081da177e4SLinus Torvalds struct dentry *parent; 1109e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1110e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 11111775fd3eSDavid Quigley struct nfs4_label *label = NULL; 11121da177e4SLinus Torvalds int error; 11131da177e4SLinus Torvalds 1114d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) { 111550d77739SNeilBrown parent = ACCESS_ONCE(dentry->d_parent); 11162b0143b5SDavid Howells dir = d_inode_rcu(parent); 1117d51ac1a8SNeilBrown if (!dir) 111834286d66SNick Piggin return -ECHILD; 1119d51ac1a8SNeilBrown } else { 11201da177e4SLinus Torvalds parent = dget_parent(dentry); 11212b0143b5SDavid Howells dir = d_inode(parent); 1122d51ac1a8SNeilBrown } 112391d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 11242b0143b5SDavid Howells inode = d_inode(dentry); 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds if (!inode) { 1127912a108dSNeilBrown if (nfs_neg_need_reval(dir, dentry, flags)) { 1128d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1129d51ac1a8SNeilBrown return -ECHILD; 11301da177e4SLinus Torvalds goto out_bad; 1131912a108dSNeilBrown } 1132d69ee9b8STrond Myklebust goto out_valid_noent; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds if (is_bad_inode(inode)) { 1136d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1137d51ac1a8SNeilBrown return -ECHILD; 11386de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 11396de1472fSAl Viro __func__, dentry); 11401da177e4SLinus Torvalds goto out_bad; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 1143011e2a7fSBryan Schumaker if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 114415860ab1STrond Myklebust goto out_set_verifier; 114515860ab1STrond Myklebust 1146912a108dSNeilBrown /* Force a full look up iff the parent directory has changed */ 1147912a108dSNeilBrown if (!nfs_is_exclusive_create(dir, flags) && 1148912a108dSNeilBrown nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 1149912a108dSNeilBrown 11501fa1e384SNeilBrown if (nfs_lookup_verify_inode(inode, flags)) { 1151d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) 1152d51ac1a8SNeilBrown return -ECHILD; 11531da177e4SLinus Torvalds goto out_zap_parent; 11541fa1e384SNeilBrown } 11551da177e4SLinus Torvalds goto out_valid; 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 1158912a108dSNeilBrown if (flags & LOOKUP_RCU) 1159912a108dSNeilBrown return -ECHILD; 1160912a108dSNeilBrown 11611da177e4SLinus Torvalds if (NFS_STALE(inode)) 11621da177e4SLinus Torvalds goto out_bad; 11631da177e4SLinus Torvalds 1164e1fb4d05STrond Myklebust error = -ENOMEM; 1165e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1166e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1167e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1168e1fb4d05STrond Myklebust goto out_error; 1169e1fb4d05STrond Myklebust 117014c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 117114c43f76SDavid Quigley if (IS_ERR(label)) 117214c43f76SDavid Quigley goto out_error; 117314c43f76SDavid Quigley 11746e0d0be7STrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 11751775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 11766e0d0be7STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 11771da177e4SLinus Torvalds if (error) 11781da177e4SLinus Torvalds goto out_bad; 1179e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11801da177e4SLinus Torvalds goto out_bad; 1181e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 11821da177e4SLinus Torvalds goto out_bad; 11831da177e4SLinus Torvalds 1184aa9c2669SDavid Quigley nfs_setsecurity(inode, fattr, label); 1185aa9c2669SDavid Quigley 1186e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1187e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 118814c43f76SDavid Quigley nfs4_label_free(label); 118914c43f76SDavid Quigley 119015860ab1STrond Myklebust out_set_verifier: 1191cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11921da177e4SLinus Torvalds out_valid: 1193d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1194d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1195d69ee9b8STrond Myklebust out_valid_noent: 1196d51ac1a8SNeilBrown if (flags & LOOKUP_RCU) { 119750d77739SNeilBrown if (parent != ACCESS_ONCE(dentry->d_parent)) 1198d51ac1a8SNeilBrown return -ECHILD; 1199d51ac1a8SNeilBrown } else 12001da177e4SLinus Torvalds dput(parent); 12016de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12026de1472fSAl Viro __func__, dentry); 12031da177e4SLinus Torvalds return 1; 12041da177e4SLinus Torvalds out_zap_parent: 12051da177e4SLinus Torvalds nfs_zap_caches(dir); 12061da177e4SLinus Torvalds out_bad: 1207d51ac1a8SNeilBrown WARN_ON(flags & LOOKUP_RCU); 1208c44600c9SAl Viro nfs_free_fattr(fattr); 1209c44600c9SAl Viro nfs_free_fhandle(fhandle); 121014c43f76SDavid Quigley nfs4_label_free(label); 1211a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12121da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12131da177e4SLinus Torvalds /* Purge readdir caches. */ 12141da177e4SLinus Torvalds nfs_zap_caches(inode); 1215a3f432bfSJ. Bruce Fields /* 1216a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1217a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1218a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1219a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1220a3f432bfSJ. Bruce Fields */ 1221a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 1222d9e80b7dSAl Viro goto out_valid; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds dput(parent); 12256de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12266de1472fSAl Viro __func__, dentry); 12271da177e4SLinus Torvalds return 0; 1228e1fb4d05STrond Myklebust out_error: 1229d51ac1a8SNeilBrown WARN_ON(flags & LOOKUP_RCU); 1230e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1231e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 123214c43f76SDavid Quigley nfs4_label_free(label); 1233e1fb4d05STrond Myklebust dput(parent); 12346de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12356de1472fSAl Viro __func__, dentry, error); 1236e1fb4d05STrond Myklebust return error; 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds /* 12402b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1241ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1242ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1243ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1244ecf3d1f1SJeff Layton * 1245ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1246ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1247ecf3d1f1SJeff Layton */ 1248ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1249ecf3d1f1SJeff Layton { 1250ecf3d1f1SJeff Layton int error; 12512b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 1252ecf3d1f1SJeff Layton 1253ecf3d1f1SJeff Layton /* 1254ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1255ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1256ecf3d1f1SJeff Layton * eventually need to do something more here. 1257ecf3d1f1SJeff Layton */ 1258ecf3d1f1SJeff Layton if (!inode) { 12596de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 12606de1472fSAl Viro __func__, dentry); 1261ecf3d1f1SJeff Layton return 1; 1262ecf3d1f1SJeff Layton } 1263ecf3d1f1SJeff Layton 1264ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 12656de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12666de1472fSAl Viro __func__, dentry); 1267ecf3d1f1SJeff Layton return 0; 1268ecf3d1f1SJeff Layton } 1269ecf3d1f1SJeff Layton 1270ecf3d1f1SJeff Layton error = nfs_revalidate_inode(NFS_SERVER(inode), inode); 1271ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1272ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1273ecf3d1f1SJeff Layton return !error; 1274ecf3d1f1SJeff Layton } 1275ecf3d1f1SJeff Layton 1276ecf3d1f1SJeff Layton /* 12771da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 12781da177e4SLinus Torvalds */ 1279fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 12801da177e4SLinus Torvalds { 12816de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 12826de1472fSAl Viro dentry, dentry->d_flags); 12831da177e4SLinus Torvalds 128477f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 12852b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 128677f11192STrond Myklebust return 1; 128777f11192STrond Myklebust 12881da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12891da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 12901da177e4SLinus Torvalds return 1; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 12931da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 12941da177e4SLinus Torvalds * files will be cleaned up during umount */ 12951da177e4SLinus Torvalds return 1; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds return 0; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13021b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13031b83d707STrond Myklebust { 13041b83d707STrond Myklebust spin_lock(&inode->i_lock); 13051f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 13061f018458STrond Myklebust if (inode->i_nlink == 1) 13071f018458STrond Myklebust clear_nlink(inode); 13081f018458STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; 13091b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13101b83d707STrond Myklebust } 13111b83d707STrond Myklebust 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * Called when the dentry loses inode. 13141da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13151da177e4SLinus Torvalds */ 13161da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13171da177e4SLinus Torvalds { 131883672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 131983672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 132083672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 132183672d39SNeil Brown 13221da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1323e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13241f018458STrond Myklebust nfs_drop_nlink(inode); 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds iput(inode); 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 1329b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1330b1942c5fSAl Viro { 1331b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1332b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1333b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1334b1942c5fSAl Viro WARN_ON(1); 1335b1942c5fSAl Viro else 1336b1942c5fSAl Viro kfree(dentry->d_fsdata); 1337b1942c5fSAl Viro } 1338b1942c5fSAl Viro } 1339b1942c5fSAl Viro 1340f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 13411da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1342ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 13431da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13441da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 134536d43a43SDavid Howells .d_automount = nfs_d_automount, 1346b1942c5fSAl Viro .d_release = nfs_d_release, 13471da177e4SLinus Torvalds }; 1348ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 13491da177e4SLinus Torvalds 1350597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds struct dentry *res; 1353565277f6STrond Myklebust struct dentry *parent; 13541da177e4SLinus Torvalds struct inode *inode = NULL; 1355e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1356e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 13571775fd3eSDavid Quigley struct nfs4_label *label = NULL; 13581da177e4SLinus Torvalds int error; 13591da177e4SLinus Torvalds 13606de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 136191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 13641da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 13651da177e4SLinus Torvalds goto out; 13661da177e4SLinus Torvalds 1367fd684071STrond Myklebust /* 1368fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1369fd684071STrond Myklebust * but don't hash the dentry. 1370fd684071STrond Myklebust */ 137100cd8dd3SAl Viro if (nfs_is_exclusive_create(dir, flags)) { 1372fd684071STrond Myklebust d_instantiate(dentry, NULL); 1373fd684071STrond Myklebust res = NULL; 1374fc0f684cSTrond Myklebust goto out; 1375fd684071STrond Myklebust } 13761da177e4SLinus Torvalds 1377e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1378e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1379e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1380e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1381e1fb4d05STrond Myklebust goto out; 1382e1fb4d05STrond Myklebust 138314c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 138414c43f76SDavid Quigley if (IS_ERR(label)) 138514c43f76SDavid Quigley goto out; 138614c43f76SDavid Quigley 1387565277f6STrond Myklebust parent = dentry->d_parent; 1388565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 13896e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1390565277f6STrond Myklebust nfs_block_sillyrename(parent); 13911775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 13921da177e4SLinus Torvalds if (error == -ENOENT) 13931da177e4SLinus Torvalds goto no_entry; 13941da177e4SLinus Torvalds if (error < 0) { 13951da177e4SLinus Torvalds res = ERR_PTR(error); 1396565277f6STrond Myklebust goto out_unblock_sillyrename; 13971da177e4SLinus Torvalds } 13981775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1399bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 140003f28e3aSTrond Myklebust if (IS_ERR(res)) 1401565277f6STrond Myklebust goto out_unblock_sillyrename; 140254ceac45SDavid Howells 1403d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1404d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1405d69ee9b8STrond Myklebust 14061da177e4SLinus Torvalds no_entry: 140741d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14089eaef27bSTrond Myklebust if (res != NULL) { 14099eaef27bSTrond Myklebust if (IS_ERR(res)) 1410565277f6STrond Myklebust goto out_unblock_sillyrename; 14111da177e4SLinus Torvalds dentry = res; 14129eaef27bSTrond Myklebust } 14131da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1414565277f6STrond Myklebust out_unblock_sillyrename: 1415565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 14166e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 141714c43f76SDavid Quigley nfs4_label_free(label); 14181da177e4SLinus Torvalds out: 1419e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1420e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14211da177e4SLinus Torvalds return res; 14221da177e4SLinus Torvalds } 1423ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14241da177e4SLinus Torvalds 142589d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14260b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14271da177e4SLinus Torvalds 1428f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14290ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 14301da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14311da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 143236d43a43SDavid Howells .d_automount = nfs_d_automount, 1433b1942c5fSAl Viro .d_release = nfs_d_release, 14341da177e4SLinus Torvalds }; 143589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14361da177e4SLinus Torvalds 14378a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14388a5e929dSAl Viro { 14398a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14408a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14418a5e929dSAl Viro res |= FMODE_READ; 14428a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14438a5e929dSAl Viro res |= FMODE_WRITE; 14448a5e929dSAl Viro return res; 14458a5e929dSAl Viro } 14468a5e929dSAl Viro 144751141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags) 1448cd9a1c0eSTrond Myklebust { 14495ede7b1cSAl Viro return alloc_nfs_open_context(dentry, flags_to_mode(open_flags)); 1450cd9a1c0eSTrond Myklebust } 1451cd9a1c0eSTrond Myklebust 1452cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1453cd9a1c0eSTrond Myklebust { 1454f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1455cd9a1c0eSTrond Myklebust return 0; 1456cd9a1c0eSTrond Myklebust } 1457cd9a1c0eSTrond Myklebust 1458d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 14590dd2b474SMiklos Szeredi struct dentry *dentry, 146030d90494SAl Viro struct file *file, unsigned open_flags, 146147237687SAl Viro int *opened) 1462cd9a1c0eSTrond Myklebust { 14630dd2b474SMiklos Szeredi int err; 14640dd2b474SMiklos Szeredi 146530d90494SAl Viro err = finish_open(file, dentry, do_open, opened); 146630d90494SAl Viro if (err) 1467d9585277SAl Viro goto out; 146830d90494SAl Viro nfs_file_set_open_context(file, ctx); 14690dd2b474SMiklos Szeredi 1470cd9a1c0eSTrond Myklebust out: 1471d9585277SAl Viro return err; 1472cd9a1c0eSTrond Myklebust } 1473cd9a1c0eSTrond Myklebust 147473a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 147530d90494SAl Viro struct file *file, unsigned open_flags, 147647237687SAl Viro umode_t mode, int *opened) 14771da177e4SLinus Torvalds { 1478cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 14790dd2b474SMiklos Szeredi struct dentry *res; 14800dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1481f46e0bd3STrond Myklebust struct inode *inode; 14821472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1483898f635cSTrond Myklebust int err; 14841da177e4SLinus Torvalds 14850dd2b474SMiklos Szeredi /* Expect a negative dentry */ 14862b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 14870dd2b474SMiklos Szeredi 14881e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 14896de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 14901e7cb3dcSChuck Lever 14919597c13bSJeff Layton err = nfs_check_flags(open_flags); 14929597c13bSJeff Layton if (err) 14939597c13bSJeff Layton return err; 14949597c13bSJeff Layton 14950dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 14960dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 14970dd2b474SMiklos Szeredi if (!d_unhashed(dentry)) { 14980dd2b474SMiklos Szeredi /* 14990dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15000dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15010dd2b474SMiklos Szeredi * again 15020dd2b474SMiklos Szeredi */ 1503d9585277SAl Viro return -ENOENT; 15040dd2b474SMiklos Szeredi } 15051472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15061da177e4SLinus Torvalds goto no_open; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15090dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1510d9585277SAl Viro return -ENAMETOOLONG; 15111da177e4SLinus Torvalds 15120dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1513536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 15140dd2b474SMiklos Szeredi attr.ia_mode = mode & ~current_umask(); 15150dd2b474SMiklos Szeredi } 1516536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1517536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1518536e43d1STrond Myklebust attr.ia_size = 0; 1519cd9a1c0eSTrond Myklebust } 1520cd9a1c0eSTrond Myklebust 15210dd2b474SMiklos Szeredi ctx = create_nfs_open_context(dentry, open_flags); 15220dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15230dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1524d9585277SAl Viro goto out; 15250dd2b474SMiklos Szeredi 15266e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 1527f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 15285bc2afc2STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened); 1529f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1530275bb307STrond Myklebust if (IS_ERR(inode)) { 15310dd2b474SMiklos Szeredi err = PTR_ERR(inode); 15326e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15332d9db750STrond Myklebust put_nfs_open_context(ctx); 15340dd2b474SMiklos Szeredi switch (err) { 15351da177e4SLinus Torvalds case -ENOENT: 1536275bb307STrond Myklebust d_drop(dentry); 1537f46e0bd3STrond Myklebust d_add(dentry, NULL); 1538809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15390dd2b474SMiklos Szeredi break; 15401788ea6eSJeff Layton case -EISDIR: 15416f926b5bSTrond Myklebust case -ENOTDIR: 15426f926b5bSTrond Myklebust goto no_open; 15431da177e4SLinus Torvalds case -ELOOP: 15440dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 15451da177e4SLinus Torvalds goto no_open; 15460dd2b474SMiklos Szeredi break; 15471da177e4SLinus Torvalds /* case -EINVAL: */ 15481da177e4SLinus Torvalds default: 15490dd2b474SMiklos Szeredi break; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds goto out; 15521da177e4SLinus Torvalds } 15530dd2b474SMiklos Szeredi 1554275bb307STrond Myklebust err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened); 15556e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15562d9db750STrond Myklebust put_nfs_open_context(ctx); 1557d9585277SAl Viro out: 1558d9585277SAl Viro return err; 15590dd2b474SMiklos Szeredi 15601da177e4SLinus Torvalds no_open: 15611472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 15620dd2b474SMiklos Szeredi err = PTR_ERR(res); 15630dd2b474SMiklos Szeredi if (IS_ERR(res)) 1564d9585277SAl Viro goto out; 15650dd2b474SMiklos Szeredi 1566e45198a6SAl Viro return finish_no_open(file, res); 15671da177e4SLinus Torvalds } 156889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 15691da177e4SLinus Torvalds 15700b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 15711da177e4SLinus Torvalds { 1572657e94b6SNick Piggin struct inode *inode; 157350de348cSMiklos Szeredi int ret = 0; 15741da177e4SLinus Torvalds 1575fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1576eda72afbSMiklos Szeredi goto no_open; 1577eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 15785584c306STrond Myklebust goto no_open; 157949f9a0faSTrond Myklebust if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1) 158049f9a0faSTrond Myklebust goto no_open; 15812b484297STrond Myklebust 15822b0143b5SDavid Howells inode = d_inode(dentry); 15832b484297STrond Myklebust 15841da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 15851da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 15861da177e4SLinus Torvalds */ 1587216d5d06STrond Myklebust if (inode == NULL) { 158849317a7fSNeilBrown struct dentry *parent; 158949317a7fSNeilBrown struct inode *dir; 159049317a7fSNeilBrown 1591912a108dSNeilBrown if (flags & LOOKUP_RCU) { 159250d77739SNeilBrown parent = ACCESS_ONCE(dentry->d_parent); 15932b0143b5SDavid Howells dir = d_inode_rcu(parent); 1594912a108dSNeilBrown if (!dir) 1595d51ac1a8SNeilBrown return -ECHILD; 1596912a108dSNeilBrown } else { 159749317a7fSNeilBrown parent = dget_parent(dentry); 15982b0143b5SDavid Howells dir = d_inode(parent); 1599912a108dSNeilBrown } 1600fa3c56bbSAl Viro if (!nfs_neg_need_reval(dir, dentry, flags)) 1601216d5d06STrond Myklebust ret = 1; 1602912a108dSNeilBrown else if (flags & LOOKUP_RCU) 1603912a108dSNeilBrown ret = -ECHILD; 1604912a108dSNeilBrown if (!(flags & LOOKUP_RCU)) 160549317a7fSNeilBrown dput(parent); 160650d77739SNeilBrown else if (parent != ACCESS_ONCE(dentry->d_parent)) 1607912a108dSNeilBrown return -ECHILD; 16081da177e4SLinus Torvalds goto out; 1609216d5d06STrond Myklebust } 1610216d5d06STrond Myklebust 16111da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16121da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 161349317a7fSNeilBrown goto no_open; 16141da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1615fa3c56bbSAl Viro if (flags & LOOKUP_EXCL) 161649317a7fSNeilBrown goto no_open; 16171da177e4SLinus Torvalds 16180ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1619898f635cSTrond Myklebust ret = 1; 16200ef97dcfSMiklos Szeredi 16211da177e4SLinus Torvalds out: 16221da177e4SLinus Torvalds return ret; 1623535918f1STrond Myklebust 16245584c306STrond Myklebust no_open: 16250b728e19SAl Viro return nfs_lookup_revalidate(dentry, flags); 1626c0204fd2STrond Myklebust } 1627c0204fd2STrond Myklebust 16281da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds /* 16311da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 16321da177e4SLinus Torvalds */ 16331da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 16341775fd3eSDavid Quigley struct nfs_fattr *fattr, 16351775fd3eSDavid Quigley struct nfs4_label *label) 16361da177e4SLinus Torvalds { 1637fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 16382b0143b5SDavid Howells struct inode *dir = d_inode(parent); 16391da177e4SLinus Torvalds struct inode *inode; 16401da177e4SLinus Torvalds int error = -EACCES; 16411da177e4SLinus Torvalds 1642fab728e1STrond Myklebust d_drop(dentry); 1643fab728e1STrond Myklebust 16441da177e4SLinus Torvalds /* We may have been initialized further down */ 16452b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1646fab728e1STrond Myklebust goto out; 16471da177e4SLinus Torvalds if (fhandle->size == 0) { 16481775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL); 16491da177e4SLinus Torvalds if (error) 1650fab728e1STrond Myklebust goto out_error; 16511da177e4SLinus Torvalds } 16525724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16531da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 16541da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 16551775fd3eSDavid Quigley error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL); 16561da177e4SLinus Torvalds if (error < 0) 1657fab728e1STrond Myklebust goto out_error; 16581da177e4SLinus Torvalds } 16591775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 166003f28e3aSTrond Myklebust error = PTR_ERR(inode); 166103f28e3aSTrond Myklebust if (IS_ERR(inode)) 1662fab728e1STrond Myklebust goto out_error; 1663fab728e1STrond Myklebust d_add(dentry, inode); 1664fab728e1STrond Myklebust out: 1665fab728e1STrond Myklebust dput(parent); 16661da177e4SLinus Torvalds return 0; 1667fab728e1STrond Myklebust out_error: 1668fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1669fab728e1STrond Myklebust dput(parent); 1670fab728e1STrond Myklebust return error; 16711da177e4SLinus Torvalds } 1672ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* 16751da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 16761da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 16771da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 16781da177e4SLinus Torvalds * reply path made it appear to have failed. 16791da177e4SLinus Torvalds */ 1680597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1681ebfc3b49SAl Viro umode_t mode, bool excl) 16821da177e4SLinus Torvalds { 16831da177e4SLinus Torvalds struct iattr attr; 1684ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 16851da177e4SLinus Torvalds int error; 16861da177e4SLinus Torvalds 16871e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 16886de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds attr.ia_mode = mode; 16911da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16921da177e4SLinus Torvalds 16938b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 16948867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 16958b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 16961da177e4SLinus Torvalds if (error != 0) 16971da177e4SLinus Torvalds goto out_err; 16981da177e4SLinus Torvalds return 0; 16991da177e4SLinus Torvalds out_err: 17001da177e4SLinus Torvalds d_drop(dentry); 17011da177e4SLinus Torvalds return error; 17021da177e4SLinus Torvalds } 1703ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* 17061da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17071da177e4SLinus Torvalds */ 1708597d9289SBryan Schumaker int 17091a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17101da177e4SLinus Torvalds { 17111da177e4SLinus Torvalds struct iattr attr; 17121da177e4SLinus Torvalds int status; 17131da177e4SLinus Torvalds 17141e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 17156de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 17181da177e4SLinus Torvalds return -EINVAL; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds attr.ia_mode = mode; 17211da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17221da177e4SLinus Torvalds 17231ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 17241da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 17251ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 17261da177e4SLinus Torvalds if (status != 0) 17271da177e4SLinus Torvalds goto out_err; 17281da177e4SLinus Torvalds return 0; 17291da177e4SLinus Torvalds out_err: 17301da177e4SLinus Torvalds d_drop(dentry); 17311da177e4SLinus Torvalds return status; 17321da177e4SLinus Torvalds } 1733ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds /* 17361da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17371da177e4SLinus Torvalds */ 1738597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 17391da177e4SLinus Torvalds { 17401da177e4SLinus Torvalds struct iattr attr; 17411da177e4SLinus Torvalds int error; 17421da177e4SLinus Torvalds 17431e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 17446de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17471da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 17481da177e4SLinus Torvalds 17491ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 17501da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 17511ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 17521da177e4SLinus Torvalds if (error != 0) 17531da177e4SLinus Torvalds goto out_err; 17541da177e4SLinus Torvalds return 0; 17551da177e4SLinus Torvalds out_err: 17561da177e4SLinus Torvalds d_drop(dentry); 17571da177e4SLinus Torvalds return error; 17581da177e4SLinus Torvalds } 1759ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 17601da177e4SLinus Torvalds 1761d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1762d45b9d8bSTrond Myklebust { 1763dc3f4198SAl Viro if (simple_positive(dentry)) 1764d45b9d8bSTrond Myklebust d_delete(dentry); 1765d45b9d8bSTrond Myklebust } 1766d45b9d8bSTrond Myklebust 1767597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 17681da177e4SLinus Torvalds { 17691da177e4SLinus Torvalds int error; 17701da177e4SLinus Torvalds 17711e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 17726de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17731da177e4SLinus Torvalds 17741ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 17752b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1776ba6c0592STrond Myklebust nfs_wait_on_sillyrename(dentry); 17771da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 17781da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1779ba6c0592STrond Myklebust switch (error) { 1780ba6c0592STrond Myklebust case 0: 17812b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1782ba6c0592STrond Myklebust break; 1783ba6c0592STrond Myklebust case -ENOENT: 1784d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1785ba6c0592STrond Myklebust } 1786ba6c0592STrond Myklebust } else 1787ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 17881ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds return error; 17911da177e4SLinus Torvalds } 1792ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds /* 17951da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 17961da177e4SLinus Torvalds * and after checking that the file has only one user. 17971da177e4SLinus Torvalds * 17981da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 17991da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18001da177e4SLinus Torvalds */ 18011da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18021da177e4SLinus Torvalds { 18032b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18042b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18051da177e4SLinus Torvalds int error = -EBUSY; 18061da177e4SLinus Torvalds 18076de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18101da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18111da177e4SLinus Torvalds error = 0; 18121da177e4SLinus Torvalds goto out; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 18151ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 18161da177e4SLinus Torvalds if (inode != NULL) { 181757ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 18181da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 18191da177e4SLinus Torvalds if (error == 0) 18201b83d707STrond Myklebust nfs_drop_nlink(inode); 18211da177e4SLinus Torvalds } else 18221da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1823d45b9d8bSTrond Myklebust if (error == -ENOENT) 1824d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18251ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 18261da177e4SLinus Torvalds out: 18271da177e4SLinus Torvalds return error; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 18311da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 18321da177e4SLinus Torvalds * 18331da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 18341da177e4SLinus Torvalds */ 1835597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 18361da177e4SLinus Torvalds { 18371da177e4SLinus Torvalds int error; 18381da177e4SLinus Torvalds int need_rehash = 0; 18391da177e4SLinus Torvalds 18401e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 18416de1472fSAl Viro dir->i_ino, dentry); 18421da177e4SLinus Torvalds 18431ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 18441da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 184584d08fa8SAl Viro if (d_count(dentry) > 1) { 18461da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1847ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 18482b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 18491da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 18501ca42382STrond Myklebust goto out; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 18531da177e4SLinus Torvalds __d_drop(dentry); 18541da177e4SLinus Torvalds need_rehash = 1; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 18571da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1858d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 18591da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18601da177e4SLinus Torvalds } else if (need_rehash) 18611da177e4SLinus Torvalds d_rehash(dentry); 18621ca42382STrond Myklebust out: 18631ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 18641da177e4SLinus Torvalds return error; 18651da177e4SLinus Torvalds } 1866ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 18671da177e4SLinus Torvalds 1868873101b3SChuck Lever /* 1869873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1870873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1871873101b3SChuck Lever * using prepare_write/commit_write. 1872873101b3SChuck Lever * 1873873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1874873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1875873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1876873101b3SChuck Lever * symlink request has completed on the server. 1877873101b3SChuck Lever * 1878873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1879873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1880873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1881873101b3SChuck Lever * and move the raw page into its mapping. 1882873101b3SChuck Lever */ 1883597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 18841da177e4SLinus Torvalds { 1885873101b3SChuck Lever struct page *page; 1886873101b3SChuck Lever char *kaddr; 18871da177e4SLinus Torvalds struct iattr attr; 1888873101b3SChuck Lever unsigned int pathlen = strlen(symname); 18891da177e4SLinus Torvalds int error; 18901da177e4SLinus Torvalds 18911e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 18926de1472fSAl Viro dir->i_ino, dentry, symname); 18931da177e4SLinus Torvalds 1894873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1895873101b3SChuck Lever return -ENAMETOOLONG; 18961da177e4SLinus Torvalds 1897873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1898873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 18991da177e4SLinus Torvalds 190083d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 190176566991STrond Myklebust if (!page) 1902873101b3SChuck Lever return -ENOMEM; 1903873101b3SChuck Lever 19042b86ce2dSCong Wang kaddr = kmap_atomic(page); 1905873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1906873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1907873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 19082b86ce2dSCong Wang kunmap_atomic(kaddr); 1909873101b3SChuck Lever 19101ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 191194a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19121ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1913873101b3SChuck Lever if (error != 0) { 19141e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1915873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 19166de1472fSAl Viro dentry, symname, error); 19171da177e4SLinus Torvalds d_drop(dentry); 1918873101b3SChuck Lever __free_page(page); 19191da177e4SLinus Torvalds return error; 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 1922873101b3SChuck Lever /* 1923873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1924873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1925873101b3SChuck Lever */ 19262b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 1927873101b3SChuck Lever GFP_KERNEL)) { 1928873101b3SChuck Lever SetPageUptodate(page); 1929873101b3SChuck Lever unlock_page(page); 1930a0b54addSRafael Aquini /* 1931a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 1932a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 1933a0b54addSRafael Aquini */ 1934a0b54addSRafael Aquini page_cache_release(page); 1935873101b3SChuck Lever } else 1936873101b3SChuck Lever __free_page(page); 1937873101b3SChuck Lever 1938873101b3SChuck Lever return 0; 1939873101b3SChuck Lever } 1940ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 1941873101b3SChuck Lever 1942597d9289SBryan Schumaker int 19431da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19441da177e4SLinus Torvalds { 19452b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 19461da177e4SLinus Torvalds int error; 19471da177e4SLinus Torvalds 19486de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 19496de1472fSAl Viro old_dentry, dentry); 19501da177e4SLinus Torvalds 19511fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 195257ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 19539a3936aaSTrond Myklebust 19549697d234STrond Myklebust d_drop(dentry); 19551da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1956cf809556STrond Myklebust if (error == 0) { 19577de9c6eeSAl Viro ihold(inode); 19589697d234STrond Myklebust d_add(dentry, inode); 1959cf809556STrond Myklebust } 19601fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 19611da177e4SLinus Torvalds return error; 19621da177e4SLinus Torvalds } 1963ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds /* 19661da177e4SLinus Torvalds * RENAME 19671da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 19681da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 19691da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 19701da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 19711da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 19721da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 19731da177e4SLinus Torvalds * 19741da177e4SLinus Torvalds * FIXED. 19751da177e4SLinus Torvalds * 19761da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 19771da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 19781da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 19791da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 19801da177e4SLinus Torvalds * using the inode layer 19811da177e4SLinus Torvalds * 19821da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 19831da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 19841da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 19851da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 19861da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 19871da177e4SLinus Torvalds * the rename. 19881da177e4SLinus Torvalds */ 1989597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 19901da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 19911da177e4SLinus Torvalds { 19922b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 19932b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 19941da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 199580a491fdSJeff Layton struct rpc_task *task; 19961da177e4SLinus Torvalds int error = -EBUSY; 19971da177e4SLinus Torvalds 19986de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 19996de1472fSAl Viro old_dentry, new_dentry, 200084d08fa8SAl Viro d_count(new_dentry)); 20011da177e4SLinus Torvalds 200270ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20031da177e4SLinus Torvalds /* 200428f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 200528f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 200628f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 200728f79a1aSMiklos Szeredi * the new target. 20081da177e4SLinus Torvalds */ 200927226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 201027226104SMiklos Szeredi /* 201127226104SMiklos Szeredi * To prevent any new references to the target during the 201227226104SMiklos Szeredi * rename, we unhash the dentry in advance. 201327226104SMiklos Szeredi */ 201427226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 201527226104SMiklos Szeredi d_drop(new_dentry); 201627226104SMiklos Szeredi rehash = new_dentry; 201727226104SMiklos Szeredi } 201827226104SMiklos Szeredi 201984d08fa8SAl Viro if (d_count(new_dentry) > 2) { 20201da177e4SLinus Torvalds int err; 202127226104SMiklos Szeredi 20221da177e4SLinus Torvalds /* copy the target dentry's name */ 20231da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 20241da177e4SLinus Torvalds &new_dentry->d_name); 20251da177e4SLinus Torvalds if (!dentry) 20261da177e4SLinus Torvalds goto out; 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds /* silly-rename the existing target ... */ 20291da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 203024e93025SMiklos Szeredi if (err) 20311da177e4SLinus Torvalds goto out; 203224e93025SMiklos Szeredi 203324e93025SMiklos Szeredi new_dentry = dentry; 203456335936SOGAWA Hirofumi rehash = NULL; 203524e93025SMiklos Szeredi new_inode = NULL; 2036b1e4adf4STrond Myklebust } 203727226104SMiklos Szeredi } 20381da177e4SLinus Torvalds 203957ec14c5SBryan Schumaker NFS_PROTO(old_inode)->return_delegation(old_inode); 2040b1e4adf4STrond Myklebust if (new_inode != NULL) 204157ec14c5SBryan Schumaker NFS_PROTO(new_inode)->return_delegation(new_inode); 20421da177e4SLinus Torvalds 204380a491fdSJeff Layton task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 204480a491fdSJeff Layton if (IS_ERR(task)) { 204580a491fdSJeff Layton error = PTR_ERR(task); 204680a491fdSJeff Layton goto out; 204780a491fdSJeff Layton } 204880a491fdSJeff Layton 204980a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 205080a491fdSJeff Layton if (error == 0) 205180a491fdSJeff Layton error = task->tk_status; 205280a491fdSJeff Layton rpc_put_task(task); 20535ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 20541da177e4SLinus Torvalds out: 20551da177e4SLinus Torvalds if (rehash) 20561da177e4SLinus Torvalds d_rehash(rehash); 205770ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 205870ded201STrond Myklebust new_dir, new_dentry, error); 20591da177e4SLinus Torvalds if (!error) { 2060b1e4adf4STrond Myklebust if (new_inode != NULL) 2061b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 20621da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 20638fb559f8SChuck Lever nfs_set_verifier(new_dentry, 20648fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 2065d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 2066d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds /* new dentry created? */ 20691da177e4SLinus Torvalds if (dentry) 20701da177e4SLinus Torvalds dput(dentry); 20711da177e4SLinus Torvalds return error; 20721da177e4SLinus Torvalds } 2073ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 20741da177e4SLinus Torvalds 2075cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2076cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2077cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2078cfcea3e8STrond Myklebust 20793a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 20803a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 20813a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 20823a505845STrond Myklebust 20831c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 20841c3c07e9STrond Myklebust { 20851c3c07e9STrond Myklebust put_rpccred(entry->cred); 2086f682a398SNeilBrown kfree_rcu(entry, rcu_head); 20874e857c58SPeter Zijlstra smp_mb__before_atomic(); 2088cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 20894e857c58SPeter Zijlstra smp_mb__after_atomic(); 20901c3c07e9STrond Myklebust } 20911c3c07e9STrond Myklebust 20921a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 20931a81bb8aSTrond Myklebust { 20941a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 20951a81bb8aSTrond Myklebust 20961a81bb8aSTrond Myklebust while (!list_empty(head)) { 20971a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 20981a81bb8aSTrond Myklebust list_del(&cache->lru); 20991a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 21001a81bb8aSTrond Myklebust } 21011a81bb8aSTrond Myklebust } 21021a81bb8aSTrond Myklebust 21033a505845STrond Myklebust static unsigned long 21043a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2105979df72eSTrond Myklebust { 2106979df72eSTrond Myklebust LIST_HEAD(head); 2107aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2108979df72eSTrond Myklebust struct nfs_access_entry *cache; 21091ab6c499SDave Chinner long freed = 0; 2110979df72eSTrond Myklebust 2111a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2112aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2113979df72eSTrond Myklebust struct inode *inode; 2114979df72eSTrond Myklebust 2115979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2116979df72eSTrond Myklebust break; 21179c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2118979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2119979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2120979df72eSTrond Myklebust goto remove_lru_entry; 2121979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2122979df72eSTrond Myklebust struct nfs_access_entry, lru); 2123979df72eSTrond Myklebust list_move(&cache->lru, &head); 2124979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 21251ab6c499SDave Chinner freed++; 2126979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2127979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2128979df72eSTrond Myklebust &nfs_access_lru_list); 2129979df72eSTrond Myklebust else { 2130979df72eSTrond Myklebust remove_lru_entry: 2131979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 21324e857c58SPeter Zijlstra smp_mb__before_atomic(); 2133979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 21344e857c58SPeter Zijlstra smp_mb__after_atomic(); 2135979df72eSTrond Myklebust } 213659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2137979df72eSTrond Myklebust } 2138979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21391a81bb8aSTrond Myklebust nfs_access_free_list(&head); 21401ab6c499SDave Chinner return freed; 21411ab6c499SDave Chinner } 21421ab6c499SDave Chinner 21431ab6c499SDave Chinner unsigned long 21443a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 21453a505845STrond Myklebust { 21463a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 21473a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 21483a505845STrond Myklebust 21493a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 21503a505845STrond Myklebust return SHRINK_STOP; 21513a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 21523a505845STrond Myklebust } 21533a505845STrond Myklebust 21543a505845STrond Myklebust 21553a505845STrond Myklebust unsigned long 21561ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 21571ab6c499SDave Chinner { 215855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2159979df72eSTrond Myklebust } 2160979df72eSTrond Myklebust 21613a505845STrond Myklebust static void 21623a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 21633a505845STrond Myklebust { 21643a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 21653a505845STrond Myklebust unsigned long diff; 21663a505845STrond Myklebust unsigned int nr_to_scan; 21673a505845STrond Myklebust 21683a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 21693a505845STrond Myklebust return; 21703a505845STrond Myklebust nr_to_scan = 100; 21713a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 21723a505845STrond Myklebust if (diff < nr_to_scan) 21733a505845STrond Myklebust nr_to_scan = diff; 21743a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 21753a505845STrond Myklebust } 21763a505845STrond Myklebust 21771a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 21781c3c07e9STrond Myklebust { 21791c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 21801a81bb8aSTrond Myklebust struct rb_node *n; 21811c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21821c3c07e9STrond Myklebust 21831c3c07e9STrond Myklebust /* Unhook entries from the cache */ 21841c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 21851c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 21861c3c07e9STrond Myklebust rb_erase(n, root_node); 21871a81bb8aSTrond Myklebust list_move(&entry->lru, head); 21881c3c07e9STrond Myklebust } 21891c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 21901c3c07e9STrond Myklebust } 21911c3c07e9STrond Myklebust 21921c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 21931c3c07e9STrond Myklebust { 21941a81bb8aSTrond Myklebust LIST_HEAD(head); 21951a81bb8aSTrond Myklebust 21961a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 21971a81bb8aSTrond Myklebust return; 2198cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2199cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22001a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2201cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2202cfcea3e8STrond Myklebust 22031c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22041a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 22051a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22061a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22071a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22081c3c07e9STrond Myklebust } 22091c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 22101c3c07e9STrond Myklebust 22111c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 22121c3c07e9STrond Myklebust { 22131c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 22141c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22151c3c07e9STrond Myklebust 22161c3c07e9STrond Myklebust while (n != NULL) { 22171c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22181c3c07e9STrond Myklebust 22191c3c07e9STrond Myklebust if (cred < entry->cred) 22201c3c07e9STrond Myklebust n = n->rb_left; 22211c3c07e9STrond Myklebust else if (cred > entry->cred) 22221c3c07e9STrond Myklebust n = n->rb_right; 22231c3c07e9STrond Myklebust else 22241c3c07e9STrond Myklebust return entry; 22251c3c07e9STrond Myklebust } 22261c3c07e9STrond Myklebust return NULL; 22271c3c07e9STrond Myklebust } 22281c3c07e9STrond Myklebust 2229af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 22301da177e4SLinus Torvalds { 223155296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 22321c3c07e9STrond Myklebust struct nfs_access_entry *cache; 22331c3c07e9STrond Myklebust int err = -ENOENT; 22341da177e4SLinus Torvalds 22351c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22361c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 22371c3c07e9STrond Myklebust goto out_zap; 22381c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 22391c3c07e9STrond Myklebust if (cache == NULL) 22401c3c07e9STrond Myklebust goto out; 2241b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 224264672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 22431c3c07e9STrond Myklebust goto out_stale; 22441c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 22451c3c07e9STrond Myklebust res->cred = cache->cred; 22461c3c07e9STrond Myklebust res->mask = cache->mask; 2247cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 22481c3c07e9STrond Myklebust err = 0; 22491c3c07e9STrond Myklebust out: 22501c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22511c3c07e9STrond Myklebust return err; 22521c3c07e9STrond Myklebust out_stale: 22531c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2254cfcea3e8STrond Myklebust list_del(&cache->lru); 22551c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22561c3c07e9STrond Myklebust nfs_access_free_entry(cache); 22571da177e4SLinus Torvalds return -ENOENT; 22581c3c07e9STrond Myklebust out_zap: 22591a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22601a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 22611c3c07e9STrond Myklebust return -ENOENT; 22621c3c07e9STrond Myklebust } 22631c3c07e9STrond Myklebust 2264f682a398SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 2265f682a398SNeilBrown { 2266f682a398SNeilBrown /* Only check the most recently returned cache entry, 2267f682a398SNeilBrown * but do it without locking. 2268f682a398SNeilBrown */ 2269f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2270f682a398SNeilBrown struct nfs_access_entry *cache; 2271f682a398SNeilBrown int err = -ECHILD; 2272f682a398SNeilBrown struct list_head *lh; 2273f682a398SNeilBrown 2274f682a398SNeilBrown rcu_read_lock(); 2275f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2276f682a398SNeilBrown goto out; 2277f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2278f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2279f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 2280f682a398SNeilBrown cred != cache->cred) 2281f682a398SNeilBrown cache = NULL; 2282f682a398SNeilBrown if (cache == NULL) 2283f682a398SNeilBrown goto out; 2284f682a398SNeilBrown if (!nfs_have_delegated_attributes(inode) && 2285f682a398SNeilBrown !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 2286f682a398SNeilBrown goto out; 2287f682a398SNeilBrown res->jiffies = cache->jiffies; 2288f682a398SNeilBrown res->cred = cache->cred; 2289f682a398SNeilBrown res->mask = cache->mask; 2290f682a398SNeilBrown err = 0; 2291f682a398SNeilBrown out: 2292f682a398SNeilBrown rcu_read_unlock(); 2293f682a398SNeilBrown return err; 2294f682a398SNeilBrown } 2295f682a398SNeilBrown 22961c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 22971c3c07e9STrond Myklebust { 2298cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2299cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23001c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 23011c3c07e9STrond Myklebust struct rb_node *parent = NULL; 23021c3c07e9STrond Myklebust struct nfs_access_entry *entry; 23031c3c07e9STrond Myklebust 23041c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23051c3c07e9STrond Myklebust while (*p != NULL) { 23061c3c07e9STrond Myklebust parent = *p; 23071c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 23081c3c07e9STrond Myklebust 23091c3c07e9STrond Myklebust if (set->cred < entry->cred) 23101c3c07e9STrond Myklebust p = &parent->rb_left; 23111c3c07e9STrond Myklebust else if (set->cred > entry->cred) 23121c3c07e9STrond Myklebust p = &parent->rb_right; 23131c3c07e9STrond Myklebust else 23141c3c07e9STrond Myklebust goto found; 23151c3c07e9STrond Myklebust } 23161c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 23171c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2318cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 23191c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23201c3c07e9STrond Myklebust return; 23211c3c07e9STrond Myklebust found: 23221c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2323cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2324cfcea3e8STrond Myklebust list_del(&entry->lru); 23251c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23261c3c07e9STrond Myklebust nfs_access_free_entry(entry); 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds 23296168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 23301da177e4SLinus Torvalds { 23311c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 23321c3c07e9STrond Myklebust if (cache == NULL) 23331c3c07e9STrond Myklebust return; 23341c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 23351da177e4SLinus Torvalds cache->jiffies = set->jiffies; 23361c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 23371da177e4SLinus Torvalds cache->mask = set->mask; 23381c3c07e9STrond Myklebust 2339f682a398SNeilBrown /* The above field assignments must be visible 2340f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2341f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2342f682a398SNeilBrown */ 2343f682a398SNeilBrown smp_wmb(); 23441c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2345cfcea3e8STrond Myklebust 2346cfcea3e8STrond Myklebust /* Update accounting */ 23474e857c58SPeter Zijlstra smp_mb__before_atomic(); 2348cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 23494e857c58SPeter Zijlstra smp_mb__after_atomic(); 2350cfcea3e8STrond Myklebust 2351cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 23521a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2353cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 23541a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 23551a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 23561a81bb8aSTrond Myklebust &nfs_access_lru_list); 2357cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2358cfcea3e8STrond Myklebust } 23593a505845STrond Myklebust nfs_access_cache_enforce_limit(); 23601da177e4SLinus Torvalds } 23616168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 23626168f62cSWeston Andros Adamson 23636168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 23646168f62cSWeston Andros Adamson { 23656168f62cSWeston Andros Adamson entry->mask = 0; 23666168f62cSWeston Andros Adamson if (access_result & NFS4_ACCESS_READ) 23676168f62cSWeston Andros Adamson entry->mask |= MAY_READ; 23686168f62cSWeston Andros Adamson if (access_result & 23696168f62cSWeston Andros Adamson (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) 23706168f62cSWeston Andros Adamson entry->mask |= MAY_WRITE; 23716168f62cSWeston Andros Adamson if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) 23726168f62cSWeston Andros Adamson entry->mask |= MAY_EXEC; 23736168f62cSWeston Andros Adamson } 23746168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 23771da177e4SLinus Torvalds { 23781da177e4SLinus Torvalds struct nfs_access_entry cache; 23791da177e4SLinus Torvalds int status; 23801da177e4SLinus Torvalds 2381f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2382f4ce1299STrond Myklebust 2383f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2384f682a398SNeilBrown if (status != 0) 23851da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 23861da177e4SLinus Torvalds if (status == 0) 2387f4ce1299STrond Myklebust goto out_cached; 23881da177e4SLinus Torvalds 2389f3324a2aSNeilBrown status = -ECHILD; 2390f3324a2aSNeilBrown if (mask & MAY_NOT_BLOCK) 2391f3324a2aSNeilBrown goto out; 2392f3324a2aSNeilBrown 23931da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 23941da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 23951da177e4SLinus Torvalds cache.cred = cred; 23961da177e4SLinus Torvalds cache.jiffies = jiffies; 23971da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2398a71ee337SSuresh Jayaraman if (status != 0) { 2399a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2400a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2401a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2402a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2403a71ee337SSuresh Jayaraman } 2404f4ce1299STrond Myklebust goto out; 2405a71ee337SSuresh Jayaraman } 24061da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2407f4ce1299STrond Myklebust out_cached: 2408f4ce1299STrond Myklebust if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2409f4ce1299STrond Myklebust status = -EACCES; 24101da177e4SLinus Torvalds out: 2411f4ce1299STrond Myklebust trace_nfs_access_exit(inode, status); 2412f4ce1299STrond Myklebust return status; 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds 2415af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2416af22f94aSTrond Myklebust { 2417af22f94aSTrond Myklebust int mask = 0; 2418af22f94aSTrond Myklebust 2419f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2420f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2421f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2422f8d9a897SWeston Andros Adamson } else { 24238a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2424af22f94aSTrond Myklebust mask |= MAY_READ; 24258a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2426af22f94aSTrond Myklebust mask |= MAY_WRITE; 2427f8d9a897SWeston Andros Adamson } 2428f8d9a897SWeston Andros Adamson 2429af22f94aSTrond Myklebust return mask; 2430af22f94aSTrond Myklebust } 2431af22f94aSTrond Myklebust 2432af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2433af22f94aSTrond Myklebust { 2434af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2435af22f94aSTrond Myklebust } 243689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2437af22f94aSTrond Myklebust 243810556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 24391da177e4SLinus Torvalds { 24401da177e4SLinus Torvalds struct rpc_cred *cred; 24411da177e4SLinus Torvalds int res = 0; 24421da177e4SLinus Torvalds 244391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 244491d5b470SChuck Lever 2445e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 24461da177e4SLinus Torvalds goto out; 24471da177e4SLinus Torvalds /* Is this sys_access() ? */ 24489cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 24491da177e4SLinus Torvalds goto force_lookup; 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 24521da177e4SLinus Torvalds case S_IFLNK: 24531da177e4SLinus Torvalds goto out; 24541da177e4SLinus Torvalds case S_IFREG: 24551da177e4SLinus Torvalds break; 24561da177e4SLinus Torvalds case S_IFDIR: 24571da177e4SLinus Torvalds /* 24581da177e4SLinus Torvalds * Optimize away all write operations, since the server 24591da177e4SLinus Torvalds * will check permissions when we perform the op. 24601da177e4SLinus Torvalds */ 24611da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 24621da177e4SLinus Torvalds goto out; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds force_lookup: 24661da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 24671da177e4SLinus Torvalds goto out_notsup; 24681da177e4SLinus Torvalds 2469f3324a2aSNeilBrown /* Always try fast lookups first */ 2470f3324a2aSNeilBrown rcu_read_lock(); 2471f3324a2aSNeilBrown cred = rpc_lookup_cred_nonblock(); 2472f3324a2aSNeilBrown if (!IS_ERR(cred)) 2473f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2474f3324a2aSNeilBrown else 2475f3324a2aSNeilBrown res = PTR_ERR(cred); 2476f3324a2aSNeilBrown rcu_read_unlock(); 2477f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2478f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 247998a8e323STrond Myklebust cred = rpc_lookup_cred(); 24801da177e4SLinus Torvalds if (!IS_ERR(cred)) { 24811da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 24821da177e4SLinus Torvalds put_rpccred(cred); 24831da177e4SLinus Torvalds } else 24841da177e4SLinus Torvalds res = PTR_ERR(cred); 2485f3324a2aSNeilBrown } 24861da177e4SLinus Torvalds out: 2487f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2488f696a365SMiklos Szeredi res = -EACCES; 2489f696a365SMiklos Szeredi 24901e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 24911e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 24921da177e4SLinus Torvalds return res; 24931da177e4SLinus Torvalds out_notsup: 2494d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2495d51ac1a8SNeilBrown return -ECHILD; 2496d51ac1a8SNeilBrown 24971da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 24981da177e4SLinus Torvalds if (res == 0) 24992830ba7fSAl Viro res = generic_permission(inode, mask); 25001e7cb3dcSChuck Lever goto out; 25011da177e4SLinus Torvalds } 2502ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds /* 25051da177e4SLinus Torvalds * Local variables: 25061da177e4SLinus Torvalds * version-control: t 25071da177e4SLinus Torvalds * kept-new-versions: 5 25081da177e4SLinus Torvalds * End: 25091da177e4SLinus Torvalds */ 2510