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> 42*830f1111STrond Myklebust #include <linux/hash.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 255e47a62dfSTrond Myklebust static u64 nfs_readdir_array_index_cookie(struct nfs_cache_array *array) 256e47a62dfSTrond Myklebust { 257e47a62dfSTrond Myklebust return array->size == 0 ? array->last_cookie : array->array[0].cookie; 258e47a62dfSTrond Myklebust } 259e47a62dfSTrond Myklebust 260b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 261b1e21c97STrond Myklebust { 262b1e21c97STrond Myklebust array->page_is_eof = 1; 263b1e21c97STrond Myklebust array->page_full = 1; 264b1e21c97STrond Myklebust } 265b1e21c97STrond Myklebust 266b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 267b1e21c97STrond Myklebust { 268b1e21c97STrond Myklebust return array->page_full; 269b1e21c97STrond Myklebust } 270b1e21c97STrond Myklebust 271d1bacf9eSBryan Schumaker /* 272d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 273d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 274d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 275d1bacf9eSBryan Schumaker */ 276a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 277d1bacf9eSBryan Schumaker { 278a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 279a52a8a6aSTrond Myklebust 28004e4bd1cSCatalin Marinas /* 28104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 28204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 28304e4bd1cSCatalin Marinas */ 284a52a8a6aSTrond Myklebust if (ret != NULL) 285a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 286a52a8a6aSTrond Myklebust return ret; 287d1bacf9eSBryan Schumaker } 288d1bacf9eSBryan Schumaker 2890b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void) 2900b2662b7STrond Myklebust { 2910b2662b7STrond Myklebust return (PAGE_SIZE - sizeof(struct nfs_cache_array)) / 2920b2662b7STrond Myklebust sizeof(struct nfs_cache_array_entry); 2930b2662b7STrond Myklebust } 2940b2662b7STrond Myklebust 295b1e21c97STrond Myklebust /* 296b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 297b1e21c97STrond Myklebust */ 298b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 299b1e21c97STrond Myklebust { 300b1e21c97STrond Myklebust if (array->page_full) 301b1e21c97STrond Myklebust return -ENOSPC; 3020b2662b7STrond Myklebust if (array->size == nfs_readdir_array_maxentries()) { 303b1e21c97STrond Myklebust array->page_full = 1; 304b1e21c97STrond Myklebust return -ENOSPC; 305b1e21c97STrond Myklebust } 306d1bacf9eSBryan Schumaker return 0; 307d1bacf9eSBryan Schumaker } 308d1bacf9eSBryan Schumaker 3090adf85b4STrond Myklebust static int nfs_readdir_page_array_append(struct page *page, 3100adf85b4STrond Myklebust const struct nfs_entry *entry, 3110adf85b4STrond Myklebust u64 *cookie) 312d1bacf9eSBryan Schumaker { 313a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 3144a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 315a52a8a6aSTrond Myklebust const char *name; 3160adf85b4STrond Myklebust int ret = -ENOMEM; 3174a201d6eSTrond Myklebust 318a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 3193020093fSTrond Myklebust 320a52a8a6aSTrond Myklebust array = kmap_atomic(page); 3210adf85b4STrond Myklebust if (!name) 3220adf85b4STrond Myklebust goto out; 323b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 324a52a8a6aSTrond Myklebust if (ret) { 325a52a8a6aSTrond Myklebust kfree(name); 3263020093fSTrond Myklebust goto out; 327a52a8a6aSTrond Myklebust } 3283020093fSTrond Myklebust 329b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 3300adf85b4STrond Myklebust cache_entry->cookie = array->last_cookie; 3314a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 3320b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 333a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 334a52a8a6aSTrond Myklebust cache_entry->name = name; 335d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 336762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie) 337762567b7STrond Myklebust array->cookies_are_ordered = 0; 3388cd51a0cSTrond Myklebust array->size++; 33947c716cbSTrond Myklebust if (entry->eof != 0) 340b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 3414a201d6eSTrond Myklebust out: 3420adf85b4STrond Myklebust *cookie = array->last_cookie; 343a52a8a6aSTrond Myklebust kunmap_atomic(array); 3444a201d6eSTrond Myklebust return ret; 345d1bacf9eSBryan Schumaker } 346d1bacf9eSBryan Schumaker 347f648022fSTrond Myklebust #define NFS_READDIR_COOKIE_MASK (U32_MAX >> 14) 348f648022fSTrond Myklebust /* 349f648022fSTrond Myklebust * Hash algorithm allowing content addressible access to sequences 350f648022fSTrond Myklebust * of directory cookies. Content is addressed by the value of the 351f648022fSTrond Myklebust * cookie index of the first readdir entry in a page. 352f648022fSTrond Myklebust * 353*830f1111STrond Myklebust * We select only the first 18 bits to avoid issues with excessive 354f648022fSTrond Myklebust * memory use for the page cache XArray. 18 bits should allow the caching 355f648022fSTrond Myklebust * of 262144 pages of sequences of readdir entries. Since each page holds 356f648022fSTrond Myklebust * 127 readdir entries for a typical 64-bit system, that works out to a 357f648022fSTrond Myklebust * cache of ~ 33 million entries per directory. 358f648022fSTrond Myklebust */ 359f648022fSTrond Myklebust static pgoff_t nfs_readdir_page_cookie_hash(u64 cookie) 360f648022fSTrond Myklebust { 361f648022fSTrond Myklebust if (cookie == 0) 362f648022fSTrond Myklebust return 0; 363*830f1111STrond Myklebust return hash_64(cookie, 18); 364f648022fSTrond Myklebust } 365f648022fSTrond Myklebust 366d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie, 367d09e673fSTrond Myklebust u64 change_attr) 368d09e673fSTrond Myklebust { 369d09e673fSTrond Myklebust struct nfs_cache_array *array = kmap_atomic(page); 370d09e673fSTrond Myklebust int ret = true; 371d09e673fSTrond Myklebust 372d09e673fSTrond Myklebust if (array->change_attr != change_attr) 373d09e673fSTrond Myklebust ret = false; 374e47a62dfSTrond Myklebust if (nfs_readdir_array_index_cookie(array) != last_cookie) 375d09e673fSTrond Myklebust ret = false; 376d09e673fSTrond Myklebust kunmap_atomic(array); 377d09e673fSTrond Myklebust return ret; 378d09e673fSTrond Myklebust } 379d09e673fSTrond Myklebust 380d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 381d09e673fSTrond Myklebust { 382d09e673fSTrond Myklebust unlock_page(page); 383d09e673fSTrond Myklebust put_page(page); 384d09e673fSTrond Myklebust } 385d09e673fSTrond Myklebust 386648a4548STrond Myklebust static void nfs_readdir_page_init_and_validate(struct page *page, u64 cookie, 387f648022fSTrond Myklebust u64 change_attr) 3881f1d4aa4STrond Myklebust { 389648a4548STrond Myklebust if (PageUptodate(page)) { 390648a4548STrond Myklebust if (nfs_readdir_page_validate(page, cookie, change_attr)) 391648a4548STrond Myklebust return; 392648a4548STrond Myklebust nfs_readdir_clear_array(page); 393648a4548STrond Myklebust } 394648a4548STrond Myklebust nfs_readdir_page_init_array(page, cookie, change_attr); 395648a4548STrond Myklebust SetPageUptodate(page); 396648a4548STrond Myklebust } 397648a4548STrond Myklebust 398648a4548STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 399648a4548STrond Myklebust u64 cookie, u64 change_attr) 400648a4548STrond Myklebust { 401648a4548STrond Myklebust pgoff_t index = nfs_readdir_page_cookie_hash(cookie); 4021f1d4aa4STrond Myklebust struct page *page; 4031f1d4aa4STrond Myklebust 4041f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 405d09e673fSTrond Myklebust if (!page) 406d09e673fSTrond Myklebust return NULL; 407648a4548STrond Myklebust nfs_readdir_page_init_and_validate(page, cookie, change_attr); 4081f1d4aa4STrond Myklebust return page; 4091f1d4aa4STrond Myklebust } 4101f1d4aa4STrond Myklebust 4111f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 4121f1d4aa4STrond Myklebust { 4131f1d4aa4STrond Myklebust struct nfs_cache_array *array; 4141f1d4aa4STrond Myklebust u64 ret; 4151f1d4aa4STrond Myklebust 4161f1d4aa4STrond Myklebust array = kmap_atomic(page); 4171f1d4aa4STrond Myklebust ret = array->last_cookie; 4181f1d4aa4STrond Myklebust kunmap_atomic(array); 4191f1d4aa4STrond Myklebust return ret; 4201f1d4aa4STrond Myklebust } 4211f1d4aa4STrond Myklebust 4221f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 4231f1d4aa4STrond Myklebust { 4241f1d4aa4STrond Myklebust struct nfs_cache_array *array; 4251f1d4aa4STrond Myklebust bool ret; 4261f1d4aa4STrond Myklebust 4271f1d4aa4STrond Myklebust array = kmap_atomic(page); 4281f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 4291f1d4aa4STrond Myklebust kunmap_atomic(array); 4301f1d4aa4STrond Myklebust return ret; 4311f1d4aa4STrond Myklebust } 4321f1d4aa4STrond Myklebust 433b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 434b1e21c97STrond Myklebust { 435b1e21c97STrond Myklebust struct nfs_cache_array *array; 436b1e21c97STrond Myklebust 437b1e21c97STrond Myklebust array = kmap_atomic(page); 438b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 439b1e21c97STrond Myklebust kunmap_atomic(array); 440b1e21c97STrond Myklebust } 441b1e21c97STrond Myklebust 4423b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 443f648022fSTrond Myklebust u64 cookie, u64 change_attr) 4443b2a09f1STrond Myklebust { 445648a4548STrond Myklebust pgoff_t index = nfs_readdir_page_cookie_hash(cookie); 4463b2a09f1STrond Myklebust struct page *page; 4473b2a09f1STrond Myklebust 448648a4548STrond Myklebust page = grab_cache_page_nowait(mapping, index); 449b0365ccbSTrond Myklebust if (!page) 4503b2a09f1STrond Myklebust return NULL; 451648a4548STrond Myklebust nfs_readdir_page_init_and_validate(page, cookie, change_attr); 452b0365ccbSTrond Myklebust if (nfs_readdir_page_last_cookie(page) != cookie) 453b0365ccbSTrond Myklebust nfs_readdir_page_reinit_array(page, cookie, change_attr); 454b0365ccbSTrond Myklebust return page; 4553b2a09f1STrond Myklebust } 4563b2a09f1STrond Myklebust 45759e356a9STrond Myklebust static inline 45859e356a9STrond Myklebust int is_32bit_api(void) 45959e356a9STrond Myklebust { 46059e356a9STrond Myklebust #ifdef CONFIG_COMPAT 46159e356a9STrond Myklebust return in_compat_syscall(); 46259e356a9STrond Myklebust #else 46359e356a9STrond Myklebust return (BITS_PER_LONG == 32); 46459e356a9STrond Myklebust #endif 46559e356a9STrond Myklebust } 46659e356a9STrond Myklebust 46759e356a9STrond Myklebust static 46859e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 46959e356a9STrond Myklebust { 47059e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 47159e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 47259e356a9STrond Myklebust return false; 47359e356a9STrond Myklebust return true; 47459e356a9STrond Myklebust } 47559e356a9STrond Myklebust 476c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array, 477c8f0523bSTrond Myklebust struct nfs_readdir_descriptor *desc) 478c8f0523bSTrond Myklebust { 479c8f0523bSTrond Myklebust if (array->page_full) { 480c8f0523bSTrond Myklebust desc->last_cookie = array->last_cookie; 481c8f0523bSTrond Myklebust desc->current_index += array->size; 482c8f0523bSTrond Myklebust desc->cache_entry_index = 0; 483c8f0523bSTrond Myklebust desc->page_index++; 484c8f0523bSTrond Myklebust } else 485e47a62dfSTrond Myklebust desc->last_cookie = nfs_readdir_array_index_cookie(array); 486c8f0523bSTrond Myklebust } 487c8f0523bSTrond Myklebust 488f648022fSTrond Myklebust static void nfs_readdir_rewind_search(struct nfs_readdir_descriptor *desc) 489f648022fSTrond Myklebust { 490f648022fSTrond Myklebust desc->current_index = 0; 491f648022fSTrond Myklebust desc->last_cookie = 0; 492f648022fSTrond Myklebust desc->page_index = 0; 493f648022fSTrond Myklebust } 494f648022fSTrond Myklebust 4956c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 4966c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 497d1bacf9eSBryan Schumaker { 49823db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 499d1bacf9eSBryan Schumaker unsigned int index; 500d1bacf9eSBryan Schumaker 501d1bacf9eSBryan Schumaker if (diff < 0) 502d1bacf9eSBryan Schumaker goto out_eof; 503d1bacf9eSBryan Schumaker if (diff >= array->size) { 504b1e21c97STrond Myklebust if (array->page_is_eof) 505d1bacf9eSBryan Schumaker goto out_eof; 506c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 507d1bacf9eSBryan Schumaker return -EAGAIN; 508d1bacf9eSBryan Schumaker } 509d1bacf9eSBryan Schumaker 510d1bacf9eSBryan Schumaker index = (unsigned int)diff; 5112e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 512d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 513d1bacf9eSBryan Schumaker return 0; 514d1bacf9eSBryan Schumaker out_eof: 5156089dd0dSThomas Meyer desc->eof = true; 516d1bacf9eSBryan Schumaker return -EBADCOOKIE; 517d1bacf9eSBryan Schumaker } 518d1bacf9eSBryan Schumaker 519762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 520762567b7STrond Myklebust u64 cookie) 521762567b7STrond Myklebust { 522762567b7STrond Myklebust if (!array->cookies_are_ordered) 523762567b7STrond Myklebust return true; 524762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */ 525762567b7STrond Myklebust if (cookie >= array->last_cookie) 526762567b7STrond Myklebust return false; 527762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie) 528762567b7STrond Myklebust return false; 529762567b7STrond Myklebust return true; 530762567b7STrond Myklebust } 531762567b7STrond Myklebust 5326c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 5336c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 534d1bacf9eSBryan Schumaker { 535f648022fSTrond Myklebust unsigned int i; 536d1bacf9eSBryan Schumaker int status = -EAGAIN; 537d1bacf9eSBryan Schumaker 538762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 539762567b7STrond Myklebust goto check_eof; 540762567b7STrond Myklebust 541d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 5422e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 54359e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 5442e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 54559e356a9STrond Myklebust else 546f648022fSTrond Myklebust desc->ctx->pos = desc->current_index + i; 5478cd51a0cSTrond Myklebust desc->cache_entry_index = i; 54847c716cbSTrond Myklebust return 0; 5498cd51a0cSTrond Myklebust } 5508cd51a0cSTrond Myklebust } 551762567b7STrond Myklebust check_eof: 552b1e21c97STrond Myklebust if (array->page_is_eof) { 553d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 5542e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 5556089dd0dSThomas Meyer desc->eof = true; 556c8f0523bSTrond Myklebust } else 557c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 558d1bacf9eSBryan Schumaker return status; 559d1bacf9eSBryan Schumaker } 560d1bacf9eSBryan Schumaker 5616c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 562d1bacf9eSBryan Schumaker { 563d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 56447c716cbSTrond Myklebust int status; 565d1bacf9eSBryan Schumaker 566ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 567d1bacf9eSBryan Schumaker 5682e7a4641STrond Myklebust if (desc->dir_cookie == 0) 569d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 570d1bacf9eSBryan Schumaker else 571d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 572d1bacf9eSBryan Schumaker 573ed09222dSTrond Myklebust kunmap_atomic(array); 574d1bacf9eSBryan Schumaker return status; 575d1bacf9eSBryan Schumaker } 576d1bacf9eSBryan Schumaker 577d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 57893b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 579b593c09fSTrond Myklebust __be32 *verf, u64 cookie, 580b593c09fSTrond Myklebust struct page **pages, size_t bufsize, 581b593c09fSTrond Myklebust __be32 *verf_res) 582d1bacf9eSBryan Schumaker { 58382e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 58482e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 58582e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 58682e22a5eSTrond Myklebust .cred = desc->file->f_cred, 587b593c09fSTrond Myklebust .verf = verf, 58882e22a5eSTrond Myklebust .cookie = cookie, 58982e22a5eSTrond Myklebust .pages = pages, 59082e22a5eSTrond Myklebust .page_len = bufsize, 59182e22a5eSTrond Myklebust .plus = desc->plus, 59282e22a5eSTrond Myklebust }; 59382e22a5eSTrond Myklebust struct nfs_readdir_res res = { 59482e22a5eSTrond Myklebust .verf = verf_res, 59582e22a5eSTrond Myklebust }; 5964704f0e2STrond Myklebust unsigned long timestamp, gencount; 5971da177e4SLinus Torvalds int error; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds again: 6001da177e4SLinus Torvalds timestamp = jiffies; 6014704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 602a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 60382e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 6041da177e4SLinus Torvalds if (error < 0) { 6051da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 6061da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 6071da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 60882e22a5eSTrond Myklebust desc->plus = arg.plus = false; 6091da177e4SLinus Torvalds goto again; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds goto error; 6121da177e4SLinus Torvalds } 6131f4eab7eSNeil Brown desc->timestamp = timestamp; 6144704f0e2STrond Myklebust desc->gencount = gencount; 615d1bacf9eSBryan Schumaker error: 616d1bacf9eSBryan Schumaker return error; 617d1bacf9eSBryan Schumaker } 618d1bacf9eSBryan Schumaker 6196c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 620573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 621d1bacf9eSBryan Schumaker { 62259e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 623573c4e1eSChuck Lever int error; 624d1bacf9eSBryan Schumaker 62559e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 626573c4e1eSChuck Lever if (error) 627573c4e1eSChuck Lever return error; 628d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 629d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 630d1bacf9eSBryan Schumaker return 0; 631d1bacf9eSBryan Schumaker } 632d1bacf9eSBryan Schumaker 633fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 634fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 635fa923369STrond Myklebust */ 636d39ab9deSBryan Schumaker static 637d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 638d39ab9deSBryan Schumaker { 639d8fdb47fSTrond Myklebust struct inode *inode; 640fa923369STrond Myklebust struct nfs_inode *nfsi; 641fa923369STrond Myklebust 6422b0143b5SDavid Howells if (d_really_is_negative(dentry)) 6432b0143b5SDavid Howells return 0; 644fa923369STrond Myklebust 645d8fdb47fSTrond Myklebust inode = d_inode(dentry); 646d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 647d8fdb47fSTrond Myklebust return 0; 648d8fdb47fSTrond Myklebust 649d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 6507dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 651d39ab9deSBryan Schumaker return 0; 6527dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 6537dc72d5fSTrond Myklebust return 0; 6547dc72d5fSTrond Myklebust return 1; 655d39ab9deSBryan Schumaker } 656d39ab9deSBryan Schumaker 657230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL) 658230bc98fSTrond Myklebust 659230bc98fSTrond Myklebust static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx, 660230bc98fSTrond Myklebust unsigned int cache_hits, 661230bc98fSTrond Myklebust unsigned int cache_misses) 662d69ee9b8STrond Myklebust { 663d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 664d69ee9b8STrond Myklebust return false; 665230bc98fSTrond Myklebust if (ctx->pos == 0 || 666230bc98fSTrond Myklebust cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD) 667d69ee9b8STrond Myklebust return true; 668d69ee9b8STrond Myklebust return false; 669d69ee9b8STrond Myklebust } 670d69ee9b8STrond Myklebust 671d69ee9b8STrond Myklebust /* 672230bc98fSTrond Myklebust * This function is called by the getattr code to request the 67363519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 674d69ee9b8STrond Myklebust * directory. 675d69ee9b8STrond Myklebust */ 676230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_hit(struct inode *dir) 677d69ee9b8STrond Myklebust { 67863519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 679230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx; 68063519fbcSTrond Myklebust 68163519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 682230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) { 683230bc98fSTrond Myklebust rcu_read_lock(); 684230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list) 685230bc98fSTrond Myklebust atomic_inc(&ctx->cache_hits); 686230bc98fSTrond Myklebust rcu_read_unlock(); 687230bc98fSTrond Myklebust } 688d69ee9b8STrond Myklebust } 689d69ee9b8STrond Myklebust 690311324adSTrond Myklebust /* 691311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 692311324adSTrond Myklebust * 693311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 694311324adSTrond Myklebust */ 695230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_miss(struct inode *dir) 696311324adSTrond Myklebust { 69763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 698230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx; 69963519fbcSTrond Myklebust 70063519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 701230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) { 702230bc98fSTrond Myklebust rcu_read_lock(); 703230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list) 704230bc98fSTrond Myklebust atomic_inc(&ctx->cache_misses); 705230bc98fSTrond Myklebust rcu_read_unlock(); 706311324adSTrond Myklebust } 707311324adSTrond Myklebust } 708311324adSTrond Myklebust 7090b3cc71bSTrond Myklebust static void nfs_lookup_advise_force_readdirplus(struct inode *dir, 7100b3cc71bSTrond Myklebust unsigned int flags) 711230bc98fSTrond Myklebust { 7122c2c3365STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 7132c2c3365STrond Myklebust return; 7140b3cc71bSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL)) 7150b3cc71bSTrond Myklebust return; 716230bc98fSTrond Myklebust nfs_readdir_record_entry_cache_miss(dir); 717230bc98fSTrond Myklebust } 718230bc98fSTrond Myklebust 719d69ee9b8STrond Myklebust static 720a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 721a1147b82STrond Myklebust unsigned long dir_verifier) 722d39ab9deSBryan Schumaker { 72326fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 7249ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 7254a201d6eSTrond Myklebust struct dentry *dentry; 7264a201d6eSTrond Myklebust struct dentry *alias; 727d39ab9deSBryan Schumaker struct inode *inode; 728aa9c2669SDavid Quigley int status; 729d39ab9deSBryan Schumaker 730fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 731fa923369STrond Myklebust return; 7326c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 7336c441c25STrond Myklebust return; 73478d04af4STrond Myklebust if (filename.len == 0) 73578d04af4STrond Myklebust return; 73678d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 73778d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 73878d04af4STrond Myklebust return; 73978d04af4STrond Myklebust /* ...or '/' */ 74078d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 74178d04af4STrond Myklebust return; 7424a201d6eSTrond Myklebust if (filename.name[0] == '.') { 7434a201d6eSTrond Myklebust if (filename.len == 1) 7444a201d6eSTrond Myklebust return; 7454a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 7464a201d6eSTrond Myklebust return; 7474a201d6eSTrond Myklebust } 7488387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 749d39ab9deSBryan Schumaker 7504a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 7519ac3d3e8SAl Viro again: 7529ac3d3e8SAl Viro if (!dentry) { 7539ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 7549ac3d3e8SAl Viro if (IS_ERR(dentry)) 7559ac3d3e8SAl Viro return; 7569ac3d3e8SAl Viro } 7579ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 7586c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 7596c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 7606c441c25STrond Myklebust &entry->fattr->fsid)) 7616c441c25STrond Myklebust goto out; 762d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 7637dc72d5fSTrond Myklebust if (!entry->fh->size) 7647dc72d5fSTrond Myklebust goto out; 765a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 7662b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 767aa9c2669SDavid Quigley if (!status) 768dd225cb3SAnna Schumaker nfs_setsecurity(d_inode(dentry), entry->fattr); 769eace45a1STrond Myklebust trace_nfs_readdir_lookup_revalidate(d_inode(parent), 770eace45a1STrond Myklebust dentry, 0, status); 771d39ab9deSBryan Schumaker goto out; 772d39ab9deSBryan Schumaker } else { 773eace45a1STrond Myklebust trace_nfs_readdir_lookup_revalidate_failed( 774eace45a1STrond Myklebust d_inode(parent), dentry, 0); 7755542aa2fSEric W. Biederman d_invalidate(dentry); 776d39ab9deSBryan Schumaker dput(dentry); 7779ac3d3e8SAl Viro dentry = NULL; 7789ac3d3e8SAl Viro goto again; 779d39ab9deSBryan Schumaker } 780d39ab9deSBryan Schumaker } 7817dc72d5fSTrond Myklebust if (!entry->fh->size) { 7827dc72d5fSTrond Myklebust d_lookup_done(dentry); 7837dc72d5fSTrond Myklebust goto out; 7847dc72d5fSTrond Myklebust } 785d39ab9deSBryan Schumaker 786cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 78741d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 7889ac3d3e8SAl Viro d_lookup_done(dentry); 7899ac3d3e8SAl Viro if (alias) { 790d39ab9deSBryan Schumaker if (IS_ERR(alias)) 791d39ab9deSBryan Schumaker goto out; 7929ac3d3e8SAl Viro dput(dentry); 7939ac3d3e8SAl Viro dentry = alias; 7949ac3d3e8SAl Viro } 795a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 796eace45a1STrond Myklebust trace_nfs_readdir_lookup(d_inode(parent), dentry, 0); 797d39ab9deSBryan Schumaker out: 798d39ab9deSBryan Schumaker dput(dentry); 799d39ab9deSBryan Schumaker } 800d39ab9deSBryan Schumaker 801612896ecSTrond Myklebust static int nfs_readdir_entry_decode(struct nfs_readdir_descriptor *desc, 802612896ecSTrond Myklebust struct nfs_entry *entry, 803612896ecSTrond Myklebust struct xdr_stream *stream) 804612896ecSTrond Myklebust { 805612896ecSTrond Myklebust int ret; 806612896ecSTrond Myklebust 807612896ecSTrond Myklebust if (entry->fattr->label) 808612896ecSTrond Myklebust entry->fattr->label->len = NFS4_MAXLABELLEN; 809612896ecSTrond Myklebust ret = xdr_decode(desc, entry, stream); 810612896ecSTrond Myklebust if (ret || !desc->plus) 811612896ecSTrond Myklebust return ret; 812612896ecSTrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, desc->dir_verifier); 813612896ecSTrond Myklebust return 0; 814612896ecSTrond Myklebust } 815612896ecSTrond Myklebust 816d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 8173b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 8183b2a09f1STrond Myklebust struct nfs_entry *entry, 819f648022fSTrond Myklebust struct page **xdr_pages, unsigned int buflen, 820f648022fSTrond Myklebust struct page **arrays, size_t narrays, 821f648022fSTrond Myklebust u64 change_attr) 822d1bacf9eSBryan Schumaker { 8233b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 824babddc72SBryan Schumaker struct xdr_stream stream; 825f7da7a12SBenny Halevy struct xdr_buf buf; 82635df59d3STrond Myklebust struct page *scratch, *new, *page = *arrays; 8270adf85b4STrond Myklebust u64 cookie; 8285c346854STrond Myklebust int status; 829babddc72SBryan Schumaker 8306650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 8316650239aSTrond Myklebust if (scratch == NULL) 8326650239aSTrond Myklebust return -ENOMEM; 833babddc72SBryan Schumaker 834f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 8350ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch); 83699424380SBryan Schumaker 83799424380SBryan Schumaker do { 838612896ecSTrond Myklebust status = nfs_readdir_entry_decode(desc, entry, &stream); 839972bcdf2STrond Myklebust if (status != 0) 84099424380SBryan Schumaker break; 8415c346854STrond Myklebust 8420adf85b4STrond Myklebust status = nfs_readdir_page_array_append(page, entry, &cookie); 8433b2a09f1STrond Myklebust if (status != -ENOSPC) 8443b2a09f1STrond Myklebust continue; 84599424380SBryan Schumaker 84635df59d3STrond Myklebust if (page->mapping != mapping) { 84735df59d3STrond Myklebust if (!--narrays) 8483b2a09f1STrond Myklebust break; 8490adf85b4STrond Myklebust new = nfs_readdir_page_array_alloc(cookie, GFP_KERNEL); 85035df59d3STrond Myklebust if (!new) 85135df59d3STrond Myklebust break; 85235df59d3STrond Myklebust arrays++; 85335df59d3STrond Myklebust *arrays = page = new; 85435df59d3STrond Myklebust } else { 8550adf85b4STrond Myklebust new = nfs_readdir_page_get_next(mapping, cookie, 8560adf85b4STrond Myklebust change_attr); 8573b2a09f1STrond Myklebust if (!new) 8583b2a09f1STrond Myklebust break; 85935df59d3STrond Myklebust if (page != *arrays) 8603b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8613b2a09f1STrond Myklebust page = new; 86256e4ebf8SBryan Schumaker } 863f648022fSTrond Myklebust desc->page_index_max++; 8640adf85b4STrond Myklebust status = nfs_readdir_page_array_append(page, entry, &cookie); 865972bcdf2STrond Myklebust } while (!status && !entry->eof); 86656e4ebf8SBryan Schumaker 867972bcdf2STrond Myklebust switch (status) { 868972bcdf2STrond Myklebust case -EBADCOOKIE: 869612896ecSTrond Myklebust if (!entry->eof) 870972bcdf2STrond Myklebust break; 871612896ecSTrond Myklebust nfs_readdir_page_set_eof(page); 872612896ecSTrond Myklebust fallthrough; 873972bcdf2STrond Myklebust case -EAGAIN: 874972bcdf2STrond Myklebust status = 0; 875972bcdf2STrond Myklebust break; 876612896ecSTrond Myklebust case -ENOSPC: 877612896ecSTrond Myklebust status = 0; 878612896ecSTrond Myklebust if (!desc->plus) 879612896ecSTrond Myklebust break; 880612896ecSTrond Myklebust while (!nfs_readdir_entry_decode(desc, entry, &stream)) 881612896ecSTrond Myklebust ; 882972bcdf2STrond Myklebust } 8836650239aSTrond Myklebust 88435df59d3STrond Myklebust if (page != *arrays) 8853b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8866650239aSTrond Myklebust 8876650239aSTrond Myklebust put_page(scratch); 8888cd51a0cSTrond Myklebust return status; 8898cd51a0cSTrond Myklebust } 89056e4ebf8SBryan Schumaker 8911a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 89256e4ebf8SBryan Schumaker { 8931a34c8c9STrond Myklebust while (npages--) 8941a34c8c9STrond Myklebust put_page(pages[npages]); 8951a34c8c9STrond Myklebust kfree(pages); 89656e4ebf8SBryan Schumaker } 89756e4ebf8SBryan Schumaker 89856e4ebf8SBryan Schumaker /* 899bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 900bf211ca1Szhangliguang * to nfs_readdir_free_pages() 90156e4ebf8SBryan Schumaker */ 9021a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 90356e4ebf8SBryan Schumaker { 9041a34c8c9STrond Myklebust struct page **pages; 9051a34c8c9STrond Myklebust size_t i; 90656e4ebf8SBryan Schumaker 9071a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 9081a34c8c9STrond Myklebust if (!pages) 9091a34c8c9STrond Myklebust return NULL; 91056e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 91156e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 91256e4ebf8SBryan Schumaker if (page == NULL) 91356e4ebf8SBryan Schumaker goto out_freepages; 91456e4ebf8SBryan Schumaker pages[i] = page; 91556e4ebf8SBryan Schumaker } 9161a34c8c9STrond Myklebust return pages; 91756e4ebf8SBryan Schumaker 91856e4ebf8SBryan Schumaker out_freepages: 919c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 9201a34c8c9STrond Myklebust return NULL; 921d1bacf9eSBryan Schumaker } 922d1bacf9eSBryan Schumaker 9236c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 92435df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res, 92535df59d3STrond Myklebust struct page **arrays, size_t narrays) 926d1bacf9eSBryan Schumaker { 927f648022fSTrond Myklebust u64 change_attr; 9281a34c8c9STrond Myklebust struct page **pages; 92935df59d3STrond Myklebust struct page *page = *arrays; 9306b75cf9eSTrond Myklebust struct nfs_entry *entry; 9311a34c8c9STrond Myklebust size_t array_size; 932b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file); 933580f2367STrond Myklebust unsigned int dtsize = desc->dtsize; 9349ff89c25STrond Myklebust unsigned int pglen; 9358cd51a0cSTrond Myklebust int status = -ENOMEM; 936d1bacf9eSBryan Schumaker 9376b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 9386b75cf9eSTrond Myklebust if (!entry) 9396b75cf9eSTrond Myklebust return -ENOMEM; 9406b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 9416b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 942b1db9a40SAnna Schumaker entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 9436b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 9446b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 945d1bacf9eSBryan Schumaker goto out; 946d1bacf9eSBryan Schumaker 9471a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 9481a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 9491a34c8c9STrond Myklebust if (!pages) 950b1db9a40SAnna Schumaker goto out; 951d1bacf9eSBryan Schumaker 952f648022fSTrond Myklebust change_attr = inode_peek_iversion_raw(inode); 9539ff89c25STrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages, 9549ff89c25STrond Myklebust dtsize, verf_res); 955d1bacf9eSBryan Schumaker if (status < 0) 9569ff89c25STrond Myklebust goto free_pages; 957972bcdf2STrond Myklebust 958ac396128STrond Myklebust pglen = status; 9599ff89c25STrond Myklebust if (pglen != 0) 96035df59d3STrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, pglen, 961f648022fSTrond Myklebust arrays, narrays, change_attr); 9629ff89c25STrond Myklebust else 9639ff89c25STrond Myklebust nfs_readdir_page_set_eof(page); 964580f2367STrond Myklebust desc->buffer_fills++; 965d1bacf9eSBryan Schumaker 9669ff89c25STrond Myklebust free_pages: 967c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 968d1bacf9eSBryan Schumaker out: 9696b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 9706b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 9716b75cf9eSTrond Myklebust kfree(entry); 972d1bacf9eSBryan Schumaker return status; 973d1bacf9eSBryan Schumaker } 974d1bacf9eSBryan Schumaker 9751f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 9761da177e4SLinus Torvalds { 97709cbfeafSKirill A. Shutemov put_page(desc->page); 9781da177e4SLinus Torvalds desc->page = NULL; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811f1d4aa4STrond Myklebust static void 9821f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 9831da177e4SLinus Torvalds { 9841f1d4aa4STrond Myklebust unlock_page(desc->page); 9851f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 9861f1d4aa4STrond Myklebust } 9871f1d4aa4STrond Myklebust 9881f1d4aa4STrond Myklebust static struct page * 9891f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 9901f1d4aa4STrond Myklebust { 991f648022fSTrond Myklebust struct address_space *mapping = desc->file->f_mapping; 992f648022fSTrond Myklebust u64 change_attr = inode_peek_iversion_raw(mapping->host); 993b0365ccbSTrond Myklebust u64 cookie = desc->last_cookie; 994b0365ccbSTrond Myklebust struct page *page; 995f648022fSTrond Myklebust 996b0365ccbSTrond Myklebust page = nfs_readdir_page_get_locked(mapping, cookie, change_attr); 997b0365ccbSTrond Myklebust if (!page) 998b0365ccbSTrond Myklebust return NULL; 999b0365ccbSTrond Myklebust if (desc->clear_cache && !nfs_readdir_page_needs_filling(page)) 1000b0365ccbSTrond Myklebust nfs_readdir_page_reinit_array(page, cookie, change_attr); 1001b0365ccbSTrond Myklebust return page; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds /* 1005d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 1006114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 10071da177e4SLinus Torvalds */ 10086c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 1009d1bacf9eSBryan Schumaker { 1010227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 1011227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 1012b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 1013d1bacf9eSBryan Schumaker int res; 1014d1bacf9eSBryan Schumaker 10151f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 10161f1d4aa4STrond Myklebust if (!desc->page) 10171f1d4aa4STrond Myklebust return -ENOMEM; 10181f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 1019580f2367STrond Myklebust /* Grow the dtsize if we had to go back for more pages */ 1020580f2367STrond Myklebust if (desc->page_index == desc->page_index_max) 1021580f2367STrond Myklebust nfs_grow_dtsize(desc); 1022580f2367STrond Myklebust desc->page_index_max = desc->page_index; 1023310e3187STrond Myklebust trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf, 1024310e3187STrond Myklebust desc->last_cookie, 1025310e3187STrond Myklebust desc->page->index, desc->dtsize); 102635df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 102735df59d3STrond Myklebust &desc->page, 1); 10289fff59edSTrond Myklebust if (res < 0) { 10299fff59edSTrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1030310e3187STrond Myklebust trace_nfs_readdir_cache_fill_done(inode, res); 10319fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) { 10329fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping); 1033f648022fSTrond Myklebust nfs_readdir_rewind_search(desc); 103411d03d0aSTrond Myklebust trace_nfs_readdir_invalidate_cache_range( 103511d03d0aSTrond Myklebust inode, 0, MAX_LFS_FILESIZE); 10369fff59edSTrond Myklebust return -EAGAIN; 10379fff59edSTrond Myklebust } 10389fff59edSTrond Myklebust return res; 10399fff59edSTrond Myklebust } 1040f892c41cSTrond Myklebust /* 1041f892c41cSTrond Myklebust * Set the cookie verifier if the page cache was empty 1042f892c41cSTrond Myklebust */ 10436c34f05bSTrond Myklebust if (desc->last_cookie == 0 && 10446c34f05bSTrond Myklebust memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) { 1045f892c41cSTrond Myklebust memcpy(nfsi->cookieverf, verf, 1046f892c41cSTrond Myklebust sizeof(nfsi->cookieverf)); 1047f648022fSTrond Myklebust invalidate_inode_pages2_range(desc->file->f_mapping, 1, 10486c34f05bSTrond Myklebust -1); 104911d03d0aSTrond Myklebust trace_nfs_readdir_invalidate_cache_range( 1050f648022fSTrond Myklebust inode, 1, MAX_LFS_FILESIZE); 10516c34f05bSTrond Myklebust } 1052b0365ccbSTrond Myklebust desc->clear_cache = false; 10531f1d4aa4STrond Myklebust } 1054114de382STrond Myklebust res = nfs_readdir_search_array(desc); 1055ff81dfb5STrond Myklebust if (res == 0) 1056114de382STrond Myklebust return 0; 10571f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1058d1bacf9eSBryan Schumaker return res; 1059d1bacf9eSBryan Schumaker } 1060d1bacf9eSBryan Schumaker 1061d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 10626c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 10631da177e4SLinus Torvalds { 10648cd51a0cSTrond Myklebust int res; 1065d1bacf9eSBryan Schumaker 10669fff59edSTrond Myklebust do { 1067114de382STrond Myklebust res = find_and_lock_cache_page(desc); 106847c716cbSTrond Myklebust } while (res == -EAGAIN); 10691da177e4SLinus Torvalds return res; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds /* 10731da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 10741da177e4SLinus Torvalds */ 107513884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc, 107613884ff2STrond Myklebust const __be32 *verf) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds struct file *file = desc->file; 1079dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 1080c8f0523bSTrond Myklebust unsigned int i; 10818ef2ce3eSBryan Schumaker 10820795bf83SFabian Frederick array = kmap(desc->page); 1083d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 1084ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 10851da177e4SLinus Torvalds 1086ece0b423STrond Myklebust ent = &array->array[i]; 1087a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 108823db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 1089e1d2699bSTrond Myklebust desc->eob = true; 10901da177e4SLinus Torvalds break; 1091ece0b423STrond Myklebust } 109213884ff2STrond Myklebust memcpy(desc->verf, verf, sizeof(desc->verf)); 1093c8f0523bSTrond Myklebust if (i == array->size - 1) { 10942e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 1095c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 1096c8f0523bSTrond Myklebust } else { 1097c8f0523bSTrond Myklebust desc->dir_cookie = array->array[i + 1].cookie; 1098c8f0523bSTrond Myklebust desc->last_cookie = array->array[0].cookie; 1099c8f0523bSTrond Myklebust } 110059e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 11012e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 110259e356a9STrond Myklebust else 110359e356a9STrond Myklebust desc->ctx->pos++; 11048cd51a0cSTrond Myklebust } 1105b1e21c97STrond Myklebust if (array->page_is_eof) 1106e1d2699bSTrond Myklebust desc->eof = !desc->eob; 1107d1bacf9eSBryan Schumaker 11080795bf83SFabian Frederick kunmap(desc->page); 1109dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1110dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* 11141da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 11151da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 11161da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 11171da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 11181da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 11191da177e4SLinus Torvalds * 11201da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 11211da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 11221da177e4SLinus Torvalds * we should already have a complete representation of the 11231da177e4SLinus Torvalds * directory in the page cache by the time we get here. 11241da177e4SLinus Torvalds */ 11256c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 11261da177e4SLinus Torvalds { 112735df59d3STrond Myklebust struct page **arrays; 112835df59d3STrond Myklebust size_t i, sz = 512; 1129b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 113035df59d3STrond Myklebust int status = -ENOMEM; 11311da177e4SLinus Torvalds 113235df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 11332e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 11341da177e4SLinus Torvalds 113535df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 113635df59d3STrond Myklebust if (!arrays) 11371da177e4SLinus Torvalds goto out; 113835df59d3STrond Myklebust arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 113935df59d3STrond Myklebust if (!arrays[0]) 114035df59d3STrond Myklebust goto out; 11411da177e4SLinus Torvalds 11427a8e1dc3STrond Myklebust desc->page_index = 0; 1143ce292d8fStrondmy@kernel.org desc->cache_entry_index = 0; 11442e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 1145580f2367STrond Myklebust desc->page_index_max = 0; 11467a8e1dc3STrond Myklebust 1147310e3187STrond Myklebust trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie, 1148310e3187STrond Myklebust -1, desc->dtsize); 1149310e3187STrond Myklebust 115035df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1151310e3187STrond Myklebust if (status < 0) { 1152310e3187STrond Myklebust trace_nfs_readdir_uncached_done(file_inode(desc->file), status); 1153310e3187STrond Myklebust goto out_free; 1154310e3187STrond Myklebust } 1155d1bacf9eSBryan Schumaker 1156e1d2699bSTrond Myklebust for (i = 0; !desc->eob && i < sz && arrays[i]; i++) { 115735df59d3STrond Myklebust desc->page = arrays[i]; 115813884ff2STrond Myklebust nfs_do_filldir(desc, verf); 115935df59d3STrond Myklebust } 116035df59d3STrond Myklebust desc->page = NULL; 11611da177e4SLinus Torvalds 1162580f2367STrond Myklebust /* 1163580f2367STrond Myklebust * Grow the dtsize if we have to go back for more pages, 1164580f2367STrond Myklebust * or shrink it if we're reading too many. 1165580f2367STrond Myklebust */ 1166580f2367STrond Myklebust if (!desc->eof) { 1167580f2367STrond Myklebust if (!desc->eob) 1168580f2367STrond Myklebust nfs_grow_dtsize(desc); 1169580f2367STrond Myklebust else if (desc->buffer_fills == 1 && 1170580f2367STrond Myklebust i < (desc->page_index_max >> 1)) 1171580f2367STrond Myklebust nfs_shrink_dtsize(desc); 1172580f2367STrond Myklebust } 1173310e3187STrond Myklebust out_free: 117435df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++) 117535df59d3STrond Myklebust nfs_readdir_page_array_free(arrays[i]); 11761da177e4SLinus Torvalds out: 1177f648022fSTrond Myklebust if (!nfs_readdir_use_cookie(desc->file)) 1178f648022fSTrond Myklebust nfs_readdir_rewind_search(desc); 1179580f2367STrond Myklebust desc->page_index_max = -1; 118035df59d3STrond Myklebust kfree(arrays); 118135df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 11821da177e4SLinus Torvalds return status; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 1185230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL) 1186230bc98fSTrond Myklebust 1187b0365ccbSTrond Myklebust static bool nfs_readdir_handle_cache_misses(struct inode *inode, 1188230bc98fSTrond Myklebust struct nfs_readdir_descriptor *desc, 1189b0365ccbSTrond Myklebust unsigned int cache_misses, 1190b0365ccbSTrond Myklebust bool force_clear) 1191230bc98fSTrond Myklebust { 1192b0365ccbSTrond Myklebust if (desc->ctx->pos == 0 || !desc->plus) 1193b0365ccbSTrond Myklebust return false; 1194b0365ccbSTrond Myklebust if (cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD && !force_clear) 1195b0365ccbSTrond Myklebust return false; 1196b0365ccbSTrond Myklebust trace_nfs_readdir_force_readdirplus(inode); 1197b0365ccbSTrond Myklebust return true; 1198230bc98fSTrond Myklebust } 1199230bc98fSTrond Myklebust 120000a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 120100a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 120200a92642SOlivier Galibert whole directory. 12031da177e4SLinus Torvalds */ 120423db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 12051da177e4SLinus Torvalds { 1206be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 12072b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 120813884ff2STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 120923db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 12106b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 1211230bc98fSTrond Myklebust unsigned int cache_hits, cache_misses; 1212b0365ccbSTrond Myklebust bool force_clear; 12136b75cf9eSTrond Myklebust int res; 12141da177e4SLinus Torvalds 12156de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 12166de1472fSAl Viro file, (long long)ctx->pos); 121791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 121891d5b470SChuck Lever 12191da177e4SLinus Torvalds /* 122023db8620SAl Viro * ctx->pos points to the dirent entry number. 1221f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 122200a92642SOlivier Galibert * to either find the entry with the appropriate number or 122300a92642SOlivier Galibert * revalidate the cookie. 12241da177e4SLinus Torvalds */ 1225d09e673fSTrond Myklebust nfs_revalidate_mapping(inode, file->f_mapping); 12266b75cf9eSTrond Myklebust 12276b75cf9eSTrond Myklebust res = -ENOMEM; 12286b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 12296b75cf9eSTrond Myklebust if (!desc) 12306b75cf9eSTrond Myklebust goto out; 12316b75cf9eSTrond Myklebust desc->file = file; 12326b75cf9eSTrond Myklebust desc->ctx = ctx; 1233580f2367STrond Myklebust desc->page_index_max = -1; 1234fccca7fcSTrond Myklebust 12352e7a4641STrond Myklebust spin_lock(&file->f_lock); 12362e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 1237f648022fSTrond Myklebust desc->page_index = dir_ctx->page_index; 1238728dd0abSTrond Myklebust desc->last_cookie = dir_ctx->last_cookie; 12392e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 1240e1d2699bSTrond Myklebust desc->eof = dir_ctx->eof; 1241580f2367STrond Myklebust nfs_set_dtsize(desc, dir_ctx->dtsize); 1242b593c09fSTrond Myklebust memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf)); 1243230bc98fSTrond Myklebust cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0); 1244230bc98fSTrond Myklebust cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0); 1245b0365ccbSTrond Myklebust force_clear = dir_ctx->force_clear; 12462e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1247565277f6STrond Myklebust 1248e1d2699bSTrond Myklebust if (desc->eof) { 1249e1d2699bSTrond Myklebust res = 0; 1250e1d2699bSTrond Myklebust goto out_free; 1251e1d2699bSTrond Myklebust } 1252e1d2699bSTrond Myklebust 1253230bc98fSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses); 1254b0365ccbSTrond Myklebust force_clear = nfs_readdir_handle_cache_misses(inode, desc, cache_misses, 1255b0365ccbSTrond Myklebust force_clear); 1256b0365ccbSTrond Myklebust desc->clear_cache = force_clear; 1257ff81dfb5STrond Myklebust 125847c716cbSTrond Myklebust do { 12591da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 126000a92642SOlivier Galibert 12611da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1262ece0b423STrond Myklebust res = 0; 12631da177e4SLinus Torvalds /* This means either end of directory */ 12642e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 12651da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 126623db8620SAl Viro res = uncached_readdir(desc); 1267ece0b423STrond Myklebust if (res == 0) 12681da177e4SLinus Torvalds continue; 12699fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) 12709fff59edSTrond Myklebust res = 0; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds break; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 12751da177e4SLinus Torvalds nfs_zap_caches(inode); 1276a7a3b1e9SBenjamin Coddington desc->plus = false; 1277a7a3b1e9SBenjamin Coddington desc->eof = false; 12781da177e4SLinus Torvalds continue; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds if (res < 0) 12811da177e4SLinus Torvalds break; 12821da177e4SLinus Torvalds 128313884ff2STrond Myklebust nfs_do_filldir(desc, nfsi->cookieverf); 12841f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1285b0365ccbSTrond Myklebust if (desc->page_index == desc->page_index_max) 1286b0365ccbSTrond Myklebust desc->clear_cache = force_clear; 1287e1d2699bSTrond Myklebust } while (!desc->eob && !desc->eof); 12882e7a4641STrond Myklebust 12892e7a4641STrond Myklebust spin_lock(&file->f_lock); 12902e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 1291728dd0abSTrond Myklebust dir_ctx->last_cookie = desc->last_cookie; 12922e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 1293ff81dfb5STrond Myklebust dir_ctx->page_index = desc->page_index; 1294b0365ccbSTrond Myklebust dir_ctx->force_clear = force_clear; 1295e1d2699bSTrond Myklebust dir_ctx->eof = desc->eof; 1296580f2367STrond Myklebust dir_ctx->dtsize = desc->dtsize; 1297b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 12982e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1299e1d2699bSTrond Myklebust out_free: 13006b75cf9eSTrond Myklebust kfree(desc); 13016b75cf9eSTrond Myklebust 1302fccca7fcSTrond Myklebust out: 13036de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 13041da177e4SLinus Torvalds return res; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 1307965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1308f0dd2136STrond Myklebust { 1309480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1310b84e06c5SChuck Lever 13116de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 13126de1472fSAl Viro filp, offset, whence); 1313b84e06c5SChuck Lever 1314965c8e59SAndrew Morton switch (whence) { 1315f0dd2136STrond Myklebust default: 1316b2b1ff3dSTrond Myklebust return -EINVAL; 1317b2b1ff3dSTrond Myklebust case SEEK_SET: 1318b2b1ff3dSTrond Myklebust if (offset < 0) 1319b2b1ff3dSTrond Myklebust return -EINVAL; 132083f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1321b2b1ff3dSTrond Myklebust break; 1322b2b1ff3dSTrond Myklebust case SEEK_CUR: 1323b2b1ff3dSTrond Myklebust if (offset == 0) 1324b2b1ff3dSTrond Myklebust return filp->f_pos; 132583f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1326b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1327b2b1ff3dSTrond Myklebust if (offset < 0) { 132883f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1329b2b1ff3dSTrond Myklebust return -EINVAL; 1330b2b1ff3dSTrond Myklebust } 1331f0dd2136STrond Myklebust } 1332f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1333f0dd2136STrond Myklebust filp->f_pos = offset; 1334728dd0abSTrond Myklebust dir_ctx->page_index = 0; 1335f648022fSTrond Myklebust if (!nfs_readdir_use_cookie(filp)) { 13369c3f4d98STrond Myklebust dir_ctx->dir_cookie = 0; 1337f648022fSTrond Myklebust dir_ctx->last_cookie = 0; 1338f648022fSTrond Myklebust } else { 1339728dd0abSTrond Myklebust dir_ctx->dir_cookie = offset; 1340f648022fSTrond Myklebust dir_ctx->last_cookie = offset; 1341f648022fSTrond Myklebust } 1342e1d2699bSTrond Myklebust dir_ctx->eof = false; 1343f0dd2136STrond Myklebust } 134483f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1345f0dd2136STrond Myklebust return offset; 1346f0dd2136STrond Myklebust } 1347f0dd2136STrond Myklebust 13481da177e4SLinus Torvalds /* 13491da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 13501da177e4SLinus Torvalds * is a dummy operation. 13511da177e4SLinus Torvalds */ 135202c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 135302c24a82SJosef Bacik int datasync) 13541da177e4SLinus Torvalds { 13556de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 13561e7cb3dcSChuck Lever 135711decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 13581da177e4SLinus Torvalds return 0; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 1361bfc69a45STrond Myklebust /** 1362bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1363302fad7bSTrond Myklebust * @dir: pointer to directory inode 1364bfc69a45STrond Myklebust * 1365bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1366bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1367bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1368bfc69a45STrond Myklebust * 1369efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1370efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1371efeda80dSTrond Myklebust * 1372bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1373bfc69a45STrond Myklebust */ 1374bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1375bfc69a45STrond Myklebust { 1376efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1377bfc69a45STrond Myklebust } 137889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1379bfc69a45STrond Myklebust 1380efeda80dSTrond Myklebust /** 1381efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1382efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1383efeda80dSTrond Myklebust * @verf: previously saved change attribute 1384efeda80dSTrond Myklebust * 1385efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1386efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1387efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1388efeda80dSTrond Myklebust */ 1389efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1390efeda80dSTrond Myklebust { 1391efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1392efeda80dSTrond Myklebust } 1393efeda80dSTrond Myklebust 1394efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1395efeda80dSTrond Myklebust { 1396efeda80dSTrond Myklebust *verf |= 1UL; 1397efeda80dSTrond Myklebust } 1398efeda80dSTrond Myklebust 1399efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1400efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1401efeda80dSTrond Myklebust { 1402efeda80dSTrond Myklebust *verf &= ~1UL; 1403efeda80dSTrond Myklebust } 1404efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1405efeda80dSTrond Myklebust 1406efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1407efeda80dSTrond Myklebust { 1408efeda80dSTrond Myklebust return verf & 1; 1409efeda80dSTrond Myklebust } 1410efeda80dSTrond Myklebust 1411efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1412efeda80dSTrond Myklebust { 1413efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1414efeda80dSTrond Myklebust } 1415efeda80dSTrond Myklebust 1416efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1417efeda80dSTrond Myklebust { 1418efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1419cec08f45STrond Myklebust struct inode *dir = d_inode(dentry->d_parent); 1420efeda80dSTrond Myklebust 1421cec08f45STrond Myklebust if (!nfs_verify_change_attribute(dir, verf)) 1422cec08f45STrond Myklebust return; 1423efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1424efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1425efeda80dSTrond Myklebust dentry->d_time = verf; 1426efeda80dSTrond Myklebust } 1427efeda80dSTrond Myklebust 1428efeda80dSTrond Myklebust /** 1429efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1430efeda80dSTrond Myklebust * @dentry: pointer to dentry 1431efeda80dSTrond Myklebust * @verf: verifier to save 1432efeda80dSTrond Myklebust * 1433efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1434efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1435efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1436efeda80dSTrond Myklebust * look it up again after a directory change. 1437efeda80dSTrond Myklebust */ 1438efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1439efeda80dSTrond Myklebust { 1440efeda80dSTrond Myklebust 1441efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1442efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1443efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1444efeda80dSTrond Myklebust } 1445efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1446efeda80dSTrond Myklebust 1447efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1448efeda80dSTrond Myklebust /** 1449efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1450efeda80dSTrond Myklebust * @inode: pointer to inode 1451efeda80dSTrond Myklebust * 1452efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1453efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1454efeda80dSTrond Myklebust * while holding a delegation. 1455efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1456efeda80dSTrond Myklebust * returned or revoked. 1457efeda80dSTrond Myklebust */ 1458efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1459efeda80dSTrond Myklebust { 1460efeda80dSTrond Myklebust struct dentry *alias; 1461efeda80dSTrond Myklebust 1462efeda80dSTrond Myklebust if (!inode) 1463efeda80dSTrond Myklebust return; 1464efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1465efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1466efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1467efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1468efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1469efeda80dSTrond Myklebust } 1470efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1471efeda80dSTrond Myklebust } 1472efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1473efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1474efeda80dSTrond Myklebust 14758ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry) 14768ce37abdSTrond Myklebust { 14778ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) && 14788ce37abdSTrond Myklebust d_really_is_negative(dentry)) 14798ce37abdSTrond Myklebust return dentry->d_time == inode_peek_iversion_raw(dir); 14808ce37abdSTrond Myklebust return nfs_verify_change_attribute(dir, dentry->d_time); 14818ce37abdSTrond Myklebust } 14828ce37abdSTrond Myklebust 14831da177e4SLinus Torvalds /* 14841da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 14851da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 14861da177e4SLinus Torvalds * and may need to be looked up again. 1487912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 14881da177e4SLinus Torvalds */ 1489912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1490912a108dSNeilBrown int rcu_walk) 14911da177e4SLinus Torvalds { 14921da177e4SLinus Torvalds if (IS_ROOT(dentry)) 14931da177e4SLinus Torvalds return 1; 14944eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 14954eec952eSTrond Myklebust return 0; 14968ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 14976ecc5e8fSTrond Myklebust return 0; 1498f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 14991cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1500912a108dSNeilBrown if (rcu_walk) 1501f2c77f4eSTrond Myklebust return 0; 15021cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 15031cd9cb05STrond Myklebust return 0; 15041cd9cb05STrond Myklebust } 15058ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 1506f2c77f4eSTrond Myklebust return 0; 1507f2c77f4eSTrond Myklebust return 1; 15081da177e4SLinus Torvalds } 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds /* 1511a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1512a12802caSTrond Myklebust * an O_EXCL create using this path component. 1513a12802caSTrond Myklebust */ 1514fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1515a12802caSTrond Myklebust { 1516a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1517a12802caSTrond Myklebust return 0; 1518fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1519a12802caSTrond Myklebust } 1520a12802caSTrond Myklebust 1521a12802caSTrond Myklebust /* 15221d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 15231d6757fbSTrond Myklebust * 15241d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 15251d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 15261d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 15271d6757fbSTrond Myklebust * 15281d6757fbSTrond Myklebust */ 152965a0c149STrond Myklebust static 1530fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 15311da177e4SLinus Torvalds { 15321da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 153365a0c149STrond Myklebust int ret; 15341da177e4SLinus Torvalds 153536d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 15364e99a1ffSTrond Myklebust return 0; 153747921921STrond Myklebust 153847921921STrond Myklebust if (flags & LOOKUP_OPEN) { 153947921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 154047921921STrond Myklebust case S_IFREG: 154147921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 154247921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 154347921921STrond Myklebust goto out; 1544df561f66SGustavo A. R. Silva fallthrough; 154547921921STrond Myklebust case S_IFDIR: 154647921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 154747921921STrond Myklebust break; 154847921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 154947921921STrond Myklebust goto out_force; 155047921921STrond Myklebust } 155147921921STrond Myklebust } 155247921921STrond Myklebust 15531da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1554fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 15551da177e4SLinus Torvalds goto out_force; 155665a0c149STrond Myklebust out: 155743245ecaSOlga Kornievskaia if (inode->i_nlink > 0 || 155843245ecaSOlga Kornievskaia (inode->i_nlink == 0 && 155943245ecaSOlga Kornievskaia test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags))) 156043245ecaSOlga Kornievskaia return 0; 156143245ecaSOlga Kornievskaia else 156243245ecaSOlga Kornievskaia return -ESTALE; 15631da177e4SLinus Torvalds out_force: 15641fa1e384SNeilBrown if (flags & LOOKUP_RCU) 15651fa1e384SNeilBrown return -ECHILD; 156665a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 156765a0c149STrond Myklebust if (ret != 0) 156865a0c149STrond Myklebust return ret; 156965a0c149STrond Myklebust goto out; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 157282e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode) 157382e7ca13STrond Myklebust { 157482e7ca13STrond Myklebust spin_lock(&inode->i_lock); 1575a6a361c4STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 157682e7ca13STrond Myklebust spin_unlock(&inode->i_lock); 157782e7ca13STrond Myklebust } 157882e7ca13STrond Myklebust 15791da177e4SLinus Torvalds /* 15801da177e4SLinus Torvalds * We judge how long we want to trust negative 15811da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 15821da177e4SLinus Torvalds * 15831da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 15841da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1585912a108dSNeilBrown * 1586912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1587912a108dSNeilBrown * suggesting a reval is needed. 15889f6d44d4STrond Myklebust * 15899f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 15909f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 15911da177e4SLinus Torvalds */ 15921da177e4SLinus Torvalds static inline 15931da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1594fa3c56bbSAl Viro unsigned int flags) 15951da177e4SLinus Torvalds { 15969f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 15971da177e4SLinus Torvalds return 0; 15984eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 15994eec952eSTrond Myklebust return 1; 160098ca3ee6STrond Myklebust /* Case insensitive server? Revalidate negative dentries */ 160198ca3ee6STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 160298ca3ee6STrond Myklebust return 1; 1603912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 16065ceb9d7fSTrond Myklebust static int 16075ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 16085ceb9d7fSTrond Myklebust struct inode *inode, int error) 16091da177e4SLinus Torvalds { 16105ceb9d7fSTrond Myklebust switch (error) { 16115ceb9d7fSTrond Myklebust case 1: 16122eef8a31STrond Myklebust break; 16135ceb9d7fSTrond Myklebust case 0: 1614a3f432bfSJ. Bruce Fields /* 1615a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1616a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1617a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1618a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1619a3f432bfSJ. Bruce Fields */ 162047397915STrond Myklebust if (inode && IS_ROOT(dentry)) 16212eef8a31STrond Myklebust error = 1; 16222eef8a31STrond Myklebust break; 16235ceb9d7fSTrond Myklebust } 16242eef8a31STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error); 1625e1fb4d05STrond Myklebust return error; 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 16285ceb9d7fSTrond Myklebust static int 16295ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 16305ceb9d7fSTrond Myklebust unsigned int flags) 16315ceb9d7fSTrond Myklebust { 16325ceb9d7fSTrond Myklebust int ret = 1; 16335ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 16345ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 16355ceb9d7fSTrond Myklebust return -ECHILD; 16365ceb9d7fSTrond Myklebust ret = 0; 16375ceb9d7fSTrond Myklebust } 16385ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 16395ceb9d7fSTrond Myklebust } 16405ceb9d7fSTrond Myklebust 16415ceb9d7fSTrond Myklebust static int 16425ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 16435ceb9d7fSTrond Myklebust struct inode *inode) 16445ceb9d7fSTrond Myklebust { 16455ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16465ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 16475ceb9d7fSTrond Myklebust } 16485ceb9d7fSTrond Myklebust 16490b3cc71bSTrond Myklebust static int nfs_lookup_revalidate_dentry(struct inode *dir, 16500b3cc71bSTrond Myklebust struct dentry *dentry, 16510b3cc71bSTrond Myklebust struct inode *inode, unsigned int flags) 16525ceb9d7fSTrond Myklebust { 16535ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 16545ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 1655a1147b82STrond Myklebust unsigned long dir_verifier; 16565ceb9d7fSTrond Myklebust int ret; 16575ceb9d7fSTrond Myklebust 16580b3cc71bSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 16590b3cc71bSTrond Myklebust 16605ceb9d7fSTrond Myklebust ret = -ENOMEM; 16615ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 16629558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 16639558a007SAnna Schumaker if (fhandle == NULL || fattr == NULL) 16645ceb9d7fSTrond Myklebust goto out; 16655ceb9d7fSTrond Myklebust 1666a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 16679558a007SAnna Schumaker ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 16685ceb9d7fSTrond Myklebust if (ret < 0) { 1669f7b37b8bSTrond Myklebust switch (ret) { 1670f7b37b8bSTrond Myklebust case -ESTALE: 1671f7b37b8bSTrond Myklebust case -ENOENT: 16725ceb9d7fSTrond Myklebust ret = 0; 1673f7b37b8bSTrond Myklebust break; 1674f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1675f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1676f7b37b8bSTrond Myklebust ret = 1; 1677f7b37b8bSTrond Myklebust } 16785ceb9d7fSTrond Myklebust goto out; 16795ceb9d7fSTrond Myklebust } 16800b3cc71bSTrond Myklebust 16810b3cc71bSTrond Myklebust /* Request help from readdirplus */ 16820b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags); 16830b3cc71bSTrond Myklebust 16845ceb9d7fSTrond Myklebust ret = 0; 16855ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 16865ceb9d7fSTrond Myklebust goto out; 16875ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 16885ceb9d7fSTrond Myklebust goto out; 16895ceb9d7fSTrond Myklebust 1690dd225cb3SAnna Schumaker nfs_setsecurity(inode, fattr); 1691a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 16925ceb9d7fSTrond Myklebust 16935ceb9d7fSTrond Myklebust ret = 1; 16945ceb9d7fSTrond Myklebust out: 16955ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 16965ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 169782e7ca13STrond Myklebust 169882e7ca13STrond Myklebust /* 169982e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being 170082e7ca13STrond Myklebust * a match, then we should revalidate the directory cache. 170182e7ca13STrond Myklebust */ 17028ce37abdSTrond Myklebust if (!ret && nfs_dentry_verify_change(dir, dentry)) 170382e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 17045ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 17055ceb9d7fSTrond Myklebust } 17065ceb9d7fSTrond Myklebust 17075ceb9d7fSTrond Myklebust /* 17085ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 17095ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 17105ceb9d7fSTrond Myklebust * lookup. 17115ceb9d7fSTrond Myklebust * 17125ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 17135ceb9d7fSTrond Myklebust * we have an inode! 17145ceb9d7fSTrond Myklebust * 17155ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 17165ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 17175ceb9d7fSTrond Myklebust */ 17185ceb9d7fSTrond Myklebust static int 17195ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 17205ceb9d7fSTrond Myklebust unsigned int flags) 17215ceb9d7fSTrond Myklebust { 17225ceb9d7fSTrond Myklebust struct inode *inode; 17235ceb9d7fSTrond Myklebust int error; 17245ceb9d7fSTrond Myklebust 17255ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 17265ceb9d7fSTrond Myklebust inode = d_inode(dentry); 17275ceb9d7fSTrond Myklebust 17285ceb9d7fSTrond Myklebust if (!inode) 17295ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 17305ceb9d7fSTrond Myklebust 17315ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 17325ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 17335ceb9d7fSTrond Myklebust __func__, dentry); 17345ceb9d7fSTrond Myklebust goto out_bad; 17355ceb9d7fSTrond Myklebust } 17365ceb9d7fSTrond Myklebust 1737efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 17385ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 17395ceb9d7fSTrond Myklebust 17405ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 17415ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 17425ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 17435ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 17445ceb9d7fSTrond Myklebust if (error) { 17455ceb9d7fSTrond Myklebust if (error == -ESTALE) 174682e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 17475ceb9d7fSTrond Myklebust goto out_bad; 17485ceb9d7fSTrond Myklebust } 17495ceb9d7fSTrond Myklebust goto out_valid; 17505ceb9d7fSTrond Myklebust } 17515ceb9d7fSTrond Myklebust 17525ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 17535ceb9d7fSTrond Myklebust return -ECHILD; 17545ceb9d7fSTrond Myklebust 17555ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 17565ceb9d7fSTrond Myklebust goto out_bad; 17575ceb9d7fSTrond Myklebust 17580b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags); 17595ceb9d7fSTrond Myklebust out_valid: 17605ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 17615ceb9d7fSTrond Myklebust out_bad: 17625ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 17635ceb9d7fSTrond Myklebust return -ECHILD; 17645ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 17655ceb9d7fSTrond Myklebust } 17665ceb9d7fSTrond Myklebust 17675ceb9d7fSTrond Myklebust static int 1768c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1769c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 17705ceb9d7fSTrond Myklebust { 17715ceb9d7fSTrond Myklebust struct dentry *parent; 17725ceb9d7fSTrond Myklebust struct inode *dir; 17735ceb9d7fSTrond Myklebust int ret; 17745ceb9d7fSTrond Myklebust 17755ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 17765ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 17775ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 17785ceb9d7fSTrond Myklebust if (!dir) 17795ceb9d7fSTrond Myklebust return -ECHILD; 1780c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 17815ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 17825ceb9d7fSTrond Myklebust return -ECHILD; 17835ceb9d7fSTrond Myklebust } else { 17845ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1785c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 17865ceb9d7fSTrond Myklebust dput(parent); 17875ceb9d7fSTrond Myklebust } 17885ceb9d7fSTrond Myklebust return ret; 17895ceb9d7fSTrond Myklebust } 17905ceb9d7fSTrond Myklebust 1791c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1792c7944ebbSTrond Myklebust { 1793c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1794c7944ebbSTrond Myklebust } 1795c7944ebbSTrond Myklebust 17961da177e4SLinus Torvalds /* 17972b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1798ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1799ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1800ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1801ecf3d1f1SJeff Layton * 1802ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1803ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1804ecf3d1f1SJeff Layton */ 1805ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1806ecf3d1f1SJeff Layton { 18072b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18089cdd1d3fSTrond Myklebust int error = 0; 1809ecf3d1f1SJeff Layton 1810ecf3d1f1SJeff Layton /* 1811ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1812ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1813ecf3d1f1SJeff Layton * eventually need to do something more here. 1814ecf3d1f1SJeff Layton */ 1815ecf3d1f1SJeff Layton if (!inode) { 18166de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 18176de1472fSAl Viro __func__, dentry); 1818ecf3d1f1SJeff Layton return 1; 1819ecf3d1f1SJeff Layton } 1820ecf3d1f1SJeff Layton 1821ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 18226de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 18236de1472fSAl Viro __func__, dentry); 1824ecf3d1f1SJeff Layton return 0; 1825ecf3d1f1SJeff Layton } 1826ecf3d1f1SJeff Layton 1827b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1828ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1829ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1830ecf3d1f1SJeff Layton return !error; 1831ecf3d1f1SJeff Layton } 1832ecf3d1f1SJeff Layton 1833ecf3d1f1SJeff Layton /* 18341da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 18351da177e4SLinus Torvalds */ 1836fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 18371da177e4SLinus Torvalds { 18386de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 18396de1472fSAl Viro dentry, dentry->d_flags); 18401da177e4SLinus Torvalds 184177f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 18422b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 184377f11192STrond Myklebust return 1; 184477f11192STrond Myklebust 18451da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18461da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 18471da177e4SLinus Torvalds return 1; 18481da177e4SLinus Torvalds } 18491751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 18501da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 18511da177e4SLinus Torvalds * files will be cleaned up during umount */ 18521da177e4SLinus Torvalds return 1; 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds return 0; 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 18591b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 18601b83d707STrond Myklebust { 18611b83d707STrond Myklebust spin_lock(&inode->i_lock); 18621f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 186359a707b0STrond Myklebust if (inode->i_nlink > 0) 186459a707b0STrond Myklebust drop_nlink(inode); 186559a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1866ac46b3d7STrond Myklebust nfs_set_cache_invalid( 1867ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 18681301e421STrond Myklebust NFS_INO_INVALID_NLINK); 18691b83d707STrond Myklebust spin_unlock(&inode->i_lock); 18701b83d707STrond Myklebust } 18711b83d707STrond Myklebust 18721da177e4SLinus Torvalds /* 18731da177e4SLinus Torvalds * Called when the dentry loses inode. 18741da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 18751da177e4SLinus Torvalds */ 18761da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 18771da177e4SLinus Torvalds { 18781da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1879e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 18801f018458STrond Myklebust nfs_drop_nlink(inode); 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds iput(inode); 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 1885b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1886b1942c5fSAl Viro { 1887b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1888b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1889b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1890b1942c5fSAl Viro WARN_ON(1); 1891b1942c5fSAl Viro else 1892b1942c5fSAl Viro kfree(dentry->d_fsdata); 1893b1942c5fSAl Viro } 1894b1942c5fSAl Viro } 1895b1942c5fSAl Viro 1896f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 18971da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1898ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 18991da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 19001da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 190136d43a43SDavid Howells .d_automount = nfs_d_automount, 1902b1942c5fSAl Viro .d_release = nfs_d_release, 19031da177e4SLinus Torvalds }; 1904ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 19051da177e4SLinus Torvalds 1906597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 19071da177e4SLinus Torvalds { 19081da177e4SLinus Torvalds struct dentry *res; 19091da177e4SLinus Torvalds struct inode *inode = NULL; 1910e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1911e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 1912a1147b82STrond Myklebust unsigned long dir_verifier; 19131da177e4SLinus Torvalds int error; 19141da177e4SLinus Torvalds 19156de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 191691d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 19171da177e4SLinus Torvalds 1918130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1919130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 19201da177e4SLinus Torvalds 1921fd684071STrond Myklebust /* 1922fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1923fd684071STrond Myklebust * but don't hash the dentry. 1924fd684071STrond Myklebust */ 19259f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1926130f9ab7SAl Viro return NULL; 19271da177e4SLinus Torvalds 1928e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1929e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 19309558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir)); 1931e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1932e1fb4d05STrond Myklebust goto out; 1933e1fb4d05STrond Myklebust 1934a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 19356e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 19369558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 19378ce37abdSTrond Myklebust if (error == -ENOENT) { 19388ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 19398ce37abdSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 19401da177e4SLinus Torvalds goto no_entry; 19418ce37abdSTrond Myklebust } 19421da177e4SLinus Torvalds if (error < 0) { 19431da177e4SLinus Torvalds res = ERR_PTR(error); 19449558a007SAnna Schumaker goto out; 19451da177e4SLinus Torvalds } 1946cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1947bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 194803f28e3aSTrond Myklebust if (IS_ERR(res)) 19499558a007SAnna Schumaker goto out; 195054ceac45SDavid Howells 195163519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 19520b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags); 1953d69ee9b8STrond Myklebust 19541da177e4SLinus Torvalds no_entry: 195541d28bcaSAl Viro res = d_splice_alias(inode, dentry); 19569eaef27bSTrond Myklebust if (res != NULL) { 19579eaef27bSTrond Myklebust if (IS_ERR(res)) 19589558a007SAnna Schumaker goto out; 19591da177e4SLinus Torvalds dentry = res; 19609eaef27bSTrond Myklebust } 1961a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 19621da177e4SLinus Torvalds out: 19639558a007SAnna Schumaker trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res)); 1964e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1965e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 19661da177e4SLinus Torvalds return res; 19671da177e4SLinus Torvalds } 1968ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 19691da177e4SLinus Torvalds 197000bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode) 197100bdadc7STrond Myklebust { 197200bdadc7STrond Myklebust /* Case insensitive server? Revalidate dentries */ 197300bdadc7STrond Myklebust if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE)) 197400bdadc7STrond Myklebust d_prune_aliases(inode); 197500bdadc7STrond Myklebust } 197600bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases); 197700bdadc7STrond Myklebust 197889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 19790b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 19801da177e4SLinus Torvalds 1981f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 19820ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1983b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 19841da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 19851da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 198636d43a43SDavid Howells .d_automount = nfs_d_automount, 1987b1942c5fSAl Viro .d_release = nfs_d_release, 19881da177e4SLinus Torvalds }; 198989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 19901da177e4SLinus Torvalds 1991532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1992cd9a1c0eSTrond Myklebust { 1993532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1994cd9a1c0eSTrond Myklebust } 1995cd9a1c0eSTrond Myklebust 1996cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1997cd9a1c0eSTrond Myklebust { 1998f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1999cd9a1c0eSTrond Myklebust return 0; 2000cd9a1c0eSTrond Myklebust } 2001cd9a1c0eSTrond Myklebust 2002d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 20030dd2b474SMiklos Szeredi struct dentry *dentry, 2004b452a458SAl Viro struct file *file, unsigned open_flags) 2005cd9a1c0eSTrond Myklebust { 20060dd2b474SMiklos Szeredi int err; 20070dd2b474SMiklos Szeredi 2008be12af3eSAl Viro err = finish_open(file, dentry, do_open); 200930d90494SAl Viro if (err) 2010d9585277SAl Viro goto out; 2011eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 201230d90494SAl Viro nfs_file_set_open_context(file, ctx); 2013eaa2b82cSNeilBrown else 20149821421aSTrond Myklebust err = -EOPENSTALE; 2015cd9a1c0eSTrond Myklebust out: 2016d9585277SAl Viro return err; 2017cd9a1c0eSTrond Myklebust } 2018cd9a1c0eSTrond Myklebust 201973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 202030d90494SAl Viro struct file *file, unsigned open_flags, 202144907d79SAl Viro umode_t mode) 20221da177e4SLinus Torvalds { 2023c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 2024cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 20250dd2b474SMiklos Szeredi struct dentry *res; 20260dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 2027f46e0bd3STrond Myklebust struct inode *inode; 20281472b83eSTrond Myklebust unsigned int lookup_flags = 0; 202968eaba4cSTrond Myklebust unsigned long dir_verifier; 2030c94c0953SAl Viro bool switched = false; 203173a09dd9SAl Viro int created = 0; 2032898f635cSTrond Myklebust int err; 20331da177e4SLinus Torvalds 20340dd2b474SMiklos Szeredi /* Expect a negative dentry */ 20352b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 20360dd2b474SMiklos Szeredi 20371e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 20386de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20391e7cb3dcSChuck Lever 20409597c13bSJeff Layton err = nfs_check_flags(open_flags); 20419597c13bSJeff Layton if (err) 20429597c13bSJeff Layton return err; 20439597c13bSJeff Layton 20440dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 20450dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 204600699ad8SAl Viro if (!d_in_lookup(dentry)) { 20470dd2b474SMiklos Szeredi /* 20480dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 20490dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 20500dd2b474SMiklos Szeredi * again 20510dd2b474SMiklos Szeredi */ 2052d9585277SAl Viro return -ENOENT; 20530dd2b474SMiklos Szeredi } 20541472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 20551da177e4SLinus Torvalds goto no_open; 20561da177e4SLinus Torvalds } 20571da177e4SLinus Torvalds 20580dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 2059d9585277SAl Viro return -ENAMETOOLONG; 20601da177e4SLinus Torvalds 20610dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 2062dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 2063dff25ddbSAndreas Gruenbacher 2064dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 2065dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 2066dff25ddbSAndreas Gruenbacher 2067536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 2068dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 20690dd2b474SMiklos Szeredi } 2070536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 2071536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 2072536e43d1STrond Myklebust attr.ia_size = 0; 2073cd9a1c0eSTrond Myklebust } 2074cd9a1c0eSTrond Myklebust 2075c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 2076c94c0953SAl Viro d_drop(dentry); 2077c94c0953SAl Viro switched = true; 2078c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 2079c94c0953SAl Viro &dentry->d_name, &wq); 2080c94c0953SAl Viro if (IS_ERR(dentry)) 2081c94c0953SAl Viro return PTR_ERR(dentry); 2082c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 2083c94c0953SAl Viro return finish_no_open(file, dentry); 2084c94c0953SAl Viro } 2085c94c0953SAl Viro 2086532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 20870dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 20880dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 2089d9585277SAl Viro goto out; 20900dd2b474SMiklos Szeredi 20916e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 209273a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 209373a09dd9SAl Viro if (created) 209473a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 2095275bb307STrond Myklebust if (IS_ERR(inode)) { 20960dd2b474SMiklos Szeredi err = PTR_ERR(inode); 20976e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 20982d9db750STrond Myklebust put_nfs_open_context(ctx); 2099d20cb71dSAl Viro d_drop(dentry); 21000dd2b474SMiklos Szeredi switch (err) { 21011da177e4SLinus Torvalds case -ENOENT: 2102774d9513SPeng Tao d_splice_alias(NULL, dentry); 210368eaba4cSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 210468eaba4cSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 210568eaba4cSTrond Myklebust else 210668eaba4cSTrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 210768eaba4cSTrond Myklebust nfs_set_verifier(dentry, dir_verifier); 21080dd2b474SMiklos Szeredi break; 21091788ea6eSJeff Layton case -EISDIR: 21106f926b5bSTrond Myklebust case -ENOTDIR: 21116f926b5bSTrond Myklebust goto no_open; 21121da177e4SLinus Torvalds case -ELOOP: 21130dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 21141da177e4SLinus Torvalds goto no_open; 21150dd2b474SMiklos Szeredi break; 21161da177e4SLinus Torvalds /* case -EINVAL: */ 21171da177e4SLinus Torvalds default: 21180dd2b474SMiklos Szeredi break; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds goto out; 21211da177e4SLinus Torvalds } 21220dd2b474SMiklos Szeredi 2123b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 21246e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 21252d9db750STrond Myklebust put_nfs_open_context(ctx); 2126d9585277SAl Viro out: 2127c94c0953SAl Viro if (unlikely(switched)) { 2128c94c0953SAl Viro d_lookup_done(dentry); 2129c94c0953SAl Viro dput(dentry); 2130c94c0953SAl Viro } 2131d9585277SAl Viro return err; 21320dd2b474SMiklos Szeredi 21331da177e4SLinus Torvalds no_open: 21341472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 2135ac795161STrond Myklebust if (!res) { 2136ac795161STrond Myklebust inode = d_inode(dentry); 2137ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2138e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) 2139ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 21401751fc1dSTrond Myklebust else if (inode && S_ISREG(inode->i_mode)) 21411751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2142ac795161STrond Myklebust } else if (!IS_ERR(res)) { 2143ac795161STrond Myklebust inode = d_inode(res); 2144ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2145e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) { 2146ac795161STrond Myklebust dput(res); 2147ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 21481751fc1dSTrond Myklebust } else if (inode && S_ISREG(inode->i_mode)) { 21491751fc1dSTrond Myklebust dput(res); 21501751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2151ac795161STrond Myklebust } 2152ac795161STrond Myklebust } 2153c94c0953SAl Viro if (switched) { 2154c94c0953SAl Viro d_lookup_done(dentry); 2155c94c0953SAl Viro if (!res) 2156c94c0953SAl Viro res = dentry; 2157c94c0953SAl Viro else 2158c94c0953SAl Viro dput(dentry); 2159c94c0953SAl Viro } 21600dd2b474SMiklos Szeredi if (IS_ERR(res)) 2161c94c0953SAl Viro return PTR_ERR(res); 2162e45198a6SAl Viro return finish_no_open(file, res); 21631da177e4SLinus Torvalds } 216489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 21651da177e4SLinus Torvalds 2166c7944ebbSTrond Myklebust static int 2167c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 2168c7944ebbSTrond Myklebust unsigned int flags) 21691da177e4SLinus Torvalds { 2170657e94b6SNick Piggin struct inode *inode; 21711da177e4SLinus Torvalds 2172fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 2173c7944ebbSTrond Myklebust goto full_reval; 2174eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 2175c7944ebbSTrond Myklebust goto full_reval; 21762b484297STrond Myklebust 21772b0143b5SDavid Howells inode = d_inode(dentry); 21782b484297STrond Myklebust 21791da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 21801da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 21811da177e4SLinus Torvalds */ 2182c7944ebbSTrond Myklebust if (inode == NULL) 2183c7944ebbSTrond Myklebust goto full_reval; 218449317a7fSNeilBrown 2185efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 2186c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2187216d5d06STrond Myklebust 21881da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 21891da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 2190c7944ebbSTrond Myklebust goto full_reval; 2191c7944ebbSTrond Myklebust 21921da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 2193c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2194c7944ebbSTrond Myklebust goto reval_dentry; 2195c7944ebbSTrond Myklebust 2196c7944ebbSTrond Myklebust /* Check if the directory changed */ 2197c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2198c7944ebbSTrond Myklebust goto reval_dentry; 21991da177e4SLinus Torvalds 22000ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 2201c7944ebbSTrond Myklebust return 1; 2202c7944ebbSTrond Myklebust reval_dentry: 2203c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 2204c7944ebbSTrond Myklebust return -ECHILD; 22050b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags); 22060ef97dcfSMiklos Szeredi 2207c7944ebbSTrond Myklebust full_reval: 2208c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 2209c7944ebbSTrond Myklebust } 2210535918f1STrond Myklebust 2211c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2212c7944ebbSTrond Myklebust { 2213c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 2214c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 2215c0204fd2STrond Myklebust } 2216c0204fd2STrond Myklebust 22171da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 22181da177e4SLinus Torvalds 2219406cd915SBenjamin Coddington struct dentry * 2220406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 2221cc6f3298SAnna Schumaker struct nfs_fattr *fattr) 22221da177e4SLinus Torvalds { 2223fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 22242b0143b5SDavid Howells struct inode *dir = d_inode(parent); 22251da177e4SLinus Torvalds struct inode *inode; 2226b0c6108eSAl Viro struct dentry *d; 2227406cd915SBenjamin Coddington int error; 22281da177e4SLinus Torvalds 2229fab728e1STrond Myklebust d_drop(dentry); 2230fab728e1STrond Myklebust 22311da177e4SLinus Torvalds if (fhandle->size == 0) { 22329558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 22331da177e4SLinus Torvalds if (error) 2234fab728e1STrond Myklebust goto out_error; 22351da177e4SLinus Torvalds } 22365724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 22371da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 22381da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 2239a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 22402ef61e0eSAnna Schumaker fattr, NULL); 22411da177e4SLinus Torvalds if (error < 0) 2242fab728e1STrond Myklebust goto out_error; 22431da177e4SLinus Torvalds } 2244cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 2245b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 2246fab728e1STrond Myklebust out: 2247fab728e1STrond Myklebust dput(parent); 2248406cd915SBenjamin Coddington return d; 2249fab728e1STrond Myklebust out_error: 2250406cd915SBenjamin Coddington d = ERR_PTR(error); 2251406cd915SBenjamin Coddington goto out; 2252406cd915SBenjamin Coddington } 2253406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2254406cd915SBenjamin Coddington 2255406cd915SBenjamin Coddington /* 2256406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 2257406cd915SBenjamin Coddington */ 2258406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2259d91bfc46SAnna Schumaker struct nfs_fattr *fattr) 2260406cd915SBenjamin Coddington { 2261406cd915SBenjamin Coddington struct dentry *d; 2262406cd915SBenjamin Coddington 2263cc6f3298SAnna Schumaker d = nfs_add_or_obtain(dentry, fhandle, fattr); 2264406cd915SBenjamin Coddington if (IS_ERR(d)) 2265406cd915SBenjamin Coddington return PTR_ERR(d); 2266406cd915SBenjamin Coddington 2267406cd915SBenjamin Coddington /* Callers don't care */ 2268406cd915SBenjamin Coddington dput(d); 2269406cd915SBenjamin Coddington return 0; 22701da177e4SLinus Torvalds } 2271ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds /* 22741da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 22751da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 22761da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 22771da177e4SLinus Torvalds * reply path made it appear to have failed. 22781da177e4SLinus Torvalds */ 2279549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2280549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds struct iattr attr; 2283ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 22841da177e4SLinus Torvalds int error; 22851da177e4SLinus Torvalds 22861e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 22876de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds attr.ia_mode = mode; 22901da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 22911da177e4SLinus Torvalds 22928b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 22938867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 22948b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 22951da177e4SLinus Torvalds if (error != 0) 22961da177e4SLinus Torvalds goto out_err; 22971da177e4SLinus Torvalds return 0; 22981da177e4SLinus Torvalds out_err: 22991da177e4SLinus Torvalds d_drop(dentry); 23001da177e4SLinus Torvalds return error; 23011da177e4SLinus Torvalds } 2302ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds /* 23051da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 23061da177e4SLinus Torvalds */ 2307597d9289SBryan Schumaker int 2308549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2309549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev) 23101da177e4SLinus Torvalds { 23111da177e4SLinus Torvalds struct iattr attr; 23121da177e4SLinus Torvalds int status; 23131da177e4SLinus Torvalds 23141e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 23156de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds attr.ia_mode = mode; 23181da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 23191da177e4SLinus Torvalds 23201ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 23211da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 23221ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 23231da177e4SLinus Torvalds if (status != 0) 23241da177e4SLinus Torvalds goto out_err; 23251da177e4SLinus Torvalds return 0; 23261da177e4SLinus Torvalds out_err: 23271da177e4SLinus Torvalds d_drop(dentry); 23281da177e4SLinus Torvalds return status; 23291da177e4SLinus Torvalds } 2330ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds /* 23331da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 23341da177e4SLinus Torvalds */ 2335549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2336549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 23371da177e4SLinus Torvalds { 23381da177e4SLinus Torvalds struct iattr attr; 23391da177e4SLinus Torvalds int error; 23401da177e4SLinus Torvalds 23411e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 23426de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 23451da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 23461da177e4SLinus Torvalds 23471ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 23481da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 23491ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 23501da177e4SLinus Torvalds if (error != 0) 23511da177e4SLinus Torvalds goto out_err; 23521da177e4SLinus Torvalds return 0; 23531da177e4SLinus Torvalds out_err: 23541da177e4SLinus Torvalds d_drop(dentry); 23551da177e4SLinus Torvalds return error; 23561da177e4SLinus Torvalds } 2357ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 23581da177e4SLinus Torvalds 2359d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2360d45b9d8bSTrond Myklebust { 2361dc3f4198SAl Viro if (simple_positive(dentry)) 2362d45b9d8bSTrond Myklebust d_delete(dentry); 2363d45b9d8bSTrond Myklebust } 2364d45b9d8bSTrond Myklebust 23659019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir, 23669019fb39STrond Myklebust struct dentry *dentry, int error) 23679019fb39STrond Myklebust { 23689019fb39STrond Myklebust switch (error) { 23699019fb39STrond Myklebust case -ENOENT: 23709019fb39STrond Myklebust d_delete(dentry); 237100bdadc7STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 237200bdadc7STrond Myklebust break; 23739019fb39STrond Myklebust case 0: 237400bdadc7STrond Myklebust nfs_d_prune_case_insensitive_aliases(d_inode(dentry)); 23759019fb39STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 23769019fb39STrond Myklebust } 23779019fb39STrond Myklebust } 23789019fb39STrond Myklebust 2379597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 23801da177e4SLinus Torvalds { 23811da177e4SLinus Torvalds int error; 23821da177e4SLinus Torvalds 23831e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 23846de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23851da177e4SLinus Torvalds 23861ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 23872b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2388884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 23891da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 23901da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2391ba6c0592STrond Myklebust switch (error) { 2392ba6c0592STrond Myklebust case 0: 23932b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2394ba6c0592STrond Myklebust break; 2395ba6c0592STrond Myklebust case -ENOENT: 2396d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2397ba6c0592STrond Myklebust } 2398884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2399ba6c0592STrond Myklebust } else 2400ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 24019019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 24021ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds return error; 24051da177e4SLinus Torvalds } 2406ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds /* 24091da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 24101da177e4SLinus Torvalds * and after checking that the file has only one user. 24111da177e4SLinus Torvalds * 24121da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 24131da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 24141da177e4SLinus Torvalds */ 24151da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 24161da177e4SLinus Torvalds { 24172b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 24182b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 24191da177e4SLinus Torvalds int error = -EBUSY; 24201da177e4SLinus Torvalds 24216de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 24241da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 24251da177e4SLinus Torvalds error = 0; 24261da177e4SLinus Torvalds goto out; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 24291ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 24301da177e4SLinus Torvalds if (inode != NULL) { 2431912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 24321da177e4SLinus Torvalds if (error == 0) 24331b83d707STrond Myklebust nfs_drop_nlink(inode); 24341da177e4SLinus Torvalds } else 2435912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2436d45b9d8bSTrond Myklebust if (error == -ENOENT) 2437d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 24381ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 24391da177e4SLinus Torvalds out: 24401da177e4SLinus Torvalds return error; 24411da177e4SLinus Torvalds } 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 24441da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 24451da177e4SLinus Torvalds * 24461da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 24471da177e4SLinus Torvalds */ 2448597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 24491da177e4SLinus Torvalds { 24501da177e4SLinus Torvalds int error; 24511da177e4SLinus Torvalds int need_rehash = 0; 24521da177e4SLinus Torvalds 24531e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 24546de1472fSAl Viro dir->i_ino, dentry); 24551da177e4SLinus Torvalds 24561ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 24571da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 245843245ecaSOlga Kornievskaia if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED, 245943245ecaSOlga Kornievskaia &NFS_I(d_inode(dentry))->flags)) { 24601da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2461ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 24622b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 24631da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 24641ca42382STrond Myklebust goto out; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 24671da177e4SLinus Torvalds __d_drop(dentry); 24681da177e4SLinus Torvalds need_rehash = 1; 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 24711da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 24729019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 24739019fb39STrond Myklebust if (need_rehash) 24741da177e4SLinus Torvalds d_rehash(dentry); 24751ca42382STrond Myklebust out: 24761ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 24771da177e4SLinus Torvalds return error; 24781da177e4SLinus Torvalds } 2479ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 24801da177e4SLinus Torvalds 2481873101b3SChuck Lever /* 2482873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2483873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2484873101b3SChuck Lever * using prepare_write/commit_write. 2485873101b3SChuck Lever * 2486873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2487873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2488873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2489873101b3SChuck Lever * symlink request has completed on the server. 2490873101b3SChuck Lever * 2491873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2492873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2493873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2494873101b3SChuck Lever * and move the raw page into its mapping. 2495873101b3SChuck Lever */ 2496549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 2497549c7297SChristian Brauner struct dentry *dentry, const char *symname) 24981da177e4SLinus Torvalds { 2499873101b3SChuck Lever struct page *page; 2500873101b3SChuck Lever char *kaddr; 25011da177e4SLinus Torvalds struct iattr attr; 2502873101b3SChuck Lever unsigned int pathlen = strlen(symname); 25031da177e4SLinus Torvalds int error; 25041da177e4SLinus Torvalds 25051e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 25066de1472fSAl Viro dir->i_ino, dentry, symname); 25071da177e4SLinus Torvalds 2508873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2509873101b3SChuck Lever return -ENAMETOOLONG; 25101da177e4SLinus Torvalds 2511873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2512873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 25131da177e4SLinus Torvalds 2514e8ecde25SAl Viro page = alloc_page(GFP_USER); 251576566991STrond Myklebust if (!page) 2516873101b3SChuck Lever return -ENOMEM; 2517873101b3SChuck Lever 2518e8ecde25SAl Viro kaddr = page_address(page); 2519873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2520873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2521873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2522873101b3SChuck Lever 25231ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 252494a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 25251ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2526873101b3SChuck Lever if (error != 0) { 25271e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2528873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 25296de1472fSAl Viro dentry, symname, error); 25301da177e4SLinus Torvalds d_drop(dentry); 2531873101b3SChuck Lever __free_page(page); 25321da177e4SLinus Torvalds return error; 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 2535342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2536342a67f0STrond Myklebust 2537873101b3SChuck Lever /* 2538873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2539873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2540873101b3SChuck Lever */ 25412b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2542873101b3SChuck Lever GFP_KERNEL)) { 2543873101b3SChuck Lever SetPageUptodate(page); 2544873101b3SChuck Lever unlock_page(page); 2545a0b54addSRafael Aquini /* 2546a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2547a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2548a0b54addSRafael Aquini */ 254909cbfeafSKirill A. Shutemov put_page(page); 2550873101b3SChuck Lever } else 2551873101b3SChuck Lever __free_page(page); 2552873101b3SChuck Lever 2553873101b3SChuck Lever return 0; 2554873101b3SChuck Lever } 2555ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2556873101b3SChuck Lever 2557597d9289SBryan Schumaker int 25581da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 25591da177e4SLinus Torvalds { 25602b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 25611da177e4SLinus Torvalds int error; 25621da177e4SLinus Torvalds 25636de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 25646de1472fSAl Viro old_dentry, dentry); 25651da177e4SLinus Torvalds 25661fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 25679697d234STrond Myklebust d_drop(dentry); 256820497503STrond Myklebust if (S_ISREG(inode->i_mode)) 256920497503STrond Myklebust nfs_sync_inode(inode); 25701da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2571cf809556STrond Myklebust if (error == 0) { 2572342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 25737de9c6eeSAl Viro ihold(inode); 25749697d234STrond Myklebust d_add(dentry, inode); 2575cf809556STrond Myklebust } 25761fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 25771da177e4SLinus Torvalds return error; 25781da177e4SLinus Torvalds } 2579ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds /* 25821da177e4SLinus Torvalds * RENAME 25831da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 25841da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 25851da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 25861da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 25871da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 25881da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 25891da177e4SLinus Torvalds * 25901da177e4SLinus Torvalds * FIXED. 25911da177e4SLinus Torvalds * 25921da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 25931da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 25941da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 25951da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 25961da177e4SLinus Torvalds * using the inode layer 25971da177e4SLinus Torvalds * 25981da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 25991da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 26001da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 26011da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 26021da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 26031da177e4SLinus Torvalds * the rename. 26041da177e4SLinus Torvalds */ 2605549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2606549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 2607549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 26081da177e4SLinus Torvalds { 26092b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 26102b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2611d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 261280a491fdSJeff Layton struct rpc_task *task; 26131da177e4SLinus Torvalds int error = -EBUSY; 26141da177e4SLinus Torvalds 26151cd66c93SMiklos Szeredi if (flags) 26161cd66c93SMiklos Szeredi return -EINVAL; 26171cd66c93SMiklos Szeredi 26186de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 26196de1472fSAl Viro old_dentry, new_dentry, 262084d08fa8SAl Viro d_count(new_dentry)); 26211da177e4SLinus Torvalds 262270ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 26231da177e4SLinus Torvalds /* 262428f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 262528f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 262628f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 262728f79a1aSMiklos Szeredi * the new target. 26281da177e4SLinus Torvalds */ 262927226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 263027226104SMiklos Szeredi /* 263127226104SMiklos Szeredi * To prevent any new references to the target during the 263227226104SMiklos Szeredi * rename, we unhash the dentry in advance. 263327226104SMiklos Szeredi */ 2634d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 263527226104SMiklos Szeredi d_drop(new_dentry); 2636d9f29500SBenjamin Coddington rehash = new_dentry; 2637d9f29500SBenjamin Coddington } 263827226104SMiklos Szeredi 263984d08fa8SAl Viro if (d_count(new_dentry) > 2) { 26401da177e4SLinus Torvalds int err; 264127226104SMiklos Szeredi 26421da177e4SLinus Torvalds /* copy the target dentry's name */ 26431da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 26441da177e4SLinus Torvalds &new_dentry->d_name); 26451da177e4SLinus Torvalds if (!dentry) 26461da177e4SLinus Torvalds goto out; 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds /* silly-rename the existing target ... */ 26491da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 265024e93025SMiklos Szeredi if (err) 26511da177e4SLinus Torvalds goto out; 265224e93025SMiklos Szeredi 265324e93025SMiklos Szeredi new_dentry = dentry; 2654d9f29500SBenjamin Coddington rehash = NULL; 265524e93025SMiklos Szeredi new_inode = NULL; 2656b1e4adf4STrond Myklebust } 265727226104SMiklos Szeredi } 26581da177e4SLinus Torvalds 26596ff9d99bSTrond Myklebust if (S_ISREG(old_inode->i_mode)) 26606ff9d99bSTrond Myklebust nfs_sync_inode(old_inode); 2661d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 266280a491fdSJeff Layton if (IS_ERR(task)) { 266380a491fdSJeff Layton error = PTR_ERR(task); 266480a491fdSJeff Layton goto out; 266580a491fdSJeff Layton } 266680a491fdSJeff Layton 266780a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2668818a8dbeSBenjamin Coddington if (error != 0) { 2669818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2670818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2671818a8dbeSBenjamin Coddington smp_wmb(); 2672818a8dbeSBenjamin Coddington } else 267380a491fdSJeff Layton error = task->tk_status; 267480a491fdSJeff Layton rpc_put_task(task); 267559a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 267659a707b0STrond Myklebust if (error == 0) { 267759a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 267859a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2679ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2680ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME | 2681ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 268259a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 268359a707b0STrond Myklebust } 26841da177e4SLinus Torvalds out: 2685d9f29500SBenjamin Coddington if (rehash) 2686d9f29500SBenjamin Coddington d_rehash(rehash); 268770ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 268870ded201STrond Myklebust new_dir, new_dentry, error); 2689d9f29500SBenjamin Coddington if (!error) { 2690d9f29500SBenjamin Coddington if (new_inode != NULL) 2691d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2692d9f29500SBenjamin Coddington /* 2693d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2694d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2695d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2696d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2697d9f29500SBenjamin Coddington */ 2698d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2699d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2700d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2701d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2702d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2703d9f29500SBenjamin Coddington 27041da177e4SLinus Torvalds /* new dentry created? */ 27051da177e4SLinus Torvalds if (dentry) 27061da177e4SLinus Torvalds dput(dentry); 27071da177e4SLinus Torvalds return error; 27081da177e4SLinus Torvalds } 2709ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 27101da177e4SLinus Torvalds 2711cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2712cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2713cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2714cfcea3e8STrond Myklebust 2715a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 27163a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 27173a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 27183a505845STrond Myklebust 27191c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 27201c3c07e9STrond Myklebust { 27216238aec8SNeilBrown put_group_info(entry->group_info); 2722f682a398SNeilBrown kfree_rcu(entry, rcu_head); 27234e857c58SPeter Zijlstra smp_mb__before_atomic(); 2724cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 27254e857c58SPeter Zijlstra smp_mb__after_atomic(); 27261c3c07e9STrond Myklebust } 27271c3c07e9STrond Myklebust 27281a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 27291a81bb8aSTrond Myklebust { 27301a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 27311a81bb8aSTrond Myklebust 27321a81bb8aSTrond Myklebust while (!list_empty(head)) { 27331a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 27341a81bb8aSTrond Myklebust list_del(&cache->lru); 27351a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 27361a81bb8aSTrond Myklebust } 27371a81bb8aSTrond Myklebust } 27381a81bb8aSTrond Myklebust 27393a505845STrond Myklebust static unsigned long 27403a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2741979df72eSTrond Myklebust { 2742979df72eSTrond Myklebust LIST_HEAD(head); 2743aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2744979df72eSTrond Myklebust struct nfs_access_entry *cache; 27451ab6c499SDave Chinner long freed = 0; 2746979df72eSTrond Myklebust 2747a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2748aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2749979df72eSTrond Myklebust struct inode *inode; 2750979df72eSTrond Myklebust 2751979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2752979df72eSTrond Myklebust break; 27539c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2754979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2755979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2756979df72eSTrond Myklebust goto remove_lru_entry; 2757979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2758979df72eSTrond Myklebust struct nfs_access_entry, lru); 2759979df72eSTrond Myklebust list_move(&cache->lru, &head); 2760979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 27611ab6c499SDave Chinner freed++; 2762979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2763979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2764979df72eSTrond Myklebust &nfs_access_lru_list); 2765979df72eSTrond Myklebust else { 2766979df72eSTrond Myklebust remove_lru_entry: 2767979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 27684e857c58SPeter Zijlstra smp_mb__before_atomic(); 2769979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 27704e857c58SPeter Zijlstra smp_mb__after_atomic(); 2771979df72eSTrond Myklebust } 277259844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2773979df72eSTrond Myklebust } 2774979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 27751a81bb8aSTrond Myklebust nfs_access_free_list(&head); 27761ab6c499SDave Chinner return freed; 27771ab6c499SDave Chinner } 27781ab6c499SDave Chinner 27791ab6c499SDave Chinner unsigned long 27803a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 27813a505845STrond Myklebust { 27823a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 27833a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 27843a505845STrond Myklebust 27853a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 27863a505845STrond Myklebust return SHRINK_STOP; 27873a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 27883a505845STrond Myklebust } 27893a505845STrond Myklebust 27903a505845STrond Myklebust 27913a505845STrond Myklebust unsigned long 27921ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 27931ab6c499SDave Chinner { 279455f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2795979df72eSTrond Myklebust } 2796979df72eSTrond Myklebust 27973a505845STrond Myklebust static void 27983a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 27993a505845STrond Myklebust { 28003a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 28013a505845STrond Myklebust unsigned long diff; 28023a505845STrond Myklebust unsigned int nr_to_scan; 28033a505845STrond Myklebust 28043a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 28053a505845STrond Myklebust return; 28063a505845STrond Myklebust nr_to_scan = 100; 28073a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 28083a505845STrond Myklebust if (diff < nr_to_scan) 28093a505845STrond Myklebust nr_to_scan = diff; 28103a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 28113a505845STrond Myklebust } 28123a505845STrond Myklebust 28131a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 28141c3c07e9STrond Myklebust { 28151c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 28161a81bb8aSTrond Myklebust struct rb_node *n; 28171c3c07e9STrond Myklebust struct nfs_access_entry *entry; 28181c3c07e9STrond Myklebust 28191c3c07e9STrond Myklebust /* Unhook entries from the cache */ 28201c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 28211c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 28221c3c07e9STrond Myklebust rb_erase(n, root_node); 28231a81bb8aSTrond Myklebust list_move(&entry->lru, head); 28241c3c07e9STrond Myklebust } 28251c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 28261c3c07e9STrond Myklebust } 28271c3c07e9STrond Myklebust 28281c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 28291c3c07e9STrond Myklebust { 28301a81bb8aSTrond Myklebust LIST_HEAD(head); 28311a81bb8aSTrond Myklebust 28321a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 28331a81bb8aSTrond Myklebust return; 2834cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2835cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 28361a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2837cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2838cfcea3e8STrond Myklebust 28391c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 28401a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 28411a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 28421a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 28431a81bb8aSTrond Myklebust nfs_access_free_list(&head); 28441c3c07e9STrond Myklebust } 28451c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 28461c3c07e9STrond Myklebust 28476238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b) 28486238aec8SNeilBrown { 28496238aec8SNeilBrown struct group_info *ga, *gb; 28506238aec8SNeilBrown int g; 28516238aec8SNeilBrown 28526238aec8SNeilBrown if (uid_lt(a->fsuid, b->fsuid)) 28536238aec8SNeilBrown return -1; 28546238aec8SNeilBrown if (uid_gt(a->fsuid, b->fsuid)) 28556238aec8SNeilBrown return 1; 28566238aec8SNeilBrown 28576238aec8SNeilBrown if (gid_lt(a->fsgid, b->fsgid)) 28586238aec8SNeilBrown return -1; 28596238aec8SNeilBrown if (gid_gt(a->fsgid, b->fsgid)) 28606238aec8SNeilBrown return 1; 28616238aec8SNeilBrown 28626238aec8SNeilBrown ga = a->group_info; 28636238aec8SNeilBrown gb = b->group_info; 28646238aec8SNeilBrown if (ga == gb) 28656238aec8SNeilBrown return 0; 28666238aec8SNeilBrown if (ga == NULL) 28676238aec8SNeilBrown return -1; 28686238aec8SNeilBrown if (gb == NULL) 28696238aec8SNeilBrown return 1; 28706238aec8SNeilBrown if (ga->ngroups < gb->ngroups) 28716238aec8SNeilBrown return -1; 28726238aec8SNeilBrown if (ga->ngroups > gb->ngroups) 28736238aec8SNeilBrown return 1; 28746238aec8SNeilBrown 28756238aec8SNeilBrown for (g = 0; g < ga->ngroups; g++) { 28766238aec8SNeilBrown if (gid_lt(ga->gid[g], gb->gid[g])) 28776238aec8SNeilBrown return -1; 28786238aec8SNeilBrown if (gid_gt(ga->gid[g], gb->gid[g])) 28796238aec8SNeilBrown return 1; 28806238aec8SNeilBrown } 28816238aec8SNeilBrown return 0; 28826238aec8SNeilBrown } 28836238aec8SNeilBrown 2884b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 28851c3c07e9STrond Myklebust { 28861c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 28871c3c07e9STrond Myklebust 28881c3c07e9STrond Myklebust while (n != NULL) { 2889b68572e0SNeilBrown struct nfs_access_entry *entry = 2890b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 28916238aec8SNeilBrown int cmp = access_cmp(cred, entry); 28921c3c07e9STrond Myklebust 2893b68572e0SNeilBrown if (cmp < 0) 28941c3c07e9STrond Myklebust n = n->rb_left; 2895b68572e0SNeilBrown else if (cmp > 0) 28961c3c07e9STrond Myklebust n = n->rb_right; 28971c3c07e9STrond Myklebust else 28981c3c07e9STrond Myklebust return entry; 28991c3c07e9STrond Myklebust } 29001c3c07e9STrond Myklebust return NULL; 29011c3c07e9STrond Myklebust } 29021c3c07e9STrond Myklebust 2903b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block) 29041da177e4SLinus Torvalds { 290555296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 29061c3c07e9STrond Myklebust struct nfs_access_entry *cache; 290757b69181STrond Myklebust bool retry = true; 290857b69181STrond Myklebust int err; 29091da177e4SLinus Torvalds 29101c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 291157b69181STrond Myklebust for(;;) { 29121c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 29131c3c07e9STrond Myklebust goto out_zap; 29141c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 291557b69181STrond Myklebust err = -ENOENT; 29161c3c07e9STrond Myklebust if (cache == NULL) 29171c3c07e9STrond Myklebust goto out; 291857b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 291921c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 292057b69181STrond Myklebust break; 29215c965db8STrond Myklebust if (!retry) 29225c965db8STrond Myklebust break; 292357b69181STrond Myklebust err = -ECHILD; 292457b69181STrond Myklebust if (!may_block) 292557b69181STrond Myklebust goto out; 292657b69181STrond Myklebust spin_unlock(&inode->i_lock); 292757b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 292857b69181STrond Myklebust if (err) 292957b69181STrond Myklebust return err; 293057b69181STrond Myklebust spin_lock(&inode->i_lock); 293157b69181STrond Myklebust retry = false; 293257b69181STrond Myklebust } 2933b5e7b59cSNeilBrown *mask = cache->mask; 2934cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 29351c3c07e9STrond Myklebust err = 0; 29361c3c07e9STrond Myklebust out: 29371c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29381c3c07e9STrond Myklebust return err; 29391c3c07e9STrond Myklebust out_zap: 29401a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 29411a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 29421c3c07e9STrond Myklebust return -ENOENT; 29431c3c07e9STrond Myklebust } 29441c3c07e9STrond Myklebust 2945b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask) 2946f682a398SNeilBrown { 2947f682a398SNeilBrown /* Only check the most recently returned cache entry, 2948f682a398SNeilBrown * but do it without locking. 2949f682a398SNeilBrown */ 2950f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2951f682a398SNeilBrown struct nfs_access_entry *cache; 2952f682a398SNeilBrown int err = -ECHILD; 2953f682a398SNeilBrown struct list_head *lh; 2954f682a398SNeilBrown 2955f682a398SNeilBrown rcu_read_lock(); 2956f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2957f682a398SNeilBrown goto out; 29589f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2959f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2960f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 29616238aec8SNeilBrown access_cmp(cred, cache) != 0) 2962f682a398SNeilBrown cache = NULL; 2963f682a398SNeilBrown if (cache == NULL) 2964f682a398SNeilBrown goto out; 296521c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2966f682a398SNeilBrown goto out; 2967b5e7b59cSNeilBrown *mask = cache->mask; 296821c3ba7eSTrond Myklebust err = 0; 2969f682a398SNeilBrown out: 2970f682a398SNeilBrown rcu_read_unlock(); 2971f682a398SNeilBrown return err; 2972f682a398SNeilBrown } 2973f682a398SNeilBrown 2974b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred, 2975b5e7b59cSNeilBrown u32 *mask, bool may_block) 2976d2ae4f8bSFrank van der Linden { 2977d2ae4f8bSFrank van der Linden int status; 2978d2ae4f8bSFrank van der Linden 2979b5e7b59cSNeilBrown status = nfs_access_get_cached_rcu(inode, cred, mask); 2980d2ae4f8bSFrank van der Linden if (status != 0) 2981b5e7b59cSNeilBrown status = nfs_access_get_cached_locked(inode, cred, mask, 2982d2ae4f8bSFrank van der Linden may_block); 2983d2ae4f8bSFrank van der Linden 2984d2ae4f8bSFrank van der Linden return status; 2985d2ae4f8bSFrank van der Linden } 2986d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2987d2ae4f8bSFrank van der Linden 298873fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode, 298973fbb3faSNeilBrown struct nfs_access_entry *set, 299073fbb3faSNeilBrown const struct cred *cred) 29911c3c07e9STrond Myklebust { 2992cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2993cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 29941c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 29951c3c07e9STrond Myklebust struct rb_node *parent = NULL; 29961c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2997b68572e0SNeilBrown int cmp; 29981c3c07e9STrond Myklebust 29991c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 30001c3c07e9STrond Myklebust while (*p != NULL) { 30011c3c07e9STrond Myklebust parent = *p; 30021c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 30036238aec8SNeilBrown cmp = access_cmp(cred, entry); 30041c3c07e9STrond Myklebust 3005b68572e0SNeilBrown if (cmp < 0) 30061c3c07e9STrond Myklebust p = &parent->rb_left; 3007b68572e0SNeilBrown else if (cmp > 0) 30081c3c07e9STrond Myklebust p = &parent->rb_right; 30091c3c07e9STrond Myklebust else 30101c3c07e9STrond Myklebust goto found; 30111c3c07e9STrond Myklebust } 30121c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 30131c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 3014cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 30151c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 30161c3c07e9STrond Myklebust return; 30171c3c07e9STrond Myklebust found: 30181c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 3019cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 3020cfcea3e8STrond Myklebust list_del(&entry->lru); 30211c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 30221c3c07e9STrond Myklebust nfs_access_free_entry(entry); 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds 302573fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set, 302673fbb3faSNeilBrown const struct cred *cred) 30271da177e4SLinus Torvalds { 30281c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 30291c3c07e9STrond Myklebust if (cache == NULL) 30301c3c07e9STrond Myklebust return; 30311c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 30326238aec8SNeilBrown cache->fsuid = cred->fsuid; 30336238aec8SNeilBrown cache->fsgid = cred->fsgid; 30346238aec8SNeilBrown cache->group_info = get_group_info(cred->group_info); 30351da177e4SLinus Torvalds cache->mask = set->mask; 30361c3c07e9STrond Myklebust 3037f682a398SNeilBrown /* The above field assignments must be visible 3038f682a398SNeilBrown * before this item appears on the lru. We cannot easily 3039f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 3040f682a398SNeilBrown */ 3041f682a398SNeilBrown smp_wmb(); 304273fbb3faSNeilBrown nfs_access_add_rbtree(inode, cache, cred); 3043cfcea3e8STrond Myklebust 3044cfcea3e8STrond Myklebust /* Update accounting */ 30454e857c58SPeter Zijlstra smp_mb__before_atomic(); 3046cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 30474e857c58SPeter Zijlstra smp_mb__after_atomic(); 3048cfcea3e8STrond Myklebust 3049cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 30501a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 3051cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 30521a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 30531a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 30541a81bb8aSTrond Myklebust &nfs_access_lru_list); 3055cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 3056cfcea3e8STrond Myklebust } 30573a505845STrond Myklebust nfs_access_cache_enforce_limit(); 30581da177e4SLinus Torvalds } 30596168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 30606168f62cSWeston Andros Adamson 30613c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 30623c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 30633c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 30643c181827SAnna Schumaker NFS_ACCESS_DELETE) 30653c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 30663c181827SAnna Schumaker NFS_ACCESS_EXTEND) 3067ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 30683c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 30693c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 307015d4b73aSTrond Myklebust static int 3071ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 307215d4b73aSTrond Myklebust { 307315d4b73aSTrond Myklebust int mask = 0; 307415d4b73aSTrond Myklebust 307515d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 307615d4b73aSTrond Myklebust mask |= MAY_READ; 3077ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 3078ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 307915d4b73aSTrond Myklebust mask |= MAY_WRITE; 3080ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 308115d4b73aSTrond Myklebust mask |= MAY_EXEC; 3082ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 3083ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 3084ecbb903cSTrond Myklebust mask |= MAY_WRITE; 3085ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 308615d4b73aSTrond Myklebust mask |= MAY_EXEC; 3087ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 3088ecbb903cSTrond Myklebust mask |= MAY_WRITE; 308915d4b73aSTrond Myklebust return mask; 309015d4b73aSTrond Myklebust } 309115d4b73aSTrond Myklebust 30926168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 30936168f62cSWeston Andros Adamson { 3094bd8b2441STrond Myklebust entry->mask = access_result; 30956168f62cSWeston Andros Adamson } 30966168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 30971da177e4SLinus Torvalds 3098b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 30991da177e4SLinus Torvalds { 31001da177e4SLinus Torvalds struct nfs_access_entry cache; 310157b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 3102e8194b7dSTrond Myklebust int cache_mask = -1; 31031da177e4SLinus Torvalds int status; 31041da177e4SLinus Torvalds 3105f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 3106f4ce1299STrond Myklebust 3107b5e7b59cSNeilBrown status = nfs_access_get_cached(inode, cred, &cache.mask, may_block); 31081da177e4SLinus Torvalds if (status == 0) 3109f4ce1299STrond Myklebust goto out_cached; 31101da177e4SLinus Torvalds 3111f3324a2aSNeilBrown status = -ECHILD; 311257b69181STrond Myklebust if (!may_block) 3113f3324a2aSNeilBrown goto out; 3114f3324a2aSNeilBrown 31151750d929SAnna Schumaker /* 31161750d929SAnna Schumaker * Determine which access bits we want to ask for... 31171750d929SAnna Schumaker */ 311884631f84STrond Myklebust cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND | 311984631f84STrond Myklebust nfs_access_xattr_mask(NFS_SERVER(inode)); 31201750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 31211750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 31221750d929SAnna Schumaker else 31231750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 312473fbb3faSNeilBrown status = NFS_PROTO(inode)->access(inode, &cache, cred); 3125a71ee337SSuresh Jayaraman if (status != 0) { 3126a71ee337SSuresh Jayaraman if (status == -ESTALE) { 3127a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 312893ce4af7STrond Myklebust nfs_set_inode_stale(inode); 312993ce4af7STrond Myklebust else 313093ce4af7STrond Myklebust nfs_zap_caches(inode); 3131a71ee337SSuresh Jayaraman } 3132f4ce1299STrond Myklebust goto out; 3133a71ee337SSuresh Jayaraman } 313473fbb3faSNeilBrown nfs_access_add_cache(inode, &cache, cred); 3135f4ce1299STrond Myklebust out_cached: 3136ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 3137bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 3138f4ce1299STrond Myklebust status = -EACCES; 31391da177e4SLinus Torvalds out: 3140e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 3141f4ce1299STrond Myklebust return status; 31421da177e4SLinus Torvalds } 31431da177e4SLinus Torvalds 3144af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 3145af22f94aSTrond Myklebust { 3146af22f94aSTrond Myklebust int mask = 0; 3147af22f94aSTrond Myklebust 3148f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 3149f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 3150f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 3151f8d9a897SWeston Andros Adamson } else { 31528a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 3153af22f94aSTrond Myklebust mask |= MAY_READ; 31548a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 3155af22f94aSTrond Myklebust mask |= MAY_WRITE; 3156f8d9a897SWeston Andros Adamson } 3157f8d9a897SWeston Andros Adamson 3158af22f94aSTrond Myklebust return mask; 3159af22f94aSTrond Myklebust } 3160af22f94aSTrond Myklebust 3161b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 3162af22f94aSTrond Myklebust { 3163af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 3164af22f94aSTrond Myklebust } 316589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 3166af22f94aSTrond Myklebust 31675c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 31685c5fc09aSTrond Myklebust { 31695c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 317021c3ba7eSTrond Myklebust int ret = 0; 31715c5fc09aSTrond Myklebust 31723825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 31733825827eSTrond Myklebust return 0; 3174720869ebSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { 31755c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 317621c3ba7eSTrond Myklebust return -ECHILD; 317721c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 317821c3ba7eSTrond Myklebust } 31795c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 31805c5fc09aSTrond Myklebust ret = -EACCES; 31815c5fc09aSTrond Myklebust return ret; 31825c5fc09aSTrond Myklebust } 31835c5fc09aSTrond Myklebust 3184549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns, 3185549c7297SChristian Brauner struct inode *inode, 3186549c7297SChristian Brauner int mask) 31871da177e4SLinus Torvalds { 3188b68572e0SNeilBrown const struct cred *cred = current_cred(); 31891da177e4SLinus Torvalds int res = 0; 31901da177e4SLinus Torvalds 319191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 319291d5b470SChuck Lever 3193e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 31941da177e4SLinus Torvalds goto out; 31951da177e4SLinus Torvalds /* Is this sys_access() ? */ 31969cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 31971da177e4SLinus Torvalds goto force_lookup; 31981da177e4SLinus Torvalds 31991da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 32001da177e4SLinus Torvalds case S_IFLNK: 32011da177e4SLinus Torvalds goto out; 32021da177e4SLinus Torvalds case S_IFREG: 3203762674f8STrond Myklebust if ((mask & MAY_OPEN) && 3204762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 3205762674f8STrond Myklebust return 0; 32061da177e4SLinus Torvalds break; 32071da177e4SLinus Torvalds case S_IFDIR: 32081da177e4SLinus Torvalds /* 32091da177e4SLinus Torvalds * Optimize away all write operations, since the server 32101da177e4SLinus Torvalds * will check permissions when we perform the op. 32111da177e4SLinus Torvalds */ 32121da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 32131da177e4SLinus Torvalds goto out; 32141da177e4SLinus Torvalds } 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds force_lookup: 32171da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 32181da177e4SLinus Torvalds goto out_notsup; 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 32211da177e4SLinus Torvalds out: 32225c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 32235c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 3224f696a365SMiklos Szeredi 32251e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 32261e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 32271da177e4SLinus Torvalds return res; 32281da177e4SLinus Torvalds out_notsup: 3229d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 3230d51ac1a8SNeilBrown return -ECHILD; 3231d51ac1a8SNeilBrown 3232720869ebSTrond Myklebust res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE | 3233720869ebSTrond Myklebust NFS_INO_INVALID_OTHER); 32341da177e4SLinus Torvalds if (res == 0) 323547291baaSChristian Brauner res = generic_permission(&init_user_ns, inode, mask); 32361e7cb3dcSChuck Lever goto out; 32371da177e4SLinus Torvalds } 3238ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 3239