1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/dir.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * nfs directory handling functions 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * 10 Apr 1996 Added silly rename for unlink --okir 101da177e4SLinus Torvalds * 28 Sep 1996 Improved directory cache --okir 111da177e4SLinus Torvalds * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de 121da177e4SLinus Torvalds * Re-implemented silly rename for unlink, newly implemented 131da177e4SLinus Torvalds * silly rename for nfs_rename() following the suggestions 141da177e4SLinus Torvalds * of Olaf Kirch (okir) found in this file. 151da177e4SLinus Torvalds * Following Linus comments on my original hack, this version 161da177e4SLinus Torvalds * depends only on the dcache stuff and doesn't touch the inode 171da177e4SLinus Torvalds * layer (iput() and friends). 181da177e4SLinus Torvalds * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 21ddda8e0aSBryan Schumaker #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/errno.h> 241da177e4SLinus Torvalds #include <linux/stat.h> 251da177e4SLinus Torvalds #include <linux/fcntl.h> 261da177e4SLinus Torvalds #include <linux/string.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/slab.h> 291da177e4SLinus Torvalds #include <linux/mm.h> 301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 311da177e4SLinus Torvalds #include <linux/nfs_fs.h> 321da177e4SLinus Torvalds #include <linux/nfs_mount.h> 331da177e4SLinus Torvalds #include <linux/pagemap.h> 34873101b3SChuck Lever #include <linux/pagevec.h> 351da177e4SLinus Torvalds #include <linux/namei.h> 3654ceac45SDavid Howells #include <linux/mount.h> 37a0b8cab3SMel Gorman #include <linux/swap.h> 38e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include "delegation.h" 4391d5b470SChuck Lever #include "iostat.h" 444c30d56eSAdrian Bunk #include "internal.h" 45cd9a1c0eSTrond Myklebust #include "fscache.h" 461da177e4SLinus Torvalds 47f4ce1299STrond Myklebust #include "nfstrace.h" 48f4ce1299STrond Myklebust 491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 571da177e4SLinus Torvalds 584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 59f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 601da177e4SLinus Torvalds .read = generic_read_dir, 6193a6ab7bSTrond Myklebust .iterate_shared = nfs_readdir, 621da177e4SLinus Torvalds .open = nfs_opendir, 63480c2006SBryan Schumaker .release = nfs_closedir, 641da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 651da177e4SLinus Torvalds }; 661da177e4SLinus Torvalds 6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6811de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 69d1bacf9eSBryan Schumaker }; 70d1bacf9eSBryan Schumaker 71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred) 72480c2006SBryan Schumaker { 73311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 74480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 75480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 76480c2006SBryan Schumaker if (ctx != NULL) { 778ef2ce3eSBryan Schumaker ctx->duped = 0; 78311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 79480c2006SBryan Schumaker ctx->dir_cookie = 0; 808ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 81684f39b4SNeilBrown ctx->cred = get_cred(cred); 82311324adSTrond Myklebust spin_lock(&dir->i_lock); 831c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 841c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 851c341b77STrond Myklebust nfsi->cache_validity |= NFS_INO_INVALID_DATA | 861c341b77STrond Myklebust NFS_INO_REVAL_FORCED; 87311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 88311324adSTrond Myklebust spin_unlock(&dir->i_lock); 89480c2006SBryan Schumaker return ctx; 90480c2006SBryan Schumaker } 910c030806STrond Myklebust return ERR_PTR(-ENOMEM); 920c030806STrond Myklebust } 93480c2006SBryan Schumaker 94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 95480c2006SBryan Schumaker { 96311324adSTrond Myklebust spin_lock(&dir->i_lock); 97311324adSTrond Myklebust list_del(&ctx->list); 98311324adSTrond Myklebust spin_unlock(&dir->i_lock); 99684f39b4SNeilBrown put_cred(ctx->cred); 100480c2006SBryan Schumaker kfree(ctx); 101480c2006SBryan Schumaker } 102480c2006SBryan Schumaker 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * Open file 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds static int 1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1081da177e4SLinus Torvalds { 109480c2006SBryan Schumaker int res = 0; 110480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1111da177e4SLinus Torvalds 1126de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 113cc0dd2d1SChuck Lever 114cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1151e7cb3dcSChuck Lever 116684f39b4SNeilBrown ctx = alloc_nfs_open_dir_context(inode, current_cred()); 117480c2006SBryan Schumaker if (IS_ERR(ctx)) { 118480c2006SBryan Schumaker res = PTR_ERR(ctx); 119480c2006SBryan Schumaker goto out; 120480c2006SBryan Schumaker } 121480c2006SBryan Schumaker filp->private_data = ctx; 122480c2006SBryan Schumaker out: 1231da177e4SLinus Torvalds return res; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126480c2006SBryan Schumaker static int 127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 128480c2006SBryan Schumaker { 129a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 130480c2006SBryan Schumaker return 0; 131480c2006SBryan Schumaker } 132480c2006SBryan Schumaker 133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 134d1bacf9eSBryan Schumaker u64 cookie; 135d1bacf9eSBryan Schumaker u64 ino; 136d1bacf9eSBryan Schumaker struct qstr string; 1370b26a0bfSTrond Myklebust unsigned char d_type; 138d1bacf9eSBryan Schumaker }; 139d1bacf9eSBryan Schumaker 140d1bacf9eSBryan Schumaker struct nfs_cache_array { 14188b8e133SChuck Lever int size; 142d1bacf9eSBryan Schumaker int eof_index; 143d1bacf9eSBryan Schumaker u64 last_cookie; 1445601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 145d1bacf9eSBryan Schumaker }; 146d1bacf9eSBryan Schumaker 147*2e7a4641STrond Myklebust typedef struct nfs_readdir_descriptor { 1481da177e4SLinus Torvalds struct file *file; 1491da177e4SLinus Torvalds struct page *page; 15023db8620SAl Viro struct dir_context *ctx; 1511da177e4SLinus Torvalds unsigned long page_index; 152*2e7a4641STrond Myklebust u64 dir_cookie; 1530aded708STrond Myklebust u64 last_cookie; 154*2e7a4641STrond Myklebust u64 dup_cookie; 155f0dd2136STrond Myklebust loff_t current_index; 15659e356a9STrond Myklebust loff_t prev_index; 157d1bacf9eSBryan Schumaker 158a1147b82STrond Myklebust unsigned long dir_verifier; 1591f4eab7eSNeil Brown unsigned long timestamp; 1604704f0e2STrond Myklebust unsigned long gencount; 161*2e7a4641STrond Myklebust unsigned long attr_gencount; 162d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 163*2e7a4641STrond Myklebust signed char duped; 164a7a3b1e9SBenjamin Coddington bool plus; 165a7a3b1e9SBenjamin Coddington bool eof; 1661da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1671da177e4SLinus Torvalds 1684b310319STrond Myklebust static 1694b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 1704b310319STrond Myklebust { 1714b310319STrond Myklebust struct nfs_cache_array *array; 1724b310319STrond Myklebust 1734b310319STrond Myklebust array = kmap_atomic(page); 1744b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1754b310319STrond Myklebust array->eof_index = -1; 1764b310319STrond Myklebust kunmap_atomic(array); 1774b310319STrond Myklebust } 1784b310319STrond Myklebust 179d1bacf9eSBryan Schumaker /* 180d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 181d1bacf9eSBryan Schumaker */ 182d1bacf9eSBryan Schumaker static 18311de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 184d1bacf9eSBryan Schumaker { 18511de3b11STrond Myklebust struct nfs_cache_array *array; 186d1bacf9eSBryan Schumaker int i; 1878cd51a0cSTrond Myklebust 1882b86ce2dSCong Wang array = kmap_atomic(page); 189d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 190d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1914b310319STrond Myklebust array->size = 0; 1922b86ce2dSCong Wang kunmap_atomic(array); 193d1bacf9eSBryan Schumaker } 194d1bacf9eSBryan Schumaker 195d1bacf9eSBryan Schumaker /* 196d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 197d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 198d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 199d1bacf9eSBryan Schumaker */ 200d1bacf9eSBryan Schumaker static 2014a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 202d1bacf9eSBryan Schumaker { 203d1bacf9eSBryan Schumaker string->len = len; 2043803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2054a201d6eSTrond Myklebust if (string->name == NULL) 2064a201d6eSTrond Myklebust return -ENOMEM; 20704e4bd1cSCatalin Marinas /* 20804e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 20904e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 21004e4bd1cSCatalin Marinas */ 21104e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2128387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2134a201d6eSTrond Myklebust return 0; 214d1bacf9eSBryan Schumaker } 215d1bacf9eSBryan Schumaker 216d1bacf9eSBryan Schumaker static 217d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 218d1bacf9eSBryan Schumaker { 2190795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2204a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2214a201d6eSTrond Myklebust int ret; 2224a201d6eSTrond Myklebust 2234a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2243020093fSTrond Myklebust 2253020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2263020093fSTrond Myklebust ret = -ENOSPC; 2273020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2283020093fSTrond Myklebust goto out; 2293020093fSTrond Myklebust 2304a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2314a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2320b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2334a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2344a201d6eSTrond Myklebust if (ret) 2354a201d6eSTrond Myklebust goto out; 236d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2378cd51a0cSTrond Myklebust array->size++; 23847c716cbSTrond Myklebust if (entry->eof != 0) 239d1bacf9eSBryan Schumaker array->eof_index = array->size; 2404a201d6eSTrond Myklebust out: 2410795bf83SFabian Frederick kunmap(page); 2424a201d6eSTrond Myklebust return ret; 243d1bacf9eSBryan Schumaker } 244d1bacf9eSBryan Schumaker 24559e356a9STrond Myklebust static inline 24659e356a9STrond Myklebust int is_32bit_api(void) 24759e356a9STrond Myklebust { 24859e356a9STrond Myklebust #ifdef CONFIG_COMPAT 24959e356a9STrond Myklebust return in_compat_syscall(); 25059e356a9STrond Myklebust #else 25159e356a9STrond Myklebust return (BITS_PER_LONG == 32); 25259e356a9STrond Myklebust #endif 25359e356a9STrond Myklebust } 25459e356a9STrond Myklebust 25559e356a9STrond Myklebust static 25659e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 25759e356a9STrond Myklebust { 25859e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 25959e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 26059e356a9STrond Myklebust return false; 26159e356a9STrond Myklebust return true; 26259e356a9STrond Myklebust } 26359e356a9STrond Myklebust 264d1bacf9eSBryan Schumaker static 265d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 266d1bacf9eSBryan Schumaker { 26723db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 268d1bacf9eSBryan Schumaker unsigned int index; 269d1bacf9eSBryan Schumaker 270d1bacf9eSBryan Schumaker if (diff < 0) 271d1bacf9eSBryan Schumaker goto out_eof; 272d1bacf9eSBryan Schumaker if (diff >= array->size) { 2738cd51a0cSTrond Myklebust if (array->eof_index >= 0) 274d1bacf9eSBryan Schumaker goto out_eof; 275d1bacf9eSBryan Schumaker return -EAGAIN; 276d1bacf9eSBryan Schumaker } 277d1bacf9eSBryan Schumaker 278d1bacf9eSBryan Schumaker index = (unsigned int)diff; 279*2e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 280d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 281d1bacf9eSBryan Schumaker return 0; 282d1bacf9eSBryan Schumaker out_eof: 2836089dd0dSThomas Meyer desc->eof = true; 284d1bacf9eSBryan Schumaker return -EBADCOOKIE; 285d1bacf9eSBryan Schumaker } 286d1bacf9eSBryan Schumaker 2874db72b40SJeff Layton static bool 2884db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2894db72b40SJeff Layton { 2904db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2914db72b40SJeff Layton return false; 2924db72b40SJeff Layton smp_rmb(); 2934db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2944db72b40SJeff Layton } 2954db72b40SJeff Layton 296d1bacf9eSBryan Schumaker static 297d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 298d1bacf9eSBryan Schumaker { 299d1bacf9eSBryan Schumaker int i; 3008ef2ce3eSBryan Schumaker loff_t new_pos; 301d1bacf9eSBryan Schumaker int status = -EAGAIN; 302d1bacf9eSBryan Schumaker 303d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 304*2e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 305496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 3060c030806STrond Myklebust 3078ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 308*2e7a4641STrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount || 3094db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 310*2e7a4641STrond Myklebust desc->duped = 0; 311*2e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 31259e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 313*2e7a4641STrond Myklebust if (desc->duped > 0 314*2e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 3150c030806STrond Myklebust if (printk_ratelimit()) { 3166de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 3170c030806STrond Myklebust "Please contact your server vendor. " 3189581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 3199581a4aeSJeff Layton desc->file, array->array[i].string.len, 320*2e7a4641STrond Myklebust array->array[i].string.name, desc->dir_cookie); 3210c030806STrond Myklebust } 3220c030806STrond Myklebust status = -ELOOP; 3230c030806STrond Myklebust goto out; 3240c030806STrond Myklebust } 325*2e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 326*2e7a4641STrond Myklebust desc->duped = -1; 3278ef2ce3eSBryan Schumaker } 32859e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 329*2e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 33059e356a9STrond Myklebust else 33123db8620SAl Viro desc->ctx->pos = new_pos; 33259e356a9STrond Myklebust desc->prev_index = new_pos; 3338cd51a0cSTrond Myklebust desc->cache_entry_index = i; 33447c716cbSTrond Myklebust return 0; 3358cd51a0cSTrond Myklebust } 3368cd51a0cSTrond Myklebust } 33747c716cbSTrond Myklebust if (array->eof_index >= 0) { 338d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 339*2e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 3406089dd0dSThomas Meyer desc->eof = true; 341d1bacf9eSBryan Schumaker } 3420c030806STrond Myklebust out: 343d1bacf9eSBryan Schumaker return status; 344d1bacf9eSBryan Schumaker } 345d1bacf9eSBryan Schumaker 346d1bacf9eSBryan Schumaker static 347d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 348d1bacf9eSBryan Schumaker { 349d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 35047c716cbSTrond Myklebust int status; 351d1bacf9eSBryan Schumaker 3520795bf83SFabian Frederick array = kmap(desc->page); 353d1bacf9eSBryan Schumaker 354*2e7a4641STrond Myklebust if (desc->dir_cookie == 0) 355d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 356d1bacf9eSBryan Schumaker else 357d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 358d1bacf9eSBryan Schumaker 35947c716cbSTrond Myklebust if (status == -EAGAIN) { 3600aded708STrond Myklebust desc->last_cookie = array->last_cookie; 361e47c085aSTrond Myklebust desc->current_index += array->size; 36247c716cbSTrond Myklebust desc->page_index++; 36347c716cbSTrond Myklebust } 3640795bf83SFabian Frederick kunmap(desc->page); 365d1bacf9eSBryan Schumaker return status; 366d1bacf9eSBryan Schumaker } 367d1bacf9eSBryan Schumaker 368d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 369d1bacf9eSBryan Schumaker static 37056e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 371d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 372d1bacf9eSBryan Schumaker { 373480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 374684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3754704f0e2STrond Myklebust unsigned long timestamp, gencount; 3761da177e4SLinus Torvalds int error; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds again: 3791da177e4SLinus Torvalds timestamp = jiffies; 3804704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 381a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 382be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3831da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3841da177e4SLinus Torvalds if (error < 0) { 3851da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3861da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3871da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3883a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 389a7a3b1e9SBenjamin Coddington desc->plus = false; 3901da177e4SLinus Torvalds goto again; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds goto error; 3931da177e4SLinus Torvalds } 3941f4eab7eSNeil Brown desc->timestamp = timestamp; 3954704f0e2STrond Myklebust desc->gencount = gencount; 396d1bacf9eSBryan Schumaker error: 397d1bacf9eSBryan Schumaker return error; 398d1bacf9eSBryan Schumaker } 399d1bacf9eSBryan Schumaker 400573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 401573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 402d1bacf9eSBryan Schumaker { 40359e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 404573c4e1eSChuck Lever int error; 405d1bacf9eSBryan Schumaker 40659e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 407573c4e1eSChuck Lever if (error) 408573c4e1eSChuck Lever return error; 409d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 410d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 411d1bacf9eSBryan Schumaker return 0; 412d1bacf9eSBryan Schumaker } 413d1bacf9eSBryan Schumaker 414fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 415fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 416fa923369STrond Myklebust */ 417d39ab9deSBryan Schumaker static 418d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 419d39ab9deSBryan Schumaker { 420d8fdb47fSTrond Myklebust struct inode *inode; 421fa923369STrond Myklebust struct nfs_inode *nfsi; 422fa923369STrond Myklebust 4232b0143b5SDavid Howells if (d_really_is_negative(dentry)) 4242b0143b5SDavid Howells return 0; 425fa923369STrond Myklebust 426d8fdb47fSTrond Myklebust inode = d_inode(dentry); 427d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 428d8fdb47fSTrond Myklebust return 0; 429d8fdb47fSTrond Myklebust 430d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4317dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 432d39ab9deSBryan Schumaker return 0; 4337dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4347dc72d5fSTrond Myklebust return 0; 4357dc72d5fSTrond Myklebust return 1; 436d39ab9deSBryan Schumaker } 437d39ab9deSBryan Schumaker 438d39ab9deSBryan Schumaker static 43923db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 440d69ee9b8STrond Myklebust { 441d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 442d69ee9b8STrond Myklebust return false; 443d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 444d69ee9b8STrond Myklebust return true; 44523db8620SAl Viro if (ctx->pos == 0) 446d69ee9b8STrond Myklebust return true; 447d69ee9b8STrond Myklebust return false; 448d69ee9b8STrond Myklebust } 449d69ee9b8STrond Myklebust 450d69ee9b8STrond Myklebust /* 45163519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 45263519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 453d69ee9b8STrond Myklebust * directory. 454d69ee9b8STrond Myklebust */ 455d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 456d69ee9b8STrond Myklebust { 45763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 45863519fbcSTrond Myklebust 45963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 46063519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 46163519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 462d69ee9b8STrond Myklebust } 463d69ee9b8STrond Myklebust 464311324adSTrond Myklebust /* 465311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 466311324adSTrond Myklebust * 467311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 468311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 469311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 470311324adSTrond Myklebust * cache flush. 471311324adSTrond Myklebust */ 472311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 473311324adSTrond Myklebust { 47463519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 47563519fbcSTrond Myklebust 47663519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 47763519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 47863519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 479227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 480227823d2SDai Ngo nfsi->page_index + 1, -1); 481311324adSTrond Myklebust } 482311324adSTrond Myklebust } 483311324adSTrond Myklebust 484d69ee9b8STrond Myklebust static 485a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 486a1147b82STrond Myklebust unsigned long dir_verifier) 487d39ab9deSBryan Schumaker { 48826fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4899ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4904a201d6eSTrond Myklebust struct dentry *dentry; 4914a201d6eSTrond Myklebust struct dentry *alias; 492d39ab9deSBryan Schumaker struct inode *inode; 493aa9c2669SDavid Quigley int status; 494d39ab9deSBryan Schumaker 495fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 496fa923369STrond Myklebust return; 4976c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4986c441c25STrond Myklebust return; 49978d04af4STrond Myklebust if (filename.len == 0) 50078d04af4STrond Myklebust return; 50178d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 50278d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 50378d04af4STrond Myklebust return; 50478d04af4STrond Myklebust /* ...or '/' */ 50578d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 50678d04af4STrond Myklebust return; 5074a201d6eSTrond Myklebust if (filename.name[0] == '.') { 5084a201d6eSTrond Myklebust if (filename.len == 1) 5094a201d6eSTrond Myklebust return; 5104a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 5114a201d6eSTrond Myklebust return; 5124a201d6eSTrond Myklebust } 5138387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 514d39ab9deSBryan Schumaker 5154a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 5169ac3d3e8SAl Viro again: 5179ac3d3e8SAl Viro if (!dentry) { 5189ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 5199ac3d3e8SAl Viro if (IS_ERR(dentry)) 5209ac3d3e8SAl Viro return; 5219ac3d3e8SAl Viro } 5229ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 5236c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 5246c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 5256c441c25STrond Myklebust &entry->fattr->fsid)) 5266c441c25STrond Myklebust goto out; 527d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5287dc72d5fSTrond Myklebust if (!entry->fh->size) 5297dc72d5fSTrond Myklebust goto out; 530a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 5312b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 532aa9c2669SDavid Quigley if (!status) 5332b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 534d39ab9deSBryan Schumaker goto out; 535d39ab9deSBryan Schumaker } else { 5365542aa2fSEric W. Biederman d_invalidate(dentry); 537d39ab9deSBryan Schumaker dput(dentry); 5389ac3d3e8SAl Viro dentry = NULL; 5399ac3d3e8SAl Viro goto again; 540d39ab9deSBryan Schumaker } 541d39ab9deSBryan Schumaker } 5427dc72d5fSTrond Myklebust if (!entry->fh->size) { 5437dc72d5fSTrond Myklebust d_lookup_done(dentry); 5447dc72d5fSTrond Myklebust goto out; 5457dc72d5fSTrond Myklebust } 546d39ab9deSBryan Schumaker 5471775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 54841d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5499ac3d3e8SAl Viro d_lookup_done(dentry); 5509ac3d3e8SAl Viro if (alias) { 551d39ab9deSBryan Schumaker if (IS_ERR(alias)) 552d39ab9deSBryan Schumaker goto out; 5539ac3d3e8SAl Viro dput(dentry); 5549ac3d3e8SAl Viro dentry = alias; 5559ac3d3e8SAl Viro } 556a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 557d39ab9deSBryan Schumaker out: 558d39ab9deSBryan Schumaker dput(dentry); 559d39ab9deSBryan Schumaker } 560d39ab9deSBryan Schumaker 561d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 562d1bacf9eSBryan Schumaker static 5638cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5646650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 565d1bacf9eSBryan Schumaker { 566babddc72SBryan Schumaker struct xdr_stream stream; 567f7da7a12SBenny Halevy struct xdr_buf buf; 5686650239aSTrond Myklebust struct page *scratch; 56999424380SBryan Schumaker struct nfs_cache_array *array; 5705c346854STrond Myklebust unsigned int count = 0; 5715c346854STrond Myklebust int status; 572babddc72SBryan Schumaker 5736650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5746650239aSTrond Myklebust if (scratch == NULL) 5756650239aSTrond Myklebust return -ENOMEM; 576babddc72SBryan Schumaker 577ce85cfbeSBenjamin Coddington if (buflen == 0) 578ce85cfbeSBenjamin Coddington goto out_nopages; 579ce85cfbeSBenjamin Coddington 580f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5816650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 58299424380SBryan Schumaker 58399424380SBryan Schumaker do { 584d33030e2SJeffrey Mitchell if (entry->label) 585d33030e2SJeffrey Mitchell entry->label->len = NFS4_MAXLABELLEN; 586d33030e2SJeffrey Mitchell 58799424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5888cd51a0cSTrond Myklebust if (status != 0) { 5898cd51a0cSTrond Myklebust if (status == -EAGAIN) 5908cd51a0cSTrond Myklebust status = 0; 59199424380SBryan Schumaker break; 5928cd51a0cSTrond Myklebust } 59399424380SBryan Schumaker 5945c346854STrond Myklebust count++; 5955c346854STrond Myklebust 596a7a3b1e9SBenjamin Coddington if (desc->plus) 597a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 598a1147b82STrond Myklebust desc->dir_verifier); 5998cd51a0cSTrond Myklebust 600db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 6018cd51a0cSTrond Myklebust if (status != 0) 6028cd51a0cSTrond Myklebust break; 60399424380SBryan Schumaker } while (!entry->eof); 60499424380SBryan Schumaker 605ce85cfbeSBenjamin Coddington out_nopages: 60647c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 607db531db9SMax Kellermann array = kmap(page); 6088cd51a0cSTrond Myklebust array->eof_index = array->size; 60999424380SBryan Schumaker status = 0; 610db531db9SMax Kellermann kunmap(page); 61156e4ebf8SBryan Schumaker } 6126650239aSTrond Myklebust 6136650239aSTrond Myklebust put_page(scratch); 6148cd51a0cSTrond Myklebust return status; 6158cd51a0cSTrond Myklebust } 61656e4ebf8SBryan Schumaker 61756e4ebf8SBryan Schumaker static 618c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 61956e4ebf8SBryan Schumaker { 62056e4ebf8SBryan Schumaker unsigned int i; 62156e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 62256e4ebf8SBryan Schumaker put_page(pages[i]); 62356e4ebf8SBryan Schumaker } 62456e4ebf8SBryan Schumaker 62556e4ebf8SBryan Schumaker /* 626bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 627bf211ca1Szhangliguang * to nfs_readdir_free_pages() 62856e4ebf8SBryan Schumaker */ 62956e4ebf8SBryan Schumaker static 630c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 63156e4ebf8SBryan Schumaker { 63256e4ebf8SBryan Schumaker unsigned int i; 63356e4ebf8SBryan Schumaker 63456e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 63556e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 63656e4ebf8SBryan Schumaker if (page == NULL) 63756e4ebf8SBryan Schumaker goto out_freepages; 63856e4ebf8SBryan Schumaker pages[i] = page; 63956e4ebf8SBryan Schumaker } 6406650239aSTrond Myklebust return 0; 64156e4ebf8SBryan Schumaker 64256e4ebf8SBryan Schumaker out_freepages: 643c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6446650239aSTrond Myklebust return -ENOMEM; 645d1bacf9eSBryan Schumaker } 646d1bacf9eSBryan Schumaker 647d1bacf9eSBryan Schumaker static 648d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 649d1bacf9eSBryan Schumaker { 65056e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 651d1bacf9eSBryan Schumaker struct nfs_entry entry; 652d1bacf9eSBryan Schumaker struct file *file = desc->file; 653d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6548cd51a0cSTrond Myklebust int status = -ENOMEM; 65556e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 656d1bacf9eSBryan Schumaker 6574b310319STrond Myklebust nfs_readdir_init_array(page); 6584b310319STrond Myklebust 659d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6600aded708STrond Myklebust entry.cookie = desc->last_cookie; 661d1bacf9eSBryan Schumaker entry.eof = 0; 662d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 663d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 664573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 665d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 666d1bacf9eSBryan Schumaker goto out; 667d1bacf9eSBryan Schumaker 66814c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 66914c43f76SDavid Quigley if (IS_ERR(entry.label)) { 67014c43f76SDavid Quigley status = PTR_ERR(entry.label); 67114c43f76SDavid Quigley goto out; 67214c43f76SDavid Quigley } 67314c43f76SDavid Quigley 6740795bf83SFabian Frederick array = kmap(page); 675d1bacf9eSBryan Schumaker 676c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6776650239aSTrond Myklebust if (status < 0) 678d1bacf9eSBryan Schumaker goto out_release_array; 679d1bacf9eSBryan Schumaker do { 680ac396128STrond Myklebust unsigned int pglen; 68156e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 682babddc72SBryan Schumaker 683d1bacf9eSBryan Schumaker if (status < 0) 684d1bacf9eSBryan Schumaker break; 685ac396128STrond Myklebust pglen = status; 6866650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6878cd51a0cSTrond Myklebust if (status < 0) { 6888cd51a0cSTrond Myklebust if (status == -ENOSPC) 6898cd51a0cSTrond Myklebust status = 0; 6908cd51a0cSTrond Myklebust break; 6918cd51a0cSTrond Myklebust } 6928cd51a0cSTrond Myklebust } while (array->eof_index < 0); 693d1bacf9eSBryan Schumaker 694c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 695d1bacf9eSBryan Schumaker out_release_array: 6960795bf83SFabian Frederick kunmap(page); 69714c43f76SDavid Quigley nfs4_label_free(entry.label); 698d1bacf9eSBryan Schumaker out: 699d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 700d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 701d1bacf9eSBryan Schumaker return status; 702d1bacf9eSBryan Schumaker } 703d1bacf9eSBryan Schumaker 704d1bacf9eSBryan Schumaker /* 705d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 706d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 707d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 708d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7091da177e4SLinus Torvalds */ 710d1bacf9eSBryan Schumaker static 711a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 712d1bacf9eSBryan Schumaker { 713a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 714496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7158cd51a0cSTrond Myklebust int ret; 716d1bacf9eSBryan Schumaker 7178cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7188cd51a0cSTrond Myklebust if (ret < 0) 719d1bacf9eSBryan Schumaker goto error; 720d1bacf9eSBryan Schumaker SetPageUptodate(page); 721d1bacf9eSBryan Schumaker 7222aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 723cd9ae2b6STrond Myklebust /* Should never happen */ 724cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 725cd9ae2b6STrond Myklebust } 7261da177e4SLinus Torvalds unlock_page(page); 7271da177e4SLinus Torvalds return 0; 7281da177e4SLinus Torvalds error: 7294b310319STrond Myklebust nfs_readdir_clear_array(page); 7301da177e4SLinus Torvalds unlock_page(page); 7318cd51a0cSTrond Myklebust return ret; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 734d1bacf9eSBryan Schumaker static 735d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7361da177e4SLinus Torvalds { 73709cbfeafSKirill A. Shutemov put_page(desc->page); 7381da177e4SLinus Torvalds desc->page = NULL; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741d1bacf9eSBryan Schumaker static 742d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7431da177e4SLinus Torvalds { 744a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 745a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /* 749d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 750114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7511da177e4SLinus Torvalds */ 752d1bacf9eSBryan Schumaker static 753114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 754d1bacf9eSBryan Schumaker { 755227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 756227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 757d1bacf9eSBryan Schumaker int res; 758d1bacf9eSBryan Schumaker 759d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 760d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 761d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 762114de382STrond Myklebust res = lock_page_killable(desc->page); 76347c716cbSTrond Myklebust if (res != 0) 764114de382STrond Myklebust goto error; 765114de382STrond Myklebust res = -EAGAIN; 766114de382STrond Myklebust if (desc->page->mapping != NULL) { 767114de382STrond Myklebust res = nfs_readdir_search_array(desc); 768227823d2SDai Ngo if (res == 0) { 769227823d2SDai Ngo nfsi->page_index = desc->page_index; 770114de382STrond Myklebust return 0; 771114de382STrond Myklebust } 772227823d2SDai Ngo } 773114de382STrond Myklebust unlock_page(desc->page); 774114de382STrond Myklebust error: 775d1bacf9eSBryan Schumaker cache_page_release(desc); 776d1bacf9eSBryan Schumaker return res; 777d1bacf9eSBryan Schumaker } 778d1bacf9eSBryan Schumaker 779d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7801da177e4SLinus Torvalds static inline 7811da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7821da177e4SLinus Torvalds { 7838cd51a0cSTrond Myklebust int res; 784d1bacf9eSBryan Schumaker 7850aded708STrond Myklebust if (desc->page_index == 0) { 7868cd51a0cSTrond Myklebust desc->current_index = 0; 78759e356a9STrond Myklebust desc->prev_index = 0; 7880aded708STrond Myklebust desc->last_cookie = 0; 7890aded708STrond Myklebust } 79047c716cbSTrond Myklebust do { 791114de382STrond Myklebust res = find_and_lock_cache_page(desc); 79247c716cbSTrond Myklebust } while (res == -EAGAIN); 7931da177e4SLinus Torvalds return res; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds static 80023db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 8011da177e4SLinus Torvalds { 8021da177e4SLinus Torvalds struct file *file = desc->file; 803d1bacf9eSBryan Schumaker int i = 0; 804d1bacf9eSBryan Schumaker int res = 0; 805d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 8068ef2ce3eSBryan Schumaker 8070795bf83SFabian Frederick array = kmap(desc->page); 808d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 809ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8101da177e4SLinus Torvalds 811ece0b423STrond Myklebust ent = &array->array[i]; 81223db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 81323db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 8146089dd0dSThomas Meyer desc->eof = true; 8151da177e4SLinus Torvalds break; 816ece0b423STrond Myklebust } 817d1bacf9eSBryan Schumaker if (i < (array->size-1)) 818*2e7a4641STrond Myklebust desc->dir_cookie = array->array[i+1].cookie; 819d1bacf9eSBryan Schumaker else 820*2e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 82159e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 822*2e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 82359e356a9STrond Myklebust else 82459e356a9STrond Myklebust desc->ctx->pos++; 825*2e7a4641STrond Myklebust if (desc->duped != 0) 826*2e7a4641STrond Myklebust desc->duped = 1; 8278cd51a0cSTrond Myklebust } 82847c716cbSTrond Myklebust if (array->eof_index >= 0) 8296089dd0dSThomas Meyer desc->eof = true; 830d1bacf9eSBryan Schumaker 8310795bf83SFabian Frederick kunmap(desc->page); 8321e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 833*2e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie, res); 8341da177e4SLinus Torvalds return res; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* 8381da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8391da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8401da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8411da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8421da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8431da177e4SLinus Torvalds * 8441da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8451da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8461da177e4SLinus Torvalds * we should already have a complete representation of the 8471da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8481da177e4SLinus Torvalds */ 8491da177e4SLinus Torvalds static inline 85023db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8511da177e4SLinus Torvalds { 8521da177e4SLinus Torvalds struct page *page = NULL; 8531da177e4SLinus Torvalds int status; 854496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8551da177e4SLinus Torvalds 8561e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 857*2e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8601da177e4SLinus Torvalds if (!page) { 8611da177e4SLinus Torvalds status = -ENOMEM; 8621da177e4SLinus Torvalds goto out; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8657a8e1dc3STrond Myklebust desc->page_index = 0; 866*2e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 8677a8e1dc3STrond Myklebust desc->page = page; 868*2e7a4641STrond Myklebust desc->duped = 0; 8697a8e1dc3STrond Myklebust 87085f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 87185f8607eSTrond Myklebust if (status < 0) 872d1bacf9eSBryan Schumaker goto out_release; 873d1bacf9eSBryan Schumaker 87423db8620SAl Viro status = nfs_do_filldir(desc); 8751da177e4SLinus Torvalds 876114de382STrond Myklebust out_release: 877114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 878114de382STrond Myklebust cache_page_release(desc); 8791da177e4SLinus Torvalds out: 8801e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8813110ff80SHarvey Harrison __func__, status); 8821da177e4SLinus Torvalds return status; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 88500a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 88600a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 88700a92642SOlivier Galibert whole directory. 8881da177e4SLinus Torvalds */ 88923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8901da177e4SLinus Torvalds { 891be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8922b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 89323db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 89459e356a9STrond Myklebust nfs_readdir_descriptor_t my_desc = { 89559e356a9STrond Myklebust .file = file, 89659e356a9STrond Myklebust .ctx = ctx, 89759e356a9STrond Myklebust .plus = nfs_use_readdirplus(inode, ctx), 89859e356a9STrond Myklebust }, 89959e356a9STrond Myklebust *desc = &my_desc; 90007b5ce8eSScott Mayhew int res = 0; 9011da177e4SLinus Torvalds 9026de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9036de1472fSAl Viro file, (long long)ctx->pos); 90491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 90591d5b470SChuck Lever 9061da177e4SLinus Torvalds /* 90723db8620SAl Viro * ctx->pos points to the dirent entry number. 908f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 90900a92642SOlivier Galibert * to either find the entry with the appropriate number or 91000a92642SOlivier Galibert * revalidate the cookie. 9111da177e4SLinus Torvalds */ 91279f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 91323db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 914fccca7fcSTrond Myklebust if (res < 0) 915fccca7fcSTrond Myklebust goto out; 916fccca7fcSTrond Myklebust 917*2e7a4641STrond Myklebust spin_lock(&file->f_lock); 918*2e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 919*2e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 920*2e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 921*2e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 922*2e7a4641STrond Myklebust spin_unlock(&file->f_lock); 923*2e7a4641STrond Myklebust 92447c716cbSTrond Myklebust do { 9251da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 92600a92642SOlivier Galibert 9271da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 928ece0b423STrond Myklebust res = 0; 9291da177e4SLinus Torvalds /* This means either end of directory */ 930*2e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 9311da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 93223db8620SAl Viro res = uncached_readdir(desc); 933ece0b423STrond Myklebust if (res == 0) 9341da177e4SLinus Torvalds continue; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds break; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9393a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9401da177e4SLinus Torvalds nfs_zap_caches(inode); 941baf57a09STrond Myklebust desc->page_index = 0; 942a7a3b1e9SBenjamin Coddington desc->plus = false; 943a7a3b1e9SBenjamin Coddington desc->eof = false; 9441da177e4SLinus Torvalds continue; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds if (res < 0) 9471da177e4SLinus Torvalds break; 9481da177e4SLinus Torvalds 94923db8620SAl Viro res = nfs_do_filldir(desc); 950114de382STrond Myklebust unlock_page(desc->page); 951114de382STrond Myklebust cache_page_release(desc); 952ece0b423STrond Myklebust if (res < 0) 9531da177e4SLinus Torvalds break; 95447c716cbSTrond Myklebust } while (!desc->eof); 955*2e7a4641STrond Myklebust 956*2e7a4641STrond Myklebust spin_lock(&file->f_lock); 957*2e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 958*2e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 959*2e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 960*2e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 961*2e7a4641STrond Myklebust spin_unlock(&file->f_lock); 962*2e7a4641STrond Myklebust 963fccca7fcSTrond Myklebust out: 9641e7cb3dcSChuck Lever if (res > 0) 9651e7cb3dcSChuck Lever res = 0; 9666de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9671da177e4SLinus Torvalds return res; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 970965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 971f0dd2136STrond Myklebust { 972480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 973b84e06c5SChuck Lever 9746de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9756de1472fSAl Viro filp, offset, whence); 976b84e06c5SChuck Lever 977965c8e59SAndrew Morton switch (whence) { 978f0dd2136STrond Myklebust default: 979b2b1ff3dSTrond Myklebust return -EINVAL; 980b2b1ff3dSTrond Myklebust case SEEK_SET: 981b2b1ff3dSTrond Myklebust if (offset < 0) 982b2b1ff3dSTrond Myklebust return -EINVAL; 98383f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 984b2b1ff3dSTrond Myklebust break; 985b2b1ff3dSTrond Myklebust case SEEK_CUR: 986b2b1ff3dSTrond Myklebust if (offset == 0) 987b2b1ff3dSTrond Myklebust return filp->f_pos; 98883f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 989b2b1ff3dSTrond Myklebust offset += filp->f_pos; 990b2b1ff3dSTrond Myklebust if (offset < 0) { 99183f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 992b2b1ff3dSTrond Myklebust return -EINVAL; 993b2b1ff3dSTrond Myklebust } 994f0dd2136STrond Myklebust } 995f0dd2136STrond Myklebust if (offset != filp->f_pos) { 996f0dd2136STrond Myklebust filp->f_pos = offset; 99759e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 99859e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 99959e356a9STrond Myklebust else 1000480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 10018ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1002f0dd2136STrond Myklebust } 100383f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1004f0dd2136STrond Myklebust return offset; 1005f0dd2136STrond Myklebust } 1006f0dd2136STrond Myklebust 10071da177e4SLinus Torvalds /* 10081da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 10091da177e4SLinus Torvalds * is a dummy operation. 10101da177e4SLinus Torvalds */ 101102c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 101202c24a82SJosef Bacik int datasync) 10131da177e4SLinus Torvalds { 10146de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 10151e7cb3dcSChuck Lever 101611decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 10171da177e4SLinus Torvalds return 0; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 1020bfc69a45STrond Myklebust /** 1021bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1022302fad7bSTrond Myklebust * @dir: pointer to directory inode 1023bfc69a45STrond Myklebust * 1024bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1025bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1026bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1027bfc69a45STrond Myklebust * 1028efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1029efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1030efeda80dSTrond Myklebust * 1031bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1032bfc69a45STrond Myklebust */ 1033bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1034bfc69a45STrond Myklebust { 1035efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1036bfc69a45STrond Myklebust } 103789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1038bfc69a45STrond Myklebust 1039efeda80dSTrond Myklebust /** 1040efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1041efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1042efeda80dSTrond Myklebust * @verf: previously saved change attribute 1043efeda80dSTrond Myklebust * 1044efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1045efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1046efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1047efeda80dSTrond Myklebust */ 1048efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1049efeda80dSTrond Myklebust { 1050efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1051efeda80dSTrond Myklebust } 1052efeda80dSTrond Myklebust 1053efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1054efeda80dSTrond Myklebust { 1055efeda80dSTrond Myklebust *verf |= 1UL; 1056efeda80dSTrond Myklebust } 1057efeda80dSTrond Myklebust 1058efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1059efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1060efeda80dSTrond Myklebust { 1061efeda80dSTrond Myklebust *verf &= ~1UL; 1062efeda80dSTrond Myklebust } 1063efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1064efeda80dSTrond Myklebust 1065efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1066efeda80dSTrond Myklebust { 1067efeda80dSTrond Myklebust return verf & 1; 1068efeda80dSTrond Myklebust } 1069efeda80dSTrond Myklebust 1070efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1071efeda80dSTrond Myklebust { 1072efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1073efeda80dSTrond Myklebust } 1074efeda80dSTrond Myklebust 1075efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1076efeda80dSTrond Myklebust { 1077efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1078efeda80dSTrond Myklebust 1079efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1080efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1081efeda80dSTrond Myklebust goto out; 1082efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1083efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1084efeda80dSTrond Myklebust out: 1085efeda80dSTrond Myklebust dentry->d_time = verf; 1086efeda80dSTrond Myklebust } 1087efeda80dSTrond Myklebust 1088efeda80dSTrond Myklebust /** 1089efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1090efeda80dSTrond Myklebust * @dentry: pointer to dentry 1091efeda80dSTrond Myklebust * @verf: verifier to save 1092efeda80dSTrond Myklebust * 1093efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1094efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1095efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1096efeda80dSTrond Myklebust * look it up again after a directory change. 1097efeda80dSTrond Myklebust */ 1098efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1099efeda80dSTrond Myklebust { 1100efeda80dSTrond Myklebust 1101efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1102efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1103efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1104efeda80dSTrond Myklebust } 1105efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1106efeda80dSTrond Myklebust 1107efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1108efeda80dSTrond Myklebust /** 1109efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1110efeda80dSTrond Myklebust * @inode: pointer to inode 1111efeda80dSTrond Myklebust * 1112efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1113efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1114efeda80dSTrond Myklebust * while holding a delegation. 1115efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1116efeda80dSTrond Myklebust * returned or revoked. 1117efeda80dSTrond Myklebust */ 1118efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1119efeda80dSTrond Myklebust { 1120efeda80dSTrond Myklebust struct dentry *alias; 1121efeda80dSTrond Myklebust 1122efeda80dSTrond Myklebust if (!inode) 1123efeda80dSTrond Myklebust return; 1124efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1125efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1126efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1127efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1128efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1129efeda80dSTrond Myklebust } 1130efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1131efeda80dSTrond Myklebust } 1132efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1133efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1134efeda80dSTrond Myklebust 11351da177e4SLinus Torvalds /* 11361da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 11371da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 11381da177e4SLinus Torvalds * and may need to be looked up again. 1139912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 11401da177e4SLinus Torvalds */ 1141912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1142912a108dSNeilBrown int rcu_walk) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds if (IS_ROOT(dentry)) 11451da177e4SLinus Torvalds return 1; 11464eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 11474eec952eSTrond Myklebust return 0; 1148f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 11496ecc5e8fSTrond Myklebust return 0; 1150f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 11511cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1152912a108dSNeilBrown if (rcu_walk) 1153f2c77f4eSTrond Myklebust return 0; 11541cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 11551cd9cb05STrond Myklebust return 0; 11561cd9cb05STrond Myklebust } 1157f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1158f2c77f4eSTrond Myklebust return 0; 1159f2c77f4eSTrond Myklebust return 1; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds /* 1163a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1164a12802caSTrond Myklebust * an O_EXCL create using this path component. 1165a12802caSTrond Myklebust */ 1166fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1167a12802caSTrond Myklebust { 1168a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1169a12802caSTrond Myklebust return 0; 1170fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1171a12802caSTrond Myklebust } 1172a12802caSTrond Myklebust 1173a12802caSTrond Myklebust /* 11741d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 11751d6757fbSTrond Myklebust * 11761d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 11771d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 11781d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 11791d6757fbSTrond Myklebust * 11801d6757fbSTrond Myklebust */ 118165a0c149STrond Myklebust static 1182fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 11831da177e4SLinus Torvalds { 11841da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 118565a0c149STrond Myklebust int ret; 11861da177e4SLinus Torvalds 118736d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 11884e99a1ffSTrond Myklebust return 0; 118947921921STrond Myklebust 119047921921STrond Myklebust if (flags & LOOKUP_OPEN) { 119147921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 119247921921STrond Myklebust case S_IFREG: 119347921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 119447921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 119547921921STrond Myklebust goto out; 1196df561f66SGustavo A. R. Silva fallthrough; 119747921921STrond Myklebust case S_IFDIR: 119847921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 119947921921STrond Myklebust break; 120047921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 120147921921STrond Myklebust goto out_force; 120247921921STrond Myklebust } 120347921921STrond Myklebust } 120447921921STrond Myklebust 12051da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1206fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 12071da177e4SLinus Torvalds goto out_force; 120865a0c149STrond Myklebust out: 1209a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 12101da177e4SLinus Torvalds out_force: 12111fa1e384SNeilBrown if (flags & LOOKUP_RCU) 12121fa1e384SNeilBrown return -ECHILD; 121365a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 121465a0c149STrond Myklebust if (ret != 0) 121565a0c149STrond Myklebust return ret; 121665a0c149STrond Myklebust goto out; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds /* 12201da177e4SLinus Torvalds * We judge how long we want to trust negative 12211da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 12221da177e4SLinus Torvalds * 12231da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 12241da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1225912a108dSNeilBrown * 1226912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1227912a108dSNeilBrown * suggesting a reval is needed. 12289f6d44d4STrond Myklebust * 12299f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 12309f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 12311da177e4SLinus Torvalds */ 12321da177e4SLinus Torvalds static inline 12331da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1234fa3c56bbSAl Viro unsigned int flags) 12351da177e4SLinus Torvalds { 12369f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 12371da177e4SLinus Torvalds return 0; 12384eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 12394eec952eSTrond Myklebust return 1; 1240912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12435ceb9d7fSTrond Myklebust static int 12445ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 12455ceb9d7fSTrond Myklebust struct inode *inode, int error) 12461da177e4SLinus Torvalds { 12475ceb9d7fSTrond Myklebust switch (error) { 12485ceb9d7fSTrond Myklebust case 1: 12496de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12506de1472fSAl Viro __func__, dentry); 12511da177e4SLinus Torvalds return 1; 12525ceb9d7fSTrond Myklebust case 0: 1253a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12541da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12551da177e4SLinus Torvalds /* Purge readdir caches. */ 12561da177e4SLinus Torvalds nfs_zap_caches(inode); 1257a3f432bfSJ. Bruce Fields /* 1258a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1259a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1260a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1261a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1262a3f432bfSJ. Bruce Fields */ 1263a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 12645ceb9d7fSTrond Myklebust return 1; 12651da177e4SLinus Torvalds } 12666de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12676de1472fSAl Viro __func__, dentry); 12681da177e4SLinus Torvalds return 0; 12695ceb9d7fSTrond Myklebust } 12706de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12716de1472fSAl Viro __func__, dentry, error); 1272e1fb4d05STrond Myklebust return error; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12755ceb9d7fSTrond Myklebust static int 12765ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 12775ceb9d7fSTrond Myklebust unsigned int flags) 12785ceb9d7fSTrond Myklebust { 12795ceb9d7fSTrond Myklebust int ret = 1; 12805ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 12815ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12825ceb9d7fSTrond Myklebust return -ECHILD; 12835ceb9d7fSTrond Myklebust ret = 0; 12845ceb9d7fSTrond Myklebust } 12855ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 12865ceb9d7fSTrond Myklebust } 12875ceb9d7fSTrond Myklebust 12885ceb9d7fSTrond Myklebust static int 12895ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 12905ceb9d7fSTrond Myklebust struct inode *inode) 12915ceb9d7fSTrond Myklebust { 12925ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12935ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12945ceb9d7fSTrond Myklebust } 12955ceb9d7fSTrond Myklebust 12965ceb9d7fSTrond Myklebust static int 12975ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 12985ceb9d7fSTrond Myklebust struct inode *inode) 12995ceb9d7fSTrond Myklebust { 13005ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 13015ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 13025ceb9d7fSTrond Myklebust struct nfs4_label *label; 1303a1147b82STrond Myklebust unsigned long dir_verifier; 13045ceb9d7fSTrond Myklebust int ret; 13055ceb9d7fSTrond Myklebust 13065ceb9d7fSTrond Myklebust ret = -ENOMEM; 13075ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 13085ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 13095ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 13105ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 13115ceb9d7fSTrond Myklebust goto out; 13125ceb9d7fSTrond Myklebust 1313a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1314f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 13155ceb9d7fSTrond Myklebust if (ret < 0) { 1316f7b37b8bSTrond Myklebust switch (ret) { 1317f7b37b8bSTrond Myklebust case -ESTALE: 1318f7b37b8bSTrond Myklebust case -ENOENT: 13195ceb9d7fSTrond Myklebust ret = 0; 1320f7b37b8bSTrond Myklebust break; 1321f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1322f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1323f7b37b8bSTrond Myklebust ret = 1; 1324f7b37b8bSTrond Myklebust } 13255ceb9d7fSTrond Myklebust goto out; 13265ceb9d7fSTrond Myklebust } 13275ceb9d7fSTrond Myklebust ret = 0; 13285ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 13295ceb9d7fSTrond Myklebust goto out; 13305ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 13315ceb9d7fSTrond Myklebust goto out; 13325ceb9d7fSTrond Myklebust 13335ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1334a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 13355ceb9d7fSTrond Myklebust 13365ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 13375ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 13385ceb9d7fSTrond Myklebust ret = 1; 13395ceb9d7fSTrond Myklebust out: 13405ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 13415ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 13425ceb9d7fSTrond Myklebust nfs4_label_free(label); 13435ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 13445ceb9d7fSTrond Myklebust } 13455ceb9d7fSTrond Myklebust 13465ceb9d7fSTrond Myklebust /* 13475ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 13485ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 13495ceb9d7fSTrond Myklebust * lookup. 13505ceb9d7fSTrond Myklebust * 13515ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 13525ceb9d7fSTrond Myklebust * we have an inode! 13535ceb9d7fSTrond Myklebust * 13545ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 13555ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 13565ceb9d7fSTrond Myklebust */ 13575ceb9d7fSTrond Myklebust static int 13585ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 13595ceb9d7fSTrond Myklebust unsigned int flags) 13605ceb9d7fSTrond Myklebust { 13615ceb9d7fSTrond Myklebust struct inode *inode; 13625ceb9d7fSTrond Myklebust int error; 13635ceb9d7fSTrond Myklebust 13645ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 13655ceb9d7fSTrond Myklebust inode = d_inode(dentry); 13665ceb9d7fSTrond Myklebust 13675ceb9d7fSTrond Myklebust if (!inode) 13685ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 13695ceb9d7fSTrond Myklebust 13705ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 13715ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13725ceb9d7fSTrond Myklebust __func__, dentry); 13735ceb9d7fSTrond Myklebust goto out_bad; 13745ceb9d7fSTrond Myklebust } 13755ceb9d7fSTrond Myklebust 1376efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 13775ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 13785ceb9d7fSTrond Myklebust 13795ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 13805ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 13815ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 13825ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 13835ceb9d7fSTrond Myklebust if (error) { 13845ceb9d7fSTrond Myklebust if (error == -ESTALE) 13855ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 13865ceb9d7fSTrond Myklebust goto out_bad; 13875ceb9d7fSTrond Myklebust } 13885ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 13895ceb9d7fSTrond Myklebust goto out_valid; 13905ceb9d7fSTrond Myklebust } 13915ceb9d7fSTrond Myklebust 13925ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13935ceb9d7fSTrond Myklebust return -ECHILD; 13945ceb9d7fSTrond Myklebust 13955ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 13965ceb9d7fSTrond Myklebust goto out_bad; 13975ceb9d7fSTrond Myklebust 13985ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 13995ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 14005ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 14015ceb9d7fSTrond Myklebust return error; 14025ceb9d7fSTrond Myklebust out_valid: 14035ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 14045ceb9d7fSTrond Myklebust out_bad: 14055ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 14065ceb9d7fSTrond Myklebust return -ECHILD; 14075ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 14085ceb9d7fSTrond Myklebust } 14095ceb9d7fSTrond Myklebust 14105ceb9d7fSTrond Myklebust static int 1411c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1412c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 14135ceb9d7fSTrond Myklebust { 14145ceb9d7fSTrond Myklebust struct dentry *parent; 14155ceb9d7fSTrond Myklebust struct inode *dir; 14165ceb9d7fSTrond Myklebust int ret; 14175ceb9d7fSTrond Myklebust 14185ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 14195ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 14205ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 14215ceb9d7fSTrond Myklebust if (!dir) 14225ceb9d7fSTrond Myklebust return -ECHILD; 1423c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 14245ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 14255ceb9d7fSTrond Myklebust return -ECHILD; 14265ceb9d7fSTrond Myklebust } else { 14275ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1428c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 14295ceb9d7fSTrond Myklebust dput(parent); 14305ceb9d7fSTrond Myklebust } 14315ceb9d7fSTrond Myklebust return ret; 14325ceb9d7fSTrond Myklebust } 14335ceb9d7fSTrond Myklebust 1434c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1435c7944ebbSTrond Myklebust { 1436c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1437c7944ebbSTrond Myklebust } 1438c7944ebbSTrond Myklebust 14391da177e4SLinus Torvalds /* 14402b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1441ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1442ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1443ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1444ecf3d1f1SJeff Layton * 1445ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1446ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1447ecf3d1f1SJeff Layton */ 1448ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1449ecf3d1f1SJeff Layton { 14502b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 14519cdd1d3fSTrond Myklebust int error = 0; 1452ecf3d1f1SJeff Layton 1453ecf3d1f1SJeff Layton /* 1454ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1455ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1456ecf3d1f1SJeff Layton * eventually need to do something more here. 1457ecf3d1f1SJeff Layton */ 1458ecf3d1f1SJeff Layton if (!inode) { 14596de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 14606de1472fSAl Viro __func__, dentry); 1461ecf3d1f1SJeff Layton return 1; 1462ecf3d1f1SJeff Layton } 1463ecf3d1f1SJeff Layton 1464ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 14656de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 14666de1472fSAl Viro __func__, dentry); 1467ecf3d1f1SJeff Layton return 0; 1468ecf3d1f1SJeff Layton } 1469ecf3d1f1SJeff Layton 1470b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1471ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1472ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1473ecf3d1f1SJeff Layton return !error; 1474ecf3d1f1SJeff Layton } 1475ecf3d1f1SJeff Layton 1476ecf3d1f1SJeff Layton /* 14771da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 14781da177e4SLinus Torvalds */ 1479fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 14801da177e4SLinus Torvalds { 14816de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 14826de1472fSAl Viro dentry, dentry->d_flags); 14831da177e4SLinus Torvalds 148477f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 14852b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 148677f11192STrond Myklebust return 1; 148777f11192STrond Myklebust 14881da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 14891da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 14901da177e4SLinus Torvalds return 1; 14911da177e4SLinus Torvalds } 14921751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 14931da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 14941da177e4SLinus Torvalds * files will be cleaned up during umount */ 14951da177e4SLinus Torvalds return 1; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds return 0; 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 15021b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 15031b83d707STrond Myklebust { 15041b83d707STrond Myklebust spin_lock(&inode->i_lock); 15051f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 150659a707b0STrond Myklebust if (inode->i_nlink > 0) 150759a707b0STrond Myklebust drop_nlink(inode); 150859a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 150916e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 151016e14375STrond Myklebust | NFS_INO_INVALID_CTIME 151159a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 151259a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 15131b83d707STrond Myklebust spin_unlock(&inode->i_lock); 15141b83d707STrond Myklebust } 15151b83d707STrond Myklebust 15161da177e4SLinus Torvalds /* 15171da177e4SLinus Torvalds * Called when the dentry loses inode. 15181da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 15191da177e4SLinus Torvalds */ 15201da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 15211da177e4SLinus Torvalds { 152283672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 152383672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 152483672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 152583672d39SNeil Brown 15261da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1527e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 15281f018458STrond Myklebust nfs_drop_nlink(inode); 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds iput(inode); 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 1533b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1534b1942c5fSAl Viro { 1535b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1536b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1537b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1538b1942c5fSAl Viro WARN_ON(1); 1539b1942c5fSAl Viro else 1540b1942c5fSAl Viro kfree(dentry->d_fsdata); 1541b1942c5fSAl Viro } 1542b1942c5fSAl Viro } 1543b1942c5fSAl Viro 1544f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 15451da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1546ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 15471da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15481da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 154936d43a43SDavid Howells .d_automount = nfs_d_automount, 1550b1942c5fSAl Viro .d_release = nfs_d_release, 15511da177e4SLinus Torvalds }; 1552ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 15531da177e4SLinus Torvalds 1554597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds struct dentry *res; 15571da177e4SLinus Torvalds struct inode *inode = NULL; 1558e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1559e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 15601775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1561a1147b82STrond Myklebust unsigned long dir_verifier; 15621da177e4SLinus Torvalds int error; 15631da177e4SLinus Torvalds 15646de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 156591d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 15661da177e4SLinus Torvalds 1567130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1568130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 15691da177e4SLinus Torvalds 1570fd684071STrond Myklebust /* 1571fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1572fd684071STrond Myklebust * but don't hash the dentry. 1573fd684071STrond Myklebust */ 15749f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1575130f9ab7SAl Viro return NULL; 15761da177e4SLinus Torvalds 1577e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1578e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1579e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1580e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1581e1fb4d05STrond Myklebust goto out; 1582e1fb4d05STrond Myklebust 158314c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 158414c43f76SDavid Quigley if (IS_ERR(label)) 158514c43f76SDavid Quigley goto out; 158614c43f76SDavid Quigley 1587a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 15886e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1589f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 15901da177e4SLinus Torvalds if (error == -ENOENT) 15911da177e4SLinus Torvalds goto no_entry; 15921da177e4SLinus Torvalds if (error < 0) { 15931da177e4SLinus Torvalds res = ERR_PTR(error); 1594bf130914SAl Viro goto out_label; 15951da177e4SLinus Torvalds } 15961775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1597bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 159803f28e3aSTrond Myklebust if (IS_ERR(res)) 1599bf130914SAl Viro goto out_label; 160054ceac45SDavid Howells 160163519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 160263519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1603d69ee9b8STrond Myklebust 16041da177e4SLinus Torvalds no_entry: 160541d28bcaSAl Viro res = d_splice_alias(inode, dentry); 16069eaef27bSTrond Myklebust if (res != NULL) { 16079eaef27bSTrond Myklebust if (IS_ERR(res)) 1608bf130914SAl Viro goto out_label; 16091da177e4SLinus Torvalds dentry = res; 16109eaef27bSTrond Myklebust } 1611a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1612bf130914SAl Viro out_label: 16136e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 161414c43f76SDavid Quigley nfs4_label_free(label); 16151da177e4SLinus Torvalds out: 1616e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1617e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 16181da177e4SLinus Torvalds return res; 16191da177e4SLinus Torvalds } 1620ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 16211da177e4SLinus Torvalds 162289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 16230b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 16241da177e4SLinus Torvalds 1625f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 16260ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1627b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 16281da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 16291da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 163036d43a43SDavid Howells .d_automount = nfs_d_automount, 1631b1942c5fSAl Viro .d_release = nfs_d_release, 16321da177e4SLinus Torvalds }; 163389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 16341da177e4SLinus Torvalds 16358a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 16368a5e929dSAl Viro { 16378a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 16388a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 16398a5e929dSAl Viro res |= FMODE_READ; 16408a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 16418a5e929dSAl Viro res |= FMODE_WRITE; 16428a5e929dSAl Viro return res; 16438a5e929dSAl Viro } 16448a5e929dSAl Viro 1645532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1646cd9a1c0eSTrond Myklebust { 1647532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1648cd9a1c0eSTrond Myklebust } 1649cd9a1c0eSTrond Myklebust 1650cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1651cd9a1c0eSTrond Myklebust { 1652f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1653cd9a1c0eSTrond Myklebust return 0; 1654cd9a1c0eSTrond Myklebust } 1655cd9a1c0eSTrond Myklebust 1656d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 16570dd2b474SMiklos Szeredi struct dentry *dentry, 1658b452a458SAl Viro struct file *file, unsigned open_flags) 1659cd9a1c0eSTrond Myklebust { 16600dd2b474SMiklos Szeredi int err; 16610dd2b474SMiklos Szeredi 1662be12af3eSAl Viro err = finish_open(file, dentry, do_open); 166330d90494SAl Viro if (err) 1664d9585277SAl Viro goto out; 1665eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 166630d90494SAl Viro nfs_file_set_open_context(file, ctx); 1667eaa2b82cSNeilBrown else 16689821421aSTrond Myklebust err = -EOPENSTALE; 1669cd9a1c0eSTrond Myklebust out: 1670d9585277SAl Viro return err; 1671cd9a1c0eSTrond Myklebust } 1672cd9a1c0eSTrond Myklebust 167373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 167430d90494SAl Viro struct file *file, unsigned open_flags, 167544907d79SAl Viro umode_t mode) 16761da177e4SLinus Torvalds { 1677c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1678cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 16790dd2b474SMiklos Szeredi struct dentry *res; 16800dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1681f46e0bd3STrond Myklebust struct inode *inode; 16821472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1683c94c0953SAl Viro bool switched = false; 168473a09dd9SAl Viro int created = 0; 1685898f635cSTrond Myklebust int err; 16861da177e4SLinus Torvalds 16870dd2b474SMiklos Szeredi /* Expect a negative dentry */ 16882b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 16890dd2b474SMiklos Szeredi 16901e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 16916de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 16921e7cb3dcSChuck Lever 16939597c13bSJeff Layton err = nfs_check_flags(open_flags); 16949597c13bSJeff Layton if (err) 16959597c13bSJeff Layton return err; 16969597c13bSJeff Layton 16970dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 16980dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 169900699ad8SAl Viro if (!d_in_lookup(dentry)) { 17000dd2b474SMiklos Szeredi /* 17010dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 17020dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 17030dd2b474SMiklos Szeredi * again 17040dd2b474SMiklos Szeredi */ 1705d9585277SAl Viro return -ENOENT; 17060dd2b474SMiklos Szeredi } 17071472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 17081da177e4SLinus Torvalds goto no_open; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 17110dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1712d9585277SAl Viro return -ENAMETOOLONG; 17131da177e4SLinus Torvalds 17140dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1715dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1716dff25ddbSAndreas Gruenbacher 1717dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1718dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1719dff25ddbSAndreas Gruenbacher 1720536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1721dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 17220dd2b474SMiklos Szeredi } 1723536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1724536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1725536e43d1STrond Myklebust attr.ia_size = 0; 1726cd9a1c0eSTrond Myklebust } 1727cd9a1c0eSTrond Myklebust 1728c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1729c94c0953SAl Viro d_drop(dentry); 1730c94c0953SAl Viro switched = true; 1731c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1732c94c0953SAl Viro &dentry->d_name, &wq); 1733c94c0953SAl Viro if (IS_ERR(dentry)) 1734c94c0953SAl Viro return PTR_ERR(dentry); 1735c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1736c94c0953SAl Viro return finish_no_open(file, dentry); 1737c94c0953SAl Viro } 1738c94c0953SAl Viro 1739532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 17400dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 17410dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1742d9585277SAl Viro goto out; 17430dd2b474SMiklos Szeredi 17446e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 174573a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 174673a09dd9SAl Viro if (created) 174773a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1748275bb307STrond Myklebust if (IS_ERR(inode)) { 17490dd2b474SMiklos Szeredi err = PTR_ERR(inode); 17506e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17512d9db750STrond Myklebust put_nfs_open_context(ctx); 1752d20cb71dSAl Viro d_drop(dentry); 17530dd2b474SMiklos Szeredi switch (err) { 17541da177e4SLinus Torvalds case -ENOENT: 1755774d9513SPeng Tao d_splice_alias(NULL, dentry); 1756809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17570dd2b474SMiklos Szeredi break; 17581788ea6eSJeff Layton case -EISDIR: 17596f926b5bSTrond Myklebust case -ENOTDIR: 17606f926b5bSTrond Myklebust goto no_open; 17611da177e4SLinus Torvalds case -ELOOP: 17620dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 17631da177e4SLinus Torvalds goto no_open; 17640dd2b474SMiklos Szeredi break; 17651da177e4SLinus Torvalds /* case -EINVAL: */ 17661da177e4SLinus Torvalds default: 17670dd2b474SMiklos Szeredi break; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds goto out; 17701da177e4SLinus Torvalds } 17710dd2b474SMiklos Szeredi 1772b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 17736e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17742d9db750STrond Myklebust put_nfs_open_context(ctx); 1775d9585277SAl Viro out: 1776c94c0953SAl Viro if (unlikely(switched)) { 1777c94c0953SAl Viro d_lookup_done(dentry); 1778c94c0953SAl Viro dput(dentry); 1779c94c0953SAl Viro } 1780d9585277SAl Viro return err; 17810dd2b474SMiklos Szeredi 17821da177e4SLinus Torvalds no_open: 17831472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1784c94c0953SAl Viro if (switched) { 1785c94c0953SAl Viro d_lookup_done(dentry); 1786c94c0953SAl Viro if (!res) 1787c94c0953SAl Viro res = dentry; 1788c94c0953SAl Viro else 1789c94c0953SAl Viro dput(dentry); 1790c94c0953SAl Viro } 17910dd2b474SMiklos Szeredi if (IS_ERR(res)) 1792c94c0953SAl Viro return PTR_ERR(res); 1793e45198a6SAl Viro return finish_no_open(file, res); 17941da177e4SLinus Torvalds } 179589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 17961da177e4SLinus Torvalds 1797c7944ebbSTrond Myklebust static int 1798c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1799c7944ebbSTrond Myklebust unsigned int flags) 18001da177e4SLinus Torvalds { 1801657e94b6SNick Piggin struct inode *inode; 18021da177e4SLinus Torvalds 1803fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1804c7944ebbSTrond Myklebust goto full_reval; 1805eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1806c7944ebbSTrond Myklebust goto full_reval; 18072b484297STrond Myklebust 18082b0143b5SDavid Howells inode = d_inode(dentry); 18092b484297STrond Myklebust 18101da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 18111da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 18121da177e4SLinus Torvalds */ 1813c7944ebbSTrond Myklebust if (inode == NULL) 1814c7944ebbSTrond Myklebust goto full_reval; 181549317a7fSNeilBrown 1816efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 1817c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1818216d5d06STrond Myklebust 18191da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 18201da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1821c7944ebbSTrond Myklebust goto full_reval; 1822c7944ebbSTrond Myklebust 18231da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1824c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1825c7944ebbSTrond Myklebust goto reval_dentry; 1826c7944ebbSTrond Myklebust 1827c7944ebbSTrond Myklebust /* Check if the directory changed */ 1828c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1829c7944ebbSTrond Myklebust goto reval_dentry; 18301da177e4SLinus Torvalds 18310ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1832c7944ebbSTrond Myklebust return 1; 1833c7944ebbSTrond Myklebust reval_dentry: 1834c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1835c7944ebbSTrond Myklebust return -ECHILD; 183642f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 18370ef97dcfSMiklos Szeredi 1838c7944ebbSTrond Myklebust full_reval: 1839c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1840c7944ebbSTrond Myklebust } 1841535918f1STrond Myklebust 1842c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1843c7944ebbSTrond Myklebust { 1844c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1845c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1846c0204fd2STrond Myklebust } 1847c0204fd2STrond Myklebust 18481da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 18491da177e4SLinus Torvalds 1850406cd915SBenjamin Coddington struct dentry * 1851406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 18521775fd3eSDavid Quigley struct nfs_fattr *fattr, 18531775fd3eSDavid Quigley struct nfs4_label *label) 18541da177e4SLinus Torvalds { 1855fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 18562b0143b5SDavid Howells struct inode *dir = d_inode(parent); 18571da177e4SLinus Torvalds struct inode *inode; 1858b0c6108eSAl Viro struct dentry *d; 1859406cd915SBenjamin Coddington int error; 18601da177e4SLinus Torvalds 1861fab728e1STrond Myklebust d_drop(dentry); 1862fab728e1STrond Myklebust 18631da177e4SLinus Torvalds if (fhandle->size == 0) { 1864f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 18651da177e4SLinus Torvalds if (error) 1866fab728e1STrond Myklebust goto out_error; 18671da177e4SLinus Torvalds } 18685724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18691da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 18701da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1871a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1872a841b54dSTrond Myklebust fattr, NULL, NULL); 18731da177e4SLinus Torvalds if (error < 0) 1874fab728e1STrond Myklebust goto out_error; 18751da177e4SLinus Torvalds } 18761775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1877b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1878fab728e1STrond Myklebust out: 1879fab728e1STrond Myklebust dput(parent); 1880406cd915SBenjamin Coddington return d; 1881fab728e1STrond Myklebust out_error: 1882fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1883406cd915SBenjamin Coddington d = ERR_PTR(error); 1884406cd915SBenjamin Coddington goto out; 1885406cd915SBenjamin Coddington } 1886406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1887406cd915SBenjamin Coddington 1888406cd915SBenjamin Coddington /* 1889406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1890406cd915SBenjamin Coddington */ 1891406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1892406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1893406cd915SBenjamin Coddington struct nfs4_label *label) 1894406cd915SBenjamin Coddington { 1895406cd915SBenjamin Coddington struct dentry *d; 1896406cd915SBenjamin Coddington 1897406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1898406cd915SBenjamin Coddington if (IS_ERR(d)) 1899406cd915SBenjamin Coddington return PTR_ERR(d); 1900406cd915SBenjamin Coddington 1901406cd915SBenjamin Coddington /* Callers don't care */ 1902406cd915SBenjamin Coddington dput(d); 1903406cd915SBenjamin Coddington return 0; 19041da177e4SLinus Torvalds } 1905ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds /* 19081da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 19091da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 19101da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 19111da177e4SLinus Torvalds * reply path made it appear to have failed. 19121da177e4SLinus Torvalds */ 1913597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1914ebfc3b49SAl Viro umode_t mode, bool excl) 19151da177e4SLinus Torvalds { 19161da177e4SLinus Torvalds struct iattr attr; 1917ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 19181da177e4SLinus Torvalds int error; 19191da177e4SLinus Torvalds 19201e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 19216de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds attr.ia_mode = mode; 19241da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19251da177e4SLinus Torvalds 19268b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 19278867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 19288b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 19291da177e4SLinus Torvalds if (error != 0) 19301da177e4SLinus Torvalds goto out_err; 19311da177e4SLinus Torvalds return 0; 19321da177e4SLinus Torvalds out_err: 19331da177e4SLinus Torvalds d_drop(dentry); 19341da177e4SLinus Torvalds return error; 19351da177e4SLinus Torvalds } 1936ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds /* 19391da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19401da177e4SLinus Torvalds */ 1941597d9289SBryan Schumaker int 19421a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 19431da177e4SLinus Torvalds { 19441da177e4SLinus Torvalds struct iattr attr; 19451da177e4SLinus Torvalds int status; 19461da177e4SLinus Torvalds 19471e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 19486de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds attr.ia_mode = mode; 19511da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19521da177e4SLinus Torvalds 19531ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 19541da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 19551ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 19561da177e4SLinus Torvalds if (status != 0) 19571da177e4SLinus Torvalds goto out_err; 19581da177e4SLinus Torvalds return 0; 19591da177e4SLinus Torvalds out_err: 19601da177e4SLinus Torvalds d_drop(dentry); 19611da177e4SLinus Torvalds return status; 19621da177e4SLinus Torvalds } 1963ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds /* 19661da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19671da177e4SLinus Torvalds */ 1968597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19691da177e4SLinus Torvalds { 19701da177e4SLinus Torvalds struct iattr attr; 19711da177e4SLinus Torvalds int error; 19721da177e4SLinus Torvalds 19731e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 19746de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19771da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 19781da177e4SLinus Torvalds 19791ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 19801da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 19811ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 19821da177e4SLinus Torvalds if (error != 0) 19831da177e4SLinus Torvalds goto out_err; 19841da177e4SLinus Torvalds return 0; 19851da177e4SLinus Torvalds out_err: 19861da177e4SLinus Torvalds d_drop(dentry); 19871da177e4SLinus Torvalds return error; 19881da177e4SLinus Torvalds } 1989ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 19901da177e4SLinus Torvalds 1991d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1992d45b9d8bSTrond Myklebust { 1993dc3f4198SAl Viro if (simple_positive(dentry)) 1994d45b9d8bSTrond Myklebust d_delete(dentry); 1995d45b9d8bSTrond Myklebust } 1996d45b9d8bSTrond Myklebust 1997597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 19981da177e4SLinus Torvalds { 19991da177e4SLinus Torvalds int error; 20001da177e4SLinus Torvalds 20011e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 20026de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20031da177e4SLinus Torvalds 20041ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 20052b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2006884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 20071da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 20081da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2009ba6c0592STrond Myklebust switch (error) { 2010ba6c0592STrond Myklebust case 0: 20112b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2012ba6c0592STrond Myklebust break; 2013ba6c0592STrond Myklebust case -ENOENT: 2014d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2015ba6c0592STrond Myklebust } 2016884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2017ba6c0592STrond Myklebust } else 2018ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 20191ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds return error; 20221da177e4SLinus Torvalds } 2023ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds /* 20261da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 20271da177e4SLinus Torvalds * and after checking that the file has only one user. 20281da177e4SLinus Torvalds * 20291da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 20301da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 20311da177e4SLinus Torvalds */ 20321da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 20331da177e4SLinus Torvalds { 20342b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 20352b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 20361da177e4SLinus Torvalds int error = -EBUSY; 20371da177e4SLinus Torvalds 20386de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 20411da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 20421da177e4SLinus Torvalds error = 0; 20431da177e4SLinus Torvalds goto out; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 20471da177e4SLinus Torvalds if (inode != NULL) { 2048912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 20491da177e4SLinus Torvalds if (error == 0) 20501b83d707STrond Myklebust nfs_drop_nlink(inode); 20511da177e4SLinus Torvalds } else 2052912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2053d45b9d8bSTrond Myklebust if (error == -ENOENT) 2054d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 20551ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 20561da177e4SLinus Torvalds out: 20571da177e4SLinus Torvalds return error; 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 20611da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 20621da177e4SLinus Torvalds * 20631da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 20641da177e4SLinus Torvalds */ 2065597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 20661da177e4SLinus Torvalds { 20671da177e4SLinus Torvalds int error; 20681da177e4SLinus Torvalds int need_rehash = 0; 20691da177e4SLinus Torvalds 20701e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 20716de1472fSAl Viro dir->i_ino, dentry); 20721da177e4SLinus Torvalds 20731ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 20741da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 207584d08fa8SAl Viro if (d_count(dentry) > 1) { 20761da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2077ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 20782b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 20791da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 20801ca42382STrond Myklebust goto out; 20811da177e4SLinus Torvalds } 20821da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 20831da177e4SLinus Torvalds __d_drop(dentry); 20841da177e4SLinus Torvalds need_rehash = 1; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 20871da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2088d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 20891da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 20901da177e4SLinus Torvalds } else if (need_rehash) 20911da177e4SLinus Torvalds d_rehash(dentry); 20921ca42382STrond Myklebust out: 20931ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 20941da177e4SLinus Torvalds return error; 20951da177e4SLinus Torvalds } 2096ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 20971da177e4SLinus Torvalds 2098873101b3SChuck Lever /* 2099873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2100873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2101873101b3SChuck Lever * using prepare_write/commit_write. 2102873101b3SChuck Lever * 2103873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2104873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2105873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2106873101b3SChuck Lever * symlink request has completed on the server. 2107873101b3SChuck Lever * 2108873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2109873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2110873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2111873101b3SChuck Lever * and move the raw page into its mapping. 2112873101b3SChuck Lever */ 2113597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 21141da177e4SLinus Torvalds { 2115873101b3SChuck Lever struct page *page; 2116873101b3SChuck Lever char *kaddr; 21171da177e4SLinus Torvalds struct iattr attr; 2118873101b3SChuck Lever unsigned int pathlen = strlen(symname); 21191da177e4SLinus Torvalds int error; 21201da177e4SLinus Torvalds 21211e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 21226de1472fSAl Viro dir->i_ino, dentry, symname); 21231da177e4SLinus Torvalds 2124873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2125873101b3SChuck Lever return -ENAMETOOLONG; 21261da177e4SLinus Torvalds 2127873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2128873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 21291da177e4SLinus Torvalds 2130e8ecde25SAl Viro page = alloc_page(GFP_USER); 213176566991STrond Myklebust if (!page) 2132873101b3SChuck Lever return -ENOMEM; 2133873101b3SChuck Lever 2134e8ecde25SAl Viro kaddr = page_address(page); 2135873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2136873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2137873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2138873101b3SChuck Lever 21391ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 214094a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 21411ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2142873101b3SChuck Lever if (error != 0) { 21431e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2144873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 21456de1472fSAl Viro dentry, symname, error); 21461da177e4SLinus Torvalds d_drop(dentry); 2147873101b3SChuck Lever __free_page(page); 21481da177e4SLinus Torvalds return error; 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds 2151873101b3SChuck Lever /* 2152873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2153873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2154873101b3SChuck Lever */ 21552b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2156873101b3SChuck Lever GFP_KERNEL)) { 2157873101b3SChuck Lever SetPageUptodate(page); 2158873101b3SChuck Lever unlock_page(page); 2159a0b54addSRafael Aquini /* 2160a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2161a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2162a0b54addSRafael Aquini */ 216309cbfeafSKirill A. Shutemov put_page(page); 2164873101b3SChuck Lever } else 2165873101b3SChuck Lever __free_page(page); 2166873101b3SChuck Lever 2167873101b3SChuck Lever return 0; 2168873101b3SChuck Lever } 2169ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2170873101b3SChuck Lever 2171597d9289SBryan Schumaker int 21721da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 21731da177e4SLinus Torvalds { 21742b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 21751da177e4SLinus Torvalds int error; 21761da177e4SLinus Torvalds 21776de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 21786de1472fSAl Viro old_dentry, dentry); 21791da177e4SLinus Torvalds 21801fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 21819697d234STrond Myklebust d_drop(dentry); 21821da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2183cf809556STrond Myklebust if (error == 0) { 21847de9c6eeSAl Viro ihold(inode); 21859697d234STrond Myklebust d_add(dentry, inode); 2186cf809556STrond Myklebust } 21871fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 21881da177e4SLinus Torvalds return error; 21891da177e4SLinus Torvalds } 2190ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds /* 21931da177e4SLinus Torvalds * RENAME 21941da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 21951da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 21961da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 21971da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 21981da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 21991da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 22001da177e4SLinus Torvalds * 22011da177e4SLinus Torvalds * FIXED. 22021da177e4SLinus Torvalds * 22031da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 22041da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 22051da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 22061da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 22071da177e4SLinus Torvalds * using the inode layer 22081da177e4SLinus Torvalds * 22091da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 22101da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 22111da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 22121da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 22131da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 22141da177e4SLinus Torvalds * the rename. 22151da177e4SLinus Torvalds */ 2216597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 22171cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 22181cd66c93SMiklos Szeredi unsigned int flags) 22191da177e4SLinus Torvalds { 22202b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 22212b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2222d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 222380a491fdSJeff Layton struct rpc_task *task; 22241da177e4SLinus Torvalds int error = -EBUSY; 22251da177e4SLinus Torvalds 22261cd66c93SMiklos Szeredi if (flags) 22271cd66c93SMiklos Szeredi return -EINVAL; 22281cd66c93SMiklos Szeredi 22296de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 22306de1472fSAl Viro old_dentry, new_dentry, 223184d08fa8SAl Viro d_count(new_dentry)); 22321da177e4SLinus Torvalds 223370ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 22341da177e4SLinus Torvalds /* 223528f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 223628f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 223728f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 223828f79a1aSMiklos Szeredi * the new target. 22391da177e4SLinus Torvalds */ 224027226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 224127226104SMiklos Szeredi /* 224227226104SMiklos Szeredi * To prevent any new references to the target during the 224327226104SMiklos Szeredi * rename, we unhash the dentry in advance. 224427226104SMiklos Szeredi */ 2245d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 224627226104SMiklos Szeredi d_drop(new_dentry); 2247d9f29500SBenjamin Coddington rehash = new_dentry; 2248d9f29500SBenjamin Coddington } 224927226104SMiklos Szeredi 225084d08fa8SAl Viro if (d_count(new_dentry) > 2) { 22511da177e4SLinus Torvalds int err; 225227226104SMiklos Szeredi 22531da177e4SLinus Torvalds /* copy the target dentry's name */ 22541da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 22551da177e4SLinus Torvalds &new_dentry->d_name); 22561da177e4SLinus Torvalds if (!dentry) 22571da177e4SLinus Torvalds goto out; 22581da177e4SLinus Torvalds 22591da177e4SLinus Torvalds /* silly-rename the existing target ... */ 22601da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 226124e93025SMiklos Szeredi if (err) 22621da177e4SLinus Torvalds goto out; 226324e93025SMiklos Szeredi 226424e93025SMiklos Szeredi new_dentry = dentry; 2265d9f29500SBenjamin Coddington rehash = NULL; 226624e93025SMiklos Szeredi new_inode = NULL; 2267b1e4adf4STrond Myklebust } 226827226104SMiklos Szeredi } 22691da177e4SLinus Torvalds 2270d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 227180a491fdSJeff Layton if (IS_ERR(task)) { 227280a491fdSJeff Layton error = PTR_ERR(task); 227380a491fdSJeff Layton goto out; 227480a491fdSJeff Layton } 227580a491fdSJeff Layton 227680a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2277818a8dbeSBenjamin Coddington if (error != 0) { 2278818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2279818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2280818a8dbeSBenjamin Coddington smp_wmb(); 2281818a8dbeSBenjamin Coddington } else 228280a491fdSJeff Layton error = task->tk_status; 228380a491fdSJeff Layton rpc_put_task(task); 228459a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 228559a707b0STrond Myklebust if (error == 0) { 228659a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 228759a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 228859a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 228959a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 229059a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 229159a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 229259a707b0STrond Myklebust } 22931da177e4SLinus Torvalds out: 2294d9f29500SBenjamin Coddington if (rehash) 2295d9f29500SBenjamin Coddington d_rehash(rehash); 229670ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 229770ded201STrond Myklebust new_dir, new_dentry, error); 2298d9f29500SBenjamin Coddington if (!error) { 2299d9f29500SBenjamin Coddington if (new_inode != NULL) 2300d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2301d9f29500SBenjamin Coddington /* 2302d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2303d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2304d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2305d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2306d9f29500SBenjamin Coddington */ 2307d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2308d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2309d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2310d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2311d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2312d9f29500SBenjamin Coddington 23131da177e4SLinus Torvalds /* new dentry created? */ 23141da177e4SLinus Torvalds if (dentry) 23151da177e4SLinus Torvalds dput(dentry); 23161da177e4SLinus Torvalds return error; 23171da177e4SLinus Torvalds } 2318ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 23191da177e4SLinus Torvalds 2320cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2321cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2322cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2323cfcea3e8STrond Myklebust 2324a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 23253a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 23263a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 23273a505845STrond Myklebust 23281c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 23291c3c07e9STrond Myklebust { 2330b68572e0SNeilBrown put_cred(entry->cred); 2331f682a398SNeilBrown kfree_rcu(entry, rcu_head); 23324e857c58SPeter Zijlstra smp_mb__before_atomic(); 2333cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 23344e857c58SPeter Zijlstra smp_mb__after_atomic(); 23351c3c07e9STrond Myklebust } 23361c3c07e9STrond Myklebust 23371a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 23381a81bb8aSTrond Myklebust { 23391a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 23401a81bb8aSTrond Myklebust 23411a81bb8aSTrond Myklebust while (!list_empty(head)) { 23421a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 23431a81bb8aSTrond Myklebust list_del(&cache->lru); 23441a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 23451a81bb8aSTrond Myklebust } 23461a81bb8aSTrond Myklebust } 23471a81bb8aSTrond Myklebust 23483a505845STrond Myklebust static unsigned long 23493a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2350979df72eSTrond Myklebust { 2351979df72eSTrond Myklebust LIST_HEAD(head); 2352aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2353979df72eSTrond Myklebust struct nfs_access_entry *cache; 23541ab6c499SDave Chinner long freed = 0; 2355979df72eSTrond Myklebust 2356a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2357aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2358979df72eSTrond Myklebust struct inode *inode; 2359979df72eSTrond Myklebust 2360979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2361979df72eSTrond Myklebust break; 23629c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2363979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2364979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2365979df72eSTrond Myklebust goto remove_lru_entry; 2366979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2367979df72eSTrond Myklebust struct nfs_access_entry, lru); 2368979df72eSTrond Myklebust list_move(&cache->lru, &head); 2369979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 23701ab6c499SDave Chinner freed++; 2371979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2372979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2373979df72eSTrond Myklebust &nfs_access_lru_list); 2374979df72eSTrond Myklebust else { 2375979df72eSTrond Myklebust remove_lru_entry: 2376979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 23774e857c58SPeter Zijlstra smp_mb__before_atomic(); 2378979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 23794e857c58SPeter Zijlstra smp_mb__after_atomic(); 2380979df72eSTrond Myklebust } 238159844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2382979df72eSTrond Myklebust } 2383979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23841a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23851ab6c499SDave Chinner return freed; 23861ab6c499SDave Chinner } 23871ab6c499SDave Chinner 23881ab6c499SDave Chinner unsigned long 23893a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 23903a505845STrond Myklebust { 23913a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 23923a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 23933a505845STrond Myklebust 23943a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 23953a505845STrond Myklebust return SHRINK_STOP; 23963a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 23973a505845STrond Myklebust } 23983a505845STrond Myklebust 23993a505845STrond Myklebust 24003a505845STrond Myklebust unsigned long 24011ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 24021ab6c499SDave Chinner { 240355f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2404979df72eSTrond Myklebust } 2405979df72eSTrond Myklebust 24063a505845STrond Myklebust static void 24073a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 24083a505845STrond Myklebust { 24093a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 24103a505845STrond Myklebust unsigned long diff; 24113a505845STrond Myklebust unsigned int nr_to_scan; 24123a505845STrond Myklebust 24133a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 24143a505845STrond Myklebust return; 24153a505845STrond Myklebust nr_to_scan = 100; 24163a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 24173a505845STrond Myklebust if (diff < nr_to_scan) 24183a505845STrond Myklebust nr_to_scan = diff; 24193a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 24203a505845STrond Myklebust } 24213a505845STrond Myklebust 24221a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 24231c3c07e9STrond Myklebust { 24241c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24251a81bb8aSTrond Myklebust struct rb_node *n; 24261c3c07e9STrond Myklebust struct nfs_access_entry *entry; 24271c3c07e9STrond Myklebust 24281c3c07e9STrond Myklebust /* Unhook entries from the cache */ 24291c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 24301c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 24311c3c07e9STrond Myklebust rb_erase(n, root_node); 24321a81bb8aSTrond Myklebust list_move(&entry->lru, head); 24331c3c07e9STrond Myklebust } 24341c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 24351c3c07e9STrond Myklebust } 24361c3c07e9STrond Myklebust 24371c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 24381c3c07e9STrond Myklebust { 24391a81bb8aSTrond Myklebust LIST_HEAD(head); 24401a81bb8aSTrond Myklebust 24411a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 24421a81bb8aSTrond Myklebust return; 2443cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2444cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24451a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2446cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2447cfcea3e8STrond Myklebust 24481c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24491a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 24501a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 24511a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24521a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24531c3c07e9STrond Myklebust } 24541c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 24551c3c07e9STrond Myklebust 2456b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 24571c3c07e9STrond Myklebust { 24581c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 24591c3c07e9STrond Myklebust 24601c3c07e9STrond Myklebust while (n != NULL) { 2461b68572e0SNeilBrown struct nfs_access_entry *entry = 2462b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2463b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 24641c3c07e9STrond Myklebust 2465b68572e0SNeilBrown if (cmp < 0) 24661c3c07e9STrond Myklebust n = n->rb_left; 2467b68572e0SNeilBrown else if (cmp > 0) 24681c3c07e9STrond Myklebust n = n->rb_right; 24691c3c07e9STrond Myklebust else 24701c3c07e9STrond Myklebust return entry; 24711c3c07e9STrond Myklebust } 24721c3c07e9STrond Myklebust return NULL; 24731c3c07e9STrond Myklebust } 24741c3c07e9STrond Myklebust 2475d2ae4f8bSFrank van der Linden static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 24761da177e4SLinus Torvalds { 247755296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 24781c3c07e9STrond Myklebust struct nfs_access_entry *cache; 247957b69181STrond Myklebust bool retry = true; 248057b69181STrond Myklebust int err; 24811da177e4SLinus Torvalds 24821c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 248357b69181STrond Myklebust for(;;) { 24841c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 24851c3c07e9STrond Myklebust goto out_zap; 24861c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 248757b69181STrond Myklebust err = -ENOENT; 24881c3c07e9STrond Myklebust if (cache == NULL) 24891c3c07e9STrond Myklebust goto out; 249057b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 249121c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 249257b69181STrond Myklebust break; 24935c965db8STrond Myklebust if (!retry) 24945c965db8STrond Myklebust break; 249557b69181STrond Myklebust err = -ECHILD; 249657b69181STrond Myklebust if (!may_block) 249757b69181STrond Myklebust goto out; 249857b69181STrond Myklebust spin_unlock(&inode->i_lock); 249957b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 250057b69181STrond Myklebust if (err) 250157b69181STrond Myklebust return err; 250257b69181STrond Myklebust spin_lock(&inode->i_lock); 250357b69181STrond Myklebust retry = false; 250457b69181STrond Myklebust } 25051c3c07e9STrond Myklebust res->cred = cache->cred; 25061c3c07e9STrond Myklebust res->mask = cache->mask; 2507cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 25081c3c07e9STrond Myklebust err = 0; 25091c3c07e9STrond Myklebust out: 25101c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25111c3c07e9STrond Myklebust return err; 25121c3c07e9STrond Myklebust out_zap: 25131a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 25141a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 25151c3c07e9STrond Myklebust return -ENOENT; 25161c3c07e9STrond Myklebust } 25171c3c07e9STrond Myklebust 2518b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2519f682a398SNeilBrown { 2520f682a398SNeilBrown /* Only check the most recently returned cache entry, 2521f682a398SNeilBrown * but do it without locking. 2522f682a398SNeilBrown */ 2523f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2524f682a398SNeilBrown struct nfs_access_entry *cache; 2525f682a398SNeilBrown int err = -ECHILD; 2526f682a398SNeilBrown struct list_head *lh; 2527f682a398SNeilBrown 2528f682a398SNeilBrown rcu_read_lock(); 2529f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2530f682a398SNeilBrown goto out; 25319f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2532f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2533f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 25349a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2535f682a398SNeilBrown cache = NULL; 2536f682a398SNeilBrown if (cache == NULL) 2537f682a398SNeilBrown goto out; 253821c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2539f682a398SNeilBrown goto out; 2540f682a398SNeilBrown res->cred = cache->cred; 2541f682a398SNeilBrown res->mask = cache->mask; 254221c3ba7eSTrond Myklebust err = 0; 2543f682a398SNeilBrown out: 2544f682a398SNeilBrown rcu_read_unlock(); 2545f682a398SNeilBrown return err; 2546f682a398SNeilBrown } 2547f682a398SNeilBrown 2548d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct 2549d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block) 2550d2ae4f8bSFrank van der Linden { 2551d2ae4f8bSFrank van der Linden int status; 2552d2ae4f8bSFrank van der Linden 2553d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_rcu(inode, cred, res); 2554d2ae4f8bSFrank van der Linden if (status != 0) 2555d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_locked(inode, cred, res, 2556d2ae4f8bSFrank van der Linden may_block); 2557d2ae4f8bSFrank van der Linden 2558d2ae4f8bSFrank van der Linden return status; 2559d2ae4f8bSFrank van der Linden } 2560d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2561d2ae4f8bSFrank van der Linden 25621c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 25631c3c07e9STrond Myklebust { 2564cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2565cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 25661c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 25671c3c07e9STrond Myklebust struct rb_node *parent = NULL; 25681c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2569b68572e0SNeilBrown int cmp; 25701c3c07e9STrond Myklebust 25711c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 25721c3c07e9STrond Myklebust while (*p != NULL) { 25731c3c07e9STrond Myklebust parent = *p; 25741c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2575b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 25761c3c07e9STrond Myklebust 2577b68572e0SNeilBrown if (cmp < 0) 25781c3c07e9STrond Myklebust p = &parent->rb_left; 2579b68572e0SNeilBrown else if (cmp > 0) 25801c3c07e9STrond Myklebust p = &parent->rb_right; 25811c3c07e9STrond Myklebust else 25821c3c07e9STrond Myklebust goto found; 25831c3c07e9STrond Myklebust } 25841c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 25851c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2586cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 25871c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25881c3c07e9STrond Myklebust return; 25891c3c07e9STrond Myklebust found: 25901c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2591cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2592cfcea3e8STrond Myklebust list_del(&entry->lru); 25931c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25941c3c07e9STrond Myklebust nfs_access_free_entry(entry); 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds 25976168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 25981da177e4SLinus Torvalds { 25991c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 26001c3c07e9STrond Myklebust if (cache == NULL) 26011c3c07e9STrond Myklebust return; 26021c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2603b68572e0SNeilBrown cache->cred = get_cred(set->cred); 26041da177e4SLinus Torvalds cache->mask = set->mask; 26051c3c07e9STrond Myklebust 2606f682a398SNeilBrown /* The above field assignments must be visible 2607f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2608f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2609f682a398SNeilBrown */ 2610f682a398SNeilBrown smp_wmb(); 26111c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2612cfcea3e8STrond Myklebust 2613cfcea3e8STrond Myklebust /* Update accounting */ 26144e857c58SPeter Zijlstra smp_mb__before_atomic(); 2615cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 26164e857c58SPeter Zijlstra smp_mb__after_atomic(); 2617cfcea3e8STrond Myklebust 2618cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 26191a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2620cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 26211a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 26221a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 26231a81bb8aSTrond Myklebust &nfs_access_lru_list); 2624cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2625cfcea3e8STrond Myklebust } 26263a505845STrond Myklebust nfs_access_cache_enforce_limit(); 26271da177e4SLinus Torvalds } 26286168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 26296168f62cSWeston Andros Adamson 26303c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 26313c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26323c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 26333c181827SAnna Schumaker NFS_ACCESS_DELETE) 26343c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26353c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2636ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 26373c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 26383c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 263915d4b73aSTrond Myklebust static int 2640ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 264115d4b73aSTrond Myklebust { 264215d4b73aSTrond Myklebust int mask = 0; 264315d4b73aSTrond Myklebust 264415d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 264515d4b73aSTrond Myklebust mask |= MAY_READ; 2646ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2647ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 264815d4b73aSTrond Myklebust mask |= MAY_WRITE; 2649ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 265015d4b73aSTrond Myklebust mask |= MAY_EXEC; 2651ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2652ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2653ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2654ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 265515d4b73aSTrond Myklebust mask |= MAY_EXEC; 2656ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2657ecbb903cSTrond Myklebust mask |= MAY_WRITE; 265815d4b73aSTrond Myklebust return mask; 265915d4b73aSTrond Myklebust } 266015d4b73aSTrond Myklebust 26616168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 26626168f62cSWeston Andros Adamson { 2663bd8b2441STrond Myklebust entry->mask = access_result; 26646168f62cSWeston Andros Adamson } 26656168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 26661da177e4SLinus Torvalds 2667b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds struct nfs_access_entry cache; 267057b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2671e8194b7dSTrond Myklebust int cache_mask = -1; 26721da177e4SLinus Torvalds int status; 26731da177e4SLinus Torvalds 2674f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2675f4ce1299STrond Myklebust 267657b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 26771da177e4SLinus Torvalds if (status == 0) 2678f4ce1299STrond Myklebust goto out_cached; 26791da177e4SLinus Torvalds 2680f3324a2aSNeilBrown status = -ECHILD; 268157b69181STrond Myklebust if (!may_block) 2682f3324a2aSNeilBrown goto out; 2683f3324a2aSNeilBrown 26841750d929SAnna Schumaker /* 26851750d929SAnna Schumaker * Determine which access bits we want to ask for... 26861750d929SAnna Schumaker */ 26871750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 268872832a24SFrank van der Linden if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 268972832a24SFrank van der Linden cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 269072832a24SFrank van der Linden NFS_ACCESS_XALIST; 269172832a24SFrank van der Linden } 26921750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 26931750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 26941750d929SAnna Schumaker else 26951750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 26961da177e4SLinus Torvalds cache.cred = cred; 26971da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2698a71ee337SSuresh Jayaraman if (status != 0) { 2699a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2700a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 270193ce4af7STrond Myklebust nfs_set_inode_stale(inode); 270293ce4af7STrond Myklebust else 270393ce4af7STrond Myklebust nfs_zap_caches(inode); 2704a71ee337SSuresh Jayaraman } 2705f4ce1299STrond Myklebust goto out; 2706a71ee337SSuresh Jayaraman } 27071da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2708f4ce1299STrond Myklebust out_cached: 2709ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2710bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2711f4ce1299STrond Myklebust status = -EACCES; 27121da177e4SLinus Torvalds out: 2713e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2714f4ce1299STrond Myklebust return status; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds 2717af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2718af22f94aSTrond Myklebust { 2719af22f94aSTrond Myklebust int mask = 0; 2720af22f94aSTrond Myklebust 2721f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2722f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2723f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2724f8d9a897SWeston Andros Adamson } else { 27258a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2726af22f94aSTrond Myklebust mask |= MAY_READ; 27278a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2728af22f94aSTrond Myklebust mask |= MAY_WRITE; 2729f8d9a897SWeston Andros Adamson } 2730f8d9a897SWeston Andros Adamson 2731af22f94aSTrond Myklebust return mask; 2732af22f94aSTrond Myklebust } 2733af22f94aSTrond Myklebust 2734b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2735af22f94aSTrond Myklebust { 2736af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2737af22f94aSTrond Myklebust } 273889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2739af22f94aSTrond Myklebust 27405c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 27415c5fc09aSTrond Myklebust { 27425c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 274321c3ba7eSTrond Myklebust int ret = 0; 27445c5fc09aSTrond Myklebust 27453825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 27463825827eSTrond Myklebust return 0; 2747cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 27485c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 274921c3ba7eSTrond Myklebust return -ECHILD; 275021c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 275121c3ba7eSTrond Myklebust } 27525c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 27535c5fc09aSTrond Myklebust ret = -EACCES; 27545c5fc09aSTrond Myklebust return ret; 27555c5fc09aSTrond Myklebust } 27565c5fc09aSTrond Myklebust 275710556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 27581da177e4SLinus Torvalds { 2759b68572e0SNeilBrown const struct cred *cred = current_cred(); 27601da177e4SLinus Torvalds int res = 0; 27611da177e4SLinus Torvalds 276291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 276391d5b470SChuck Lever 2764e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 27651da177e4SLinus Torvalds goto out; 27661da177e4SLinus Torvalds /* Is this sys_access() ? */ 27679cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 27681da177e4SLinus Torvalds goto force_lookup; 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 27711da177e4SLinus Torvalds case S_IFLNK: 27721da177e4SLinus Torvalds goto out; 27731da177e4SLinus Torvalds case S_IFREG: 2774762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2775762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2776762674f8STrond Myklebust return 0; 27771da177e4SLinus Torvalds break; 27781da177e4SLinus Torvalds case S_IFDIR: 27791da177e4SLinus Torvalds /* 27801da177e4SLinus Torvalds * Optimize away all write operations, since the server 27811da177e4SLinus Torvalds * will check permissions when we perform the op. 27821da177e4SLinus Torvalds */ 27831da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 27841da177e4SLinus Torvalds goto out; 27851da177e4SLinus Torvalds } 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds force_lookup: 27881da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 27891da177e4SLinus Torvalds goto out_notsup; 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 27921da177e4SLinus Torvalds out: 27935c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 27945c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2795f696a365SMiklos Szeredi 27961e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 27971e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 27981da177e4SLinus Torvalds return res; 27991da177e4SLinus Torvalds out_notsup: 2800d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2801d51ac1a8SNeilBrown return -ECHILD; 2802d51ac1a8SNeilBrown 28031da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 28041da177e4SLinus Torvalds if (res == 0) 28052830ba7fSAl Viro res = generic_permission(inode, mask); 28061e7cb3dcSChuck Lever goto out; 28071da177e4SLinus Torvalds } 2808ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds /* 28111da177e4SLinus Torvalds * Local variables: 28121da177e4SLinus Torvalds * version-control: t 28131da177e4SLinus Torvalds * kept-new-versions: 5 28141da177e4SLinus Torvalds * End: 28151da177e4SLinus Torvalds */ 2816