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 21b6459415SJakub Kicinski #include <linux/compat.h> 22ddda8e0aSBryan Schumaker #include <linux/module.h> 231da177e4SLinus Torvalds #include <linux/time.h> 241da177e4SLinus Torvalds #include <linux/errno.h> 251da177e4SLinus Torvalds #include <linux/stat.h> 261da177e4SLinus Torvalds #include <linux/fcntl.h> 271da177e4SLinus Torvalds #include <linux/string.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 291da177e4SLinus Torvalds #include <linux/slab.h> 301da177e4SLinus Torvalds #include <linux/mm.h> 311da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 321da177e4SLinus Torvalds #include <linux/nfs_fs.h> 331da177e4SLinus Torvalds #include <linux/nfs_mount.h> 341da177e4SLinus Torvalds #include <linux/pagemap.h> 35873101b3SChuck Lever #include <linux/pagevec.h> 361da177e4SLinus Torvalds #include <linux/namei.h> 3754ceac45SDavid Howells #include <linux/mount.h> 38a0b8cab3SMel Gorman #include <linux/swap.h> 39e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 4004e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 4164c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 42f648022fSTrond Myklebust #include <linux/xxhash.h> 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include "delegation.h" 4591d5b470SChuck Lever #include "iostat.h" 464c30d56eSAdrian Bunk #include "internal.h" 47cd9a1c0eSTrond Myklebust #include "fscache.h" 481da177e4SLinus Torvalds 49f4ce1299STrond Myklebust #include "nfstrace.h" 50f4ce1299STrond Myklebust 511da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 54480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5523db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 57f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5811de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 591da177e4SLinus Torvalds 604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 61f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 621da177e4SLinus Torvalds .read = generic_read_dir, 6393a6ab7bSTrond Myklebust .iterate_shared = nfs_readdir, 641da177e4SLinus Torvalds .open = nfs_opendir, 65480c2006SBryan Schumaker .release = nfs_closedir, 661da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 671da177e4SLinus Torvalds }; 681da177e4SLinus Torvalds 6911de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 7011de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 71d1bacf9eSBryan Schumaker }; 72d1bacf9eSBryan Schumaker 73580f2367STrond Myklebust #define NFS_INIT_DTSIZE PAGE_SIZE 74580f2367STrond Myklebust 75281f31b2STrond Myklebust static struct nfs_open_dir_context * 76281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir) 77480c2006SBryan Schumaker { 78311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 79480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 80281f31b2STrond Myklebust 81281f31b2STrond Myklebust ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); 82480c2006SBryan Schumaker if (ctx != NULL) { 83311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 84580f2367STrond Myklebust ctx->dtsize = NFS_INIT_DTSIZE; 85311324adSTrond Myklebust spin_lock(&dir->i_lock); 861c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 871c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 88ac46b3d7STrond Myklebust nfs_set_cache_invalid(dir, 89ac46b3d7STrond Myklebust NFS_INO_INVALID_DATA | 90ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 91230bc98fSTrond Myklebust list_add_tail_rcu(&ctx->list, &nfsi->open_files); 92d1e32ea3STrond Myklebust memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf)); 93311324adSTrond Myklebust spin_unlock(&dir->i_lock); 94480c2006SBryan Schumaker return ctx; 95480c2006SBryan Schumaker } 960c030806STrond Myklebust return ERR_PTR(-ENOMEM); 970c030806STrond Myklebust } 98480c2006SBryan Schumaker 99311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 100480c2006SBryan Schumaker { 101311324adSTrond Myklebust spin_lock(&dir->i_lock); 102230bc98fSTrond Myklebust list_del_rcu(&ctx->list); 103311324adSTrond Myklebust spin_unlock(&dir->i_lock); 104230bc98fSTrond Myklebust kfree_rcu(ctx, rcu_head); 105480c2006SBryan Schumaker } 106480c2006SBryan Schumaker 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Open file 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds static int 1111da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1121da177e4SLinus Torvalds { 113480c2006SBryan Schumaker int res = 0; 114480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1151da177e4SLinus Torvalds 1166de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 117cc0dd2d1SChuck Lever 118cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1191e7cb3dcSChuck Lever 12093b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode); 121480c2006SBryan Schumaker if (IS_ERR(ctx)) { 122480c2006SBryan Schumaker res = PTR_ERR(ctx); 123480c2006SBryan Schumaker goto out; 124480c2006SBryan Schumaker } 125480c2006SBryan Schumaker filp->private_data = ctx; 126480c2006SBryan Schumaker out: 1271da177e4SLinus Torvalds return res; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 130480c2006SBryan Schumaker static int 131480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 132480c2006SBryan Schumaker { 133a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 134480c2006SBryan Schumaker return 0; 135480c2006SBryan Schumaker } 136480c2006SBryan Schumaker 137d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 138d1bacf9eSBryan Schumaker u64 cookie; 139d1bacf9eSBryan Schumaker u64 ino; 140a52a8a6aSTrond Myklebust const char *name; 141a52a8a6aSTrond Myklebust unsigned int name_len; 1420b26a0bfSTrond Myklebust unsigned char d_type; 143d1bacf9eSBryan Schumaker }; 144d1bacf9eSBryan Schumaker 145d1bacf9eSBryan Schumaker struct nfs_cache_array { 146d09e673fSTrond Myklebust u64 change_attr; 147d1bacf9eSBryan Schumaker u64 last_cookie; 148b1e21c97STrond Myklebust unsigned int size; 149b1e21c97STrond Myklebust unsigned char page_full : 1, 150762567b7STrond Myklebust page_is_eof : 1, 151762567b7STrond Myklebust cookies_are_ordered : 1; 1525601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 153d1bacf9eSBryan Schumaker }; 154d1bacf9eSBryan Schumaker 1556c981effSTrond Myklebust struct nfs_readdir_descriptor { 1561da177e4SLinus Torvalds struct file *file; 1571da177e4SLinus Torvalds struct page *page; 15823db8620SAl Viro struct dir_context *ctx; 1591f1d4aa4STrond Myklebust pgoff_t page_index; 160580f2367STrond Myklebust pgoff_t page_index_max; 1612e7a4641STrond Myklebust u64 dir_cookie; 1620aded708STrond Myklebust u64 last_cookie; 163f0dd2136STrond Myklebust loff_t current_index; 164d1bacf9eSBryan Schumaker 165b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 166a1147b82STrond Myklebust unsigned long dir_verifier; 1671f4eab7eSNeil Brown unsigned long timestamp; 1684704f0e2STrond Myklebust unsigned long gencount; 1692e7a4641STrond Myklebust unsigned long attr_gencount; 170d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 171580f2367STrond Myklebust unsigned int buffer_fills; 172580f2367STrond Myklebust unsigned int dtsize; 173b0365ccbSTrond Myklebust bool clear_cache; 174a7a3b1e9SBenjamin Coddington bool plus; 175e1d2699bSTrond Myklebust bool eob; 176a7a3b1e9SBenjamin Coddington bool eof; 1776c981effSTrond Myklebust }; 1781da177e4SLinus Torvalds 179580f2367STrond Myklebust static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz) 180580f2367STrond Myklebust { 181580f2367STrond Myklebust struct nfs_server *server = NFS_SERVER(file_inode(desc->file)); 182580f2367STrond Myklebust unsigned int maxsize = server->dtsize; 183580f2367STrond Myklebust 184580f2367STrond Myklebust if (sz > maxsize) 185580f2367STrond Myklebust sz = maxsize; 186580f2367STrond Myklebust if (sz < NFS_MIN_FILE_IO_SIZE) 187580f2367STrond Myklebust sz = NFS_MIN_FILE_IO_SIZE; 188580f2367STrond Myklebust desc->dtsize = sz; 189580f2367STrond Myklebust } 190580f2367STrond Myklebust 191580f2367STrond Myklebust static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc) 192580f2367STrond Myklebust { 193580f2367STrond Myklebust nfs_set_dtsize(desc, desc->dtsize >> 1); 194580f2367STrond Myklebust } 195580f2367STrond Myklebust 196580f2367STrond Myklebust static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc) 197580f2367STrond Myklebust { 198580f2367STrond Myklebust nfs_set_dtsize(desc, desc->dtsize << 1); 199580f2367STrond Myklebust } 200580f2367STrond Myklebust 201d09e673fSTrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie, 202d09e673fSTrond Myklebust u64 change_attr) 2034b310319STrond Myklebust { 2044b310319STrond Myklebust struct nfs_cache_array *array; 2054b310319STrond Myklebust 2064b310319STrond Myklebust array = kmap_atomic(page); 207d09e673fSTrond Myklebust array->change_attr = change_attr; 2081f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 2099332cf14STrond Myklebust array->size = 0; 2109332cf14STrond Myklebust array->page_full = 0; 2119332cf14STrond Myklebust array->page_is_eof = 0; 212762567b7STrond Myklebust array->cookies_are_ordered = 1; 2134b310319STrond Myklebust kunmap_atomic(array); 2144b310319STrond Myklebust } 2154b310319STrond Myklebust 216d1bacf9eSBryan Schumaker /* 217d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 218d1bacf9eSBryan Schumaker */ 2199332cf14STrond Myklebust static void nfs_readdir_clear_array(struct page *page) 220d1bacf9eSBryan Schumaker { 22111de3b11STrond Myklebust struct nfs_cache_array *array; 2229332cf14STrond Myklebust unsigned int i; 2238cd51a0cSTrond Myklebust 2242b86ce2dSCong Wang array = kmap_atomic(page); 225d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 226a52a8a6aSTrond Myklebust kfree(array->array[i].name); 2279332cf14STrond Myklebust array->size = 0; 2282b86ce2dSCong Wang kunmap_atomic(array); 229d1bacf9eSBryan Schumaker } 230d1bacf9eSBryan Schumaker 231b0365ccbSTrond Myklebust static void nfs_readdir_page_reinit_array(struct page *page, u64 last_cookie, 232b0365ccbSTrond Myklebust u64 change_attr) 233b0365ccbSTrond Myklebust { 234b0365ccbSTrond Myklebust nfs_readdir_clear_array(page); 235b0365ccbSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, change_attr); 236b0365ccbSTrond Myklebust } 237b0365ccbSTrond Myklebust 23835df59d3STrond Myklebust static struct page * 23935df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags) 24035df59d3STrond Myklebust { 24135df59d3STrond Myklebust struct page *page = alloc_page(gfp_flags); 24235df59d3STrond Myklebust if (page) 243d09e673fSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, 0); 24435df59d3STrond Myklebust return page; 24535df59d3STrond Myklebust } 24635df59d3STrond Myklebust 24735df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page) 24835df59d3STrond Myklebust { 24935df59d3STrond Myklebust if (page) { 25035df59d3STrond Myklebust nfs_readdir_clear_array(page); 25135df59d3STrond Myklebust put_page(page); 25235df59d3STrond Myklebust } 25335df59d3STrond Myklebust } 25435df59d3STrond Myklebust 255b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 256b1e21c97STrond Myklebust { 257b1e21c97STrond Myklebust array->page_is_eof = 1; 258b1e21c97STrond Myklebust array->page_full = 1; 259b1e21c97STrond Myklebust } 260b1e21c97STrond Myklebust 261b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 262b1e21c97STrond Myklebust { 263b1e21c97STrond Myklebust return array->page_full; 264b1e21c97STrond Myklebust } 265b1e21c97STrond Myklebust 266d1bacf9eSBryan Schumaker /* 267d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 268d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 269d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 270d1bacf9eSBryan Schumaker */ 271a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 272d1bacf9eSBryan Schumaker { 273a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 274a52a8a6aSTrond Myklebust 27504e4bd1cSCatalin Marinas /* 27604e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 27704e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 27804e4bd1cSCatalin Marinas */ 279a52a8a6aSTrond Myklebust if (ret != NULL) 280a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 281a52a8a6aSTrond Myklebust return ret; 282d1bacf9eSBryan Schumaker } 283d1bacf9eSBryan Schumaker 2840b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void) 2850b2662b7STrond Myklebust { 2860b2662b7STrond Myklebust return (PAGE_SIZE - sizeof(struct nfs_cache_array)) / 2870b2662b7STrond Myklebust sizeof(struct nfs_cache_array_entry); 2880b2662b7STrond Myklebust } 2890b2662b7STrond Myklebust 290b1e21c97STrond Myklebust /* 291b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 292b1e21c97STrond Myklebust */ 293b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 294b1e21c97STrond Myklebust { 295b1e21c97STrond Myklebust if (array->page_full) 296b1e21c97STrond Myklebust return -ENOSPC; 2970b2662b7STrond Myklebust if (array->size == nfs_readdir_array_maxentries()) { 298b1e21c97STrond Myklebust array->page_full = 1; 299b1e21c97STrond Myklebust return -ENOSPC; 300b1e21c97STrond Myklebust } 301d1bacf9eSBryan Schumaker return 0; 302d1bacf9eSBryan Schumaker } 303d1bacf9eSBryan Schumaker 304*0adf85b4STrond Myklebust static int nfs_readdir_page_array_append(struct page *page, 305*0adf85b4STrond Myklebust const struct nfs_entry *entry, 306*0adf85b4STrond Myklebust u64 *cookie) 307d1bacf9eSBryan Schumaker { 308a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 3094a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 310a52a8a6aSTrond Myklebust const char *name; 311*0adf85b4STrond Myklebust int ret = -ENOMEM; 3124a201d6eSTrond Myklebust 313a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 3143020093fSTrond Myklebust 315a52a8a6aSTrond Myklebust array = kmap_atomic(page); 316*0adf85b4STrond Myklebust if (!name) 317*0adf85b4STrond Myklebust goto out; 318b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 319a52a8a6aSTrond Myklebust if (ret) { 320a52a8a6aSTrond Myklebust kfree(name); 3213020093fSTrond Myklebust goto out; 322a52a8a6aSTrond Myklebust } 3233020093fSTrond Myklebust 324b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 325*0adf85b4STrond Myklebust cache_entry->cookie = array->last_cookie; 3264a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 3270b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 328a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 329a52a8a6aSTrond Myklebust cache_entry->name = name; 330d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 331762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie) 332762567b7STrond Myklebust array->cookies_are_ordered = 0; 3338cd51a0cSTrond Myklebust array->size++; 33447c716cbSTrond Myklebust if (entry->eof != 0) 335b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 3364a201d6eSTrond Myklebust out: 337*0adf85b4STrond Myklebust *cookie = array->last_cookie; 338a52a8a6aSTrond Myklebust kunmap_atomic(array); 3394a201d6eSTrond Myklebust return ret; 340d1bacf9eSBryan Schumaker } 341d1bacf9eSBryan Schumaker 342f648022fSTrond Myklebust #define NFS_READDIR_COOKIE_MASK (U32_MAX >> 14) 343f648022fSTrond Myklebust /* 344f648022fSTrond Myklebust * Hash algorithm allowing content addressible access to sequences 345f648022fSTrond Myklebust * of directory cookies. Content is addressed by the value of the 346f648022fSTrond Myklebust * cookie index of the first readdir entry in a page. 347f648022fSTrond Myklebust * 348f648022fSTrond Myklebust * The xxhash algorithm is chosen because it is fast, and is supposed 349f648022fSTrond Myklebust * to result in a decent flat distribution of hashes. 350f648022fSTrond Myklebust * 351f648022fSTrond Myklebust * We then select only the first 18 bits to avoid issues with excessive 352f648022fSTrond Myklebust * memory use for the page cache XArray. 18 bits should allow the caching 353f648022fSTrond Myklebust * of 262144 pages of sequences of readdir entries. Since each page holds 354f648022fSTrond Myklebust * 127 readdir entries for a typical 64-bit system, that works out to a 355f648022fSTrond Myklebust * cache of ~ 33 million entries per directory. 356f648022fSTrond Myklebust */ 357f648022fSTrond Myklebust static pgoff_t nfs_readdir_page_cookie_hash(u64 cookie) 358f648022fSTrond Myklebust { 359f648022fSTrond Myklebust if (cookie == 0) 360f648022fSTrond Myklebust return 0; 361f648022fSTrond Myklebust return xxhash(&cookie, sizeof(cookie), 0) & NFS_READDIR_COOKIE_MASK; 362f648022fSTrond Myklebust } 363f648022fSTrond Myklebust 364d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie, 365d09e673fSTrond Myklebust u64 change_attr) 366d09e673fSTrond Myklebust { 367d09e673fSTrond Myklebust struct nfs_cache_array *array = kmap_atomic(page); 368d09e673fSTrond Myklebust int ret = true; 369d09e673fSTrond Myklebust 370d09e673fSTrond Myklebust if (array->change_attr != change_attr) 371d09e673fSTrond Myklebust ret = false; 372d09e673fSTrond Myklebust if (array->size > 0 && array->array[0].cookie != last_cookie) 373d09e673fSTrond Myklebust ret = false; 374d09e673fSTrond Myklebust kunmap_atomic(array); 375d09e673fSTrond Myklebust return ret; 376d09e673fSTrond Myklebust } 377d09e673fSTrond Myklebust 378d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 379d09e673fSTrond Myklebust { 380d09e673fSTrond Myklebust unlock_page(page); 381d09e673fSTrond Myklebust put_page(page); 382d09e673fSTrond Myklebust } 383d09e673fSTrond Myklebust 3841f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 385f648022fSTrond Myklebust u64 last_cookie, 386f648022fSTrond Myklebust u64 change_attr) 3871f1d4aa4STrond Myklebust { 388f648022fSTrond Myklebust pgoff_t index = nfs_readdir_page_cookie_hash(last_cookie); 3891f1d4aa4STrond Myklebust struct page *page; 3901f1d4aa4STrond Myklebust 3911f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 392d09e673fSTrond Myklebust if (!page) 393d09e673fSTrond Myklebust return NULL; 394d09e673fSTrond Myklebust if (PageUptodate(page)) { 395d09e673fSTrond Myklebust if (nfs_readdir_page_validate(page, last_cookie, change_attr)) 396d09e673fSTrond Myklebust return page; 397d09e673fSTrond Myklebust nfs_readdir_clear_array(page); 3981f1d4aa4STrond Myklebust } 399d09e673fSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, change_attr); 400d09e673fSTrond Myklebust SetPageUptodate(page); 4011f1d4aa4STrond Myklebust return page; 4021f1d4aa4STrond Myklebust } 4031f1d4aa4STrond Myklebust 4041f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 4051f1d4aa4STrond Myklebust { 4061f1d4aa4STrond Myklebust struct nfs_cache_array *array; 4071f1d4aa4STrond Myklebust u64 ret; 4081f1d4aa4STrond Myklebust 4091f1d4aa4STrond Myklebust array = kmap_atomic(page); 4101f1d4aa4STrond Myklebust ret = array->last_cookie; 4111f1d4aa4STrond Myklebust kunmap_atomic(array); 4121f1d4aa4STrond Myklebust return ret; 4131f1d4aa4STrond Myklebust } 4141f1d4aa4STrond Myklebust 4151f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 4161f1d4aa4STrond Myklebust { 4171f1d4aa4STrond Myklebust struct nfs_cache_array *array; 4181f1d4aa4STrond Myklebust bool ret; 4191f1d4aa4STrond Myklebust 4201f1d4aa4STrond Myklebust array = kmap_atomic(page); 4211f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 4221f1d4aa4STrond Myklebust kunmap_atomic(array); 4231f1d4aa4STrond Myklebust return ret; 4241f1d4aa4STrond Myklebust } 4251f1d4aa4STrond Myklebust 426b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 427b1e21c97STrond Myklebust { 428b1e21c97STrond Myklebust struct nfs_cache_array *array; 429b1e21c97STrond Myklebust 430b1e21c97STrond Myklebust array = kmap_atomic(page); 431b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 432b1e21c97STrond Myklebust kunmap_atomic(array); 433b1e21c97STrond Myklebust } 434b1e21c97STrond Myklebust 4353b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 436f648022fSTrond Myklebust u64 cookie, u64 change_attr) 4373b2a09f1STrond Myklebust { 4383b2a09f1STrond Myklebust struct page *page; 4393b2a09f1STrond Myklebust 440f648022fSTrond Myklebust page = nfs_readdir_page_get_locked(mapping, cookie, change_attr); 441b0365ccbSTrond Myklebust if (!page) 4423b2a09f1STrond Myklebust return NULL; 443b0365ccbSTrond Myklebust if (nfs_readdir_page_last_cookie(page) != cookie) 444b0365ccbSTrond Myklebust nfs_readdir_page_reinit_array(page, cookie, change_attr); 445b0365ccbSTrond Myklebust return page; 4463b2a09f1STrond Myklebust } 4473b2a09f1STrond Myklebust 44859e356a9STrond Myklebust static inline 44959e356a9STrond Myklebust int is_32bit_api(void) 45059e356a9STrond Myklebust { 45159e356a9STrond Myklebust #ifdef CONFIG_COMPAT 45259e356a9STrond Myklebust return in_compat_syscall(); 45359e356a9STrond Myklebust #else 45459e356a9STrond Myklebust return (BITS_PER_LONG == 32); 45559e356a9STrond Myklebust #endif 45659e356a9STrond Myklebust } 45759e356a9STrond Myklebust 45859e356a9STrond Myklebust static 45959e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 46059e356a9STrond Myklebust { 46159e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 46259e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 46359e356a9STrond Myklebust return false; 46459e356a9STrond Myklebust return true; 46559e356a9STrond Myklebust } 46659e356a9STrond Myklebust 467c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array, 468c8f0523bSTrond Myklebust struct nfs_readdir_descriptor *desc) 469c8f0523bSTrond Myklebust { 470c8f0523bSTrond Myklebust if (array->page_full) { 471c8f0523bSTrond Myklebust desc->last_cookie = array->last_cookie; 472c8f0523bSTrond Myklebust desc->current_index += array->size; 473c8f0523bSTrond Myklebust desc->cache_entry_index = 0; 474c8f0523bSTrond Myklebust desc->page_index++; 475c8f0523bSTrond Myklebust } else 476c8f0523bSTrond Myklebust desc->last_cookie = array->array[0].cookie; 477c8f0523bSTrond Myklebust } 478c8f0523bSTrond Myklebust 479f648022fSTrond Myklebust static void nfs_readdir_rewind_search(struct nfs_readdir_descriptor *desc) 480f648022fSTrond Myklebust { 481f648022fSTrond Myklebust desc->current_index = 0; 482f648022fSTrond Myklebust desc->last_cookie = 0; 483f648022fSTrond Myklebust desc->page_index = 0; 484f648022fSTrond Myklebust } 485f648022fSTrond Myklebust 4866c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 4876c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 488d1bacf9eSBryan Schumaker { 48923db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 490d1bacf9eSBryan Schumaker unsigned int index; 491d1bacf9eSBryan Schumaker 492d1bacf9eSBryan Schumaker if (diff < 0) 493d1bacf9eSBryan Schumaker goto out_eof; 494d1bacf9eSBryan Schumaker if (diff >= array->size) { 495b1e21c97STrond Myklebust if (array->page_is_eof) 496d1bacf9eSBryan Schumaker goto out_eof; 497c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 498d1bacf9eSBryan Schumaker return -EAGAIN; 499d1bacf9eSBryan Schumaker } 500d1bacf9eSBryan Schumaker 501d1bacf9eSBryan Schumaker index = (unsigned int)diff; 5022e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 503d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 504d1bacf9eSBryan Schumaker return 0; 505d1bacf9eSBryan Schumaker out_eof: 5066089dd0dSThomas Meyer desc->eof = true; 507d1bacf9eSBryan Schumaker return -EBADCOOKIE; 508d1bacf9eSBryan Schumaker } 509d1bacf9eSBryan Schumaker 510762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 511762567b7STrond Myklebust u64 cookie) 512762567b7STrond Myklebust { 513762567b7STrond Myklebust if (!array->cookies_are_ordered) 514762567b7STrond Myklebust return true; 515762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */ 516762567b7STrond Myklebust if (cookie >= array->last_cookie) 517762567b7STrond Myklebust return false; 518762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie) 519762567b7STrond Myklebust return false; 520762567b7STrond Myklebust return true; 521762567b7STrond Myklebust } 522762567b7STrond Myklebust 5236c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 5246c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 525d1bacf9eSBryan Schumaker { 526f648022fSTrond Myklebust unsigned int i; 527d1bacf9eSBryan Schumaker int status = -EAGAIN; 528d1bacf9eSBryan Schumaker 529762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 530762567b7STrond Myklebust goto check_eof; 531762567b7STrond Myklebust 532d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 5332e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 53459e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 5352e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 53659e356a9STrond Myklebust else 537f648022fSTrond Myklebust desc->ctx->pos = desc->current_index + i; 5388cd51a0cSTrond Myklebust desc->cache_entry_index = i; 53947c716cbSTrond Myklebust return 0; 5408cd51a0cSTrond Myklebust } 5418cd51a0cSTrond Myklebust } 542762567b7STrond Myklebust check_eof: 543b1e21c97STrond Myklebust if (array->page_is_eof) { 544d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 5452e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 5466089dd0dSThomas Meyer desc->eof = true; 547c8f0523bSTrond Myklebust } else 548c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 549d1bacf9eSBryan Schumaker return status; 550d1bacf9eSBryan Schumaker } 551d1bacf9eSBryan Schumaker 5526c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 553d1bacf9eSBryan Schumaker { 554d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 55547c716cbSTrond Myklebust int status; 556d1bacf9eSBryan Schumaker 557ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 558d1bacf9eSBryan Schumaker 5592e7a4641STrond Myklebust if (desc->dir_cookie == 0) 560d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 561d1bacf9eSBryan Schumaker else 562d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 563d1bacf9eSBryan Schumaker 564ed09222dSTrond Myklebust kunmap_atomic(array); 565d1bacf9eSBryan Schumaker return status; 566d1bacf9eSBryan Schumaker } 567d1bacf9eSBryan Schumaker 568d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 56993b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 570b593c09fSTrond Myklebust __be32 *verf, u64 cookie, 571b593c09fSTrond Myklebust struct page **pages, size_t bufsize, 572b593c09fSTrond Myklebust __be32 *verf_res) 573d1bacf9eSBryan Schumaker { 57482e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 57582e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 57682e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 57782e22a5eSTrond Myklebust .cred = desc->file->f_cred, 578b593c09fSTrond Myklebust .verf = verf, 57982e22a5eSTrond Myklebust .cookie = cookie, 58082e22a5eSTrond Myklebust .pages = pages, 58182e22a5eSTrond Myklebust .page_len = bufsize, 58282e22a5eSTrond Myklebust .plus = desc->plus, 58382e22a5eSTrond Myklebust }; 58482e22a5eSTrond Myklebust struct nfs_readdir_res res = { 58582e22a5eSTrond Myklebust .verf = verf_res, 58682e22a5eSTrond Myklebust }; 5874704f0e2STrond Myklebust unsigned long timestamp, gencount; 5881da177e4SLinus Torvalds int error; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds again: 5911da177e4SLinus Torvalds timestamp = jiffies; 5924704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 593a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 59482e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 5951da177e4SLinus Torvalds if (error < 0) { 5961da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 5971da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 5981da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 59982e22a5eSTrond Myklebust desc->plus = arg.plus = false; 6001da177e4SLinus Torvalds goto again; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds goto error; 6031da177e4SLinus Torvalds } 6041f4eab7eSNeil Brown desc->timestamp = timestamp; 6054704f0e2STrond Myklebust desc->gencount = gencount; 606d1bacf9eSBryan Schumaker error: 607d1bacf9eSBryan Schumaker return error; 608d1bacf9eSBryan Schumaker } 609d1bacf9eSBryan Schumaker 6106c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 611573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 612d1bacf9eSBryan Schumaker { 61359e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 614573c4e1eSChuck Lever int error; 615d1bacf9eSBryan Schumaker 61659e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 617573c4e1eSChuck Lever if (error) 618573c4e1eSChuck Lever return error; 619d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 620d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 621d1bacf9eSBryan Schumaker return 0; 622d1bacf9eSBryan Schumaker } 623d1bacf9eSBryan Schumaker 624fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 625fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 626fa923369STrond Myklebust */ 627d39ab9deSBryan Schumaker static 628d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 629d39ab9deSBryan Schumaker { 630d8fdb47fSTrond Myklebust struct inode *inode; 631fa923369STrond Myklebust struct nfs_inode *nfsi; 632fa923369STrond Myklebust 6332b0143b5SDavid Howells if (d_really_is_negative(dentry)) 6342b0143b5SDavid Howells return 0; 635fa923369STrond Myklebust 636d8fdb47fSTrond Myklebust inode = d_inode(dentry); 637d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 638d8fdb47fSTrond Myklebust return 0; 639d8fdb47fSTrond Myklebust 640d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 6417dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 642d39ab9deSBryan Schumaker return 0; 6437dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 6447dc72d5fSTrond Myklebust return 0; 6457dc72d5fSTrond Myklebust return 1; 646d39ab9deSBryan Schumaker } 647d39ab9deSBryan Schumaker 648230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL) 649230bc98fSTrond Myklebust 650230bc98fSTrond Myklebust static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx, 651230bc98fSTrond Myklebust unsigned int cache_hits, 652230bc98fSTrond Myklebust unsigned int cache_misses) 653d69ee9b8STrond Myklebust { 654d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 655d69ee9b8STrond Myklebust return false; 656230bc98fSTrond Myklebust if (ctx->pos == 0 || 657230bc98fSTrond Myklebust cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD) 658d69ee9b8STrond Myklebust return true; 659d69ee9b8STrond Myklebust return false; 660d69ee9b8STrond Myklebust } 661d69ee9b8STrond Myklebust 662d69ee9b8STrond Myklebust /* 663230bc98fSTrond Myklebust * This function is called by the getattr code to request the 66463519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 665d69ee9b8STrond Myklebust * directory. 666d69ee9b8STrond Myklebust */ 667230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_hit(struct inode *dir) 668d69ee9b8STrond Myklebust { 66963519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 670230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx; 67163519fbcSTrond Myklebust 67263519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 673230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) { 674230bc98fSTrond Myklebust rcu_read_lock(); 675230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list) 676230bc98fSTrond Myklebust atomic_inc(&ctx->cache_hits); 677230bc98fSTrond Myklebust rcu_read_unlock(); 678230bc98fSTrond Myklebust } 679d69ee9b8STrond Myklebust } 680d69ee9b8STrond Myklebust 681311324adSTrond Myklebust /* 682311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 683311324adSTrond Myklebust * 684311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 685311324adSTrond Myklebust */ 686230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_miss(struct inode *dir) 687311324adSTrond Myklebust { 68863519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 689230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx; 69063519fbcSTrond Myklebust 69163519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 692230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) { 693230bc98fSTrond Myklebust rcu_read_lock(); 694230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list) 695230bc98fSTrond Myklebust atomic_inc(&ctx->cache_misses); 696230bc98fSTrond Myklebust rcu_read_unlock(); 697311324adSTrond Myklebust } 698311324adSTrond Myklebust } 699311324adSTrond Myklebust 7000b3cc71bSTrond Myklebust static void nfs_lookup_advise_force_readdirplus(struct inode *dir, 7010b3cc71bSTrond Myklebust unsigned int flags) 702230bc98fSTrond Myklebust { 7032c2c3365STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 7042c2c3365STrond Myklebust return; 7050b3cc71bSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL)) 7060b3cc71bSTrond Myklebust return; 707230bc98fSTrond Myklebust nfs_readdir_record_entry_cache_miss(dir); 708230bc98fSTrond Myklebust } 709230bc98fSTrond Myklebust 710d69ee9b8STrond Myklebust static 711a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 712a1147b82STrond Myklebust unsigned long dir_verifier) 713d39ab9deSBryan Schumaker { 71426fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 7159ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 7164a201d6eSTrond Myklebust struct dentry *dentry; 7174a201d6eSTrond Myklebust struct dentry *alias; 718d39ab9deSBryan Schumaker struct inode *inode; 719aa9c2669SDavid Quigley int status; 720d39ab9deSBryan Schumaker 721fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 722fa923369STrond Myklebust return; 7236c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 7246c441c25STrond Myklebust return; 72578d04af4STrond Myklebust if (filename.len == 0) 72678d04af4STrond Myklebust return; 72778d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 72878d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 72978d04af4STrond Myklebust return; 73078d04af4STrond Myklebust /* ...or '/' */ 73178d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 73278d04af4STrond Myklebust return; 7334a201d6eSTrond Myklebust if (filename.name[0] == '.') { 7344a201d6eSTrond Myklebust if (filename.len == 1) 7354a201d6eSTrond Myklebust return; 7364a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 7374a201d6eSTrond Myklebust return; 7384a201d6eSTrond Myklebust } 7398387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 740d39ab9deSBryan Schumaker 7414a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 7429ac3d3e8SAl Viro again: 7439ac3d3e8SAl Viro if (!dentry) { 7449ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 7459ac3d3e8SAl Viro if (IS_ERR(dentry)) 7469ac3d3e8SAl Viro return; 7479ac3d3e8SAl Viro } 7489ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 7496c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 7506c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 7516c441c25STrond Myklebust &entry->fattr->fsid)) 7526c441c25STrond Myklebust goto out; 753d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 7547dc72d5fSTrond Myklebust if (!entry->fh->size) 7557dc72d5fSTrond Myklebust goto out; 756a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 7572b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 758aa9c2669SDavid Quigley if (!status) 759dd225cb3SAnna Schumaker nfs_setsecurity(d_inode(dentry), entry->fattr); 760eace45a1STrond Myklebust trace_nfs_readdir_lookup_revalidate(d_inode(parent), 761eace45a1STrond Myklebust dentry, 0, status); 762d39ab9deSBryan Schumaker goto out; 763d39ab9deSBryan Schumaker } else { 764eace45a1STrond Myklebust trace_nfs_readdir_lookup_revalidate_failed( 765eace45a1STrond Myklebust d_inode(parent), dentry, 0); 7665542aa2fSEric W. Biederman d_invalidate(dentry); 767d39ab9deSBryan Schumaker dput(dentry); 7689ac3d3e8SAl Viro dentry = NULL; 7699ac3d3e8SAl Viro goto again; 770d39ab9deSBryan Schumaker } 771d39ab9deSBryan Schumaker } 7727dc72d5fSTrond Myklebust if (!entry->fh->size) { 7737dc72d5fSTrond Myklebust d_lookup_done(dentry); 7747dc72d5fSTrond Myklebust goto out; 7757dc72d5fSTrond Myklebust } 776d39ab9deSBryan Schumaker 777cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 77841d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 7799ac3d3e8SAl Viro d_lookup_done(dentry); 7809ac3d3e8SAl Viro if (alias) { 781d39ab9deSBryan Schumaker if (IS_ERR(alias)) 782d39ab9deSBryan Schumaker goto out; 7839ac3d3e8SAl Viro dput(dentry); 7849ac3d3e8SAl Viro dentry = alias; 7859ac3d3e8SAl Viro } 786a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 787eace45a1STrond Myklebust trace_nfs_readdir_lookup(d_inode(parent), dentry, 0); 788d39ab9deSBryan Schumaker out: 789d39ab9deSBryan Schumaker dput(dentry); 790d39ab9deSBryan Schumaker } 791d39ab9deSBryan Schumaker 792d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 7933b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 7943b2a09f1STrond Myklebust struct nfs_entry *entry, 795f648022fSTrond Myklebust struct page **xdr_pages, unsigned int buflen, 796f648022fSTrond Myklebust struct page **arrays, size_t narrays, 797f648022fSTrond Myklebust u64 change_attr) 798d1bacf9eSBryan Schumaker { 7993b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 800babddc72SBryan Schumaker struct xdr_stream stream; 801f7da7a12SBenny Halevy struct xdr_buf buf; 80235df59d3STrond Myklebust struct page *scratch, *new, *page = *arrays; 803*0adf85b4STrond Myklebust u64 cookie; 8045c346854STrond Myklebust int status; 805babddc72SBryan Schumaker 8066650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 8076650239aSTrond Myklebust if (scratch == NULL) 8086650239aSTrond Myklebust return -ENOMEM; 809babddc72SBryan Schumaker 810f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 8110ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch); 81299424380SBryan Schumaker 81399424380SBryan Schumaker do { 814b1db9a40SAnna Schumaker if (entry->fattr->label) 815b1db9a40SAnna Schumaker entry->fattr->label->len = NFS4_MAXLABELLEN; 816d33030e2SJeffrey Mitchell 81799424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 818972bcdf2STrond Myklebust if (status != 0) 81999424380SBryan Schumaker break; 8205c346854STrond Myklebust 821a7a3b1e9SBenjamin Coddington if (desc->plus) 822a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 823a1147b82STrond Myklebust desc->dir_verifier); 8248cd51a0cSTrond Myklebust 825*0adf85b4STrond Myklebust status = nfs_readdir_page_array_append(page, entry, &cookie); 8263b2a09f1STrond Myklebust if (status != -ENOSPC) 8273b2a09f1STrond Myklebust continue; 82899424380SBryan Schumaker 82935df59d3STrond Myklebust if (page->mapping != mapping) { 83035df59d3STrond Myklebust if (!--narrays) 8313b2a09f1STrond Myklebust break; 832*0adf85b4STrond Myklebust new = nfs_readdir_page_array_alloc(cookie, GFP_KERNEL); 83335df59d3STrond Myklebust if (!new) 83435df59d3STrond Myklebust break; 83535df59d3STrond Myklebust arrays++; 83635df59d3STrond Myklebust *arrays = page = new; 83735df59d3STrond Myklebust } else { 838*0adf85b4STrond Myklebust new = nfs_readdir_page_get_next(mapping, cookie, 839*0adf85b4STrond Myklebust change_attr); 8403b2a09f1STrond Myklebust if (!new) 8413b2a09f1STrond Myklebust break; 84235df59d3STrond Myklebust if (page != *arrays) 8433b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8443b2a09f1STrond Myklebust page = new; 84556e4ebf8SBryan Schumaker } 846f648022fSTrond Myklebust desc->page_index_max++; 847*0adf85b4STrond Myklebust status = nfs_readdir_page_array_append(page, entry, &cookie); 848972bcdf2STrond Myklebust } while (!status && !entry->eof); 84956e4ebf8SBryan Schumaker 850972bcdf2STrond Myklebust switch (status) { 851972bcdf2STrond Myklebust case -EBADCOOKIE: 852972bcdf2STrond Myklebust if (entry->eof) { 853b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 8548cd51a0cSTrond Myklebust status = 0; 85556e4ebf8SBryan Schumaker } 856972bcdf2STrond Myklebust break; 857972bcdf2STrond Myklebust case -ENOSPC: 858972bcdf2STrond Myklebust case -EAGAIN: 859972bcdf2STrond Myklebust status = 0; 860972bcdf2STrond Myklebust break; 861972bcdf2STrond Myklebust } 8626650239aSTrond Myklebust 86335df59d3STrond Myklebust if (page != *arrays) 8643b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8656650239aSTrond Myklebust 8666650239aSTrond Myklebust put_page(scratch); 867d1bacf9eSBryan Schumaker return status; 86856e4ebf8SBryan Schumaker } 86956e4ebf8SBryan Schumaker 8701a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 87156e4ebf8SBryan Schumaker { 8721a34c8c9STrond Myklebust while (npages--) 8731a34c8c9STrond Myklebust put_page(pages[npages]); 8741a34c8c9STrond Myklebust kfree(pages); 87556e4ebf8SBryan Schumaker } 87656e4ebf8SBryan Schumaker 87756e4ebf8SBryan Schumaker /* 878bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 879bf211ca1Szhangliguang * to nfs_readdir_free_pages() 88056e4ebf8SBryan Schumaker */ 8811a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 88256e4ebf8SBryan Schumaker { 8831a34c8c9STrond Myklebust struct page **pages; 8841a34c8c9STrond Myklebust size_t i; 88556e4ebf8SBryan Schumaker 8861a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 8871a34c8c9STrond Myklebust if (!pages) 8881a34c8c9STrond Myklebust return NULL; 88956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 89056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 89156e4ebf8SBryan Schumaker if (page == NULL) 89256e4ebf8SBryan Schumaker goto out_freepages; 89356e4ebf8SBryan Schumaker pages[i] = page; 89456e4ebf8SBryan Schumaker } 8951a34c8c9STrond Myklebust return pages; 89656e4ebf8SBryan Schumaker 89756e4ebf8SBryan Schumaker out_freepages: 898c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 8991a34c8c9STrond Myklebust return NULL; 900d1bacf9eSBryan Schumaker } 901d1bacf9eSBryan Schumaker 9026c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 90335df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res, 90435df59d3STrond Myklebust struct page **arrays, size_t narrays) 905d1bacf9eSBryan Schumaker { 906f648022fSTrond Myklebust u64 change_attr; 9071a34c8c9STrond Myklebust struct page **pages; 90835df59d3STrond Myklebust struct page *page = *arrays; 9096b75cf9eSTrond Myklebust struct nfs_entry *entry; 9101a34c8c9STrond Myklebust size_t array_size; 911b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file); 912580f2367STrond Myklebust unsigned int dtsize = desc->dtsize; 9139ff89c25STrond Myklebust unsigned int pglen; 9148cd51a0cSTrond Myklebust int status = -ENOMEM; 915d1bacf9eSBryan Schumaker 9166b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 9176b75cf9eSTrond Myklebust if (!entry) 9186b75cf9eSTrond Myklebust return -ENOMEM; 9196b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 9206b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 921b1db9a40SAnna Schumaker entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 9226b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 9236b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 924d1bacf9eSBryan Schumaker goto out; 925d1bacf9eSBryan Schumaker 9261a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 9271a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 9281a34c8c9STrond Myklebust if (!pages) 929b1db9a40SAnna Schumaker goto out; 930d1bacf9eSBryan Schumaker 931f648022fSTrond Myklebust change_attr = inode_peek_iversion_raw(inode); 9329ff89c25STrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages, 9339ff89c25STrond Myklebust dtsize, verf_res); 934d1bacf9eSBryan Schumaker if (status < 0) 9359ff89c25STrond Myklebust goto free_pages; 936972bcdf2STrond Myklebust 937ac396128STrond Myklebust pglen = status; 9389ff89c25STrond Myklebust if (pglen != 0) 93935df59d3STrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, pglen, 940f648022fSTrond Myklebust arrays, narrays, change_attr); 9419ff89c25STrond Myklebust else 9429ff89c25STrond Myklebust nfs_readdir_page_set_eof(page); 943580f2367STrond Myklebust desc->buffer_fills++; 944d1bacf9eSBryan Schumaker 9459ff89c25STrond Myklebust free_pages: 946c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 947d1bacf9eSBryan Schumaker out: 9486b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 9496b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 9506b75cf9eSTrond Myklebust kfree(entry); 951d1bacf9eSBryan Schumaker return status; 952d1bacf9eSBryan Schumaker } 953d1bacf9eSBryan Schumaker 9541f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 9551da177e4SLinus Torvalds { 95609cbfeafSKirill A. Shutemov put_page(desc->page); 9571da177e4SLinus Torvalds desc->page = NULL; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9601f1d4aa4STrond Myklebust static void 9611f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 9621da177e4SLinus Torvalds { 9631f1d4aa4STrond Myklebust unlock_page(desc->page); 9641f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 9651f1d4aa4STrond Myklebust } 9661f1d4aa4STrond Myklebust 9671f1d4aa4STrond Myklebust static struct page * 9681f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 9691f1d4aa4STrond Myklebust { 970f648022fSTrond Myklebust struct address_space *mapping = desc->file->f_mapping; 971f648022fSTrond Myklebust u64 change_attr = inode_peek_iversion_raw(mapping->host); 972b0365ccbSTrond Myklebust u64 cookie = desc->last_cookie; 973b0365ccbSTrond Myklebust struct page *page; 974f648022fSTrond Myklebust 975b0365ccbSTrond Myklebust page = nfs_readdir_page_get_locked(mapping, cookie, change_attr); 976b0365ccbSTrond Myklebust if (!page) 977b0365ccbSTrond Myklebust return NULL; 978b0365ccbSTrond Myklebust if (desc->clear_cache && !nfs_readdir_page_needs_filling(page)) 979b0365ccbSTrond Myklebust nfs_readdir_page_reinit_array(page, cookie, change_attr); 980b0365ccbSTrond Myklebust return page; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds /* 984d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 985114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 9861da177e4SLinus Torvalds */ 9876c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 988d1bacf9eSBryan Schumaker { 989227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 990227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 991b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 992d1bacf9eSBryan Schumaker int res; 993d1bacf9eSBryan Schumaker 9941f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 9951f1d4aa4STrond Myklebust if (!desc->page) 9961f1d4aa4STrond Myklebust return -ENOMEM; 9971f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 998580f2367STrond Myklebust /* Grow the dtsize if we had to go back for more pages */ 999580f2367STrond Myklebust if (desc->page_index == desc->page_index_max) 1000580f2367STrond Myklebust nfs_grow_dtsize(desc); 1001580f2367STrond Myklebust desc->page_index_max = desc->page_index; 1002310e3187STrond Myklebust trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf, 1003310e3187STrond Myklebust desc->last_cookie, 1004310e3187STrond Myklebust desc->page->index, desc->dtsize); 100535df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 100635df59d3STrond Myklebust &desc->page, 1); 10079fff59edSTrond Myklebust if (res < 0) { 10089fff59edSTrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1009310e3187STrond Myklebust trace_nfs_readdir_cache_fill_done(inode, res); 10109fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) { 10119fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping); 1012f648022fSTrond Myklebust nfs_readdir_rewind_search(desc); 101311d03d0aSTrond Myklebust trace_nfs_readdir_invalidate_cache_range( 101411d03d0aSTrond Myklebust inode, 0, MAX_LFS_FILESIZE); 10159fff59edSTrond Myklebust return -EAGAIN; 10169fff59edSTrond Myklebust } 10179fff59edSTrond Myklebust return res; 10189fff59edSTrond Myklebust } 1019f892c41cSTrond Myklebust /* 1020f892c41cSTrond Myklebust * Set the cookie verifier if the page cache was empty 1021f892c41cSTrond Myklebust */ 10226c34f05bSTrond Myklebust if (desc->last_cookie == 0 && 10236c34f05bSTrond Myklebust memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) { 1024f892c41cSTrond Myklebust memcpy(nfsi->cookieverf, verf, 1025f892c41cSTrond Myklebust sizeof(nfsi->cookieverf)); 1026f648022fSTrond Myklebust invalidate_inode_pages2_range(desc->file->f_mapping, 1, 10276c34f05bSTrond Myklebust -1); 102811d03d0aSTrond Myklebust trace_nfs_readdir_invalidate_cache_range( 1029f648022fSTrond Myklebust inode, 1, MAX_LFS_FILESIZE); 10306c34f05bSTrond Myklebust } 1031b0365ccbSTrond Myklebust desc->clear_cache = false; 10321f1d4aa4STrond Myklebust } 1033114de382STrond Myklebust res = nfs_readdir_search_array(desc); 1034ff81dfb5STrond Myklebust if (res == 0) 1035114de382STrond Myklebust return 0; 10361f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1037d1bacf9eSBryan Schumaker return res; 1038d1bacf9eSBryan Schumaker } 1039d1bacf9eSBryan Schumaker 1040d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 10416c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 10421da177e4SLinus Torvalds { 10438cd51a0cSTrond Myklebust int res; 1044d1bacf9eSBryan Schumaker 10459fff59edSTrond Myklebust do { 1046114de382STrond Myklebust res = find_and_lock_cache_page(desc); 104747c716cbSTrond Myklebust } while (res == -EAGAIN); 10481da177e4SLinus Torvalds return res; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* 10521da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 10531da177e4SLinus Torvalds */ 105413884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc, 105513884ff2STrond Myklebust const __be32 *verf) 10561da177e4SLinus Torvalds { 10571da177e4SLinus Torvalds struct file *file = desc->file; 1058dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 1059c8f0523bSTrond Myklebust unsigned int i; 10608ef2ce3eSBryan Schumaker 10610795bf83SFabian Frederick array = kmap(desc->page); 1062d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 1063ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 10641da177e4SLinus Torvalds 1065ece0b423STrond Myklebust ent = &array->array[i]; 1066a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 106723db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 1068e1d2699bSTrond Myklebust desc->eob = true; 10691da177e4SLinus Torvalds break; 1070ece0b423STrond Myklebust } 107113884ff2STrond Myklebust memcpy(desc->verf, verf, sizeof(desc->verf)); 1072c8f0523bSTrond Myklebust if (i == array->size - 1) { 10732e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 1074c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 1075c8f0523bSTrond Myklebust } else { 1076c8f0523bSTrond Myklebust desc->dir_cookie = array->array[i + 1].cookie; 1077c8f0523bSTrond Myklebust desc->last_cookie = array->array[0].cookie; 1078c8f0523bSTrond Myklebust } 107959e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 10802e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 108159e356a9STrond Myklebust else 108259e356a9STrond Myklebust desc->ctx->pos++; 10838cd51a0cSTrond Myklebust } 1084b1e21c97STrond Myklebust if (array->page_is_eof) 1085e1d2699bSTrond Myklebust desc->eof = !desc->eob; 1086d1bacf9eSBryan Schumaker 10870795bf83SFabian Frederick kunmap(desc->page); 1088dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1089dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds /* 10931da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 10941da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 10951da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 10961da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 10971da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 10981da177e4SLinus Torvalds * 10991da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 11001da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 11011da177e4SLinus Torvalds * we should already have a complete representation of the 11021da177e4SLinus Torvalds * directory in the page cache by the time we get here. 11031da177e4SLinus Torvalds */ 11046c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 11051da177e4SLinus Torvalds { 110635df59d3STrond Myklebust struct page **arrays; 110735df59d3STrond Myklebust size_t i, sz = 512; 1108b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 110935df59d3STrond Myklebust int status = -ENOMEM; 11101da177e4SLinus Torvalds 111135df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 11122e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 11131da177e4SLinus Torvalds 111435df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 111535df59d3STrond Myklebust if (!arrays) 11161da177e4SLinus Torvalds goto out; 111735df59d3STrond Myklebust arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 111835df59d3STrond Myklebust if (!arrays[0]) 111935df59d3STrond Myklebust goto out; 11201da177e4SLinus Torvalds 11217a8e1dc3STrond Myklebust desc->page_index = 0; 1122ce292d8fStrondmy@kernel.org desc->cache_entry_index = 0; 11232e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 1124580f2367STrond Myklebust desc->page_index_max = 0; 11257a8e1dc3STrond Myklebust 1126310e3187STrond Myklebust trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie, 1127310e3187STrond Myklebust -1, desc->dtsize); 1128310e3187STrond Myklebust 112935df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1130310e3187STrond Myklebust if (status < 0) { 1131310e3187STrond Myklebust trace_nfs_readdir_uncached_done(file_inode(desc->file), status); 1132310e3187STrond Myklebust goto out_free; 1133310e3187STrond Myklebust } 1134d1bacf9eSBryan Schumaker 1135e1d2699bSTrond Myklebust for (i = 0; !desc->eob && i < sz && arrays[i]; i++) { 113635df59d3STrond Myklebust desc->page = arrays[i]; 113713884ff2STrond Myklebust nfs_do_filldir(desc, verf); 113835df59d3STrond Myklebust } 113935df59d3STrond Myklebust desc->page = NULL; 11401da177e4SLinus Torvalds 1141580f2367STrond Myklebust /* 1142580f2367STrond Myklebust * Grow the dtsize if we have to go back for more pages, 1143580f2367STrond Myklebust * or shrink it if we're reading too many. 1144580f2367STrond Myklebust */ 1145580f2367STrond Myklebust if (!desc->eof) { 1146580f2367STrond Myklebust if (!desc->eob) 1147580f2367STrond Myklebust nfs_grow_dtsize(desc); 1148580f2367STrond Myklebust else if (desc->buffer_fills == 1 && 1149580f2367STrond Myklebust i < (desc->page_index_max >> 1)) 1150580f2367STrond Myklebust nfs_shrink_dtsize(desc); 1151580f2367STrond Myklebust } 1152310e3187STrond Myklebust out_free: 115335df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++) 115435df59d3STrond Myklebust nfs_readdir_page_array_free(arrays[i]); 11551da177e4SLinus Torvalds out: 1156f648022fSTrond Myklebust if (!nfs_readdir_use_cookie(desc->file)) 1157f648022fSTrond Myklebust nfs_readdir_rewind_search(desc); 1158580f2367STrond Myklebust desc->page_index_max = -1; 115935df59d3STrond Myklebust kfree(arrays); 116035df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 11611da177e4SLinus Torvalds return status; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 1164230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL) 1165230bc98fSTrond Myklebust 1166b0365ccbSTrond Myklebust static bool nfs_readdir_handle_cache_misses(struct inode *inode, 1167230bc98fSTrond Myklebust struct nfs_readdir_descriptor *desc, 1168b0365ccbSTrond Myklebust unsigned int cache_misses, 1169b0365ccbSTrond Myklebust bool force_clear) 1170230bc98fSTrond Myklebust { 1171b0365ccbSTrond Myklebust if (desc->ctx->pos == 0 || !desc->plus) 1172b0365ccbSTrond Myklebust return false; 1173b0365ccbSTrond Myklebust if (cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD && !force_clear) 1174b0365ccbSTrond Myklebust return false; 1175b0365ccbSTrond Myklebust trace_nfs_readdir_force_readdirplus(inode); 1176b0365ccbSTrond Myklebust return true; 1177230bc98fSTrond Myklebust } 1178230bc98fSTrond Myklebust 117900a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 118000a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 118100a92642SOlivier Galibert whole directory. 11821da177e4SLinus Torvalds */ 118323db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 11841da177e4SLinus Torvalds { 1185be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 11862b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 118713884ff2STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 118823db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 11896b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 1190230bc98fSTrond Myklebust unsigned int cache_hits, cache_misses; 1191b0365ccbSTrond Myklebust bool force_clear; 11926b75cf9eSTrond Myklebust int res; 11931da177e4SLinus Torvalds 11946de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 11956de1472fSAl Viro file, (long long)ctx->pos); 119691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 119791d5b470SChuck Lever 11981da177e4SLinus Torvalds /* 119923db8620SAl Viro * ctx->pos points to the dirent entry number. 1200f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 120100a92642SOlivier Galibert * to either find the entry with the appropriate number or 120200a92642SOlivier Galibert * revalidate the cookie. 12031da177e4SLinus Torvalds */ 1204d09e673fSTrond Myklebust nfs_revalidate_mapping(inode, file->f_mapping); 12056b75cf9eSTrond Myklebust 12066b75cf9eSTrond Myklebust res = -ENOMEM; 12076b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 12086b75cf9eSTrond Myklebust if (!desc) 12096b75cf9eSTrond Myklebust goto out; 12106b75cf9eSTrond Myklebust desc->file = file; 12116b75cf9eSTrond Myklebust desc->ctx = ctx; 1212580f2367STrond Myklebust desc->page_index_max = -1; 1213fccca7fcSTrond Myklebust 12142e7a4641STrond Myklebust spin_lock(&file->f_lock); 12152e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 1216f648022fSTrond Myklebust desc->page_index = dir_ctx->page_index; 1217728dd0abSTrond Myklebust desc->last_cookie = dir_ctx->last_cookie; 12182e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 1219e1d2699bSTrond Myklebust desc->eof = dir_ctx->eof; 1220580f2367STrond Myklebust nfs_set_dtsize(desc, dir_ctx->dtsize); 1221b593c09fSTrond Myklebust memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf)); 1222230bc98fSTrond Myklebust cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0); 1223230bc98fSTrond Myklebust cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0); 1224b0365ccbSTrond Myklebust force_clear = dir_ctx->force_clear; 12252e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1226565277f6STrond Myklebust 1227e1d2699bSTrond Myklebust if (desc->eof) { 1228e1d2699bSTrond Myklebust res = 0; 1229e1d2699bSTrond Myklebust goto out_free; 1230e1d2699bSTrond Myklebust } 1231e1d2699bSTrond Myklebust 1232230bc98fSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses); 1233b0365ccbSTrond Myklebust force_clear = nfs_readdir_handle_cache_misses(inode, desc, cache_misses, 1234b0365ccbSTrond Myklebust force_clear); 1235b0365ccbSTrond Myklebust desc->clear_cache = force_clear; 1236ff81dfb5STrond Myklebust 123747c716cbSTrond Myklebust do { 12381da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 123900a92642SOlivier Galibert 12401da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1241ece0b423STrond Myklebust res = 0; 12421da177e4SLinus Torvalds /* This means either end of directory */ 12432e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 12441da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 124523db8620SAl Viro res = uncached_readdir(desc); 1246ece0b423STrond Myklebust if (res == 0) 12471da177e4SLinus Torvalds continue; 12489fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) 12499fff59edSTrond Myklebust res = 0; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds break; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 12541da177e4SLinus Torvalds nfs_zap_caches(inode); 1255a7a3b1e9SBenjamin Coddington desc->plus = false; 1256a7a3b1e9SBenjamin Coddington desc->eof = false; 12571da177e4SLinus Torvalds continue; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds if (res < 0) 12601da177e4SLinus Torvalds break; 12611da177e4SLinus Torvalds 126213884ff2STrond Myklebust nfs_do_filldir(desc, nfsi->cookieverf); 12631f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1264b0365ccbSTrond Myklebust if (desc->page_index == desc->page_index_max) 1265b0365ccbSTrond Myklebust desc->clear_cache = force_clear; 1266e1d2699bSTrond Myklebust } while (!desc->eob && !desc->eof); 12672e7a4641STrond Myklebust 12682e7a4641STrond Myklebust spin_lock(&file->f_lock); 12692e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 1270728dd0abSTrond Myklebust dir_ctx->last_cookie = desc->last_cookie; 12712e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 1272ff81dfb5STrond Myklebust dir_ctx->page_index = desc->page_index; 1273b0365ccbSTrond Myklebust dir_ctx->force_clear = force_clear; 1274e1d2699bSTrond Myklebust dir_ctx->eof = desc->eof; 1275580f2367STrond Myklebust dir_ctx->dtsize = desc->dtsize; 1276b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 12772e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1278e1d2699bSTrond Myklebust out_free: 12796b75cf9eSTrond Myklebust kfree(desc); 12806b75cf9eSTrond Myklebust 1281fccca7fcSTrond Myklebust out: 12826de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 12831da177e4SLinus Torvalds return res; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 1286965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1287f0dd2136STrond Myklebust { 1288480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1289b84e06c5SChuck Lever 12906de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 12916de1472fSAl Viro filp, offset, whence); 1292b84e06c5SChuck Lever 1293965c8e59SAndrew Morton switch (whence) { 1294f0dd2136STrond Myklebust default: 1295b2b1ff3dSTrond Myklebust return -EINVAL; 1296b2b1ff3dSTrond Myklebust case SEEK_SET: 1297b2b1ff3dSTrond Myklebust if (offset < 0) 1298b2b1ff3dSTrond Myklebust return -EINVAL; 129983f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1300b2b1ff3dSTrond Myklebust break; 1301b2b1ff3dSTrond Myklebust case SEEK_CUR: 1302b2b1ff3dSTrond Myklebust if (offset == 0) 1303b2b1ff3dSTrond Myklebust return filp->f_pos; 130483f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1305b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1306b2b1ff3dSTrond Myklebust if (offset < 0) { 130783f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1308b2b1ff3dSTrond Myklebust return -EINVAL; 1309b2b1ff3dSTrond Myklebust } 1310f0dd2136STrond Myklebust } 1311f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1312f0dd2136STrond Myklebust filp->f_pos = offset; 1313728dd0abSTrond Myklebust dir_ctx->page_index = 0; 1314f648022fSTrond Myklebust if (!nfs_readdir_use_cookie(filp)) { 13159c3f4d98STrond Myklebust dir_ctx->dir_cookie = 0; 1316f648022fSTrond Myklebust dir_ctx->last_cookie = 0; 1317f648022fSTrond Myklebust } else { 1318728dd0abSTrond Myklebust dir_ctx->dir_cookie = offset; 1319f648022fSTrond Myklebust dir_ctx->last_cookie = offset; 1320f648022fSTrond Myklebust } 1321e1d2699bSTrond Myklebust dir_ctx->eof = false; 1322f0dd2136STrond Myklebust } 132383f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1324f0dd2136STrond Myklebust return offset; 1325f0dd2136STrond Myklebust } 1326f0dd2136STrond Myklebust 13271da177e4SLinus Torvalds /* 13281da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 13291da177e4SLinus Torvalds * is a dummy operation. 13301da177e4SLinus Torvalds */ 133102c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 133202c24a82SJosef Bacik int datasync) 13331da177e4SLinus Torvalds { 13346de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 13351e7cb3dcSChuck Lever 133611decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 13371da177e4SLinus Torvalds return 0; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 1340bfc69a45STrond Myklebust /** 1341bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1342302fad7bSTrond Myklebust * @dir: pointer to directory inode 1343bfc69a45STrond Myklebust * 1344bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1345bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1346bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1347bfc69a45STrond Myklebust * 1348efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1349efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1350efeda80dSTrond Myklebust * 1351bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1352bfc69a45STrond Myklebust */ 1353bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1354bfc69a45STrond Myklebust { 1355efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1356bfc69a45STrond Myklebust } 135789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1358bfc69a45STrond Myklebust 1359efeda80dSTrond Myklebust /** 1360efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1361efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1362efeda80dSTrond Myklebust * @verf: previously saved change attribute 1363efeda80dSTrond Myklebust * 1364efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1365efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1366efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1367efeda80dSTrond Myklebust */ 1368efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1369efeda80dSTrond Myklebust { 1370efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1371efeda80dSTrond Myklebust } 1372efeda80dSTrond Myklebust 1373efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1374efeda80dSTrond Myklebust { 1375efeda80dSTrond Myklebust *verf |= 1UL; 1376efeda80dSTrond Myklebust } 1377efeda80dSTrond Myklebust 1378efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1379efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1380efeda80dSTrond Myklebust { 1381efeda80dSTrond Myklebust *verf &= ~1UL; 1382efeda80dSTrond Myklebust } 1383efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1384efeda80dSTrond Myklebust 1385efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1386efeda80dSTrond Myklebust { 1387efeda80dSTrond Myklebust return verf & 1; 1388efeda80dSTrond Myklebust } 1389efeda80dSTrond Myklebust 1390efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1391efeda80dSTrond Myklebust { 1392efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1393efeda80dSTrond Myklebust } 1394efeda80dSTrond Myklebust 1395efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1396efeda80dSTrond Myklebust { 1397efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1398cec08f45STrond Myklebust struct inode *dir = d_inode(dentry->d_parent); 1399efeda80dSTrond Myklebust 1400cec08f45STrond Myklebust if (!nfs_verify_change_attribute(dir, verf)) 1401cec08f45STrond Myklebust return; 1402efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1403efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1404efeda80dSTrond Myklebust dentry->d_time = verf; 1405efeda80dSTrond Myklebust } 1406efeda80dSTrond Myklebust 1407efeda80dSTrond Myklebust /** 1408efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1409efeda80dSTrond Myklebust * @dentry: pointer to dentry 1410efeda80dSTrond Myklebust * @verf: verifier to save 1411efeda80dSTrond Myklebust * 1412efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1413efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1414efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1415efeda80dSTrond Myklebust * look it up again after a directory change. 1416efeda80dSTrond Myklebust */ 1417efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1418efeda80dSTrond Myklebust { 1419efeda80dSTrond Myklebust 1420efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1421efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1422efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1423efeda80dSTrond Myklebust } 1424efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1425efeda80dSTrond Myklebust 1426efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1427efeda80dSTrond Myklebust /** 1428efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1429efeda80dSTrond Myklebust * @inode: pointer to inode 1430efeda80dSTrond Myklebust * 1431efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1432efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1433efeda80dSTrond Myklebust * while holding a delegation. 1434efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1435efeda80dSTrond Myklebust * returned or revoked. 1436efeda80dSTrond Myklebust */ 1437efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1438efeda80dSTrond Myklebust { 1439efeda80dSTrond Myklebust struct dentry *alias; 1440efeda80dSTrond Myklebust 1441efeda80dSTrond Myklebust if (!inode) 1442efeda80dSTrond Myklebust return; 1443efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1444efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1445efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1446efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1447efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1448efeda80dSTrond Myklebust } 1449efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1450efeda80dSTrond Myklebust } 1451efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1452efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1453efeda80dSTrond Myklebust 14548ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry) 14558ce37abdSTrond Myklebust { 14568ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) && 14578ce37abdSTrond Myklebust d_really_is_negative(dentry)) 14588ce37abdSTrond Myklebust return dentry->d_time == inode_peek_iversion_raw(dir); 14598ce37abdSTrond Myklebust return nfs_verify_change_attribute(dir, dentry->d_time); 14608ce37abdSTrond Myklebust } 14618ce37abdSTrond Myklebust 14621da177e4SLinus Torvalds /* 14631da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 14641da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 14651da177e4SLinus Torvalds * and may need to be looked up again. 1466912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 14671da177e4SLinus Torvalds */ 1468912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1469912a108dSNeilBrown int rcu_walk) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds if (IS_ROOT(dentry)) 14721da177e4SLinus Torvalds return 1; 14734eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 14744eec952eSTrond Myklebust return 0; 14758ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 14766ecc5e8fSTrond Myklebust return 0; 1477f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 14781cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1479912a108dSNeilBrown if (rcu_walk) 1480f2c77f4eSTrond Myklebust return 0; 14811cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 14821cd9cb05STrond Myklebust return 0; 14831cd9cb05STrond Myklebust } 14848ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 1485f2c77f4eSTrond Myklebust return 0; 1486f2c77f4eSTrond Myklebust return 1; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds /* 1490a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1491a12802caSTrond Myklebust * an O_EXCL create using this path component. 1492a12802caSTrond Myklebust */ 1493fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1494a12802caSTrond Myklebust { 1495a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1496a12802caSTrond Myklebust return 0; 1497fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1498a12802caSTrond Myklebust } 1499a12802caSTrond Myklebust 1500a12802caSTrond Myklebust /* 15011d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 15021d6757fbSTrond Myklebust * 15031d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 15041d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 15051d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 15061d6757fbSTrond Myklebust * 15071d6757fbSTrond Myklebust */ 150865a0c149STrond Myklebust static 1509fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 15101da177e4SLinus Torvalds { 15111da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 151265a0c149STrond Myklebust int ret; 15131da177e4SLinus Torvalds 151436d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 15154e99a1ffSTrond Myklebust return 0; 151647921921STrond Myklebust 151747921921STrond Myklebust if (flags & LOOKUP_OPEN) { 151847921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 151947921921STrond Myklebust case S_IFREG: 152047921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 152147921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 152247921921STrond Myklebust goto out; 1523df561f66SGustavo A. R. Silva fallthrough; 152447921921STrond Myklebust case S_IFDIR: 152547921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 152647921921STrond Myklebust break; 152747921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 152847921921STrond Myklebust goto out_force; 152947921921STrond Myklebust } 153047921921STrond Myklebust } 153147921921STrond Myklebust 15321da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1533fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 15341da177e4SLinus Torvalds goto out_force; 153565a0c149STrond Myklebust out: 153643245ecaSOlga Kornievskaia if (inode->i_nlink > 0 || 153743245ecaSOlga Kornievskaia (inode->i_nlink == 0 && 153843245ecaSOlga Kornievskaia test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags))) 153943245ecaSOlga Kornievskaia return 0; 154043245ecaSOlga Kornievskaia else 154143245ecaSOlga Kornievskaia return -ESTALE; 15421da177e4SLinus Torvalds out_force: 15431fa1e384SNeilBrown if (flags & LOOKUP_RCU) 15441fa1e384SNeilBrown return -ECHILD; 154565a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 154665a0c149STrond Myklebust if (ret != 0) 154765a0c149STrond Myklebust return ret; 154865a0c149STrond Myklebust goto out; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 155182e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode) 155282e7ca13STrond Myklebust { 155382e7ca13STrond Myklebust spin_lock(&inode->i_lock); 1554a6a361c4STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 155582e7ca13STrond Myklebust spin_unlock(&inode->i_lock); 155682e7ca13STrond Myklebust } 155782e7ca13STrond Myklebust 15581da177e4SLinus Torvalds /* 15591da177e4SLinus Torvalds * We judge how long we want to trust negative 15601da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 15611da177e4SLinus Torvalds * 15621da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 15631da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1564912a108dSNeilBrown * 1565912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1566912a108dSNeilBrown * suggesting a reval is needed. 15679f6d44d4STrond Myklebust * 15689f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 15699f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 15701da177e4SLinus Torvalds */ 15711da177e4SLinus Torvalds static inline 15721da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1573fa3c56bbSAl Viro unsigned int flags) 15741da177e4SLinus Torvalds { 15759f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 15761da177e4SLinus Torvalds return 0; 15774eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 15784eec952eSTrond Myklebust return 1; 157998ca3ee6STrond Myklebust /* Case insensitive server? Revalidate negative dentries */ 158098ca3ee6STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 158198ca3ee6STrond Myklebust return 1; 1582912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15855ceb9d7fSTrond Myklebust static int 15865ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 15875ceb9d7fSTrond Myklebust struct inode *inode, int error) 15881da177e4SLinus Torvalds { 15895ceb9d7fSTrond Myklebust switch (error) { 15905ceb9d7fSTrond Myklebust case 1: 15912eef8a31STrond Myklebust break; 15925ceb9d7fSTrond Myklebust case 0: 1593a3f432bfSJ. Bruce Fields /* 1594a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1595a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1596a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1597a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1598a3f432bfSJ. Bruce Fields */ 159947397915STrond Myklebust if (inode && IS_ROOT(dentry)) 16002eef8a31STrond Myklebust error = 1; 16012eef8a31STrond Myklebust break; 16025ceb9d7fSTrond Myklebust } 16032eef8a31STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error); 1604e1fb4d05STrond Myklebust return error; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16075ceb9d7fSTrond Myklebust static int 16085ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 16095ceb9d7fSTrond Myklebust unsigned int flags) 16105ceb9d7fSTrond Myklebust { 16115ceb9d7fSTrond Myklebust int ret = 1; 16125ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 16135ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 16145ceb9d7fSTrond Myklebust return -ECHILD; 16155ceb9d7fSTrond Myklebust ret = 0; 16165ceb9d7fSTrond Myklebust } 16175ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 16185ceb9d7fSTrond Myklebust } 16195ceb9d7fSTrond Myklebust 16205ceb9d7fSTrond Myklebust static int 16215ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 16225ceb9d7fSTrond Myklebust struct inode *inode) 16235ceb9d7fSTrond Myklebust { 16245ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16255ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 16265ceb9d7fSTrond Myklebust } 16275ceb9d7fSTrond Myklebust 16280b3cc71bSTrond Myklebust static int nfs_lookup_revalidate_dentry(struct inode *dir, 16290b3cc71bSTrond Myklebust struct dentry *dentry, 16300b3cc71bSTrond Myklebust struct inode *inode, unsigned int flags) 16315ceb9d7fSTrond Myklebust { 16325ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 16335ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 1634a1147b82STrond Myklebust unsigned long dir_verifier; 16355ceb9d7fSTrond Myklebust int ret; 16365ceb9d7fSTrond Myklebust 16370b3cc71bSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 16380b3cc71bSTrond Myklebust 16395ceb9d7fSTrond Myklebust ret = -ENOMEM; 16405ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 16419558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 16429558a007SAnna Schumaker if (fhandle == NULL || fattr == NULL) 16435ceb9d7fSTrond Myklebust goto out; 16445ceb9d7fSTrond Myklebust 1645a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 16469558a007SAnna Schumaker ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 16475ceb9d7fSTrond Myklebust if (ret < 0) { 1648f7b37b8bSTrond Myklebust switch (ret) { 1649f7b37b8bSTrond Myklebust case -ESTALE: 1650f7b37b8bSTrond Myklebust case -ENOENT: 16515ceb9d7fSTrond Myklebust ret = 0; 1652f7b37b8bSTrond Myklebust break; 1653f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1654f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1655f7b37b8bSTrond Myklebust ret = 1; 1656f7b37b8bSTrond Myklebust } 16575ceb9d7fSTrond Myklebust goto out; 16585ceb9d7fSTrond Myklebust } 16590b3cc71bSTrond Myklebust 16600b3cc71bSTrond Myklebust /* Request help from readdirplus */ 16610b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags); 16620b3cc71bSTrond Myklebust 16635ceb9d7fSTrond Myklebust ret = 0; 16645ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 16655ceb9d7fSTrond Myklebust goto out; 16665ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 16675ceb9d7fSTrond Myklebust goto out; 16685ceb9d7fSTrond Myklebust 1669dd225cb3SAnna Schumaker nfs_setsecurity(inode, fattr); 1670a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 16715ceb9d7fSTrond Myklebust 16725ceb9d7fSTrond Myklebust ret = 1; 16735ceb9d7fSTrond Myklebust out: 16745ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 16755ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 167682e7ca13STrond Myklebust 167782e7ca13STrond Myklebust /* 167882e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being 167982e7ca13STrond Myklebust * a match, then we should revalidate the directory cache. 168082e7ca13STrond Myklebust */ 16818ce37abdSTrond Myklebust if (!ret && nfs_dentry_verify_change(dir, dentry)) 168282e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 16835ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 16845ceb9d7fSTrond Myklebust } 16855ceb9d7fSTrond Myklebust 16865ceb9d7fSTrond Myklebust /* 16875ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 16885ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 16895ceb9d7fSTrond Myklebust * lookup. 16905ceb9d7fSTrond Myklebust * 16915ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 16925ceb9d7fSTrond Myklebust * we have an inode! 16935ceb9d7fSTrond Myklebust * 16945ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 16955ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 16965ceb9d7fSTrond Myklebust */ 16975ceb9d7fSTrond Myklebust static int 16985ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 16995ceb9d7fSTrond Myklebust unsigned int flags) 17005ceb9d7fSTrond Myklebust { 17015ceb9d7fSTrond Myklebust struct inode *inode; 17025ceb9d7fSTrond Myklebust int error; 17035ceb9d7fSTrond Myklebust 17045ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 17055ceb9d7fSTrond Myklebust inode = d_inode(dentry); 17065ceb9d7fSTrond Myklebust 17075ceb9d7fSTrond Myklebust if (!inode) 17085ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 17095ceb9d7fSTrond Myklebust 17105ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 17115ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 17125ceb9d7fSTrond Myklebust __func__, dentry); 17135ceb9d7fSTrond Myklebust goto out_bad; 17145ceb9d7fSTrond Myklebust } 17155ceb9d7fSTrond Myklebust 1716efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 17175ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 17185ceb9d7fSTrond Myklebust 17195ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 17205ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 17215ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 17225ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 17235ceb9d7fSTrond Myklebust if (error) { 17245ceb9d7fSTrond Myklebust if (error == -ESTALE) 172582e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 17265ceb9d7fSTrond Myklebust goto out_bad; 17275ceb9d7fSTrond Myklebust } 17285ceb9d7fSTrond Myklebust goto out_valid; 17295ceb9d7fSTrond Myklebust } 17305ceb9d7fSTrond Myklebust 17315ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 17325ceb9d7fSTrond Myklebust return -ECHILD; 17335ceb9d7fSTrond Myklebust 17345ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 17355ceb9d7fSTrond Myklebust goto out_bad; 17365ceb9d7fSTrond Myklebust 17370b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags); 17385ceb9d7fSTrond Myklebust out_valid: 17395ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 17405ceb9d7fSTrond Myklebust out_bad: 17415ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 17425ceb9d7fSTrond Myklebust return -ECHILD; 17435ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 17445ceb9d7fSTrond Myklebust } 17455ceb9d7fSTrond Myklebust 17465ceb9d7fSTrond Myklebust static int 1747c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1748c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 17495ceb9d7fSTrond Myklebust { 17505ceb9d7fSTrond Myklebust struct dentry *parent; 17515ceb9d7fSTrond Myklebust struct inode *dir; 17525ceb9d7fSTrond Myklebust int ret; 17535ceb9d7fSTrond Myklebust 17545ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 17555ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 17565ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 17575ceb9d7fSTrond Myklebust if (!dir) 17585ceb9d7fSTrond Myklebust return -ECHILD; 1759c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 17605ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 17615ceb9d7fSTrond Myklebust return -ECHILD; 17625ceb9d7fSTrond Myklebust } else { 17635ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1764c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 17655ceb9d7fSTrond Myklebust dput(parent); 17665ceb9d7fSTrond Myklebust } 17675ceb9d7fSTrond Myklebust return ret; 17685ceb9d7fSTrond Myklebust } 17695ceb9d7fSTrond Myklebust 1770c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1771c7944ebbSTrond Myklebust { 1772c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1773c7944ebbSTrond Myklebust } 1774c7944ebbSTrond Myklebust 17751da177e4SLinus Torvalds /* 17762b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1777ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1778ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1779ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1780ecf3d1f1SJeff Layton * 1781ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1782ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1783ecf3d1f1SJeff Layton */ 1784ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1785ecf3d1f1SJeff Layton { 17862b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 17879cdd1d3fSTrond Myklebust int error = 0; 1788ecf3d1f1SJeff Layton 1789ecf3d1f1SJeff Layton /* 1790ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1791ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1792ecf3d1f1SJeff Layton * eventually need to do something more here. 1793ecf3d1f1SJeff Layton */ 1794ecf3d1f1SJeff Layton if (!inode) { 17956de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 17966de1472fSAl Viro __func__, dentry); 1797ecf3d1f1SJeff Layton return 1; 1798ecf3d1f1SJeff Layton } 1799ecf3d1f1SJeff Layton 1800ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 18016de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 18026de1472fSAl Viro __func__, dentry); 1803ecf3d1f1SJeff Layton return 0; 1804ecf3d1f1SJeff Layton } 1805ecf3d1f1SJeff Layton 1806b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1807ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1808ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1809ecf3d1f1SJeff Layton return !error; 1810ecf3d1f1SJeff Layton } 1811ecf3d1f1SJeff Layton 1812ecf3d1f1SJeff Layton /* 18131da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 18141da177e4SLinus Torvalds */ 1815fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 18161da177e4SLinus Torvalds { 18176de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 18186de1472fSAl Viro dentry, dentry->d_flags); 18191da177e4SLinus Torvalds 182077f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 18212b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 182277f11192STrond Myklebust return 1; 182377f11192STrond Myklebust 18241da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18251da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 18261da177e4SLinus Torvalds return 1; 18271da177e4SLinus Torvalds } 18281751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 18291da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 18301da177e4SLinus Torvalds * files will be cleaned up during umount */ 18311da177e4SLinus Torvalds return 1; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds return 0; 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 18371f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 18381b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 18391b83d707STrond Myklebust { 18401b83d707STrond Myklebust spin_lock(&inode->i_lock); 18411f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 184259a707b0STrond Myklebust if (inode->i_nlink > 0) 184359a707b0STrond Myklebust drop_nlink(inode); 184459a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1845ac46b3d7STrond Myklebust nfs_set_cache_invalid( 1846ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 18471301e421STrond Myklebust NFS_INO_INVALID_NLINK); 18481b83d707STrond Myklebust spin_unlock(&inode->i_lock); 18491b83d707STrond Myklebust } 18501b83d707STrond Myklebust 18511da177e4SLinus Torvalds /* 18521da177e4SLinus Torvalds * Called when the dentry loses inode. 18531da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 18541da177e4SLinus Torvalds */ 18551da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1858e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 18591f018458STrond Myklebust nfs_drop_nlink(inode); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds iput(inode); 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds 1864b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1865b1942c5fSAl Viro { 1866b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1867b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1868b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1869b1942c5fSAl Viro WARN_ON(1); 1870b1942c5fSAl Viro else 1871b1942c5fSAl Viro kfree(dentry->d_fsdata); 1872b1942c5fSAl Viro } 1873b1942c5fSAl Viro } 1874b1942c5fSAl Viro 1875f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 18761da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1877ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 18781da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 18791da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 188036d43a43SDavid Howells .d_automount = nfs_d_automount, 1881b1942c5fSAl Viro .d_release = nfs_d_release, 18821da177e4SLinus Torvalds }; 1883ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 18841da177e4SLinus Torvalds 1885597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 18861da177e4SLinus Torvalds { 18871da177e4SLinus Torvalds struct dentry *res; 18881da177e4SLinus Torvalds struct inode *inode = NULL; 1889e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1890e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 1891a1147b82STrond Myklebust unsigned long dir_verifier; 18921da177e4SLinus Torvalds int error; 18931da177e4SLinus Torvalds 18946de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 189591d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 18961da177e4SLinus Torvalds 1897130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1898130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 18991da177e4SLinus Torvalds 1900fd684071STrond Myklebust /* 1901fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1902fd684071STrond Myklebust * but don't hash the dentry. 1903fd684071STrond Myklebust */ 19049f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1905130f9ab7SAl Viro return NULL; 19061da177e4SLinus Torvalds 1907e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1908e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 19099558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir)); 1910e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1911e1fb4d05STrond Myklebust goto out; 1912e1fb4d05STrond Myklebust 1913a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 19146e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 19159558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 19168ce37abdSTrond Myklebust if (error == -ENOENT) { 19178ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 19188ce37abdSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 19191da177e4SLinus Torvalds goto no_entry; 19208ce37abdSTrond Myklebust } 19211da177e4SLinus Torvalds if (error < 0) { 19221da177e4SLinus Torvalds res = ERR_PTR(error); 19239558a007SAnna Schumaker goto out; 19241da177e4SLinus Torvalds } 1925cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1926bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 192703f28e3aSTrond Myklebust if (IS_ERR(res)) 19289558a007SAnna Schumaker goto out; 192954ceac45SDavid Howells 193063519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 19310b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags); 1932d69ee9b8STrond Myklebust 19331da177e4SLinus Torvalds no_entry: 193441d28bcaSAl Viro res = d_splice_alias(inode, dentry); 19359eaef27bSTrond Myklebust if (res != NULL) { 19369eaef27bSTrond Myklebust if (IS_ERR(res)) 19379558a007SAnna Schumaker goto out; 19381da177e4SLinus Torvalds dentry = res; 19399eaef27bSTrond Myklebust } 1940a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 19411da177e4SLinus Torvalds out: 19429558a007SAnna Schumaker trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res)); 1943e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1944e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 19451da177e4SLinus Torvalds return res; 19461da177e4SLinus Torvalds } 1947ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 19481da177e4SLinus Torvalds 194900bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode) 195000bdadc7STrond Myklebust { 195100bdadc7STrond Myklebust /* Case insensitive server? Revalidate dentries */ 195200bdadc7STrond Myklebust if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE)) 195300bdadc7STrond Myklebust d_prune_aliases(inode); 195400bdadc7STrond Myklebust } 195500bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases); 195600bdadc7STrond Myklebust 195789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 19580b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 19591da177e4SLinus Torvalds 1960f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 19610ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1962b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 19631da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 19641da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 196536d43a43SDavid Howells .d_automount = nfs_d_automount, 1966b1942c5fSAl Viro .d_release = nfs_d_release, 19671da177e4SLinus Torvalds }; 196889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 19691da177e4SLinus Torvalds 19708a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 19718a5e929dSAl Viro { 19728a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 19738a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 19748a5e929dSAl Viro res |= FMODE_READ; 19758a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 19768a5e929dSAl Viro res |= FMODE_WRITE; 19778a5e929dSAl Viro return res; 19788a5e929dSAl Viro } 19798a5e929dSAl Viro 1980532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1981cd9a1c0eSTrond Myklebust { 1982532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1983cd9a1c0eSTrond Myklebust } 1984cd9a1c0eSTrond Myklebust 1985cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1986cd9a1c0eSTrond Myklebust { 1987f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1988cd9a1c0eSTrond Myklebust return 0; 1989cd9a1c0eSTrond Myklebust } 1990cd9a1c0eSTrond Myklebust 1991d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 19920dd2b474SMiklos Szeredi struct dentry *dentry, 1993b452a458SAl Viro struct file *file, unsigned open_flags) 1994cd9a1c0eSTrond Myklebust { 19950dd2b474SMiklos Szeredi int err; 19960dd2b474SMiklos Szeredi 1997be12af3eSAl Viro err = finish_open(file, dentry, do_open); 199830d90494SAl Viro if (err) 1999d9585277SAl Viro goto out; 2000eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 200130d90494SAl Viro nfs_file_set_open_context(file, ctx); 2002eaa2b82cSNeilBrown else 20039821421aSTrond Myklebust err = -EOPENSTALE; 2004cd9a1c0eSTrond Myklebust out: 2005d9585277SAl Viro return err; 2006cd9a1c0eSTrond Myklebust } 2007cd9a1c0eSTrond Myklebust 200873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 200930d90494SAl Viro struct file *file, unsigned open_flags, 201044907d79SAl Viro umode_t mode) 20111da177e4SLinus Torvalds { 2012c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 2013cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 20140dd2b474SMiklos Szeredi struct dentry *res; 20150dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 2016f46e0bd3STrond Myklebust struct inode *inode; 20171472b83eSTrond Myklebust unsigned int lookup_flags = 0; 201868eaba4cSTrond Myklebust unsigned long dir_verifier; 2019c94c0953SAl Viro bool switched = false; 202073a09dd9SAl Viro int created = 0; 2021898f635cSTrond Myklebust int err; 20221da177e4SLinus Torvalds 20230dd2b474SMiklos Szeredi /* Expect a negative dentry */ 20242b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 20250dd2b474SMiklos Szeredi 20261e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 20276de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20281e7cb3dcSChuck Lever 20299597c13bSJeff Layton err = nfs_check_flags(open_flags); 20309597c13bSJeff Layton if (err) 20319597c13bSJeff Layton return err; 20329597c13bSJeff Layton 20330dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 20340dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 203500699ad8SAl Viro if (!d_in_lookup(dentry)) { 20360dd2b474SMiklos Szeredi /* 20370dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 20380dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 20390dd2b474SMiklos Szeredi * again 20400dd2b474SMiklos Szeredi */ 2041d9585277SAl Viro return -ENOENT; 20420dd2b474SMiklos Szeredi } 20431472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 20441da177e4SLinus Torvalds goto no_open; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 20470dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 2048d9585277SAl Viro return -ENAMETOOLONG; 20491da177e4SLinus Torvalds 20500dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 2051dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 2052dff25ddbSAndreas Gruenbacher 2053dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 2054dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 2055dff25ddbSAndreas Gruenbacher 2056536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 2057dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 20580dd2b474SMiklos Szeredi } 2059536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 2060536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 2061536e43d1STrond Myklebust attr.ia_size = 0; 2062cd9a1c0eSTrond Myklebust } 2063cd9a1c0eSTrond Myklebust 2064c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 2065c94c0953SAl Viro d_drop(dentry); 2066c94c0953SAl Viro switched = true; 2067c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 2068c94c0953SAl Viro &dentry->d_name, &wq); 2069c94c0953SAl Viro if (IS_ERR(dentry)) 2070c94c0953SAl Viro return PTR_ERR(dentry); 2071c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 2072c94c0953SAl Viro return finish_no_open(file, dentry); 2073c94c0953SAl Viro } 2074c94c0953SAl Viro 2075532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 20760dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 20770dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 2078d9585277SAl Viro goto out; 20790dd2b474SMiklos Szeredi 20806e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 208173a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 208273a09dd9SAl Viro if (created) 208373a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 2084275bb307STrond Myklebust if (IS_ERR(inode)) { 20850dd2b474SMiklos Szeredi err = PTR_ERR(inode); 20866e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 20872d9db750STrond Myklebust put_nfs_open_context(ctx); 2088d20cb71dSAl Viro d_drop(dentry); 20890dd2b474SMiklos Szeredi switch (err) { 20901da177e4SLinus Torvalds case -ENOENT: 2091774d9513SPeng Tao d_splice_alias(NULL, dentry); 209268eaba4cSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 209368eaba4cSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 209468eaba4cSTrond Myklebust else 209568eaba4cSTrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 209668eaba4cSTrond Myklebust nfs_set_verifier(dentry, dir_verifier); 20970dd2b474SMiklos Szeredi break; 20981788ea6eSJeff Layton case -EISDIR: 20996f926b5bSTrond Myklebust case -ENOTDIR: 21006f926b5bSTrond Myklebust goto no_open; 21011da177e4SLinus Torvalds case -ELOOP: 21020dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 21031da177e4SLinus Torvalds goto no_open; 21040dd2b474SMiklos Szeredi break; 21051da177e4SLinus Torvalds /* case -EINVAL: */ 21061da177e4SLinus Torvalds default: 21070dd2b474SMiklos Szeredi break; 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds goto out; 21101da177e4SLinus Torvalds } 21110dd2b474SMiklos Szeredi 2112b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 21136e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 21142d9db750STrond Myklebust put_nfs_open_context(ctx); 2115d9585277SAl Viro out: 2116c94c0953SAl Viro if (unlikely(switched)) { 2117c94c0953SAl Viro d_lookup_done(dentry); 2118c94c0953SAl Viro dput(dentry); 2119c94c0953SAl Viro } 2120d9585277SAl Viro return err; 21210dd2b474SMiklos Szeredi 21221da177e4SLinus Torvalds no_open: 21231472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 2124ac795161STrond Myklebust if (!res) { 2125ac795161STrond Myklebust inode = d_inode(dentry); 2126ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2127e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) 2128ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 21291751fc1dSTrond Myklebust else if (inode && S_ISREG(inode->i_mode)) 21301751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2131ac795161STrond Myklebust } else if (!IS_ERR(res)) { 2132ac795161STrond Myklebust inode = d_inode(res); 2133ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2134e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) { 2135ac795161STrond Myklebust dput(res); 2136ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 21371751fc1dSTrond Myklebust } else if (inode && S_ISREG(inode->i_mode)) { 21381751fc1dSTrond Myklebust dput(res); 21391751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2140ac795161STrond Myklebust } 2141ac795161STrond Myklebust } 2142c94c0953SAl Viro if (switched) { 2143c94c0953SAl Viro d_lookup_done(dentry); 2144c94c0953SAl Viro if (!res) 2145c94c0953SAl Viro res = dentry; 2146c94c0953SAl Viro else 2147c94c0953SAl Viro dput(dentry); 2148c94c0953SAl Viro } 21490dd2b474SMiklos Szeredi if (IS_ERR(res)) 2150c94c0953SAl Viro return PTR_ERR(res); 2151e45198a6SAl Viro return finish_no_open(file, res); 21521da177e4SLinus Torvalds } 215389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 21541da177e4SLinus Torvalds 2155c7944ebbSTrond Myklebust static int 2156c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 2157c7944ebbSTrond Myklebust unsigned int flags) 21581da177e4SLinus Torvalds { 2159657e94b6SNick Piggin struct inode *inode; 21601da177e4SLinus Torvalds 2161fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 2162c7944ebbSTrond Myklebust goto full_reval; 2163eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 2164c7944ebbSTrond Myklebust goto full_reval; 21652b484297STrond Myklebust 21662b0143b5SDavid Howells inode = d_inode(dentry); 21672b484297STrond Myklebust 21681da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 21691da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 21701da177e4SLinus Torvalds */ 2171c7944ebbSTrond Myklebust if (inode == NULL) 2172c7944ebbSTrond Myklebust goto full_reval; 217349317a7fSNeilBrown 2174efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 2175c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2176216d5d06STrond Myklebust 21771da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 21781da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 2179c7944ebbSTrond Myklebust goto full_reval; 2180c7944ebbSTrond Myklebust 21811da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 2182c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2183c7944ebbSTrond Myklebust goto reval_dentry; 2184c7944ebbSTrond Myklebust 2185c7944ebbSTrond Myklebust /* Check if the directory changed */ 2186c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2187c7944ebbSTrond Myklebust goto reval_dentry; 21881da177e4SLinus Torvalds 21890ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 2190c7944ebbSTrond Myklebust return 1; 2191c7944ebbSTrond Myklebust reval_dentry: 2192c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 2193c7944ebbSTrond Myklebust return -ECHILD; 21940b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags); 21950ef97dcfSMiklos Szeredi 2196c7944ebbSTrond Myklebust full_reval: 2197c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 2198c7944ebbSTrond Myklebust } 2199535918f1STrond Myklebust 2200c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2201c7944ebbSTrond Myklebust { 2202c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 2203c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 2204c0204fd2STrond Myklebust } 2205c0204fd2STrond Myklebust 22061da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 22071da177e4SLinus Torvalds 2208406cd915SBenjamin Coddington struct dentry * 2209406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 2210cc6f3298SAnna Schumaker struct nfs_fattr *fattr) 22111da177e4SLinus Torvalds { 2212fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 22132b0143b5SDavid Howells struct inode *dir = d_inode(parent); 22141da177e4SLinus Torvalds struct inode *inode; 2215b0c6108eSAl Viro struct dentry *d; 2216406cd915SBenjamin Coddington int error; 22171da177e4SLinus Torvalds 2218fab728e1STrond Myklebust d_drop(dentry); 2219fab728e1STrond Myklebust 22201da177e4SLinus Torvalds if (fhandle->size == 0) { 22219558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 22221da177e4SLinus Torvalds if (error) 2223fab728e1STrond Myklebust goto out_error; 22241da177e4SLinus Torvalds } 22255724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 22261da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 22271da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 2228a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 22292ef61e0eSAnna Schumaker fattr, NULL); 22301da177e4SLinus Torvalds if (error < 0) 2231fab728e1STrond Myklebust goto out_error; 22321da177e4SLinus Torvalds } 2233cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 2234b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 2235fab728e1STrond Myklebust out: 2236fab728e1STrond Myklebust dput(parent); 2237406cd915SBenjamin Coddington return d; 2238fab728e1STrond Myklebust out_error: 2239406cd915SBenjamin Coddington d = ERR_PTR(error); 2240406cd915SBenjamin Coddington goto out; 2241406cd915SBenjamin Coddington } 2242406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2243406cd915SBenjamin Coddington 2244406cd915SBenjamin Coddington /* 2245406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 2246406cd915SBenjamin Coddington */ 2247406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2248d91bfc46SAnna Schumaker struct nfs_fattr *fattr) 2249406cd915SBenjamin Coddington { 2250406cd915SBenjamin Coddington struct dentry *d; 2251406cd915SBenjamin Coddington 2252cc6f3298SAnna Schumaker d = nfs_add_or_obtain(dentry, fhandle, fattr); 2253406cd915SBenjamin Coddington if (IS_ERR(d)) 2254406cd915SBenjamin Coddington return PTR_ERR(d); 2255406cd915SBenjamin Coddington 2256406cd915SBenjamin Coddington /* Callers don't care */ 2257406cd915SBenjamin Coddington dput(d); 2258406cd915SBenjamin Coddington return 0; 22591da177e4SLinus Torvalds } 2260ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds /* 22631da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 22641da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 22651da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 22661da177e4SLinus Torvalds * reply path made it appear to have failed. 22671da177e4SLinus Torvalds */ 2268549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2269549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 22701da177e4SLinus Torvalds { 22711da177e4SLinus Torvalds struct iattr attr; 2272ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 22731da177e4SLinus Torvalds int error; 22741da177e4SLinus Torvalds 22751e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 22766de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds attr.ia_mode = mode; 22791da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 22801da177e4SLinus Torvalds 22818b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 22828867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 22838b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 22841da177e4SLinus Torvalds if (error != 0) 22851da177e4SLinus Torvalds goto out_err; 22861da177e4SLinus Torvalds return 0; 22871da177e4SLinus Torvalds out_err: 22881da177e4SLinus Torvalds d_drop(dentry); 22891da177e4SLinus Torvalds return error; 22901da177e4SLinus Torvalds } 2291ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds /* 22941da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 22951da177e4SLinus Torvalds */ 2296597d9289SBryan Schumaker int 2297549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2298549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev) 22991da177e4SLinus Torvalds { 23001da177e4SLinus Torvalds struct iattr attr; 23011da177e4SLinus Torvalds int status; 23021da177e4SLinus Torvalds 23031e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 23046de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds attr.ia_mode = mode; 23071da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 23081da177e4SLinus Torvalds 23091ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 23101da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 23111ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 23121da177e4SLinus Torvalds if (status != 0) 23131da177e4SLinus Torvalds goto out_err; 23141da177e4SLinus Torvalds return 0; 23151da177e4SLinus Torvalds out_err: 23161da177e4SLinus Torvalds d_drop(dentry); 23171da177e4SLinus Torvalds return status; 23181da177e4SLinus Torvalds } 2319ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds /* 23221da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 23231da177e4SLinus Torvalds */ 2324549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2325549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 23261da177e4SLinus Torvalds { 23271da177e4SLinus Torvalds struct iattr attr; 23281da177e4SLinus Torvalds int error; 23291da177e4SLinus Torvalds 23301e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 23316de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23321da177e4SLinus Torvalds 23331da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 23341da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 23351da177e4SLinus Torvalds 23361ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 23371da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 23381ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 23391da177e4SLinus Torvalds if (error != 0) 23401da177e4SLinus Torvalds goto out_err; 23411da177e4SLinus Torvalds return 0; 23421da177e4SLinus Torvalds out_err: 23431da177e4SLinus Torvalds d_drop(dentry); 23441da177e4SLinus Torvalds return error; 23451da177e4SLinus Torvalds } 2346ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 23471da177e4SLinus Torvalds 2348d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2349d45b9d8bSTrond Myklebust { 2350dc3f4198SAl Viro if (simple_positive(dentry)) 2351d45b9d8bSTrond Myklebust d_delete(dentry); 2352d45b9d8bSTrond Myklebust } 2353d45b9d8bSTrond Myklebust 23549019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir, 23559019fb39STrond Myklebust struct dentry *dentry, int error) 23569019fb39STrond Myklebust { 23579019fb39STrond Myklebust switch (error) { 23589019fb39STrond Myklebust case -ENOENT: 23599019fb39STrond Myklebust d_delete(dentry); 236000bdadc7STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 236100bdadc7STrond Myklebust break; 23629019fb39STrond Myklebust case 0: 236300bdadc7STrond Myklebust nfs_d_prune_case_insensitive_aliases(d_inode(dentry)); 23649019fb39STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 23659019fb39STrond Myklebust } 23669019fb39STrond Myklebust } 23679019fb39STrond Myklebust 2368597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 23691da177e4SLinus Torvalds { 23701da177e4SLinus Torvalds int error; 23711da177e4SLinus Torvalds 23721e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 23736de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23741da177e4SLinus Torvalds 23751ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 23762b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2377884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 23781da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 23791da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2380ba6c0592STrond Myklebust switch (error) { 2381ba6c0592STrond Myklebust case 0: 23822b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2383ba6c0592STrond Myklebust break; 2384ba6c0592STrond Myklebust case -ENOENT: 2385d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2386ba6c0592STrond Myklebust } 2387884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2388ba6c0592STrond Myklebust } else 2389ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 23909019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 23911ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds return error; 23941da177e4SLinus Torvalds } 2395ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds /* 23981da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 23991da177e4SLinus Torvalds * and after checking that the file has only one user. 24001da177e4SLinus Torvalds * 24011da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 24021da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 24031da177e4SLinus Torvalds */ 24041da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 24051da177e4SLinus Torvalds { 24062b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 24072b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 24081da177e4SLinus Torvalds int error = -EBUSY; 24091da177e4SLinus Torvalds 24106de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 24131da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 24141da177e4SLinus Torvalds error = 0; 24151da177e4SLinus Torvalds goto out; 24161da177e4SLinus Torvalds } 24171da177e4SLinus Torvalds 24181ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 24191da177e4SLinus Torvalds if (inode != NULL) { 2420912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 24211da177e4SLinus Torvalds if (error == 0) 24221b83d707STrond Myklebust nfs_drop_nlink(inode); 24231da177e4SLinus Torvalds } else 2424912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2425d45b9d8bSTrond Myklebust if (error == -ENOENT) 2426d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 24271ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 24281da177e4SLinus Torvalds out: 24291da177e4SLinus Torvalds return error; 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 24331da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 24341da177e4SLinus Torvalds * 24351da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 24361da177e4SLinus Torvalds */ 2437597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 24381da177e4SLinus Torvalds { 24391da177e4SLinus Torvalds int error; 24401da177e4SLinus Torvalds int need_rehash = 0; 24411da177e4SLinus Torvalds 24421e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 24436de1472fSAl Viro dir->i_ino, dentry); 24441da177e4SLinus Torvalds 24451ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 24461da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 244743245ecaSOlga Kornievskaia if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED, 244843245ecaSOlga Kornievskaia &NFS_I(d_inode(dentry))->flags)) { 24491da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2450ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 24512b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 24521da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 24531ca42382STrond Myklebust goto out; 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 24561da177e4SLinus Torvalds __d_drop(dentry); 24571da177e4SLinus Torvalds need_rehash = 1; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 24601da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 24619019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 24629019fb39STrond Myklebust if (need_rehash) 24631da177e4SLinus Torvalds d_rehash(dentry); 24641ca42382STrond Myklebust out: 24651ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 24661da177e4SLinus Torvalds return error; 24671da177e4SLinus Torvalds } 2468ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 24691da177e4SLinus Torvalds 2470873101b3SChuck Lever /* 2471873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2472873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2473873101b3SChuck Lever * using prepare_write/commit_write. 2474873101b3SChuck Lever * 2475873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2476873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2477873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2478873101b3SChuck Lever * symlink request has completed on the server. 2479873101b3SChuck Lever * 2480873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2481873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2482873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2483873101b3SChuck Lever * and move the raw page into its mapping. 2484873101b3SChuck Lever */ 2485549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 2486549c7297SChristian Brauner struct dentry *dentry, const char *symname) 24871da177e4SLinus Torvalds { 2488873101b3SChuck Lever struct page *page; 2489873101b3SChuck Lever char *kaddr; 24901da177e4SLinus Torvalds struct iattr attr; 2491873101b3SChuck Lever unsigned int pathlen = strlen(symname); 24921da177e4SLinus Torvalds int error; 24931da177e4SLinus Torvalds 24941e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 24956de1472fSAl Viro dir->i_ino, dentry, symname); 24961da177e4SLinus Torvalds 2497873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2498873101b3SChuck Lever return -ENAMETOOLONG; 24991da177e4SLinus Torvalds 2500873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2501873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 25021da177e4SLinus Torvalds 2503e8ecde25SAl Viro page = alloc_page(GFP_USER); 250476566991STrond Myklebust if (!page) 2505873101b3SChuck Lever return -ENOMEM; 2506873101b3SChuck Lever 2507e8ecde25SAl Viro kaddr = page_address(page); 2508873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2509873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2510873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2511873101b3SChuck Lever 25121ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 251394a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 25141ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2515873101b3SChuck Lever if (error != 0) { 25161e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2517873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 25186de1472fSAl Viro dentry, symname, error); 25191da177e4SLinus Torvalds d_drop(dentry); 2520873101b3SChuck Lever __free_page(page); 25211da177e4SLinus Torvalds return error; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 2524342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2525342a67f0STrond Myklebust 2526873101b3SChuck Lever /* 2527873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2528873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2529873101b3SChuck Lever */ 25302b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2531873101b3SChuck Lever GFP_KERNEL)) { 2532873101b3SChuck Lever SetPageUptodate(page); 2533873101b3SChuck Lever unlock_page(page); 2534a0b54addSRafael Aquini /* 2535a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2536a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2537a0b54addSRafael Aquini */ 253809cbfeafSKirill A. Shutemov put_page(page); 2539873101b3SChuck Lever } else 2540873101b3SChuck Lever __free_page(page); 2541873101b3SChuck Lever 2542873101b3SChuck Lever return 0; 2543873101b3SChuck Lever } 2544ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2545873101b3SChuck Lever 2546597d9289SBryan Schumaker int 25471da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 25481da177e4SLinus Torvalds { 25492b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 25501da177e4SLinus Torvalds int error; 25511da177e4SLinus Torvalds 25526de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 25536de1472fSAl Viro old_dentry, dentry); 25541da177e4SLinus Torvalds 25551fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 25569697d234STrond Myklebust d_drop(dentry); 255720497503STrond Myklebust if (S_ISREG(inode->i_mode)) 255820497503STrond Myklebust nfs_sync_inode(inode); 25591da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2560cf809556STrond Myklebust if (error == 0) { 2561342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 25627de9c6eeSAl Viro ihold(inode); 25639697d234STrond Myklebust d_add(dentry, inode); 2564cf809556STrond Myklebust } 25651fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 25661da177e4SLinus Torvalds return error; 25671da177e4SLinus Torvalds } 2568ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds /* 25711da177e4SLinus Torvalds * RENAME 25721da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 25731da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 25741da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 25751da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 25761da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 25771da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 25781da177e4SLinus Torvalds * 25791da177e4SLinus Torvalds * FIXED. 25801da177e4SLinus Torvalds * 25811da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 25821da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 25831da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 25841da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 25851da177e4SLinus Torvalds * using the inode layer 25861da177e4SLinus Torvalds * 25871da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 25881da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 25891da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 25901da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 25911da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 25921da177e4SLinus Torvalds * the rename. 25931da177e4SLinus Torvalds */ 2594549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2595549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 2596549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 25971da177e4SLinus Torvalds { 25982b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 25992b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2600d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 260180a491fdSJeff Layton struct rpc_task *task; 26021da177e4SLinus Torvalds int error = -EBUSY; 26031da177e4SLinus Torvalds 26041cd66c93SMiklos Szeredi if (flags) 26051cd66c93SMiklos Szeredi return -EINVAL; 26061cd66c93SMiklos Szeredi 26076de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 26086de1472fSAl Viro old_dentry, new_dentry, 260984d08fa8SAl Viro d_count(new_dentry)); 26101da177e4SLinus Torvalds 261170ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 26121da177e4SLinus Torvalds /* 261328f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 261428f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 261528f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 261628f79a1aSMiklos Szeredi * the new target. 26171da177e4SLinus Torvalds */ 261827226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 261927226104SMiklos Szeredi /* 262027226104SMiklos Szeredi * To prevent any new references to the target during the 262127226104SMiklos Szeredi * rename, we unhash the dentry in advance. 262227226104SMiklos Szeredi */ 2623d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 262427226104SMiklos Szeredi d_drop(new_dentry); 2625d9f29500SBenjamin Coddington rehash = new_dentry; 2626d9f29500SBenjamin Coddington } 262727226104SMiklos Szeredi 262884d08fa8SAl Viro if (d_count(new_dentry) > 2) { 26291da177e4SLinus Torvalds int err; 263027226104SMiklos Szeredi 26311da177e4SLinus Torvalds /* copy the target dentry's name */ 26321da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 26331da177e4SLinus Torvalds &new_dentry->d_name); 26341da177e4SLinus Torvalds if (!dentry) 26351da177e4SLinus Torvalds goto out; 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds /* silly-rename the existing target ... */ 26381da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 263924e93025SMiklos Szeredi if (err) 26401da177e4SLinus Torvalds goto out; 264124e93025SMiklos Szeredi 264224e93025SMiklos Szeredi new_dentry = dentry; 2643d9f29500SBenjamin Coddington rehash = NULL; 264424e93025SMiklos Szeredi new_inode = NULL; 2645b1e4adf4STrond Myklebust } 264627226104SMiklos Szeredi } 26471da177e4SLinus Torvalds 26486ff9d99bSTrond Myklebust if (S_ISREG(old_inode->i_mode)) 26496ff9d99bSTrond Myklebust nfs_sync_inode(old_inode); 2650d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 265180a491fdSJeff Layton if (IS_ERR(task)) { 265280a491fdSJeff Layton error = PTR_ERR(task); 265380a491fdSJeff Layton goto out; 265480a491fdSJeff Layton } 265580a491fdSJeff Layton 265680a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2657818a8dbeSBenjamin Coddington if (error != 0) { 2658818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2659818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2660818a8dbeSBenjamin Coddington smp_wmb(); 2661818a8dbeSBenjamin Coddington } else 266280a491fdSJeff Layton error = task->tk_status; 266380a491fdSJeff Layton rpc_put_task(task); 266459a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 266559a707b0STrond Myklebust if (error == 0) { 266659a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 266759a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2668ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2669ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME | 2670ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 267159a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 267259a707b0STrond Myklebust } 26731da177e4SLinus Torvalds out: 2674d9f29500SBenjamin Coddington if (rehash) 2675d9f29500SBenjamin Coddington d_rehash(rehash); 267670ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 267770ded201STrond Myklebust new_dir, new_dentry, error); 2678d9f29500SBenjamin Coddington if (!error) { 2679d9f29500SBenjamin Coddington if (new_inode != NULL) 2680d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2681d9f29500SBenjamin Coddington /* 2682d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2683d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2684d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2685d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2686d9f29500SBenjamin Coddington */ 2687d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2688d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2689d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2690d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2691d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2692d9f29500SBenjamin Coddington 26931da177e4SLinus Torvalds /* new dentry created? */ 26941da177e4SLinus Torvalds if (dentry) 26951da177e4SLinus Torvalds dput(dentry); 26961da177e4SLinus Torvalds return error; 26971da177e4SLinus Torvalds } 2698ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 26991da177e4SLinus Torvalds 2700cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2701cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2702cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2703cfcea3e8STrond Myklebust 2704a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 27053a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 27063a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 27073a505845STrond Myklebust 27081c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 27091c3c07e9STrond Myklebust { 27106238aec8SNeilBrown put_group_info(entry->group_info); 2711f682a398SNeilBrown kfree_rcu(entry, rcu_head); 27124e857c58SPeter Zijlstra smp_mb__before_atomic(); 2713cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 27144e857c58SPeter Zijlstra smp_mb__after_atomic(); 27151c3c07e9STrond Myklebust } 27161c3c07e9STrond Myklebust 27171a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 27181a81bb8aSTrond Myklebust { 27191a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 27201a81bb8aSTrond Myklebust 27211a81bb8aSTrond Myklebust while (!list_empty(head)) { 27221a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 27231a81bb8aSTrond Myklebust list_del(&cache->lru); 27241a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 27251a81bb8aSTrond Myklebust } 27261a81bb8aSTrond Myklebust } 27271a81bb8aSTrond Myklebust 27283a505845STrond Myklebust static unsigned long 27293a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2730979df72eSTrond Myklebust { 2731979df72eSTrond Myklebust LIST_HEAD(head); 2732aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2733979df72eSTrond Myklebust struct nfs_access_entry *cache; 27341ab6c499SDave Chinner long freed = 0; 2735979df72eSTrond Myklebust 2736a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2737aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2738979df72eSTrond Myklebust struct inode *inode; 2739979df72eSTrond Myklebust 2740979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2741979df72eSTrond Myklebust break; 27429c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2743979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2744979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2745979df72eSTrond Myklebust goto remove_lru_entry; 2746979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2747979df72eSTrond Myklebust struct nfs_access_entry, lru); 2748979df72eSTrond Myklebust list_move(&cache->lru, &head); 2749979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 27501ab6c499SDave Chinner freed++; 2751979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2752979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2753979df72eSTrond Myklebust &nfs_access_lru_list); 2754979df72eSTrond Myklebust else { 2755979df72eSTrond Myklebust remove_lru_entry: 2756979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 27574e857c58SPeter Zijlstra smp_mb__before_atomic(); 2758979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 27594e857c58SPeter Zijlstra smp_mb__after_atomic(); 2760979df72eSTrond Myklebust } 276159844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2762979df72eSTrond Myklebust } 2763979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 27641a81bb8aSTrond Myklebust nfs_access_free_list(&head); 27651ab6c499SDave Chinner return freed; 27661ab6c499SDave Chinner } 27671ab6c499SDave Chinner 27681ab6c499SDave Chinner unsigned long 27693a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 27703a505845STrond Myklebust { 27713a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 27723a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 27733a505845STrond Myklebust 27743a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 27753a505845STrond Myklebust return SHRINK_STOP; 27763a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 27773a505845STrond Myklebust } 27783a505845STrond Myklebust 27793a505845STrond Myklebust 27803a505845STrond Myklebust unsigned long 27811ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 27821ab6c499SDave Chinner { 278355f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2784979df72eSTrond Myklebust } 2785979df72eSTrond Myklebust 27863a505845STrond Myklebust static void 27873a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 27883a505845STrond Myklebust { 27893a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 27903a505845STrond Myklebust unsigned long diff; 27913a505845STrond Myklebust unsigned int nr_to_scan; 27923a505845STrond Myklebust 27933a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 27943a505845STrond Myklebust return; 27953a505845STrond Myklebust nr_to_scan = 100; 27963a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 27973a505845STrond Myklebust if (diff < nr_to_scan) 27983a505845STrond Myklebust nr_to_scan = diff; 27993a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 28003a505845STrond Myklebust } 28013a505845STrond Myklebust 28021a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 28031c3c07e9STrond Myklebust { 28041c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 28051a81bb8aSTrond Myklebust struct rb_node *n; 28061c3c07e9STrond Myklebust struct nfs_access_entry *entry; 28071c3c07e9STrond Myklebust 28081c3c07e9STrond Myklebust /* Unhook entries from the cache */ 28091c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 28101c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 28111c3c07e9STrond Myklebust rb_erase(n, root_node); 28121a81bb8aSTrond Myklebust list_move(&entry->lru, head); 28131c3c07e9STrond Myklebust } 28141c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 28151c3c07e9STrond Myklebust } 28161c3c07e9STrond Myklebust 28171c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 28181c3c07e9STrond Myklebust { 28191a81bb8aSTrond Myklebust LIST_HEAD(head); 28201a81bb8aSTrond Myklebust 28211a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 28221a81bb8aSTrond Myklebust return; 2823cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2824cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 28251a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2826cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2827cfcea3e8STrond Myklebust 28281c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 28291a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 28301a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 28311a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 28321a81bb8aSTrond Myklebust nfs_access_free_list(&head); 28331c3c07e9STrond Myklebust } 28341c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 28351c3c07e9STrond Myklebust 28366238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b) 28376238aec8SNeilBrown { 28386238aec8SNeilBrown struct group_info *ga, *gb; 28396238aec8SNeilBrown int g; 28406238aec8SNeilBrown 28416238aec8SNeilBrown if (uid_lt(a->fsuid, b->fsuid)) 28426238aec8SNeilBrown return -1; 28436238aec8SNeilBrown if (uid_gt(a->fsuid, b->fsuid)) 28446238aec8SNeilBrown return 1; 28456238aec8SNeilBrown 28466238aec8SNeilBrown if (gid_lt(a->fsgid, b->fsgid)) 28476238aec8SNeilBrown return -1; 28486238aec8SNeilBrown if (gid_gt(a->fsgid, b->fsgid)) 28496238aec8SNeilBrown return 1; 28506238aec8SNeilBrown 28516238aec8SNeilBrown ga = a->group_info; 28526238aec8SNeilBrown gb = b->group_info; 28536238aec8SNeilBrown if (ga == gb) 28546238aec8SNeilBrown return 0; 28556238aec8SNeilBrown if (ga == NULL) 28566238aec8SNeilBrown return -1; 28576238aec8SNeilBrown if (gb == NULL) 28586238aec8SNeilBrown return 1; 28596238aec8SNeilBrown if (ga->ngroups < gb->ngroups) 28606238aec8SNeilBrown return -1; 28616238aec8SNeilBrown if (ga->ngroups > gb->ngroups) 28626238aec8SNeilBrown return 1; 28636238aec8SNeilBrown 28646238aec8SNeilBrown for (g = 0; g < ga->ngroups; g++) { 28656238aec8SNeilBrown if (gid_lt(ga->gid[g], gb->gid[g])) 28666238aec8SNeilBrown return -1; 28676238aec8SNeilBrown if (gid_gt(ga->gid[g], gb->gid[g])) 28686238aec8SNeilBrown return 1; 28696238aec8SNeilBrown } 28706238aec8SNeilBrown return 0; 28716238aec8SNeilBrown } 28726238aec8SNeilBrown 2873b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 28741c3c07e9STrond Myklebust { 28751c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 28761c3c07e9STrond Myklebust 28771c3c07e9STrond Myklebust while (n != NULL) { 2878b68572e0SNeilBrown struct nfs_access_entry *entry = 2879b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 28806238aec8SNeilBrown int cmp = access_cmp(cred, entry); 28811c3c07e9STrond Myklebust 2882b68572e0SNeilBrown if (cmp < 0) 28831c3c07e9STrond Myklebust n = n->rb_left; 2884b68572e0SNeilBrown else if (cmp > 0) 28851c3c07e9STrond Myklebust n = n->rb_right; 28861c3c07e9STrond Myklebust else 28871c3c07e9STrond Myklebust return entry; 28881c3c07e9STrond Myklebust } 28891c3c07e9STrond Myklebust return NULL; 28901c3c07e9STrond Myklebust } 28911c3c07e9STrond Myklebust 2892b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block) 28931da177e4SLinus Torvalds { 289455296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 28951c3c07e9STrond Myklebust struct nfs_access_entry *cache; 289657b69181STrond Myklebust bool retry = true; 289757b69181STrond Myklebust int err; 28981da177e4SLinus Torvalds 28991c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 290057b69181STrond Myklebust for(;;) { 29011c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 29021c3c07e9STrond Myklebust goto out_zap; 29031c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 290457b69181STrond Myklebust err = -ENOENT; 29051c3c07e9STrond Myklebust if (cache == NULL) 29061c3c07e9STrond Myklebust goto out; 290757b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 290821c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 290957b69181STrond Myklebust break; 29105c965db8STrond Myklebust if (!retry) 29115c965db8STrond Myklebust break; 291257b69181STrond Myklebust err = -ECHILD; 291357b69181STrond Myklebust if (!may_block) 291457b69181STrond Myklebust goto out; 291557b69181STrond Myklebust spin_unlock(&inode->i_lock); 291657b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 291757b69181STrond Myklebust if (err) 291857b69181STrond Myklebust return err; 291957b69181STrond Myklebust spin_lock(&inode->i_lock); 292057b69181STrond Myklebust retry = false; 292157b69181STrond Myklebust } 2922b5e7b59cSNeilBrown *mask = cache->mask; 2923cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 29241c3c07e9STrond Myklebust err = 0; 29251c3c07e9STrond Myklebust out: 29261c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29271c3c07e9STrond Myklebust return err; 29281c3c07e9STrond Myklebust out_zap: 29291a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 29301a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 29311c3c07e9STrond Myklebust return -ENOENT; 29321c3c07e9STrond Myklebust } 29331c3c07e9STrond Myklebust 2934b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask) 2935f682a398SNeilBrown { 2936f682a398SNeilBrown /* Only check the most recently returned cache entry, 2937f682a398SNeilBrown * but do it without locking. 2938f682a398SNeilBrown */ 2939f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2940f682a398SNeilBrown struct nfs_access_entry *cache; 2941f682a398SNeilBrown int err = -ECHILD; 2942f682a398SNeilBrown struct list_head *lh; 2943f682a398SNeilBrown 2944f682a398SNeilBrown rcu_read_lock(); 2945f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2946f682a398SNeilBrown goto out; 29479f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2948f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2949f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 29506238aec8SNeilBrown access_cmp(cred, cache) != 0) 2951f682a398SNeilBrown cache = NULL; 2952f682a398SNeilBrown if (cache == NULL) 2953f682a398SNeilBrown goto out; 295421c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2955f682a398SNeilBrown goto out; 2956b5e7b59cSNeilBrown *mask = cache->mask; 295721c3ba7eSTrond Myklebust err = 0; 2958f682a398SNeilBrown out: 2959f682a398SNeilBrown rcu_read_unlock(); 2960f682a398SNeilBrown return err; 2961f682a398SNeilBrown } 2962f682a398SNeilBrown 2963b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred, 2964b5e7b59cSNeilBrown u32 *mask, bool may_block) 2965d2ae4f8bSFrank van der Linden { 2966d2ae4f8bSFrank van der Linden int status; 2967d2ae4f8bSFrank van der Linden 2968b5e7b59cSNeilBrown status = nfs_access_get_cached_rcu(inode, cred, mask); 2969d2ae4f8bSFrank van der Linden if (status != 0) 2970b5e7b59cSNeilBrown status = nfs_access_get_cached_locked(inode, cred, mask, 2971d2ae4f8bSFrank van der Linden may_block); 2972d2ae4f8bSFrank van der Linden 2973d2ae4f8bSFrank van der Linden return status; 2974d2ae4f8bSFrank van der Linden } 2975d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2976d2ae4f8bSFrank van der Linden 297773fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode, 297873fbb3faSNeilBrown struct nfs_access_entry *set, 297973fbb3faSNeilBrown const struct cred *cred) 29801c3c07e9STrond Myklebust { 2981cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2982cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 29831c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 29841c3c07e9STrond Myklebust struct rb_node *parent = NULL; 29851c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2986b68572e0SNeilBrown int cmp; 29871c3c07e9STrond Myklebust 29881c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 29891c3c07e9STrond Myklebust while (*p != NULL) { 29901c3c07e9STrond Myklebust parent = *p; 29911c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 29926238aec8SNeilBrown cmp = access_cmp(cred, entry); 29931c3c07e9STrond Myklebust 2994b68572e0SNeilBrown if (cmp < 0) 29951c3c07e9STrond Myklebust p = &parent->rb_left; 2996b68572e0SNeilBrown else if (cmp > 0) 29971c3c07e9STrond Myklebust p = &parent->rb_right; 29981c3c07e9STrond Myklebust else 29991c3c07e9STrond Myklebust goto found; 30001c3c07e9STrond Myklebust } 30011c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 30021c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 3003cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 30041c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 30051c3c07e9STrond Myklebust return; 30061c3c07e9STrond Myklebust found: 30071c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 3008cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 3009cfcea3e8STrond Myklebust list_del(&entry->lru); 30101c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 30111c3c07e9STrond Myklebust nfs_access_free_entry(entry); 30121da177e4SLinus Torvalds } 30131da177e4SLinus Torvalds 301473fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set, 301573fbb3faSNeilBrown const struct cred *cred) 30161da177e4SLinus Torvalds { 30171c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 30181c3c07e9STrond Myklebust if (cache == NULL) 30191c3c07e9STrond Myklebust return; 30201c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 30216238aec8SNeilBrown cache->fsuid = cred->fsuid; 30226238aec8SNeilBrown cache->fsgid = cred->fsgid; 30236238aec8SNeilBrown cache->group_info = get_group_info(cred->group_info); 30241da177e4SLinus Torvalds cache->mask = set->mask; 30251c3c07e9STrond Myklebust 3026f682a398SNeilBrown /* The above field assignments must be visible 3027f682a398SNeilBrown * before this item appears on the lru. We cannot easily 3028f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 3029f682a398SNeilBrown */ 3030f682a398SNeilBrown smp_wmb(); 303173fbb3faSNeilBrown nfs_access_add_rbtree(inode, cache, cred); 3032cfcea3e8STrond Myklebust 3033cfcea3e8STrond Myklebust /* Update accounting */ 30344e857c58SPeter Zijlstra smp_mb__before_atomic(); 3035cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 30364e857c58SPeter Zijlstra smp_mb__after_atomic(); 3037cfcea3e8STrond Myklebust 3038cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 30391a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 3040cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 30411a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 30421a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 30431a81bb8aSTrond Myklebust &nfs_access_lru_list); 3044cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 3045cfcea3e8STrond Myklebust } 30463a505845STrond Myklebust nfs_access_cache_enforce_limit(); 30471da177e4SLinus Torvalds } 30486168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 30496168f62cSWeston Andros Adamson 30503c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 30513c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 30523c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 30533c181827SAnna Schumaker NFS_ACCESS_DELETE) 30543c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 30553c181827SAnna Schumaker NFS_ACCESS_EXTEND) 3056ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 30573c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 30583c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 305915d4b73aSTrond Myklebust static int 3060ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 306115d4b73aSTrond Myklebust { 306215d4b73aSTrond Myklebust int mask = 0; 306315d4b73aSTrond Myklebust 306415d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 306515d4b73aSTrond Myklebust mask |= MAY_READ; 3066ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 3067ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 306815d4b73aSTrond Myklebust mask |= MAY_WRITE; 3069ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 307015d4b73aSTrond Myklebust mask |= MAY_EXEC; 3071ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 3072ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 3073ecbb903cSTrond Myklebust mask |= MAY_WRITE; 3074ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 307515d4b73aSTrond Myklebust mask |= MAY_EXEC; 3076ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 3077ecbb903cSTrond Myklebust mask |= MAY_WRITE; 307815d4b73aSTrond Myklebust return mask; 307915d4b73aSTrond Myklebust } 308015d4b73aSTrond Myklebust 30816168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 30826168f62cSWeston Andros Adamson { 3083bd8b2441STrond Myklebust entry->mask = access_result; 30846168f62cSWeston Andros Adamson } 30856168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 30861da177e4SLinus Torvalds 3087b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 30881da177e4SLinus Torvalds { 30891da177e4SLinus Torvalds struct nfs_access_entry cache; 309057b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 3091e8194b7dSTrond Myklebust int cache_mask = -1; 30921da177e4SLinus Torvalds int status; 30931da177e4SLinus Torvalds 3094f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 3095f4ce1299STrond Myklebust 3096b5e7b59cSNeilBrown status = nfs_access_get_cached(inode, cred, &cache.mask, may_block); 30971da177e4SLinus Torvalds if (status == 0) 3098f4ce1299STrond Myklebust goto out_cached; 30991da177e4SLinus Torvalds 3100f3324a2aSNeilBrown status = -ECHILD; 310157b69181STrond Myklebust if (!may_block) 3102f3324a2aSNeilBrown goto out; 3103f3324a2aSNeilBrown 31041750d929SAnna Schumaker /* 31051750d929SAnna Schumaker * Determine which access bits we want to ask for... 31061750d929SAnna Schumaker */ 310784631f84STrond Myklebust cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND | 310884631f84STrond Myklebust nfs_access_xattr_mask(NFS_SERVER(inode)); 31091750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 31101750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 31111750d929SAnna Schumaker else 31121750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 311373fbb3faSNeilBrown status = NFS_PROTO(inode)->access(inode, &cache, cred); 3114a71ee337SSuresh Jayaraman if (status != 0) { 3115a71ee337SSuresh Jayaraman if (status == -ESTALE) { 3116a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 311793ce4af7STrond Myklebust nfs_set_inode_stale(inode); 311893ce4af7STrond Myklebust else 311993ce4af7STrond Myklebust nfs_zap_caches(inode); 3120a71ee337SSuresh Jayaraman } 3121f4ce1299STrond Myklebust goto out; 3122a71ee337SSuresh Jayaraman } 312373fbb3faSNeilBrown nfs_access_add_cache(inode, &cache, cred); 3124f4ce1299STrond Myklebust out_cached: 3125ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 3126bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 3127f4ce1299STrond Myklebust status = -EACCES; 31281da177e4SLinus Torvalds out: 3129e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 3130f4ce1299STrond Myklebust return status; 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds 3133af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 3134af22f94aSTrond Myklebust { 3135af22f94aSTrond Myklebust int mask = 0; 3136af22f94aSTrond Myklebust 3137f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 3138f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 3139f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 3140f8d9a897SWeston Andros Adamson } else { 31418a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 3142af22f94aSTrond Myklebust mask |= MAY_READ; 31438a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 3144af22f94aSTrond Myklebust mask |= MAY_WRITE; 3145f8d9a897SWeston Andros Adamson } 3146f8d9a897SWeston Andros Adamson 3147af22f94aSTrond Myklebust return mask; 3148af22f94aSTrond Myklebust } 3149af22f94aSTrond Myklebust 3150b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 3151af22f94aSTrond Myklebust { 3152af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 3153af22f94aSTrond Myklebust } 315489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 3155af22f94aSTrond Myklebust 31565c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 31575c5fc09aSTrond Myklebust { 31585c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 315921c3ba7eSTrond Myklebust int ret = 0; 31605c5fc09aSTrond Myklebust 31613825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 31623825827eSTrond Myklebust return 0; 3163720869ebSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { 31645c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 316521c3ba7eSTrond Myklebust return -ECHILD; 316621c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 316721c3ba7eSTrond Myklebust } 31685c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 31695c5fc09aSTrond Myklebust ret = -EACCES; 31705c5fc09aSTrond Myklebust return ret; 31715c5fc09aSTrond Myklebust } 31725c5fc09aSTrond Myklebust 3173549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns, 3174549c7297SChristian Brauner struct inode *inode, 3175549c7297SChristian Brauner int mask) 31761da177e4SLinus Torvalds { 3177b68572e0SNeilBrown const struct cred *cred = current_cred(); 31781da177e4SLinus Torvalds int res = 0; 31791da177e4SLinus Torvalds 318091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 318191d5b470SChuck Lever 3182e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 31831da177e4SLinus Torvalds goto out; 31841da177e4SLinus Torvalds /* Is this sys_access() ? */ 31859cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 31861da177e4SLinus Torvalds goto force_lookup; 31871da177e4SLinus Torvalds 31881da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 31891da177e4SLinus Torvalds case S_IFLNK: 31901da177e4SLinus Torvalds goto out; 31911da177e4SLinus Torvalds case S_IFREG: 3192762674f8STrond Myklebust if ((mask & MAY_OPEN) && 3193762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 3194762674f8STrond Myklebust return 0; 31951da177e4SLinus Torvalds break; 31961da177e4SLinus Torvalds case S_IFDIR: 31971da177e4SLinus Torvalds /* 31981da177e4SLinus Torvalds * Optimize away all write operations, since the server 31991da177e4SLinus Torvalds * will check permissions when we perform the op. 32001da177e4SLinus Torvalds */ 32011da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 32021da177e4SLinus Torvalds goto out; 32031da177e4SLinus Torvalds } 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds force_lookup: 32061da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 32071da177e4SLinus Torvalds goto out_notsup; 32081da177e4SLinus Torvalds 32091da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 32101da177e4SLinus Torvalds out: 32115c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 32125c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 3213f696a365SMiklos Szeredi 32141e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 32151e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 32161da177e4SLinus Torvalds return res; 32171da177e4SLinus Torvalds out_notsup: 3218d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 3219d51ac1a8SNeilBrown return -ECHILD; 3220d51ac1a8SNeilBrown 3221720869ebSTrond Myklebust res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE | 3222720869ebSTrond Myklebust NFS_INO_INVALID_OTHER); 32231da177e4SLinus Torvalds if (res == 0) 322447291baaSChristian Brauner res = generic_permission(&init_user_ns, inode, mask); 32251e7cb3dcSChuck Lever goto out; 32261da177e4SLinus Torvalds } 3227ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 3228