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> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include "delegation.h" 4491d5b470SChuck Lever #include "iostat.h" 454c30d56eSAdrian Bunk #include "internal.h" 46cd9a1c0eSTrond Myklebust #include "fscache.h" 471da177e4SLinus Torvalds 48f4ce1299STrond Myklebust #include "nfstrace.h" 49f4ce1299STrond Myklebust 501da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 53480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5423db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5502c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 56f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5711de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 581da177e4SLinus Torvalds 594b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 60f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 611da177e4SLinus Torvalds .read = generic_read_dir, 6293a6ab7bSTrond Myklebust .iterate_shared = nfs_readdir, 631da177e4SLinus Torvalds .open = nfs_opendir, 64480c2006SBryan Schumaker .release = nfs_closedir, 651da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 661da177e4SLinus Torvalds }; 671da177e4SLinus Torvalds 6811de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6911de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 70d1bacf9eSBryan Schumaker }; 71d1bacf9eSBryan Schumaker 72281f31b2STrond Myklebust static struct nfs_open_dir_context * 73281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir) 74480c2006SBryan Schumaker { 75311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 76480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 77281f31b2STrond Myklebust 78281f31b2STrond Myklebust ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); 79480c2006SBryan Schumaker if (ctx != NULL) { 80311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 81311324adSTrond Myklebust spin_lock(&dir->i_lock); 821c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 831c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 84ac46b3d7STrond Myklebust nfs_set_cache_invalid(dir, 85ac46b3d7STrond Myklebust NFS_INO_INVALID_DATA | 86ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 87311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 88ff81dfb5STrond Myklebust clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags); 89d1e32ea3STrond Myklebust memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf)); 90311324adSTrond Myklebust spin_unlock(&dir->i_lock); 91480c2006SBryan Schumaker return ctx; 92480c2006SBryan Schumaker } 930c030806STrond Myklebust return ERR_PTR(-ENOMEM); 940c030806STrond Myklebust } 95480c2006SBryan Schumaker 96311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 97480c2006SBryan Schumaker { 98311324adSTrond Myklebust spin_lock(&dir->i_lock); 99311324adSTrond Myklebust list_del(&ctx->list); 100311324adSTrond Myklebust spin_unlock(&dir->i_lock); 101480c2006SBryan Schumaker kfree(ctx); 102480c2006SBryan Schumaker } 103480c2006SBryan Schumaker 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * Open file 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds static int 1081da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1091da177e4SLinus Torvalds { 110480c2006SBryan Schumaker int res = 0; 111480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1121da177e4SLinus Torvalds 1136de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 114cc0dd2d1SChuck Lever 115cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1161e7cb3dcSChuck Lever 11793b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode); 118480c2006SBryan Schumaker if (IS_ERR(ctx)) { 119480c2006SBryan Schumaker res = PTR_ERR(ctx); 120480c2006SBryan Schumaker goto out; 121480c2006SBryan Schumaker } 122480c2006SBryan Schumaker filp->private_data = ctx; 123480c2006SBryan Schumaker out: 1241da177e4SLinus Torvalds return res; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127480c2006SBryan Schumaker static int 128480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 129480c2006SBryan Schumaker { 130a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 131480c2006SBryan Schumaker return 0; 132480c2006SBryan Schumaker } 133480c2006SBryan Schumaker 134d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 135d1bacf9eSBryan Schumaker u64 cookie; 136d1bacf9eSBryan Schumaker u64 ino; 137a52a8a6aSTrond Myklebust const char *name; 138a52a8a6aSTrond Myklebust unsigned int name_len; 1390b26a0bfSTrond Myklebust unsigned char d_type; 140d1bacf9eSBryan Schumaker }; 141d1bacf9eSBryan Schumaker 142d1bacf9eSBryan Schumaker struct nfs_cache_array { 143d09e673fSTrond Myklebust u64 change_attr; 144d1bacf9eSBryan Schumaker u64 last_cookie; 145b1e21c97STrond Myklebust unsigned int size; 146b1e21c97STrond Myklebust unsigned char page_full : 1, 147762567b7STrond Myklebust page_is_eof : 1, 148762567b7STrond Myklebust cookies_are_ordered : 1; 1495601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 150d1bacf9eSBryan Schumaker }; 151d1bacf9eSBryan Schumaker 1526c981effSTrond Myklebust struct nfs_readdir_descriptor { 1531da177e4SLinus Torvalds struct file *file; 1541da177e4SLinus Torvalds struct page *page; 15523db8620SAl Viro struct dir_context *ctx; 1561f1d4aa4STrond Myklebust pgoff_t page_index; 1572e7a4641STrond Myklebust u64 dir_cookie; 1580aded708STrond Myklebust u64 last_cookie; 1592e7a4641STrond Myklebust u64 dup_cookie; 160f0dd2136STrond Myklebust loff_t current_index; 16159e356a9STrond Myklebust loff_t prev_index; 162d1bacf9eSBryan Schumaker 163b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 164a1147b82STrond Myklebust unsigned long dir_verifier; 1651f4eab7eSNeil Brown unsigned long timestamp; 1664704f0e2STrond Myklebust unsigned long gencount; 1672e7a4641STrond Myklebust unsigned long attr_gencount; 168d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 1692e7a4641STrond Myklebust signed char duped; 170a7a3b1e9SBenjamin Coddington bool plus; 171e1d2699bSTrond Myklebust bool eob; 172a7a3b1e9SBenjamin Coddington bool eof; 1736c981effSTrond Myklebust }; 1741da177e4SLinus Torvalds 1751f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array) 1761f1d4aa4STrond Myklebust { 1771f1d4aa4STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1781f1d4aa4STrond Myklebust } 1791f1d4aa4STrond Myklebust 180d09e673fSTrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie, 181d09e673fSTrond Myklebust u64 change_attr) 1824b310319STrond Myklebust { 1834b310319STrond Myklebust struct nfs_cache_array *array; 1844b310319STrond Myklebust 1854b310319STrond Myklebust array = kmap_atomic(page); 1861f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 187d09e673fSTrond Myklebust array->change_attr = change_attr; 1881f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 189762567b7STrond Myklebust array->cookies_are_ordered = 1; 1904b310319STrond Myklebust kunmap_atomic(array); 1914b310319STrond Myklebust } 1924b310319STrond Myklebust 193d1bacf9eSBryan Schumaker /* 194d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 195d1bacf9eSBryan Schumaker */ 196d1bacf9eSBryan Schumaker static 19711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 198d1bacf9eSBryan Schumaker { 19911de3b11STrond Myklebust struct nfs_cache_array *array; 200d1bacf9eSBryan Schumaker int i; 2018cd51a0cSTrond Myklebust 2022b86ce2dSCong Wang array = kmap_atomic(page); 203d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 204a52a8a6aSTrond Myklebust kfree(array->array[i].name); 2051f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 2062b86ce2dSCong Wang kunmap_atomic(array); 207d1bacf9eSBryan Schumaker } 208d1bacf9eSBryan Schumaker 20935df59d3STrond Myklebust static struct page * 21035df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags) 21135df59d3STrond Myklebust { 21235df59d3STrond Myklebust struct page *page = alloc_page(gfp_flags); 21335df59d3STrond Myklebust if (page) 214d09e673fSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, 0); 21535df59d3STrond Myklebust return page; 21635df59d3STrond Myklebust } 21735df59d3STrond Myklebust 21835df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page) 21935df59d3STrond Myklebust { 22035df59d3STrond Myklebust if (page) { 22135df59d3STrond Myklebust nfs_readdir_clear_array(page); 22235df59d3STrond Myklebust put_page(page); 22335df59d3STrond Myklebust } 22435df59d3STrond Myklebust } 22535df59d3STrond Myklebust 226b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 227b1e21c97STrond Myklebust { 228b1e21c97STrond Myklebust array->page_is_eof = 1; 229b1e21c97STrond Myklebust array->page_full = 1; 230b1e21c97STrond Myklebust } 231b1e21c97STrond Myklebust 232b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 233b1e21c97STrond Myklebust { 234b1e21c97STrond Myklebust return array->page_full; 235b1e21c97STrond Myklebust } 236b1e21c97STrond Myklebust 237d1bacf9eSBryan Schumaker /* 238d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 239d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 240d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 241d1bacf9eSBryan Schumaker */ 242a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 243d1bacf9eSBryan Schumaker { 244a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 245a52a8a6aSTrond Myklebust 24604e4bd1cSCatalin Marinas /* 24704e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 24804e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 24904e4bd1cSCatalin Marinas */ 250a52a8a6aSTrond Myklebust if (ret != NULL) 251a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 252a52a8a6aSTrond Myklebust return ret; 253d1bacf9eSBryan Schumaker } 254d1bacf9eSBryan Schumaker 2550b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void) 2560b2662b7STrond Myklebust { 2570b2662b7STrond Myklebust return (PAGE_SIZE - sizeof(struct nfs_cache_array)) / 2580b2662b7STrond Myklebust sizeof(struct nfs_cache_array_entry); 2590b2662b7STrond Myklebust } 2600b2662b7STrond Myklebust 261b1e21c97STrond Myklebust /* 262b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 263b1e21c97STrond Myklebust */ 264b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 265b1e21c97STrond Myklebust { 266b1e21c97STrond Myklebust if (array->page_full) 267b1e21c97STrond Myklebust return -ENOSPC; 2680b2662b7STrond Myklebust if (array->size == nfs_readdir_array_maxentries()) { 269b1e21c97STrond Myklebust array->page_full = 1; 270b1e21c97STrond Myklebust return -ENOSPC; 271b1e21c97STrond Myklebust } 272d1bacf9eSBryan Schumaker return 0; 273d1bacf9eSBryan Schumaker } 274d1bacf9eSBryan Schumaker 275d1bacf9eSBryan Schumaker static 276d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 277d1bacf9eSBryan Schumaker { 278a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 2794a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 280a52a8a6aSTrond Myklebust const char *name; 2814a201d6eSTrond Myklebust int ret; 2824a201d6eSTrond Myklebust 283a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 284a52a8a6aSTrond Myklebust if (!name) 285a52a8a6aSTrond Myklebust return -ENOMEM; 2863020093fSTrond Myklebust 287a52a8a6aSTrond Myklebust array = kmap_atomic(page); 288b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 289a52a8a6aSTrond Myklebust if (ret) { 290a52a8a6aSTrond Myklebust kfree(name); 2913020093fSTrond Myklebust goto out; 292a52a8a6aSTrond Myklebust } 2933020093fSTrond Myklebust 294b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 2954a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2964a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2970b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 298a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 299a52a8a6aSTrond Myklebust cache_entry->name = name; 300d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 301762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie) 302762567b7STrond Myklebust array->cookies_are_ordered = 0; 3038cd51a0cSTrond Myklebust array->size++; 30447c716cbSTrond Myklebust if (entry->eof != 0) 305b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 3064a201d6eSTrond Myklebust out: 307a52a8a6aSTrond Myklebust kunmap_atomic(array); 3084a201d6eSTrond Myklebust return ret; 309d1bacf9eSBryan Schumaker } 310d1bacf9eSBryan Schumaker 311d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie, 312d09e673fSTrond Myklebust u64 change_attr) 313d09e673fSTrond Myklebust { 314d09e673fSTrond Myklebust struct nfs_cache_array *array = kmap_atomic(page); 315d09e673fSTrond Myklebust int ret = true; 316d09e673fSTrond Myklebust 317d09e673fSTrond Myklebust if (array->change_attr != change_attr) 318d09e673fSTrond Myklebust ret = false; 319d09e673fSTrond Myklebust if (array->size > 0 && array->array[0].cookie != last_cookie) 320d09e673fSTrond Myklebust ret = false; 321d09e673fSTrond Myklebust kunmap_atomic(array); 322d09e673fSTrond Myklebust return ret; 323d09e673fSTrond Myklebust } 324d09e673fSTrond Myklebust 325d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 326d09e673fSTrond Myklebust { 327d09e673fSTrond Myklebust unlock_page(page); 328d09e673fSTrond Myklebust put_page(page); 329d09e673fSTrond Myklebust } 330d09e673fSTrond Myklebust 3311f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 3321f1d4aa4STrond Myklebust pgoff_t index, u64 last_cookie) 3331f1d4aa4STrond Myklebust { 3341f1d4aa4STrond Myklebust struct page *page; 335d09e673fSTrond Myklebust u64 change_attr; 3361f1d4aa4STrond Myklebust 3371f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 338d09e673fSTrond Myklebust if (!page) 339d09e673fSTrond Myklebust return NULL; 340d09e673fSTrond Myklebust change_attr = inode_peek_iversion_raw(mapping->host); 341d09e673fSTrond Myklebust if (PageUptodate(page)) { 342d09e673fSTrond Myklebust if (nfs_readdir_page_validate(page, last_cookie, change_attr)) 343d09e673fSTrond Myklebust return page; 344d09e673fSTrond Myklebust nfs_readdir_clear_array(page); 3451f1d4aa4STrond Myklebust } 346d09e673fSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, change_attr); 347d09e673fSTrond Myklebust SetPageUptodate(page); 3481f1d4aa4STrond Myklebust return page; 3491f1d4aa4STrond Myklebust } 3501f1d4aa4STrond Myklebust 3510b2662b7STrond Myklebust static loff_t nfs_readdir_page_offset(struct page *page) 3520b2662b7STrond Myklebust { 3530b2662b7STrond Myklebust return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries(); 3540b2662b7STrond Myklebust } 3550b2662b7STrond Myklebust 3561f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 3571f1d4aa4STrond Myklebust { 3581f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3591f1d4aa4STrond Myklebust u64 ret; 3601f1d4aa4STrond Myklebust 3611f1d4aa4STrond Myklebust array = kmap_atomic(page); 3621f1d4aa4STrond Myklebust ret = array->last_cookie; 3631f1d4aa4STrond Myklebust kunmap_atomic(array); 3641f1d4aa4STrond Myklebust return ret; 3651f1d4aa4STrond Myklebust } 3661f1d4aa4STrond Myklebust 3671f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 3681f1d4aa4STrond Myklebust { 3691f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3701f1d4aa4STrond Myklebust bool ret; 3711f1d4aa4STrond Myklebust 3721f1d4aa4STrond Myklebust array = kmap_atomic(page); 3731f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 3741f1d4aa4STrond Myklebust kunmap_atomic(array); 3751f1d4aa4STrond Myklebust return ret; 3761f1d4aa4STrond Myklebust } 3771f1d4aa4STrond Myklebust 378b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 379b1e21c97STrond Myklebust { 380b1e21c97STrond Myklebust struct nfs_cache_array *array; 381b1e21c97STrond Myklebust 382b1e21c97STrond Myklebust array = kmap_atomic(page); 383b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 384b1e21c97STrond Myklebust kunmap_atomic(array); 385b1e21c97STrond Myklebust } 386b1e21c97STrond Myklebust 3873b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 3883b2a09f1STrond Myklebust pgoff_t index, u64 cookie) 3893b2a09f1STrond Myklebust { 3903b2a09f1STrond Myklebust struct page *page; 3913b2a09f1STrond Myklebust 3923b2a09f1STrond Myklebust page = nfs_readdir_page_get_locked(mapping, index, cookie); 3933b2a09f1STrond Myklebust if (page) { 3943b2a09f1STrond Myklebust if (nfs_readdir_page_last_cookie(page) == cookie) 3953b2a09f1STrond Myklebust return page; 3963b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 3973b2a09f1STrond Myklebust } 3983b2a09f1STrond Myklebust return NULL; 3993b2a09f1STrond Myklebust } 4003b2a09f1STrond Myklebust 40159e356a9STrond Myklebust static inline 40259e356a9STrond Myklebust int is_32bit_api(void) 40359e356a9STrond Myklebust { 40459e356a9STrond Myklebust #ifdef CONFIG_COMPAT 40559e356a9STrond Myklebust return in_compat_syscall(); 40659e356a9STrond Myklebust #else 40759e356a9STrond Myklebust return (BITS_PER_LONG == 32); 40859e356a9STrond Myklebust #endif 40959e356a9STrond Myklebust } 41059e356a9STrond Myklebust 41159e356a9STrond Myklebust static 41259e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 41359e356a9STrond Myklebust { 41459e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 41559e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 41659e356a9STrond Myklebust return false; 41759e356a9STrond Myklebust return true; 41859e356a9STrond Myklebust } 41959e356a9STrond Myklebust 4206c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 4216c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 422d1bacf9eSBryan Schumaker { 42323db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 424d1bacf9eSBryan Schumaker unsigned int index; 425d1bacf9eSBryan Schumaker 426d1bacf9eSBryan Schumaker if (diff < 0) 427d1bacf9eSBryan Schumaker goto out_eof; 428d1bacf9eSBryan Schumaker if (diff >= array->size) { 429b1e21c97STrond Myklebust if (array->page_is_eof) 430d1bacf9eSBryan Schumaker goto out_eof; 431d1bacf9eSBryan Schumaker return -EAGAIN; 432d1bacf9eSBryan Schumaker } 433d1bacf9eSBryan Schumaker 434d1bacf9eSBryan Schumaker index = (unsigned int)diff; 4352e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 436d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 437d1bacf9eSBryan Schumaker return 0; 438d1bacf9eSBryan Schumaker out_eof: 4396089dd0dSThomas Meyer desc->eof = true; 440d1bacf9eSBryan Schumaker return -EBADCOOKIE; 441d1bacf9eSBryan Schumaker } 442d1bacf9eSBryan Schumaker 443762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 444762567b7STrond Myklebust u64 cookie) 445762567b7STrond Myklebust { 446762567b7STrond Myklebust if (!array->cookies_are_ordered) 447762567b7STrond Myklebust return true; 448762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */ 449762567b7STrond Myklebust if (cookie >= array->last_cookie) 450762567b7STrond Myklebust return false; 451762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie) 452762567b7STrond Myklebust return false; 453762567b7STrond Myklebust return true; 454762567b7STrond Myklebust } 455762567b7STrond Myklebust 4566c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 4576c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 458d1bacf9eSBryan Schumaker { 459d1bacf9eSBryan Schumaker int i; 4608ef2ce3eSBryan Schumaker loff_t new_pos; 461d1bacf9eSBryan Schumaker int status = -EAGAIN; 462d1bacf9eSBryan Schumaker 463762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 464762567b7STrond Myklebust goto check_eof; 465762567b7STrond Myklebust 466d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 4672e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 468496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 4690c030806STrond Myklebust 4700b2662b7STrond Myklebust new_pos = nfs_readdir_page_offset(desc->page) + i; 471d09e673fSTrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount) { 4722e7a4641STrond Myklebust desc->duped = 0; 4732e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 47459e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 4752e7a4641STrond Myklebust if (desc->duped > 0 4762e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 4770c030806STrond Myklebust if (printk_ratelimit()) { 4786de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 4790c030806STrond Myklebust "Please contact your server vendor. " 480a52a8a6aSTrond Myklebust "The file: %s has duplicate cookie %llu\n", 481a52a8a6aSTrond Myklebust desc->file, array->array[i].name, desc->dir_cookie); 4820c030806STrond Myklebust } 4830c030806STrond Myklebust status = -ELOOP; 4840c030806STrond Myklebust goto out; 4850c030806STrond Myklebust } 4862e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 4872e7a4641STrond Myklebust desc->duped = -1; 4888ef2ce3eSBryan Schumaker } 48959e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 4902e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 49159e356a9STrond Myklebust else 49223db8620SAl Viro desc->ctx->pos = new_pos; 49359e356a9STrond Myklebust desc->prev_index = new_pos; 4948cd51a0cSTrond Myklebust desc->cache_entry_index = i; 49547c716cbSTrond Myklebust return 0; 4968cd51a0cSTrond Myklebust } 4978cd51a0cSTrond Myklebust } 498762567b7STrond Myklebust check_eof: 499b1e21c97STrond Myklebust if (array->page_is_eof) { 500d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 5012e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 5026089dd0dSThomas Meyer desc->eof = true; 503d1bacf9eSBryan Schumaker } 5040c030806STrond Myklebust out: 505d1bacf9eSBryan Schumaker return status; 506d1bacf9eSBryan Schumaker } 507d1bacf9eSBryan Schumaker 5086c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 509d1bacf9eSBryan Schumaker { 510d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 51147c716cbSTrond Myklebust int status; 512d1bacf9eSBryan Schumaker 513ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 514d1bacf9eSBryan Schumaker 5152e7a4641STrond Myklebust if (desc->dir_cookie == 0) 516d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 517d1bacf9eSBryan Schumaker else 518d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 519d1bacf9eSBryan Schumaker 52047c716cbSTrond Myklebust if (status == -EAGAIN) { 5210aded708STrond Myklebust desc->last_cookie = array->last_cookie; 522e47c085aSTrond Myklebust desc->current_index += array->size; 52347c716cbSTrond Myklebust desc->page_index++; 52447c716cbSTrond Myklebust } 525ed09222dSTrond Myklebust kunmap_atomic(array); 526d1bacf9eSBryan Schumaker return status; 527d1bacf9eSBryan Schumaker } 528d1bacf9eSBryan Schumaker 529d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 53093b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 531b593c09fSTrond Myklebust __be32 *verf, u64 cookie, 532b593c09fSTrond Myklebust struct page **pages, size_t bufsize, 533b593c09fSTrond Myklebust __be32 *verf_res) 534d1bacf9eSBryan Schumaker { 53582e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 53682e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 53782e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 53882e22a5eSTrond Myklebust .cred = desc->file->f_cred, 539b593c09fSTrond Myklebust .verf = verf, 54082e22a5eSTrond Myklebust .cookie = cookie, 54182e22a5eSTrond Myklebust .pages = pages, 54282e22a5eSTrond Myklebust .page_len = bufsize, 54382e22a5eSTrond Myklebust .plus = desc->plus, 54482e22a5eSTrond Myklebust }; 54582e22a5eSTrond Myklebust struct nfs_readdir_res res = { 54682e22a5eSTrond Myklebust .verf = verf_res, 54782e22a5eSTrond Myklebust }; 5484704f0e2STrond Myklebust unsigned long timestamp, gencount; 5491da177e4SLinus Torvalds int error; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds again: 5521da177e4SLinus Torvalds timestamp = jiffies; 5534704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 554a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 55582e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 5561da177e4SLinus Torvalds if (error < 0) { 5571da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 5581da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 5591da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 5603a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 56182e22a5eSTrond Myklebust desc->plus = arg.plus = false; 5621da177e4SLinus Torvalds goto again; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds goto error; 5651da177e4SLinus Torvalds } 5661f4eab7eSNeil Brown desc->timestamp = timestamp; 5674704f0e2STrond Myklebust desc->gencount = gencount; 568d1bacf9eSBryan Schumaker error: 569d1bacf9eSBryan Schumaker return error; 570d1bacf9eSBryan Schumaker } 571d1bacf9eSBryan Schumaker 5726c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 573573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 574d1bacf9eSBryan Schumaker { 57559e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 576573c4e1eSChuck Lever int error; 577d1bacf9eSBryan Schumaker 57859e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 579573c4e1eSChuck Lever if (error) 580573c4e1eSChuck Lever return error; 581d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 582d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 583d1bacf9eSBryan Schumaker return 0; 584d1bacf9eSBryan Schumaker } 585d1bacf9eSBryan Schumaker 586fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 587fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 588fa923369STrond Myklebust */ 589d39ab9deSBryan Schumaker static 590d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 591d39ab9deSBryan Schumaker { 592d8fdb47fSTrond Myklebust struct inode *inode; 593fa923369STrond Myklebust struct nfs_inode *nfsi; 594fa923369STrond Myklebust 5952b0143b5SDavid Howells if (d_really_is_negative(dentry)) 5962b0143b5SDavid Howells return 0; 597fa923369STrond Myklebust 598d8fdb47fSTrond Myklebust inode = d_inode(dentry); 599d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 600d8fdb47fSTrond Myklebust return 0; 601d8fdb47fSTrond Myklebust 602d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 6037dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 604d39ab9deSBryan Schumaker return 0; 6057dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 6067dc72d5fSTrond Myklebust return 0; 6077dc72d5fSTrond Myklebust return 1; 608d39ab9deSBryan Schumaker } 609d39ab9deSBryan Schumaker 610d39ab9deSBryan Schumaker static 61123db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 612d69ee9b8STrond Myklebust { 613d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 614d69ee9b8STrond Myklebust return false; 615d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 616d69ee9b8STrond Myklebust return true; 61723db8620SAl Viro if (ctx->pos == 0) 618d69ee9b8STrond Myklebust return true; 619d69ee9b8STrond Myklebust return false; 620d69ee9b8STrond Myklebust } 621d69ee9b8STrond Myklebust 622d69ee9b8STrond Myklebust /* 62363519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 62463519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 625d69ee9b8STrond Myklebust * directory. 626d69ee9b8STrond Myklebust */ 627d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 628d69ee9b8STrond Myklebust { 62963519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 63063519fbcSTrond Myklebust 63163519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 63263519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 63363519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 634d69ee9b8STrond Myklebust } 635d69ee9b8STrond Myklebust 636311324adSTrond Myklebust /* 637311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 638311324adSTrond Myklebust * 639311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 640311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 641311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 642311324adSTrond Myklebust * cache flush. 643311324adSTrond Myklebust */ 644311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 645311324adSTrond Myklebust { 64663519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 64763519fbcSTrond Myklebust 64863519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 64963519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 65063519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 651ff81dfb5STrond Myklebust set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags); 652311324adSTrond Myklebust } 653311324adSTrond Myklebust } 654311324adSTrond Myklebust 655d69ee9b8STrond Myklebust static 656a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 657a1147b82STrond Myklebust unsigned long dir_verifier) 658d39ab9deSBryan Schumaker { 65926fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 6609ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 6614a201d6eSTrond Myklebust struct dentry *dentry; 6624a201d6eSTrond Myklebust struct dentry *alias; 663d39ab9deSBryan Schumaker struct inode *inode; 664aa9c2669SDavid Quigley int status; 665d39ab9deSBryan Schumaker 666fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 667fa923369STrond Myklebust return; 6686c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 6696c441c25STrond Myklebust return; 67078d04af4STrond Myklebust if (filename.len == 0) 67178d04af4STrond Myklebust return; 67278d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 67378d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 67478d04af4STrond Myklebust return; 67578d04af4STrond Myklebust /* ...or '/' */ 67678d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 67778d04af4STrond Myklebust return; 6784a201d6eSTrond Myklebust if (filename.name[0] == '.') { 6794a201d6eSTrond Myklebust if (filename.len == 1) 6804a201d6eSTrond Myklebust return; 6814a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 6824a201d6eSTrond Myklebust return; 6834a201d6eSTrond Myklebust } 6848387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 685d39ab9deSBryan Schumaker 6864a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 6879ac3d3e8SAl Viro again: 6889ac3d3e8SAl Viro if (!dentry) { 6899ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 6909ac3d3e8SAl Viro if (IS_ERR(dentry)) 6919ac3d3e8SAl Viro return; 6929ac3d3e8SAl Viro } 6939ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 6946c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 6956c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 6966c441c25STrond Myklebust &entry->fattr->fsid)) 6976c441c25STrond Myklebust goto out; 698d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 6997dc72d5fSTrond Myklebust if (!entry->fh->size) 7007dc72d5fSTrond Myklebust goto out; 701a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 7022b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 703aa9c2669SDavid Quigley if (!status) 704dd225cb3SAnna Schumaker nfs_setsecurity(d_inode(dentry), entry->fattr); 705d39ab9deSBryan Schumaker goto out; 706d39ab9deSBryan Schumaker } else { 7075542aa2fSEric W. Biederman d_invalidate(dentry); 708d39ab9deSBryan Schumaker dput(dentry); 7099ac3d3e8SAl Viro dentry = NULL; 7109ac3d3e8SAl Viro goto again; 711d39ab9deSBryan Schumaker } 712d39ab9deSBryan Schumaker } 7137dc72d5fSTrond Myklebust if (!entry->fh->size) { 7147dc72d5fSTrond Myklebust d_lookup_done(dentry); 7157dc72d5fSTrond Myklebust goto out; 7167dc72d5fSTrond Myklebust } 717d39ab9deSBryan Schumaker 718cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 71941d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 7209ac3d3e8SAl Viro d_lookup_done(dentry); 7219ac3d3e8SAl Viro if (alias) { 722d39ab9deSBryan Schumaker if (IS_ERR(alias)) 723d39ab9deSBryan Schumaker goto out; 7249ac3d3e8SAl Viro dput(dentry); 7259ac3d3e8SAl Viro dentry = alias; 7269ac3d3e8SAl Viro } 727a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 728d39ab9deSBryan Schumaker out: 729d39ab9deSBryan Schumaker dput(dentry); 730d39ab9deSBryan Schumaker } 731d39ab9deSBryan Schumaker 732d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 7333b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 7343b2a09f1STrond Myklebust struct nfs_entry *entry, 7353b2a09f1STrond Myklebust struct page **xdr_pages, 73635df59d3STrond Myklebust unsigned int buflen, 73735df59d3STrond Myklebust struct page **arrays, 73835df59d3STrond Myklebust size_t narrays) 739d1bacf9eSBryan Schumaker { 7403b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 741babddc72SBryan Schumaker struct xdr_stream stream; 742f7da7a12SBenny Halevy struct xdr_buf buf; 74335df59d3STrond Myklebust struct page *scratch, *new, *page = *arrays; 7445c346854STrond Myklebust int status; 745babddc72SBryan Schumaker 7466650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 7476650239aSTrond Myklebust if (scratch == NULL) 7486650239aSTrond Myklebust return -ENOMEM; 749babddc72SBryan Schumaker 750f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 7510ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch); 75299424380SBryan Schumaker 75399424380SBryan Schumaker do { 754b1db9a40SAnna Schumaker if (entry->fattr->label) 755b1db9a40SAnna Schumaker entry->fattr->label->len = NFS4_MAXLABELLEN; 756d33030e2SJeffrey Mitchell 75799424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 758972bcdf2STrond Myklebust if (status != 0) 75999424380SBryan Schumaker break; 7605c346854STrond Myklebust 761a7a3b1e9SBenjamin Coddington if (desc->plus) 762a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 763a1147b82STrond Myklebust desc->dir_verifier); 7648cd51a0cSTrond Myklebust 765db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 7663b2a09f1STrond Myklebust if (status != -ENOSPC) 7673b2a09f1STrond Myklebust continue; 76899424380SBryan Schumaker 76935df59d3STrond Myklebust if (page->mapping != mapping) { 77035df59d3STrond Myklebust if (!--narrays) 7713b2a09f1STrond Myklebust break; 77235df59d3STrond Myklebust new = nfs_readdir_page_array_alloc(entry->prev_cookie, 77335df59d3STrond Myklebust GFP_KERNEL); 77435df59d3STrond Myklebust if (!new) 77535df59d3STrond Myklebust break; 77635df59d3STrond Myklebust arrays++; 77735df59d3STrond Myklebust *arrays = page = new; 77835df59d3STrond Myklebust } else { 77935df59d3STrond Myklebust new = nfs_readdir_page_get_next(mapping, 78035df59d3STrond Myklebust page->index + 1, 7813b2a09f1STrond Myklebust entry->prev_cookie); 7823b2a09f1STrond Myklebust if (!new) 7833b2a09f1STrond Myklebust break; 78435df59d3STrond Myklebust if (page != *arrays) 7853b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7863b2a09f1STrond Myklebust page = new; 78756e4ebf8SBryan Schumaker } 7883b2a09f1STrond Myklebust status = nfs_readdir_add_to_array(entry, page); 789972bcdf2STrond Myklebust } while (!status && !entry->eof); 79056e4ebf8SBryan Schumaker 791972bcdf2STrond Myklebust switch (status) { 792972bcdf2STrond Myklebust case -EBADCOOKIE: 793972bcdf2STrond Myklebust if (entry->eof) { 794b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 7958cd51a0cSTrond Myklebust status = 0; 79656e4ebf8SBryan Schumaker } 797972bcdf2STrond Myklebust break; 798972bcdf2STrond Myklebust case -ENOSPC: 799972bcdf2STrond Myklebust case -EAGAIN: 800972bcdf2STrond Myklebust status = 0; 801972bcdf2STrond Myklebust break; 802972bcdf2STrond Myklebust } 8036650239aSTrond Myklebust 80435df59d3STrond Myklebust if (page != *arrays) 8053b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8066650239aSTrond Myklebust 8076650239aSTrond Myklebust put_page(scratch); 808d1bacf9eSBryan Schumaker return status; 80956e4ebf8SBryan Schumaker } 81056e4ebf8SBryan Schumaker 8111a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 81256e4ebf8SBryan Schumaker { 8131a34c8c9STrond Myklebust while (npages--) 8141a34c8c9STrond Myklebust put_page(pages[npages]); 8151a34c8c9STrond Myklebust kfree(pages); 81656e4ebf8SBryan Schumaker } 81756e4ebf8SBryan Schumaker 81856e4ebf8SBryan Schumaker /* 819bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 820bf211ca1Szhangliguang * to nfs_readdir_free_pages() 82156e4ebf8SBryan Schumaker */ 8221a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 82356e4ebf8SBryan Schumaker { 8241a34c8c9STrond Myklebust struct page **pages; 8251a34c8c9STrond Myklebust size_t i; 82656e4ebf8SBryan Schumaker 8271a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 8281a34c8c9STrond Myklebust if (!pages) 8291a34c8c9STrond Myklebust return NULL; 83056e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 83156e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 83256e4ebf8SBryan Schumaker if (page == NULL) 83356e4ebf8SBryan Schumaker goto out_freepages; 83456e4ebf8SBryan Schumaker pages[i] = page; 83556e4ebf8SBryan Schumaker } 8361a34c8c9STrond Myklebust return pages; 83756e4ebf8SBryan Schumaker 83856e4ebf8SBryan Schumaker out_freepages: 839c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 8401a34c8c9STrond Myklebust return NULL; 841d1bacf9eSBryan Schumaker } 842d1bacf9eSBryan Schumaker 8436c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 84435df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res, 84535df59d3STrond Myklebust struct page **arrays, size_t narrays) 846d1bacf9eSBryan Schumaker { 8471a34c8c9STrond Myklebust struct page **pages; 84835df59d3STrond Myklebust struct page *page = *arrays; 8496b75cf9eSTrond Myklebust struct nfs_entry *entry; 8501a34c8c9STrond Myklebust size_t array_size; 851b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file); 8521a34c8c9STrond Myklebust size_t dtsize = NFS_SERVER(inode)->dtsize; 8538cd51a0cSTrond Myklebust int status = -ENOMEM; 854d1bacf9eSBryan Schumaker 8556b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 8566b75cf9eSTrond Myklebust if (!entry) 8576b75cf9eSTrond Myklebust return -ENOMEM; 8586b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 8596b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 860b1db9a40SAnna Schumaker entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 8616b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 8626b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 863d1bacf9eSBryan Schumaker goto out; 864d1bacf9eSBryan Schumaker 8651a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 8661a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 8671a34c8c9STrond Myklebust if (!pages) 868b1db9a40SAnna Schumaker goto out; 869d1bacf9eSBryan Schumaker 870d1bacf9eSBryan Schumaker do { 871ac396128STrond Myklebust unsigned int pglen; 872b593c09fSTrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, 873b593c09fSTrond Myklebust pages, dtsize, 874b593c09fSTrond Myklebust verf_res); 875d1bacf9eSBryan Schumaker if (status < 0) 876d1bacf9eSBryan Schumaker break; 877972bcdf2STrond Myklebust 878ac396128STrond Myklebust pglen = status; 879972bcdf2STrond Myklebust if (pglen == 0) { 880972bcdf2STrond Myklebust nfs_readdir_page_set_eof(page); 8818cd51a0cSTrond Myklebust break; 8828cd51a0cSTrond Myklebust } 883972bcdf2STrond Myklebust 884ee3707aeSNagendra S Tomar verf_arg = verf_res; 885ee3707aeSNagendra S Tomar 88635df59d3STrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, pglen, 88735df59d3STrond Myklebust arrays, narrays); 888d9c4e39cStrondmy@kernel.org } while (!status && nfs_readdir_page_needs_filling(page) && 889d9c4e39cStrondmy@kernel.org page_mapping(page)); 890d1bacf9eSBryan Schumaker 891c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 892d1bacf9eSBryan Schumaker out: 8936b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 8946b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 8956b75cf9eSTrond Myklebust kfree(entry); 896d1bacf9eSBryan Schumaker return status; 897d1bacf9eSBryan Schumaker } 898d1bacf9eSBryan Schumaker 8991f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 9001da177e4SLinus Torvalds { 90109cbfeafSKirill A. Shutemov put_page(desc->page); 9021da177e4SLinus Torvalds desc->page = NULL; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 9051f1d4aa4STrond Myklebust static void 9061f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 9071da177e4SLinus Torvalds { 9081f1d4aa4STrond Myklebust unlock_page(desc->page); 9091f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 9101f1d4aa4STrond Myklebust } 9111f1d4aa4STrond Myklebust 9121f1d4aa4STrond Myklebust static struct page * 9131f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 9141f1d4aa4STrond Myklebust { 9151f1d4aa4STrond Myklebust return nfs_readdir_page_get_locked(desc->file->f_mapping, 9161f1d4aa4STrond Myklebust desc->page_index, 9171f1d4aa4STrond Myklebust desc->last_cookie); 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds /* 921d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 922114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 9231da177e4SLinus Torvalds */ 9246c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 925d1bacf9eSBryan Schumaker { 926227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 927227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 928b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 929d1bacf9eSBryan Schumaker int res; 930d1bacf9eSBryan Schumaker 9311f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 9321f1d4aa4STrond Myklebust if (!desc->page) 9331f1d4aa4STrond Myklebust return -ENOMEM; 9341f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 93535df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 93635df59d3STrond Myklebust &desc->page, 1); 9379fff59edSTrond Myklebust if (res < 0) { 9389fff59edSTrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 9399fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) { 9409fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping); 9419fff59edSTrond Myklebust desc->page_index = 0; 9429fff59edSTrond Myklebust return -EAGAIN; 9439fff59edSTrond Myklebust } 9449fff59edSTrond Myklebust return res; 9459fff59edSTrond Myklebust } 946f892c41cSTrond Myklebust /* 947f892c41cSTrond Myklebust * Set the cookie verifier if the page cache was empty 948f892c41cSTrond Myklebust */ 949f892c41cSTrond Myklebust if (desc->page_index == 0) 950f892c41cSTrond Myklebust memcpy(nfsi->cookieverf, verf, 951f892c41cSTrond Myklebust sizeof(nfsi->cookieverf)); 9521f1d4aa4STrond Myklebust } 953114de382STrond Myklebust res = nfs_readdir_search_array(desc); 954ff81dfb5STrond Myklebust if (res == 0) 955114de382STrond Myklebust return 0; 9561f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 957d1bacf9eSBryan Schumaker return res; 958d1bacf9eSBryan Schumaker } 959d1bacf9eSBryan Schumaker 960794092c5STrond Myklebust static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc) 961794092c5STrond Myklebust { 962794092c5STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 963794092c5STrond Myklebust struct inode *dir = file_inode(desc->file); 964794092c5STrond Myklebust unsigned int dtsize = NFS_SERVER(dir)->dtsize; 965794092c5STrond Myklebust loff_t size = i_size_read(dir); 966794092c5STrond Myklebust 967794092c5STrond Myklebust /* 968794092c5STrond Myklebust * Default to uncached readdir if the page cache is empty, and 969794092c5STrond Myklebust * we're looking for a non-zero cookie in a large directory. 970794092c5STrond Myklebust */ 971794092c5STrond Myklebust return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize; 972794092c5STrond Myklebust } 973794092c5STrond Myklebust 974d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 9756c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 9761da177e4SLinus Torvalds { 9778cd51a0cSTrond Myklebust int res; 978d1bacf9eSBryan Schumaker 979794092c5STrond Myklebust if (nfs_readdir_dont_search_cache(desc)) 980794092c5STrond Myklebust return -EBADCOOKIE; 981794092c5STrond Myklebust 9829fff59edSTrond Myklebust do { 9830aded708STrond Myklebust if (desc->page_index == 0) { 9848cd51a0cSTrond Myklebust desc->current_index = 0; 98559e356a9STrond Myklebust desc->prev_index = 0; 9860aded708STrond Myklebust desc->last_cookie = 0; 9870aded708STrond Myklebust } 988114de382STrond Myklebust res = find_and_lock_cache_page(desc); 98947c716cbSTrond Myklebust } while (res == -EAGAIN); 9901da177e4SLinus Torvalds return res; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* 9941da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 9951da177e4SLinus Torvalds */ 99613884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc, 99713884ff2STrond Myklebust const __be32 *verf) 9981da177e4SLinus Torvalds { 9991da177e4SLinus Torvalds struct file *file = desc->file; 1000dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 1001dbeaf8c9STrond Myklebust unsigned int i = 0; 10028ef2ce3eSBryan Schumaker 10030795bf83SFabian Frederick array = kmap(desc->page); 1004d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 1005ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 10061da177e4SLinus Torvalds 1007ece0b423STrond Myklebust ent = &array->array[i]; 1008a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 100923db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 1010e1d2699bSTrond Myklebust desc->eob = true; 10111da177e4SLinus Torvalds break; 1012ece0b423STrond Myklebust } 101313884ff2STrond Myklebust memcpy(desc->verf, verf, sizeof(desc->verf)); 1014d1bacf9eSBryan Schumaker if (i < (array->size-1)) 10152e7a4641STrond Myklebust desc->dir_cookie = array->array[i+1].cookie; 1016d1bacf9eSBryan Schumaker else 10172e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 101859e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 10192e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 102059e356a9STrond Myklebust else 102159e356a9STrond Myklebust desc->ctx->pos++; 10222e7a4641STrond Myklebust if (desc->duped != 0) 10232e7a4641STrond Myklebust desc->duped = 1; 10248cd51a0cSTrond Myklebust } 1025b1e21c97STrond Myklebust if (array->page_is_eof) 1026e1d2699bSTrond Myklebust desc->eof = !desc->eob; 1027d1bacf9eSBryan Schumaker 10280795bf83SFabian Frederick kunmap(desc->page); 1029dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1030dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds /* 10341da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 10351da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 10361da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 10371da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 10381da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 10391da177e4SLinus Torvalds * 10401da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 10411da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 10421da177e4SLinus Torvalds * we should already have a complete representation of the 10431da177e4SLinus Torvalds * directory in the page cache by the time we get here. 10441da177e4SLinus Torvalds */ 10456c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 10461da177e4SLinus Torvalds { 104735df59d3STrond Myklebust struct page **arrays; 104835df59d3STrond Myklebust size_t i, sz = 512; 1049b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 105035df59d3STrond Myklebust int status = -ENOMEM; 10511da177e4SLinus Torvalds 105235df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 10532e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 10541da177e4SLinus Torvalds 105535df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 105635df59d3STrond Myklebust if (!arrays) 10571da177e4SLinus Torvalds goto out; 105835df59d3STrond Myklebust arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 105935df59d3STrond Myklebust if (!arrays[0]) 106035df59d3STrond Myklebust goto out; 10611da177e4SLinus Torvalds 10627a8e1dc3STrond Myklebust desc->page_index = 0; 1063ce292d8fStrondmy@kernel.org desc->cache_entry_index = 0; 10642e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 10652e7a4641STrond Myklebust desc->duped = 0; 10667a8e1dc3STrond Myklebust 106735df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1068d1bacf9eSBryan Schumaker 1069e1d2699bSTrond Myklebust for (i = 0; !desc->eob && i < sz && arrays[i]; i++) { 107035df59d3STrond Myklebust desc->page = arrays[i]; 107113884ff2STrond Myklebust nfs_do_filldir(desc, verf); 107235df59d3STrond Myklebust } 107335df59d3STrond Myklebust desc->page = NULL; 10741da177e4SLinus Torvalds 107535df59d3STrond Myklebust 107635df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++) 107735df59d3STrond Myklebust nfs_readdir_page_array_free(arrays[i]); 10781da177e4SLinus Torvalds out: 107935df59d3STrond Myklebust kfree(arrays); 108035df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 10811da177e4SLinus Torvalds return status; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 108400a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 108500a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 108600a92642SOlivier Galibert whole directory. 10871da177e4SLinus Torvalds */ 108823db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 10891da177e4SLinus Torvalds { 1090be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 10912b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 109213884ff2STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 109323db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 10946b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 1095ff81dfb5STrond Myklebust pgoff_t page_index; 10966b75cf9eSTrond Myklebust int res; 10971da177e4SLinus Torvalds 10986de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 10996de1472fSAl Viro file, (long long)ctx->pos); 110091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 110191d5b470SChuck Lever 11021da177e4SLinus Torvalds /* 110323db8620SAl Viro * ctx->pos points to the dirent entry number. 1104f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 110500a92642SOlivier Galibert * to either find the entry with the appropriate number or 110600a92642SOlivier Galibert * revalidate the cookie. 11071da177e4SLinus Torvalds */ 1108d09e673fSTrond Myklebust nfs_revalidate_mapping(inode, file->f_mapping); 11096b75cf9eSTrond Myklebust 11106b75cf9eSTrond Myklebust res = -ENOMEM; 11116b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 11126b75cf9eSTrond Myklebust if (!desc) 11136b75cf9eSTrond Myklebust goto out; 11146b75cf9eSTrond Myklebust desc->file = file; 11156b75cf9eSTrond Myklebust desc->ctx = ctx; 11166b75cf9eSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx); 1117fccca7fcSTrond Myklebust 11182e7a4641STrond Myklebust spin_lock(&file->f_lock); 11192e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 11202e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 11212e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 1122ff81dfb5STrond Myklebust page_index = dir_ctx->page_index; 1123*728dd0abSTrond Myklebust desc->page_index = page_index; 1124*728dd0abSTrond Myklebust desc->last_cookie = dir_ctx->last_cookie; 11252e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 1126e1d2699bSTrond Myklebust desc->eof = dir_ctx->eof; 1127b593c09fSTrond Myklebust memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf)); 11282e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1129565277f6STrond Myklebust 1130e1d2699bSTrond Myklebust if (desc->eof) { 1131e1d2699bSTrond Myklebust res = 0; 1132e1d2699bSTrond Myklebust goto out_free; 1133e1d2699bSTrond Myklebust } 1134e1d2699bSTrond Myklebust 1135ff81dfb5STrond Myklebust if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) && 1136ff81dfb5STrond Myklebust list_is_singular(&nfsi->open_files)) 1137ff81dfb5STrond Myklebust invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1); 1138ff81dfb5STrond Myklebust 113947c716cbSTrond Myklebust do { 11401da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 114100a92642SOlivier Galibert 11421da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1143ece0b423STrond Myklebust res = 0; 11441da177e4SLinus Torvalds /* This means either end of directory */ 11452e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 11461da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 114723db8620SAl Viro res = uncached_readdir(desc); 1148ece0b423STrond Myklebust if (res == 0) 11491da177e4SLinus Torvalds continue; 11509fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) 11519fff59edSTrond Myklebust res = 0; 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds break; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 115613884ff2STrond Myklebust clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 11571da177e4SLinus Torvalds nfs_zap_caches(inode); 1158baf57a09STrond Myklebust desc->page_index = 0; 1159a7a3b1e9SBenjamin Coddington desc->plus = false; 1160a7a3b1e9SBenjamin Coddington desc->eof = false; 11611da177e4SLinus Torvalds continue; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds if (res < 0) 11641da177e4SLinus Torvalds break; 11651da177e4SLinus Torvalds 116613884ff2STrond Myklebust nfs_do_filldir(desc, nfsi->cookieverf); 11671f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1168e1d2699bSTrond Myklebust } while (!desc->eob && !desc->eof); 11692e7a4641STrond Myklebust 11702e7a4641STrond Myklebust spin_lock(&file->f_lock); 11712e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 11722e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 1173*728dd0abSTrond Myklebust dir_ctx->last_cookie = desc->last_cookie; 11742e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 11752e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 1176ff81dfb5STrond Myklebust dir_ctx->page_index = desc->page_index; 1177e1d2699bSTrond Myklebust dir_ctx->eof = desc->eof; 1178b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 11792e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1180e1d2699bSTrond Myklebust out_free: 11816b75cf9eSTrond Myklebust kfree(desc); 11826b75cf9eSTrond Myklebust 1183fccca7fcSTrond Myklebust out: 11846de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 11851da177e4SLinus Torvalds return res; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 1188965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1189f0dd2136STrond Myklebust { 1190480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1191b84e06c5SChuck Lever 11926de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 11936de1472fSAl Viro filp, offset, whence); 1194b84e06c5SChuck Lever 1195965c8e59SAndrew Morton switch (whence) { 1196f0dd2136STrond Myklebust default: 1197b2b1ff3dSTrond Myklebust return -EINVAL; 1198b2b1ff3dSTrond Myklebust case SEEK_SET: 1199b2b1ff3dSTrond Myklebust if (offset < 0) 1200b2b1ff3dSTrond Myklebust return -EINVAL; 120183f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1202b2b1ff3dSTrond Myklebust break; 1203b2b1ff3dSTrond Myklebust case SEEK_CUR: 1204b2b1ff3dSTrond Myklebust if (offset == 0) 1205b2b1ff3dSTrond Myklebust return filp->f_pos; 120683f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1207b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1208b2b1ff3dSTrond Myklebust if (offset < 0) { 120983f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1210b2b1ff3dSTrond Myklebust return -EINVAL; 1211b2b1ff3dSTrond Myklebust } 1212f0dd2136STrond Myklebust } 1213f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1214f0dd2136STrond Myklebust filp->f_pos = offset; 1215*728dd0abSTrond Myklebust if (!nfs_readdir_use_cookie(filp)) { 1216480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 1217*728dd0abSTrond Myklebust dir_ctx->page_index = 0; 1218*728dd0abSTrond Myklebust } else 1219*728dd0abSTrond Myklebust dir_ctx->dir_cookie = offset; 1220b593c09fSTrond Myklebust if (offset == 0) 1221b593c09fSTrond Myklebust memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf)); 12228ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1223e1d2699bSTrond Myklebust dir_ctx->eof = false; 1224f0dd2136STrond Myklebust } 122583f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1226f0dd2136STrond Myklebust return offset; 1227f0dd2136STrond Myklebust } 1228f0dd2136STrond Myklebust 12291da177e4SLinus Torvalds /* 12301da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 12311da177e4SLinus Torvalds * is a dummy operation. 12321da177e4SLinus Torvalds */ 123302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 123402c24a82SJosef Bacik int datasync) 12351da177e4SLinus Torvalds { 12366de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 12371e7cb3dcSChuck Lever 123811decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 12391da177e4SLinus Torvalds return 0; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 1242bfc69a45STrond Myklebust /** 1243bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1244302fad7bSTrond Myklebust * @dir: pointer to directory inode 1245bfc69a45STrond Myklebust * 1246bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1247bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1248bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1249bfc69a45STrond Myklebust * 1250efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1251efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1252efeda80dSTrond Myklebust * 1253bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1254bfc69a45STrond Myklebust */ 1255bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1256bfc69a45STrond Myklebust { 1257efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1258bfc69a45STrond Myklebust } 125989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1260bfc69a45STrond Myklebust 1261efeda80dSTrond Myklebust /** 1262efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1263efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1264efeda80dSTrond Myklebust * @verf: previously saved change attribute 1265efeda80dSTrond Myklebust * 1266efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1267efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1268efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1269efeda80dSTrond Myklebust */ 1270efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1271efeda80dSTrond Myklebust { 1272efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1273efeda80dSTrond Myklebust } 1274efeda80dSTrond Myklebust 1275efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1276efeda80dSTrond Myklebust { 1277efeda80dSTrond Myklebust *verf |= 1UL; 1278efeda80dSTrond Myklebust } 1279efeda80dSTrond Myklebust 1280efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1281efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1282efeda80dSTrond Myklebust { 1283efeda80dSTrond Myklebust *verf &= ~1UL; 1284efeda80dSTrond Myklebust } 1285efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1286efeda80dSTrond Myklebust 1287efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1288efeda80dSTrond Myklebust { 1289efeda80dSTrond Myklebust return verf & 1; 1290efeda80dSTrond Myklebust } 1291efeda80dSTrond Myklebust 1292efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1293efeda80dSTrond Myklebust { 1294efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1295efeda80dSTrond Myklebust } 1296efeda80dSTrond Myklebust 1297efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1298efeda80dSTrond Myklebust { 1299efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1300cec08f45STrond Myklebust struct inode *dir = d_inode(dentry->d_parent); 1301efeda80dSTrond Myklebust 1302cec08f45STrond Myklebust if (!nfs_verify_change_attribute(dir, verf)) 1303cec08f45STrond Myklebust return; 1304efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1305efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1306efeda80dSTrond Myklebust dentry->d_time = verf; 1307efeda80dSTrond Myklebust } 1308efeda80dSTrond Myklebust 1309efeda80dSTrond Myklebust /** 1310efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1311efeda80dSTrond Myklebust * @dentry: pointer to dentry 1312efeda80dSTrond Myklebust * @verf: verifier to save 1313efeda80dSTrond Myklebust * 1314efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1315efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1316efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1317efeda80dSTrond Myklebust * look it up again after a directory change. 1318efeda80dSTrond Myklebust */ 1319efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1320efeda80dSTrond Myklebust { 1321efeda80dSTrond Myklebust 1322efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1323efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1324efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1325efeda80dSTrond Myklebust } 1326efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1327efeda80dSTrond Myklebust 1328efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1329efeda80dSTrond Myklebust /** 1330efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1331efeda80dSTrond Myklebust * @inode: pointer to inode 1332efeda80dSTrond Myklebust * 1333efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1334efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1335efeda80dSTrond Myklebust * while holding a delegation. 1336efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1337efeda80dSTrond Myklebust * returned or revoked. 1338efeda80dSTrond Myklebust */ 1339efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1340efeda80dSTrond Myklebust { 1341efeda80dSTrond Myklebust struct dentry *alias; 1342efeda80dSTrond Myklebust 1343efeda80dSTrond Myklebust if (!inode) 1344efeda80dSTrond Myklebust return; 1345efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1346efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1347efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1348efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1349efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1350efeda80dSTrond Myklebust } 1351efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1352efeda80dSTrond Myklebust } 1353efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1354efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1355efeda80dSTrond Myklebust 13568ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry) 13578ce37abdSTrond Myklebust { 13588ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) && 13598ce37abdSTrond Myklebust d_really_is_negative(dentry)) 13608ce37abdSTrond Myklebust return dentry->d_time == inode_peek_iversion_raw(dir); 13618ce37abdSTrond Myklebust return nfs_verify_change_attribute(dir, dentry->d_time); 13628ce37abdSTrond Myklebust } 13638ce37abdSTrond Myklebust 13641da177e4SLinus Torvalds /* 13651da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 13661da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 13671da177e4SLinus Torvalds * and may need to be looked up again. 1368912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 13691da177e4SLinus Torvalds */ 1370912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1371912a108dSNeilBrown int rcu_walk) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds if (IS_ROOT(dentry)) 13741da177e4SLinus Torvalds return 1; 13754eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 13764eec952eSTrond Myklebust return 0; 13778ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 13786ecc5e8fSTrond Myklebust return 0; 1379f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 13801cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1381912a108dSNeilBrown if (rcu_walk) 1382f2c77f4eSTrond Myklebust return 0; 13831cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 13841cd9cb05STrond Myklebust return 0; 13851cd9cb05STrond Myklebust } 13868ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 1387f2c77f4eSTrond Myklebust return 0; 1388f2c77f4eSTrond Myklebust return 1; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* 1392a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1393a12802caSTrond Myklebust * an O_EXCL create using this path component. 1394a12802caSTrond Myklebust */ 1395fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1396a12802caSTrond Myklebust { 1397a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1398a12802caSTrond Myklebust return 0; 1399fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1400a12802caSTrond Myklebust } 1401a12802caSTrond Myklebust 1402a12802caSTrond Myklebust /* 14031d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 14041d6757fbSTrond Myklebust * 14051d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 14061d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 14071d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 14081d6757fbSTrond Myklebust * 14091d6757fbSTrond Myklebust */ 141065a0c149STrond Myklebust static 1411fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 14121da177e4SLinus Torvalds { 14131da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 141465a0c149STrond Myklebust int ret; 14151da177e4SLinus Torvalds 141636d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 14174e99a1ffSTrond Myklebust return 0; 141847921921STrond Myklebust 141947921921STrond Myklebust if (flags & LOOKUP_OPEN) { 142047921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 142147921921STrond Myklebust case S_IFREG: 142247921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 142347921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 142447921921STrond Myklebust goto out; 1425df561f66SGustavo A. R. Silva fallthrough; 142647921921STrond Myklebust case S_IFDIR: 142747921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 142847921921STrond Myklebust break; 142947921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 143047921921STrond Myklebust goto out_force; 143147921921STrond Myklebust } 143247921921STrond Myklebust } 143347921921STrond Myklebust 14341da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1435fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 14361da177e4SLinus Torvalds goto out_force; 143765a0c149STrond Myklebust out: 143843245ecaSOlga Kornievskaia if (inode->i_nlink > 0 || 143943245ecaSOlga Kornievskaia (inode->i_nlink == 0 && 144043245ecaSOlga Kornievskaia test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags))) 144143245ecaSOlga Kornievskaia return 0; 144243245ecaSOlga Kornievskaia else 144343245ecaSOlga Kornievskaia return -ESTALE; 14441da177e4SLinus Torvalds out_force: 14451fa1e384SNeilBrown if (flags & LOOKUP_RCU) 14461fa1e384SNeilBrown return -ECHILD; 144765a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 144865a0c149STrond Myklebust if (ret != 0) 144965a0c149STrond Myklebust return ret; 145065a0c149STrond Myklebust goto out; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 145382e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode) 145482e7ca13STrond Myklebust { 145582e7ca13STrond Myklebust spin_lock(&inode->i_lock); 1456a6a361c4STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 145782e7ca13STrond Myklebust spin_unlock(&inode->i_lock); 145882e7ca13STrond Myklebust } 145982e7ca13STrond Myklebust 14601da177e4SLinus Torvalds /* 14611da177e4SLinus Torvalds * We judge how long we want to trust negative 14621da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 14631da177e4SLinus Torvalds * 14641da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 14651da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1466912a108dSNeilBrown * 1467912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1468912a108dSNeilBrown * suggesting a reval is needed. 14699f6d44d4STrond Myklebust * 14709f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 14719f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 14721da177e4SLinus Torvalds */ 14731da177e4SLinus Torvalds static inline 14741da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1475fa3c56bbSAl Viro unsigned int flags) 14761da177e4SLinus Torvalds { 14779f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 14781da177e4SLinus Torvalds return 0; 14794eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 14804eec952eSTrond Myklebust return 1; 148198ca3ee6STrond Myklebust /* Case insensitive server? Revalidate negative dentries */ 148298ca3ee6STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 148398ca3ee6STrond Myklebust return 1; 1484912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14875ceb9d7fSTrond Myklebust static int 14885ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 14895ceb9d7fSTrond Myklebust struct inode *inode, int error) 14901da177e4SLinus Torvalds { 14915ceb9d7fSTrond Myklebust switch (error) { 14925ceb9d7fSTrond Myklebust case 1: 14932eef8a31STrond Myklebust break; 14945ceb9d7fSTrond Myklebust case 0: 1495a3f432bfSJ. Bruce Fields /* 1496a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1497a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1498a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1499a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1500a3f432bfSJ. Bruce Fields */ 150147397915STrond Myklebust if (inode && IS_ROOT(dentry)) 15022eef8a31STrond Myklebust error = 1; 15032eef8a31STrond Myklebust break; 15045ceb9d7fSTrond Myklebust } 15052eef8a31STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error); 1506e1fb4d05STrond Myklebust return error; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15095ceb9d7fSTrond Myklebust static int 15105ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 15115ceb9d7fSTrond Myklebust unsigned int flags) 15125ceb9d7fSTrond Myklebust { 15135ceb9d7fSTrond Myklebust int ret = 1; 15145ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 15155ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 15165ceb9d7fSTrond Myklebust return -ECHILD; 15175ceb9d7fSTrond Myklebust ret = 0; 15185ceb9d7fSTrond Myklebust } 15195ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 15205ceb9d7fSTrond Myklebust } 15215ceb9d7fSTrond Myklebust 15225ceb9d7fSTrond Myklebust static int 15235ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 15245ceb9d7fSTrond Myklebust struct inode *inode) 15255ceb9d7fSTrond Myklebust { 15265ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15275ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 15285ceb9d7fSTrond Myklebust } 15295ceb9d7fSTrond Myklebust 15305ceb9d7fSTrond Myklebust static int 15315ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 15325ceb9d7fSTrond Myklebust struct inode *inode) 15335ceb9d7fSTrond Myklebust { 15345ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 15355ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 1536a1147b82STrond Myklebust unsigned long dir_verifier; 15375ceb9d7fSTrond Myklebust int ret; 15385ceb9d7fSTrond Myklebust 15395ceb9d7fSTrond Myklebust ret = -ENOMEM; 15405ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 15419558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 15429558a007SAnna Schumaker if (fhandle == NULL || fattr == NULL) 15435ceb9d7fSTrond Myklebust goto out; 15445ceb9d7fSTrond Myklebust 1545a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 15469558a007SAnna Schumaker ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 15475ceb9d7fSTrond Myklebust if (ret < 0) { 1548f7b37b8bSTrond Myklebust switch (ret) { 1549f7b37b8bSTrond Myklebust case -ESTALE: 1550f7b37b8bSTrond Myklebust case -ENOENT: 15515ceb9d7fSTrond Myklebust ret = 0; 1552f7b37b8bSTrond Myklebust break; 1553f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1554f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1555f7b37b8bSTrond Myklebust ret = 1; 1556f7b37b8bSTrond Myklebust } 15575ceb9d7fSTrond Myklebust goto out; 15585ceb9d7fSTrond Myklebust } 15595ceb9d7fSTrond Myklebust ret = 0; 15605ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 15615ceb9d7fSTrond Myklebust goto out; 15625ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 15635ceb9d7fSTrond Myklebust goto out; 15645ceb9d7fSTrond Myklebust 1565dd225cb3SAnna Schumaker nfs_setsecurity(inode, fattr); 1566a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 15675ceb9d7fSTrond Myklebust 15685ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 15695ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 15705ceb9d7fSTrond Myklebust ret = 1; 15715ceb9d7fSTrond Myklebust out: 15725ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 15735ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 157482e7ca13STrond Myklebust 157582e7ca13STrond Myklebust /* 157682e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being 157782e7ca13STrond Myklebust * a match, then we should revalidate the directory cache. 157882e7ca13STrond Myklebust */ 15798ce37abdSTrond Myklebust if (!ret && nfs_dentry_verify_change(dir, dentry)) 158082e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 15815ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 15825ceb9d7fSTrond Myklebust } 15835ceb9d7fSTrond Myklebust 15845ceb9d7fSTrond Myklebust /* 15855ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 15865ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 15875ceb9d7fSTrond Myklebust * lookup. 15885ceb9d7fSTrond Myklebust * 15895ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 15905ceb9d7fSTrond Myklebust * we have an inode! 15915ceb9d7fSTrond Myklebust * 15925ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 15935ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 15945ceb9d7fSTrond Myklebust */ 15955ceb9d7fSTrond Myklebust static int 15965ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 15975ceb9d7fSTrond Myklebust unsigned int flags) 15985ceb9d7fSTrond Myklebust { 15995ceb9d7fSTrond Myklebust struct inode *inode; 16005ceb9d7fSTrond Myklebust int error; 16015ceb9d7fSTrond Myklebust 16025ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 16035ceb9d7fSTrond Myklebust inode = d_inode(dentry); 16045ceb9d7fSTrond Myklebust 16055ceb9d7fSTrond Myklebust if (!inode) 16065ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 16075ceb9d7fSTrond Myklebust 16085ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 16095ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 16105ceb9d7fSTrond Myklebust __func__, dentry); 16115ceb9d7fSTrond Myklebust goto out_bad; 16125ceb9d7fSTrond Myklebust } 16135ceb9d7fSTrond Myklebust 1614efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 16155ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 16165ceb9d7fSTrond Myklebust 16175ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 16185ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 16195ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 16205ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 16215ceb9d7fSTrond Myklebust if (error) { 16225ceb9d7fSTrond Myklebust if (error == -ESTALE) 162382e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 16245ceb9d7fSTrond Myklebust goto out_bad; 16255ceb9d7fSTrond Myklebust } 16265ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 16275ceb9d7fSTrond Myklebust goto out_valid; 16285ceb9d7fSTrond Myklebust } 16295ceb9d7fSTrond Myklebust 16305ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 16315ceb9d7fSTrond Myklebust return -ECHILD; 16325ceb9d7fSTrond Myklebust 16335ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 16345ceb9d7fSTrond Myklebust goto out_bad; 16355ceb9d7fSTrond Myklebust 16365ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 16372eef8a31STrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode); 16385ceb9d7fSTrond Myklebust out_valid: 16395ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 16405ceb9d7fSTrond Myklebust out_bad: 16415ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 16425ceb9d7fSTrond Myklebust return -ECHILD; 16435ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 16445ceb9d7fSTrond Myklebust } 16455ceb9d7fSTrond Myklebust 16465ceb9d7fSTrond Myklebust static int 1647c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1648c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 16495ceb9d7fSTrond Myklebust { 16505ceb9d7fSTrond Myklebust struct dentry *parent; 16515ceb9d7fSTrond Myklebust struct inode *dir; 16525ceb9d7fSTrond Myklebust int ret; 16535ceb9d7fSTrond Myklebust 16545ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 16555ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 16565ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 16575ceb9d7fSTrond Myklebust if (!dir) 16585ceb9d7fSTrond Myklebust return -ECHILD; 1659c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 16605ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 16615ceb9d7fSTrond Myklebust return -ECHILD; 16625ceb9d7fSTrond Myklebust } else { 16635ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1664c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 16655ceb9d7fSTrond Myklebust dput(parent); 16665ceb9d7fSTrond Myklebust } 16675ceb9d7fSTrond Myklebust return ret; 16685ceb9d7fSTrond Myklebust } 16695ceb9d7fSTrond Myklebust 1670c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1671c7944ebbSTrond Myklebust { 1672c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1673c7944ebbSTrond Myklebust } 1674c7944ebbSTrond Myklebust 16751da177e4SLinus Torvalds /* 16762b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1677ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1678ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1679ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1680ecf3d1f1SJeff Layton * 1681ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1682ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1683ecf3d1f1SJeff Layton */ 1684ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1685ecf3d1f1SJeff Layton { 16862b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 16879cdd1d3fSTrond Myklebust int error = 0; 1688ecf3d1f1SJeff Layton 1689ecf3d1f1SJeff Layton /* 1690ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1691ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1692ecf3d1f1SJeff Layton * eventually need to do something more here. 1693ecf3d1f1SJeff Layton */ 1694ecf3d1f1SJeff Layton if (!inode) { 16956de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 16966de1472fSAl Viro __func__, dentry); 1697ecf3d1f1SJeff Layton return 1; 1698ecf3d1f1SJeff Layton } 1699ecf3d1f1SJeff Layton 1700ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 17016de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 17026de1472fSAl Viro __func__, dentry); 1703ecf3d1f1SJeff Layton return 0; 1704ecf3d1f1SJeff Layton } 1705ecf3d1f1SJeff Layton 1706b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1707ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1708ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1709ecf3d1f1SJeff Layton return !error; 1710ecf3d1f1SJeff Layton } 1711ecf3d1f1SJeff Layton 1712ecf3d1f1SJeff Layton /* 17131da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 17141da177e4SLinus Torvalds */ 1715fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 17161da177e4SLinus Torvalds { 17176de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 17186de1472fSAl Viro dentry, dentry->d_flags); 17191da177e4SLinus Torvalds 172077f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 17212b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 172277f11192STrond Myklebust return 1; 172377f11192STrond Myklebust 17241da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 17251da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 17261da177e4SLinus Torvalds return 1; 17271da177e4SLinus Torvalds } 17281751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 17291da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 17301da177e4SLinus Torvalds * files will be cleaned up during umount */ 17311da177e4SLinus Torvalds return 1; 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds return 0; 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17371f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 17381b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 17391b83d707STrond Myklebust { 17401b83d707STrond Myklebust spin_lock(&inode->i_lock); 17411f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 174259a707b0STrond Myklebust if (inode->i_nlink > 0) 174359a707b0STrond Myklebust drop_nlink(inode); 174459a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1745ac46b3d7STrond Myklebust nfs_set_cache_invalid( 1746ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 17471301e421STrond Myklebust NFS_INO_INVALID_NLINK); 17481b83d707STrond Myklebust spin_unlock(&inode->i_lock); 17491b83d707STrond Myklebust } 17501b83d707STrond Myklebust 17511da177e4SLinus Torvalds /* 17521da177e4SLinus Torvalds * Called when the dentry loses inode. 17531da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 17541da177e4SLinus Torvalds */ 17551da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 17561da177e4SLinus Torvalds { 17571da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1758e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 17591f018458STrond Myklebust nfs_drop_nlink(inode); 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds iput(inode); 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 1764b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1765b1942c5fSAl Viro { 1766b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1767b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1768b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1769b1942c5fSAl Viro WARN_ON(1); 1770b1942c5fSAl Viro else 1771b1942c5fSAl Viro kfree(dentry->d_fsdata); 1772b1942c5fSAl Viro } 1773b1942c5fSAl Viro } 1774b1942c5fSAl Viro 1775f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 17761da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1777ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 17781da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 17791da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 178036d43a43SDavid Howells .d_automount = nfs_d_automount, 1781b1942c5fSAl Viro .d_release = nfs_d_release, 17821da177e4SLinus Torvalds }; 1783ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 17841da177e4SLinus Torvalds 1785597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 17861da177e4SLinus Torvalds { 17871da177e4SLinus Torvalds struct dentry *res; 17881da177e4SLinus Torvalds struct inode *inode = NULL; 1789e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1790e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 1791a1147b82STrond Myklebust unsigned long dir_verifier; 17921da177e4SLinus Torvalds int error; 17931da177e4SLinus Torvalds 17946de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 179591d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 17961da177e4SLinus Torvalds 1797130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1798130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 17991da177e4SLinus Torvalds 1800fd684071STrond Myklebust /* 1801fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1802fd684071STrond Myklebust * but don't hash the dentry. 1803fd684071STrond Myklebust */ 18049f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1805130f9ab7SAl Viro return NULL; 18061da177e4SLinus Torvalds 1807e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1808e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 18099558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir)); 1810e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1811e1fb4d05STrond Myklebust goto out; 1812e1fb4d05STrond Myklebust 1813a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 18146e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 18159558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 18168ce37abdSTrond Myklebust if (error == -ENOENT) { 18178ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 18188ce37abdSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 18191da177e4SLinus Torvalds goto no_entry; 18208ce37abdSTrond Myklebust } 18211da177e4SLinus Torvalds if (error < 0) { 18221da177e4SLinus Torvalds res = ERR_PTR(error); 18239558a007SAnna Schumaker goto out; 18241da177e4SLinus Torvalds } 1825cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1826bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 182703f28e3aSTrond Myklebust if (IS_ERR(res)) 18289558a007SAnna Schumaker goto out; 182954ceac45SDavid Howells 183063519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 183163519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1832d69ee9b8STrond Myklebust 18331da177e4SLinus Torvalds no_entry: 183441d28bcaSAl Viro res = d_splice_alias(inode, dentry); 18359eaef27bSTrond Myklebust if (res != NULL) { 18369eaef27bSTrond Myklebust if (IS_ERR(res)) 18379558a007SAnna Schumaker goto out; 18381da177e4SLinus Torvalds dentry = res; 18399eaef27bSTrond Myklebust } 1840a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 18411da177e4SLinus Torvalds out: 18429558a007SAnna Schumaker trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res)); 1843e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1844e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 18451da177e4SLinus Torvalds return res; 18461da177e4SLinus Torvalds } 1847ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 18481da177e4SLinus Torvalds 184900bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode) 185000bdadc7STrond Myklebust { 185100bdadc7STrond Myklebust /* Case insensitive server? Revalidate dentries */ 185200bdadc7STrond Myklebust if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE)) 185300bdadc7STrond Myklebust d_prune_aliases(inode); 185400bdadc7STrond Myklebust } 185500bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases); 185600bdadc7STrond Myklebust 185789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 18580b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 18591da177e4SLinus Torvalds 1860f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 18610ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1862b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 18631da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 18641da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 186536d43a43SDavid Howells .d_automount = nfs_d_automount, 1866b1942c5fSAl Viro .d_release = nfs_d_release, 18671da177e4SLinus Torvalds }; 186889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 18691da177e4SLinus Torvalds 18708a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 18718a5e929dSAl Viro { 18728a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 18738a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 18748a5e929dSAl Viro res |= FMODE_READ; 18758a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 18768a5e929dSAl Viro res |= FMODE_WRITE; 18778a5e929dSAl Viro return res; 18788a5e929dSAl Viro } 18798a5e929dSAl Viro 1880532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1881cd9a1c0eSTrond Myklebust { 1882532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1883cd9a1c0eSTrond Myklebust } 1884cd9a1c0eSTrond Myklebust 1885cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1886cd9a1c0eSTrond Myklebust { 1887f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1888cd9a1c0eSTrond Myklebust return 0; 1889cd9a1c0eSTrond Myklebust } 1890cd9a1c0eSTrond Myklebust 1891d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 18920dd2b474SMiklos Szeredi struct dentry *dentry, 1893b452a458SAl Viro struct file *file, unsigned open_flags) 1894cd9a1c0eSTrond Myklebust { 18950dd2b474SMiklos Szeredi int err; 18960dd2b474SMiklos Szeredi 1897be12af3eSAl Viro err = finish_open(file, dentry, do_open); 189830d90494SAl Viro if (err) 1899d9585277SAl Viro goto out; 1900eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 190130d90494SAl Viro nfs_file_set_open_context(file, ctx); 1902eaa2b82cSNeilBrown else 19039821421aSTrond Myklebust err = -EOPENSTALE; 1904cd9a1c0eSTrond Myklebust out: 1905d9585277SAl Viro return err; 1906cd9a1c0eSTrond Myklebust } 1907cd9a1c0eSTrond Myklebust 190873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 190930d90494SAl Viro struct file *file, unsigned open_flags, 191044907d79SAl Viro umode_t mode) 19111da177e4SLinus Torvalds { 1912c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1913cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 19140dd2b474SMiklos Szeredi struct dentry *res; 19150dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1916f46e0bd3STrond Myklebust struct inode *inode; 19171472b83eSTrond Myklebust unsigned int lookup_flags = 0; 191868eaba4cSTrond Myklebust unsigned long dir_verifier; 1919c94c0953SAl Viro bool switched = false; 192073a09dd9SAl Viro int created = 0; 1921898f635cSTrond Myklebust int err; 19221da177e4SLinus Torvalds 19230dd2b474SMiklos Szeredi /* Expect a negative dentry */ 19242b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 19250dd2b474SMiklos Szeredi 19261e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 19276de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19281e7cb3dcSChuck Lever 19299597c13bSJeff Layton err = nfs_check_flags(open_flags); 19309597c13bSJeff Layton if (err) 19319597c13bSJeff Layton return err; 19329597c13bSJeff Layton 19330dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 19340dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 193500699ad8SAl Viro if (!d_in_lookup(dentry)) { 19360dd2b474SMiklos Szeredi /* 19370dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 19380dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 19390dd2b474SMiklos Szeredi * again 19400dd2b474SMiklos Szeredi */ 1941d9585277SAl Viro return -ENOENT; 19420dd2b474SMiklos Szeredi } 19431472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 19441da177e4SLinus Torvalds goto no_open; 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19470dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1948d9585277SAl Viro return -ENAMETOOLONG; 19491da177e4SLinus Torvalds 19500dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1951dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1952dff25ddbSAndreas Gruenbacher 1953dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1954dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1955dff25ddbSAndreas Gruenbacher 1956536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1957dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 19580dd2b474SMiklos Szeredi } 1959536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1960536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1961536e43d1STrond Myklebust attr.ia_size = 0; 1962cd9a1c0eSTrond Myklebust } 1963cd9a1c0eSTrond Myklebust 1964c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1965c94c0953SAl Viro d_drop(dentry); 1966c94c0953SAl Viro switched = true; 1967c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1968c94c0953SAl Viro &dentry->d_name, &wq); 1969c94c0953SAl Viro if (IS_ERR(dentry)) 1970c94c0953SAl Viro return PTR_ERR(dentry); 1971c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1972c94c0953SAl Viro return finish_no_open(file, dentry); 1973c94c0953SAl Viro } 1974c94c0953SAl Viro 1975532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 19760dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 19770dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1978d9585277SAl Viro goto out; 19790dd2b474SMiklos Szeredi 19806e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 198173a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 198273a09dd9SAl Viro if (created) 198373a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1984275bb307STrond Myklebust if (IS_ERR(inode)) { 19850dd2b474SMiklos Szeredi err = PTR_ERR(inode); 19866e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 19872d9db750STrond Myklebust put_nfs_open_context(ctx); 1988d20cb71dSAl Viro d_drop(dentry); 19890dd2b474SMiklos Szeredi switch (err) { 19901da177e4SLinus Torvalds case -ENOENT: 1991774d9513SPeng Tao d_splice_alias(NULL, dentry); 199268eaba4cSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 199368eaba4cSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 199468eaba4cSTrond Myklebust else 199568eaba4cSTrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 199668eaba4cSTrond Myklebust nfs_set_verifier(dentry, dir_verifier); 19970dd2b474SMiklos Szeredi break; 19981788ea6eSJeff Layton case -EISDIR: 19996f926b5bSTrond Myklebust case -ENOTDIR: 20006f926b5bSTrond Myklebust goto no_open; 20011da177e4SLinus Torvalds case -ELOOP: 20020dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 20031da177e4SLinus Torvalds goto no_open; 20040dd2b474SMiklos Szeredi break; 20051da177e4SLinus Torvalds /* case -EINVAL: */ 20061da177e4SLinus Torvalds default: 20070dd2b474SMiklos Szeredi break; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds goto out; 20101da177e4SLinus Torvalds } 20110dd2b474SMiklos Szeredi 2012b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 20136e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 20142d9db750STrond Myklebust put_nfs_open_context(ctx); 2015d9585277SAl Viro out: 2016c94c0953SAl Viro if (unlikely(switched)) { 2017c94c0953SAl Viro d_lookup_done(dentry); 2018c94c0953SAl Viro dput(dentry); 2019c94c0953SAl Viro } 2020d9585277SAl Viro return err; 20210dd2b474SMiklos Szeredi 20221da177e4SLinus Torvalds no_open: 20231472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 2024ac795161STrond Myklebust if (!res) { 2025ac795161STrond Myklebust inode = d_inode(dentry); 2026ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2027e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) 2028ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 20291751fc1dSTrond Myklebust else if (inode && S_ISREG(inode->i_mode)) 20301751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2031ac795161STrond Myklebust } else if (!IS_ERR(res)) { 2032ac795161STrond Myklebust inode = d_inode(res); 2033ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2034e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) { 2035ac795161STrond Myklebust dput(res); 2036ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 20371751fc1dSTrond Myklebust } else if (inode && S_ISREG(inode->i_mode)) { 20381751fc1dSTrond Myklebust dput(res); 20391751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2040ac795161STrond Myklebust } 2041ac795161STrond Myklebust } 2042c94c0953SAl Viro if (switched) { 2043c94c0953SAl Viro d_lookup_done(dentry); 2044c94c0953SAl Viro if (!res) 2045c94c0953SAl Viro res = dentry; 2046c94c0953SAl Viro else 2047c94c0953SAl Viro dput(dentry); 2048c94c0953SAl Viro } 20490dd2b474SMiklos Szeredi if (IS_ERR(res)) 2050c94c0953SAl Viro return PTR_ERR(res); 2051e45198a6SAl Viro return finish_no_open(file, res); 20521da177e4SLinus Torvalds } 205389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 20541da177e4SLinus Torvalds 2055c7944ebbSTrond Myklebust static int 2056c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 2057c7944ebbSTrond Myklebust unsigned int flags) 20581da177e4SLinus Torvalds { 2059657e94b6SNick Piggin struct inode *inode; 20601da177e4SLinus Torvalds 2061fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 2062c7944ebbSTrond Myklebust goto full_reval; 2063eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 2064c7944ebbSTrond Myklebust goto full_reval; 20652b484297STrond Myklebust 20662b0143b5SDavid Howells inode = d_inode(dentry); 20672b484297STrond Myklebust 20681da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 20691da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 20701da177e4SLinus Torvalds */ 2071c7944ebbSTrond Myklebust if (inode == NULL) 2072c7944ebbSTrond Myklebust goto full_reval; 207349317a7fSNeilBrown 2074efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 2075c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2076216d5d06STrond Myklebust 20771da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 20781da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 2079c7944ebbSTrond Myklebust goto full_reval; 2080c7944ebbSTrond Myklebust 20811da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 2082c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2083c7944ebbSTrond Myklebust goto reval_dentry; 2084c7944ebbSTrond Myklebust 2085c7944ebbSTrond Myklebust /* Check if the directory changed */ 2086c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2087c7944ebbSTrond Myklebust goto reval_dentry; 20881da177e4SLinus Torvalds 20890ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 2090c7944ebbSTrond Myklebust return 1; 2091c7944ebbSTrond Myklebust reval_dentry: 2092c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 2093c7944ebbSTrond Myklebust return -ECHILD; 209442f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 20950ef97dcfSMiklos Szeredi 2096c7944ebbSTrond Myklebust full_reval: 2097c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 2098c7944ebbSTrond Myklebust } 2099535918f1STrond Myklebust 2100c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2101c7944ebbSTrond Myklebust { 2102c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 2103c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 2104c0204fd2STrond Myklebust } 2105c0204fd2STrond Myklebust 21061da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 21071da177e4SLinus Torvalds 2108406cd915SBenjamin Coddington struct dentry * 2109406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 2110cc6f3298SAnna Schumaker struct nfs_fattr *fattr) 21111da177e4SLinus Torvalds { 2112fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 21132b0143b5SDavid Howells struct inode *dir = d_inode(parent); 21141da177e4SLinus Torvalds struct inode *inode; 2115b0c6108eSAl Viro struct dentry *d; 2116406cd915SBenjamin Coddington int error; 21171da177e4SLinus Torvalds 2118fab728e1STrond Myklebust d_drop(dentry); 2119fab728e1STrond Myklebust 21201da177e4SLinus Torvalds if (fhandle->size == 0) { 21219558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 21221da177e4SLinus Torvalds if (error) 2123fab728e1STrond Myklebust goto out_error; 21241da177e4SLinus Torvalds } 21255724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 21261da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 21271da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 2128a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 21292ef61e0eSAnna Schumaker fattr, NULL); 21301da177e4SLinus Torvalds if (error < 0) 2131fab728e1STrond Myklebust goto out_error; 21321da177e4SLinus Torvalds } 2133cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 2134b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 2135fab728e1STrond Myklebust out: 2136fab728e1STrond Myklebust dput(parent); 2137406cd915SBenjamin Coddington return d; 2138fab728e1STrond Myklebust out_error: 2139406cd915SBenjamin Coddington d = ERR_PTR(error); 2140406cd915SBenjamin Coddington goto out; 2141406cd915SBenjamin Coddington } 2142406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2143406cd915SBenjamin Coddington 2144406cd915SBenjamin Coddington /* 2145406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 2146406cd915SBenjamin Coddington */ 2147406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2148d91bfc46SAnna Schumaker struct nfs_fattr *fattr) 2149406cd915SBenjamin Coddington { 2150406cd915SBenjamin Coddington struct dentry *d; 2151406cd915SBenjamin Coddington 2152cc6f3298SAnna Schumaker d = nfs_add_or_obtain(dentry, fhandle, fattr); 2153406cd915SBenjamin Coddington if (IS_ERR(d)) 2154406cd915SBenjamin Coddington return PTR_ERR(d); 2155406cd915SBenjamin Coddington 2156406cd915SBenjamin Coddington /* Callers don't care */ 2157406cd915SBenjamin Coddington dput(d); 2158406cd915SBenjamin Coddington return 0; 21591da177e4SLinus Torvalds } 2160ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds /* 21631da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 21641da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 21651da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 21661da177e4SLinus Torvalds * reply path made it appear to have failed. 21671da177e4SLinus Torvalds */ 2168549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2169549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 21701da177e4SLinus Torvalds { 21711da177e4SLinus Torvalds struct iattr attr; 2172ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 21731da177e4SLinus Torvalds int error; 21741da177e4SLinus Torvalds 21751e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 21766de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds attr.ia_mode = mode; 21791da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21801da177e4SLinus Torvalds 21818b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 21828867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 21838b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 21841da177e4SLinus Torvalds if (error != 0) 21851da177e4SLinus Torvalds goto out_err; 21861da177e4SLinus Torvalds return 0; 21871da177e4SLinus Torvalds out_err: 21881da177e4SLinus Torvalds d_drop(dentry); 21891da177e4SLinus Torvalds return error; 21901da177e4SLinus Torvalds } 2191ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds /* 21941da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 21951da177e4SLinus Torvalds */ 2196597d9289SBryan Schumaker int 2197549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2198549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev) 21991da177e4SLinus Torvalds { 22001da177e4SLinus Torvalds struct iattr attr; 22011da177e4SLinus Torvalds int status; 22021da177e4SLinus Torvalds 22031e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 22046de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds attr.ia_mode = mode; 22071da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 22081da177e4SLinus Torvalds 22091ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 22101da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 22111ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 22121da177e4SLinus Torvalds if (status != 0) 22131da177e4SLinus Torvalds goto out_err; 22141da177e4SLinus Torvalds return 0; 22151da177e4SLinus Torvalds out_err: 22161da177e4SLinus Torvalds d_drop(dentry); 22171da177e4SLinus Torvalds return status; 22181da177e4SLinus Torvalds } 2219ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds /* 22221da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 22231da177e4SLinus Torvalds */ 2224549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2225549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 22261da177e4SLinus Torvalds { 22271da177e4SLinus Torvalds struct iattr attr; 22281da177e4SLinus Torvalds int error; 22291da177e4SLinus Torvalds 22301e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 22316de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 22341da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 22351da177e4SLinus Torvalds 22361ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 22371da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 22381ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 22391da177e4SLinus Torvalds if (error != 0) 22401da177e4SLinus Torvalds goto out_err; 22411da177e4SLinus Torvalds return 0; 22421da177e4SLinus Torvalds out_err: 22431da177e4SLinus Torvalds d_drop(dentry); 22441da177e4SLinus Torvalds return error; 22451da177e4SLinus Torvalds } 2246ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 22471da177e4SLinus Torvalds 2248d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2249d45b9d8bSTrond Myklebust { 2250dc3f4198SAl Viro if (simple_positive(dentry)) 2251d45b9d8bSTrond Myklebust d_delete(dentry); 2252d45b9d8bSTrond Myklebust } 2253d45b9d8bSTrond Myklebust 22549019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir, 22559019fb39STrond Myklebust struct dentry *dentry, int error) 22569019fb39STrond Myklebust { 22579019fb39STrond Myklebust switch (error) { 22589019fb39STrond Myklebust case -ENOENT: 22599019fb39STrond Myklebust d_delete(dentry); 226000bdadc7STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 226100bdadc7STrond Myklebust break; 22629019fb39STrond Myklebust case 0: 226300bdadc7STrond Myklebust nfs_d_prune_case_insensitive_aliases(d_inode(dentry)); 22649019fb39STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 22659019fb39STrond Myklebust } 22669019fb39STrond Myklebust } 22679019fb39STrond Myklebust 2268597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 22691da177e4SLinus Torvalds { 22701da177e4SLinus Torvalds int error; 22711da177e4SLinus Torvalds 22721e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 22736de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22741da177e4SLinus Torvalds 22751ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 22762b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2277884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 22781da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 22791da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2280ba6c0592STrond Myklebust switch (error) { 2281ba6c0592STrond Myklebust case 0: 22822b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2283ba6c0592STrond Myklebust break; 2284ba6c0592STrond Myklebust case -ENOENT: 2285d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2286ba6c0592STrond Myklebust } 2287884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2288ba6c0592STrond Myklebust } else 2289ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 22909019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 22911ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds return error; 22941da177e4SLinus Torvalds } 2295ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds /* 22981da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 22991da177e4SLinus Torvalds * and after checking that the file has only one user. 23001da177e4SLinus Torvalds * 23011da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 23021da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 23031da177e4SLinus Torvalds */ 23041da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 23051da177e4SLinus Torvalds { 23062b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 23072b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 23081da177e4SLinus Torvalds int error = -EBUSY; 23091da177e4SLinus Torvalds 23106de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 23131da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 23141da177e4SLinus Torvalds error = 0; 23151da177e4SLinus Torvalds goto out; 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds 23181ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 23191da177e4SLinus Torvalds if (inode != NULL) { 2320912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 23211da177e4SLinus Torvalds if (error == 0) 23221b83d707STrond Myklebust nfs_drop_nlink(inode); 23231da177e4SLinus Torvalds } else 2324912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2325d45b9d8bSTrond Myklebust if (error == -ENOENT) 2326d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 23271ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 23281da177e4SLinus Torvalds out: 23291da177e4SLinus Torvalds return error; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 23331da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 23341da177e4SLinus Torvalds * 23351da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 23361da177e4SLinus Torvalds */ 2337597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 23381da177e4SLinus Torvalds { 23391da177e4SLinus Torvalds int error; 23401da177e4SLinus Torvalds int need_rehash = 0; 23411da177e4SLinus Torvalds 23421e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 23436de1472fSAl Viro dir->i_ino, dentry); 23441da177e4SLinus Torvalds 23451ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 23461da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 234743245ecaSOlga Kornievskaia if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED, 234843245ecaSOlga Kornievskaia &NFS_I(d_inode(dentry))->flags)) { 23491da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2350ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 23512b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 23521da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 23531ca42382STrond Myklebust goto out; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 23561da177e4SLinus Torvalds __d_drop(dentry); 23571da177e4SLinus Torvalds need_rehash = 1; 23581da177e4SLinus Torvalds } 23591da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 23601da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 23619019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 23629019fb39STrond Myklebust if (need_rehash) 23631da177e4SLinus Torvalds d_rehash(dentry); 23641ca42382STrond Myklebust out: 23651ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 23661da177e4SLinus Torvalds return error; 23671da177e4SLinus Torvalds } 2368ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 23691da177e4SLinus Torvalds 2370873101b3SChuck Lever /* 2371873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2372873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2373873101b3SChuck Lever * using prepare_write/commit_write. 2374873101b3SChuck Lever * 2375873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2376873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2377873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2378873101b3SChuck Lever * symlink request has completed on the server. 2379873101b3SChuck Lever * 2380873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2381873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2382873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2383873101b3SChuck Lever * and move the raw page into its mapping. 2384873101b3SChuck Lever */ 2385549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 2386549c7297SChristian Brauner struct dentry *dentry, const char *symname) 23871da177e4SLinus Torvalds { 2388873101b3SChuck Lever struct page *page; 2389873101b3SChuck Lever char *kaddr; 23901da177e4SLinus Torvalds struct iattr attr; 2391873101b3SChuck Lever unsigned int pathlen = strlen(symname); 23921da177e4SLinus Torvalds int error; 23931da177e4SLinus Torvalds 23941e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 23956de1472fSAl Viro dir->i_ino, dentry, symname); 23961da177e4SLinus Torvalds 2397873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2398873101b3SChuck Lever return -ENAMETOOLONG; 23991da177e4SLinus Torvalds 2400873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2401873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 24021da177e4SLinus Torvalds 2403e8ecde25SAl Viro page = alloc_page(GFP_USER); 240476566991STrond Myklebust if (!page) 2405873101b3SChuck Lever return -ENOMEM; 2406873101b3SChuck Lever 2407e8ecde25SAl Viro kaddr = page_address(page); 2408873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2409873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2410873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2411873101b3SChuck Lever 24121ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 241394a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 24141ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2415873101b3SChuck Lever if (error != 0) { 24161e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2417873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 24186de1472fSAl Viro dentry, symname, error); 24191da177e4SLinus Torvalds d_drop(dentry); 2420873101b3SChuck Lever __free_page(page); 24211da177e4SLinus Torvalds return error; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 2424342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2425342a67f0STrond Myklebust 2426873101b3SChuck Lever /* 2427873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2428873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2429873101b3SChuck Lever */ 24302b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2431873101b3SChuck Lever GFP_KERNEL)) { 2432873101b3SChuck Lever SetPageUptodate(page); 2433873101b3SChuck Lever unlock_page(page); 2434a0b54addSRafael Aquini /* 2435a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2436a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2437a0b54addSRafael Aquini */ 243809cbfeafSKirill A. Shutemov put_page(page); 2439873101b3SChuck Lever } else 2440873101b3SChuck Lever __free_page(page); 2441873101b3SChuck Lever 2442873101b3SChuck Lever return 0; 2443873101b3SChuck Lever } 2444ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2445873101b3SChuck Lever 2446597d9289SBryan Schumaker int 24471da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 24481da177e4SLinus Torvalds { 24492b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 24501da177e4SLinus Torvalds int error; 24511da177e4SLinus Torvalds 24526de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 24536de1472fSAl Viro old_dentry, dentry); 24541da177e4SLinus Torvalds 24551fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 24569697d234STrond Myklebust d_drop(dentry); 245720497503STrond Myklebust if (S_ISREG(inode->i_mode)) 245820497503STrond Myklebust nfs_sync_inode(inode); 24591da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2460cf809556STrond Myklebust if (error == 0) { 2461342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 24627de9c6eeSAl Viro ihold(inode); 24639697d234STrond Myklebust d_add(dentry, inode); 2464cf809556STrond Myklebust } 24651fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 24661da177e4SLinus Torvalds return error; 24671da177e4SLinus Torvalds } 2468ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds /* 24711da177e4SLinus Torvalds * RENAME 24721da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 24731da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 24741da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 24751da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 24761da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 24771da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 24781da177e4SLinus Torvalds * 24791da177e4SLinus Torvalds * FIXED. 24801da177e4SLinus Torvalds * 24811da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 24821da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 24831da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 24841da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 24851da177e4SLinus Torvalds * using the inode layer 24861da177e4SLinus Torvalds * 24871da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 24881da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 24891da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 24901da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 24911da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 24921da177e4SLinus Torvalds * the rename. 24931da177e4SLinus Torvalds */ 2494549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2495549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 2496549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 24971da177e4SLinus Torvalds { 24982b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 24992b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2500d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 250180a491fdSJeff Layton struct rpc_task *task; 25021da177e4SLinus Torvalds int error = -EBUSY; 25031da177e4SLinus Torvalds 25041cd66c93SMiklos Szeredi if (flags) 25051cd66c93SMiklos Szeredi return -EINVAL; 25061cd66c93SMiklos Szeredi 25076de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 25086de1472fSAl Viro old_dentry, new_dentry, 250984d08fa8SAl Viro d_count(new_dentry)); 25101da177e4SLinus Torvalds 251170ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 25121da177e4SLinus Torvalds /* 251328f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 251428f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 251528f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 251628f79a1aSMiklos Szeredi * the new target. 25171da177e4SLinus Torvalds */ 251827226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 251927226104SMiklos Szeredi /* 252027226104SMiklos Szeredi * To prevent any new references to the target during the 252127226104SMiklos Szeredi * rename, we unhash the dentry in advance. 252227226104SMiklos Szeredi */ 2523d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 252427226104SMiklos Szeredi d_drop(new_dentry); 2525d9f29500SBenjamin Coddington rehash = new_dentry; 2526d9f29500SBenjamin Coddington } 252727226104SMiklos Szeredi 252884d08fa8SAl Viro if (d_count(new_dentry) > 2) { 25291da177e4SLinus Torvalds int err; 253027226104SMiklos Szeredi 25311da177e4SLinus Torvalds /* copy the target dentry's name */ 25321da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 25331da177e4SLinus Torvalds &new_dentry->d_name); 25341da177e4SLinus Torvalds if (!dentry) 25351da177e4SLinus Torvalds goto out; 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds /* silly-rename the existing target ... */ 25381da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 253924e93025SMiklos Szeredi if (err) 25401da177e4SLinus Torvalds goto out; 254124e93025SMiklos Szeredi 254224e93025SMiklos Szeredi new_dentry = dentry; 2543d9f29500SBenjamin Coddington rehash = NULL; 254424e93025SMiklos Szeredi new_inode = NULL; 2545b1e4adf4STrond Myklebust } 254627226104SMiklos Szeredi } 25471da177e4SLinus Torvalds 25486ff9d99bSTrond Myklebust if (S_ISREG(old_inode->i_mode)) 25496ff9d99bSTrond Myklebust nfs_sync_inode(old_inode); 2550d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 255180a491fdSJeff Layton if (IS_ERR(task)) { 255280a491fdSJeff Layton error = PTR_ERR(task); 255380a491fdSJeff Layton goto out; 255480a491fdSJeff Layton } 255580a491fdSJeff Layton 255680a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2557818a8dbeSBenjamin Coddington if (error != 0) { 2558818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2559818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2560818a8dbeSBenjamin Coddington smp_wmb(); 2561818a8dbeSBenjamin Coddington } else 256280a491fdSJeff Layton error = task->tk_status; 256380a491fdSJeff Layton rpc_put_task(task); 256459a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 256559a707b0STrond Myklebust if (error == 0) { 256659a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 256759a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2568ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2569ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME | 2570ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 257159a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 257259a707b0STrond Myklebust } 25731da177e4SLinus Torvalds out: 2574d9f29500SBenjamin Coddington if (rehash) 2575d9f29500SBenjamin Coddington d_rehash(rehash); 257670ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 257770ded201STrond Myklebust new_dir, new_dentry, error); 2578d9f29500SBenjamin Coddington if (!error) { 2579d9f29500SBenjamin Coddington if (new_inode != NULL) 2580d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2581d9f29500SBenjamin Coddington /* 2582d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2583d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2584d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2585d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2586d9f29500SBenjamin Coddington */ 2587d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2588d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2589d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2590d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2591d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2592d9f29500SBenjamin Coddington 25931da177e4SLinus Torvalds /* new dentry created? */ 25941da177e4SLinus Torvalds if (dentry) 25951da177e4SLinus Torvalds dput(dentry); 25961da177e4SLinus Torvalds return error; 25971da177e4SLinus Torvalds } 2598ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 25991da177e4SLinus Torvalds 2600cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2601cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2602cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2603cfcea3e8STrond Myklebust 2604a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 26053a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 26063a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 26073a505845STrond Myklebust 26081c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 26091c3c07e9STrond Myklebust { 26106238aec8SNeilBrown put_group_info(entry->group_info); 2611f682a398SNeilBrown kfree_rcu(entry, rcu_head); 26124e857c58SPeter Zijlstra smp_mb__before_atomic(); 2613cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 26144e857c58SPeter Zijlstra smp_mb__after_atomic(); 26151c3c07e9STrond Myklebust } 26161c3c07e9STrond Myklebust 26171a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 26181a81bb8aSTrond Myklebust { 26191a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 26201a81bb8aSTrond Myklebust 26211a81bb8aSTrond Myklebust while (!list_empty(head)) { 26221a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 26231a81bb8aSTrond Myklebust list_del(&cache->lru); 26241a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 26251a81bb8aSTrond Myklebust } 26261a81bb8aSTrond Myklebust } 26271a81bb8aSTrond Myklebust 26283a505845STrond Myklebust static unsigned long 26293a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2630979df72eSTrond Myklebust { 2631979df72eSTrond Myklebust LIST_HEAD(head); 2632aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2633979df72eSTrond Myklebust struct nfs_access_entry *cache; 26341ab6c499SDave Chinner long freed = 0; 2635979df72eSTrond Myklebust 2636a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2637aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2638979df72eSTrond Myklebust struct inode *inode; 2639979df72eSTrond Myklebust 2640979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2641979df72eSTrond Myklebust break; 26429c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2643979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2644979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2645979df72eSTrond Myklebust goto remove_lru_entry; 2646979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2647979df72eSTrond Myklebust struct nfs_access_entry, lru); 2648979df72eSTrond Myklebust list_move(&cache->lru, &head); 2649979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 26501ab6c499SDave Chinner freed++; 2651979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2652979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2653979df72eSTrond Myklebust &nfs_access_lru_list); 2654979df72eSTrond Myklebust else { 2655979df72eSTrond Myklebust remove_lru_entry: 2656979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 26574e857c58SPeter Zijlstra smp_mb__before_atomic(); 2658979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 26594e857c58SPeter Zijlstra smp_mb__after_atomic(); 2660979df72eSTrond Myklebust } 266159844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2662979df72eSTrond Myklebust } 2663979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 26641a81bb8aSTrond Myklebust nfs_access_free_list(&head); 26651ab6c499SDave Chinner return freed; 26661ab6c499SDave Chinner } 26671ab6c499SDave Chinner 26681ab6c499SDave Chinner unsigned long 26693a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 26703a505845STrond Myklebust { 26713a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 26723a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 26733a505845STrond Myklebust 26743a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 26753a505845STrond Myklebust return SHRINK_STOP; 26763a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 26773a505845STrond Myklebust } 26783a505845STrond Myklebust 26793a505845STrond Myklebust 26803a505845STrond Myklebust unsigned long 26811ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 26821ab6c499SDave Chinner { 268355f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2684979df72eSTrond Myklebust } 2685979df72eSTrond Myklebust 26863a505845STrond Myklebust static void 26873a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 26883a505845STrond Myklebust { 26893a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 26903a505845STrond Myklebust unsigned long diff; 26913a505845STrond Myklebust unsigned int nr_to_scan; 26923a505845STrond Myklebust 26933a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 26943a505845STrond Myklebust return; 26953a505845STrond Myklebust nr_to_scan = 100; 26963a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 26973a505845STrond Myklebust if (diff < nr_to_scan) 26983a505845STrond Myklebust nr_to_scan = diff; 26993a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 27003a505845STrond Myklebust } 27013a505845STrond Myklebust 27021a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 27031c3c07e9STrond Myklebust { 27041c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 27051a81bb8aSTrond Myklebust struct rb_node *n; 27061c3c07e9STrond Myklebust struct nfs_access_entry *entry; 27071c3c07e9STrond Myklebust 27081c3c07e9STrond Myklebust /* Unhook entries from the cache */ 27091c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 27101c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 27111c3c07e9STrond Myklebust rb_erase(n, root_node); 27121a81bb8aSTrond Myklebust list_move(&entry->lru, head); 27131c3c07e9STrond Myklebust } 27141c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 27151c3c07e9STrond Myklebust } 27161c3c07e9STrond Myklebust 27171c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 27181c3c07e9STrond Myklebust { 27191a81bb8aSTrond Myklebust LIST_HEAD(head); 27201a81bb8aSTrond Myklebust 27211a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 27221a81bb8aSTrond Myklebust return; 2723cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2724cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 27251a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2726cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2727cfcea3e8STrond Myklebust 27281c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 27291a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 27301a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 27311a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 27321a81bb8aSTrond Myklebust nfs_access_free_list(&head); 27331c3c07e9STrond Myklebust } 27341c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 27351c3c07e9STrond Myklebust 27366238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b) 27376238aec8SNeilBrown { 27386238aec8SNeilBrown struct group_info *ga, *gb; 27396238aec8SNeilBrown int g; 27406238aec8SNeilBrown 27416238aec8SNeilBrown if (uid_lt(a->fsuid, b->fsuid)) 27426238aec8SNeilBrown return -1; 27436238aec8SNeilBrown if (uid_gt(a->fsuid, b->fsuid)) 27446238aec8SNeilBrown return 1; 27456238aec8SNeilBrown 27466238aec8SNeilBrown if (gid_lt(a->fsgid, b->fsgid)) 27476238aec8SNeilBrown return -1; 27486238aec8SNeilBrown if (gid_gt(a->fsgid, b->fsgid)) 27496238aec8SNeilBrown return 1; 27506238aec8SNeilBrown 27516238aec8SNeilBrown ga = a->group_info; 27526238aec8SNeilBrown gb = b->group_info; 27536238aec8SNeilBrown if (ga == gb) 27546238aec8SNeilBrown return 0; 27556238aec8SNeilBrown if (ga == NULL) 27566238aec8SNeilBrown return -1; 27576238aec8SNeilBrown if (gb == NULL) 27586238aec8SNeilBrown return 1; 27596238aec8SNeilBrown if (ga->ngroups < gb->ngroups) 27606238aec8SNeilBrown return -1; 27616238aec8SNeilBrown if (ga->ngroups > gb->ngroups) 27626238aec8SNeilBrown return 1; 27636238aec8SNeilBrown 27646238aec8SNeilBrown for (g = 0; g < ga->ngroups; g++) { 27656238aec8SNeilBrown if (gid_lt(ga->gid[g], gb->gid[g])) 27666238aec8SNeilBrown return -1; 27676238aec8SNeilBrown if (gid_gt(ga->gid[g], gb->gid[g])) 27686238aec8SNeilBrown return 1; 27696238aec8SNeilBrown } 27706238aec8SNeilBrown return 0; 27716238aec8SNeilBrown } 27726238aec8SNeilBrown 2773b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 27741c3c07e9STrond Myklebust { 27751c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 27761c3c07e9STrond Myklebust 27771c3c07e9STrond Myklebust while (n != NULL) { 2778b68572e0SNeilBrown struct nfs_access_entry *entry = 2779b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 27806238aec8SNeilBrown int cmp = access_cmp(cred, entry); 27811c3c07e9STrond Myklebust 2782b68572e0SNeilBrown if (cmp < 0) 27831c3c07e9STrond Myklebust n = n->rb_left; 2784b68572e0SNeilBrown else if (cmp > 0) 27851c3c07e9STrond Myklebust n = n->rb_right; 27861c3c07e9STrond Myklebust else 27871c3c07e9STrond Myklebust return entry; 27881c3c07e9STrond Myklebust } 27891c3c07e9STrond Myklebust return NULL; 27901c3c07e9STrond Myklebust } 27911c3c07e9STrond Myklebust 2792b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block) 27931da177e4SLinus Torvalds { 279455296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 27951c3c07e9STrond Myklebust struct nfs_access_entry *cache; 279657b69181STrond Myklebust bool retry = true; 279757b69181STrond Myklebust int err; 27981da177e4SLinus Torvalds 27991c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 280057b69181STrond Myklebust for(;;) { 28011c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 28021c3c07e9STrond Myklebust goto out_zap; 28031c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 280457b69181STrond Myklebust err = -ENOENT; 28051c3c07e9STrond Myklebust if (cache == NULL) 28061c3c07e9STrond Myklebust goto out; 280757b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 280821c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 280957b69181STrond Myklebust break; 28105c965db8STrond Myklebust if (!retry) 28115c965db8STrond Myklebust break; 281257b69181STrond Myklebust err = -ECHILD; 281357b69181STrond Myklebust if (!may_block) 281457b69181STrond Myklebust goto out; 281557b69181STrond Myklebust spin_unlock(&inode->i_lock); 281657b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 281757b69181STrond Myklebust if (err) 281857b69181STrond Myklebust return err; 281957b69181STrond Myklebust spin_lock(&inode->i_lock); 282057b69181STrond Myklebust retry = false; 282157b69181STrond Myklebust } 2822b5e7b59cSNeilBrown *mask = cache->mask; 2823cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 28241c3c07e9STrond Myklebust err = 0; 28251c3c07e9STrond Myklebust out: 28261c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 28271c3c07e9STrond Myklebust return err; 28281c3c07e9STrond Myklebust out_zap: 28291a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 28301a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 28311c3c07e9STrond Myklebust return -ENOENT; 28321c3c07e9STrond Myklebust } 28331c3c07e9STrond Myklebust 2834b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask) 2835f682a398SNeilBrown { 2836f682a398SNeilBrown /* Only check the most recently returned cache entry, 2837f682a398SNeilBrown * but do it without locking. 2838f682a398SNeilBrown */ 2839f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2840f682a398SNeilBrown struct nfs_access_entry *cache; 2841f682a398SNeilBrown int err = -ECHILD; 2842f682a398SNeilBrown struct list_head *lh; 2843f682a398SNeilBrown 2844f682a398SNeilBrown rcu_read_lock(); 2845f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2846f682a398SNeilBrown goto out; 28479f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2848f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2849f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 28506238aec8SNeilBrown access_cmp(cred, cache) != 0) 2851f682a398SNeilBrown cache = NULL; 2852f682a398SNeilBrown if (cache == NULL) 2853f682a398SNeilBrown goto out; 285421c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2855f682a398SNeilBrown goto out; 2856b5e7b59cSNeilBrown *mask = cache->mask; 285721c3ba7eSTrond Myklebust err = 0; 2858f682a398SNeilBrown out: 2859f682a398SNeilBrown rcu_read_unlock(); 2860f682a398SNeilBrown return err; 2861f682a398SNeilBrown } 2862f682a398SNeilBrown 2863b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred, 2864b5e7b59cSNeilBrown u32 *mask, bool may_block) 2865d2ae4f8bSFrank van der Linden { 2866d2ae4f8bSFrank van der Linden int status; 2867d2ae4f8bSFrank van der Linden 2868b5e7b59cSNeilBrown status = nfs_access_get_cached_rcu(inode, cred, mask); 2869d2ae4f8bSFrank van der Linden if (status != 0) 2870b5e7b59cSNeilBrown status = nfs_access_get_cached_locked(inode, cred, mask, 2871d2ae4f8bSFrank van der Linden may_block); 2872d2ae4f8bSFrank van der Linden 2873d2ae4f8bSFrank van der Linden return status; 2874d2ae4f8bSFrank van der Linden } 2875d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2876d2ae4f8bSFrank van der Linden 287773fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode, 287873fbb3faSNeilBrown struct nfs_access_entry *set, 287973fbb3faSNeilBrown const struct cred *cred) 28801c3c07e9STrond Myklebust { 2881cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2882cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 28831c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 28841c3c07e9STrond Myklebust struct rb_node *parent = NULL; 28851c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2886b68572e0SNeilBrown int cmp; 28871c3c07e9STrond Myklebust 28881c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 28891c3c07e9STrond Myklebust while (*p != NULL) { 28901c3c07e9STrond Myklebust parent = *p; 28911c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 28926238aec8SNeilBrown cmp = access_cmp(cred, entry); 28931c3c07e9STrond Myklebust 2894b68572e0SNeilBrown if (cmp < 0) 28951c3c07e9STrond Myklebust p = &parent->rb_left; 2896b68572e0SNeilBrown else if (cmp > 0) 28971c3c07e9STrond Myklebust p = &parent->rb_right; 28981c3c07e9STrond Myklebust else 28991c3c07e9STrond Myklebust goto found; 29001c3c07e9STrond Myklebust } 29011c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 29021c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2903cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 29041c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29051c3c07e9STrond Myklebust return; 29061c3c07e9STrond Myklebust found: 29071c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2908cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2909cfcea3e8STrond Myklebust list_del(&entry->lru); 29101c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29111c3c07e9STrond Myklebust nfs_access_free_entry(entry); 29121da177e4SLinus Torvalds } 29131da177e4SLinus Torvalds 291473fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set, 291573fbb3faSNeilBrown const struct cred *cred) 29161da177e4SLinus Torvalds { 29171c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 29181c3c07e9STrond Myklebust if (cache == NULL) 29191c3c07e9STrond Myklebust return; 29201c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 29216238aec8SNeilBrown cache->fsuid = cred->fsuid; 29226238aec8SNeilBrown cache->fsgid = cred->fsgid; 29236238aec8SNeilBrown cache->group_info = get_group_info(cred->group_info); 29241da177e4SLinus Torvalds cache->mask = set->mask; 29251c3c07e9STrond Myklebust 2926f682a398SNeilBrown /* The above field assignments must be visible 2927f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2928f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2929f682a398SNeilBrown */ 2930f682a398SNeilBrown smp_wmb(); 293173fbb3faSNeilBrown nfs_access_add_rbtree(inode, cache, cred); 2932cfcea3e8STrond Myklebust 2933cfcea3e8STrond Myklebust /* Update accounting */ 29344e857c58SPeter Zijlstra smp_mb__before_atomic(); 2935cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 29364e857c58SPeter Zijlstra smp_mb__after_atomic(); 2937cfcea3e8STrond Myklebust 2938cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 29391a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2940cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 29411a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 29421a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 29431a81bb8aSTrond Myklebust &nfs_access_lru_list); 2944cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2945cfcea3e8STrond Myklebust } 29463a505845STrond Myklebust nfs_access_cache_enforce_limit(); 29471da177e4SLinus Torvalds } 29486168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 29496168f62cSWeston Andros Adamson 29503c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 29513c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 29523c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 29533c181827SAnna Schumaker NFS_ACCESS_DELETE) 29543c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 29553c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2956ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 29573c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 29583c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 295915d4b73aSTrond Myklebust static int 2960ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 296115d4b73aSTrond Myklebust { 296215d4b73aSTrond Myklebust int mask = 0; 296315d4b73aSTrond Myklebust 296415d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 296515d4b73aSTrond Myklebust mask |= MAY_READ; 2966ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2967ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 296815d4b73aSTrond Myklebust mask |= MAY_WRITE; 2969ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 297015d4b73aSTrond Myklebust mask |= MAY_EXEC; 2971ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2972ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2973ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2974ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 297515d4b73aSTrond Myklebust mask |= MAY_EXEC; 2976ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2977ecbb903cSTrond Myklebust mask |= MAY_WRITE; 297815d4b73aSTrond Myklebust return mask; 297915d4b73aSTrond Myklebust } 298015d4b73aSTrond Myklebust 29816168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 29826168f62cSWeston Andros Adamson { 2983bd8b2441STrond Myklebust entry->mask = access_result; 29846168f62cSWeston Andros Adamson } 29856168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 29861da177e4SLinus Torvalds 2987b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 29881da177e4SLinus Torvalds { 29891da177e4SLinus Torvalds struct nfs_access_entry cache; 299057b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2991e8194b7dSTrond Myklebust int cache_mask = -1; 29921da177e4SLinus Torvalds int status; 29931da177e4SLinus Torvalds 2994f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2995f4ce1299STrond Myklebust 2996b5e7b59cSNeilBrown status = nfs_access_get_cached(inode, cred, &cache.mask, may_block); 29971da177e4SLinus Torvalds if (status == 0) 2998f4ce1299STrond Myklebust goto out_cached; 29991da177e4SLinus Torvalds 3000f3324a2aSNeilBrown status = -ECHILD; 300157b69181STrond Myklebust if (!may_block) 3002f3324a2aSNeilBrown goto out; 3003f3324a2aSNeilBrown 30041750d929SAnna Schumaker /* 30051750d929SAnna Schumaker * Determine which access bits we want to ask for... 30061750d929SAnna Schumaker */ 300784631f84STrond Myklebust cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND | 300884631f84STrond Myklebust nfs_access_xattr_mask(NFS_SERVER(inode)); 30091750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 30101750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 30111750d929SAnna Schumaker else 30121750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 301373fbb3faSNeilBrown status = NFS_PROTO(inode)->access(inode, &cache, cred); 3014a71ee337SSuresh Jayaraman if (status != 0) { 3015a71ee337SSuresh Jayaraman if (status == -ESTALE) { 3016a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 301793ce4af7STrond Myklebust nfs_set_inode_stale(inode); 301893ce4af7STrond Myklebust else 301993ce4af7STrond Myklebust nfs_zap_caches(inode); 3020a71ee337SSuresh Jayaraman } 3021f4ce1299STrond Myklebust goto out; 3022a71ee337SSuresh Jayaraman } 302373fbb3faSNeilBrown nfs_access_add_cache(inode, &cache, cred); 3024f4ce1299STrond Myklebust out_cached: 3025ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 3026bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 3027f4ce1299STrond Myklebust status = -EACCES; 30281da177e4SLinus Torvalds out: 3029e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 3030f4ce1299STrond Myklebust return status; 30311da177e4SLinus Torvalds } 30321da177e4SLinus Torvalds 3033af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 3034af22f94aSTrond Myklebust { 3035af22f94aSTrond Myklebust int mask = 0; 3036af22f94aSTrond Myklebust 3037f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 3038f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 3039f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 3040f8d9a897SWeston Andros Adamson } else { 30418a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 3042af22f94aSTrond Myklebust mask |= MAY_READ; 30438a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 3044af22f94aSTrond Myklebust mask |= MAY_WRITE; 3045f8d9a897SWeston Andros Adamson } 3046f8d9a897SWeston Andros Adamson 3047af22f94aSTrond Myklebust return mask; 3048af22f94aSTrond Myklebust } 3049af22f94aSTrond Myklebust 3050b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 3051af22f94aSTrond Myklebust { 3052af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 3053af22f94aSTrond Myklebust } 305489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 3055af22f94aSTrond Myklebust 30565c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 30575c5fc09aSTrond Myklebust { 30585c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 305921c3ba7eSTrond Myklebust int ret = 0; 30605c5fc09aSTrond Myklebust 30613825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 30623825827eSTrond Myklebust return 0; 3063720869ebSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { 30645c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 306521c3ba7eSTrond Myklebust return -ECHILD; 306621c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 306721c3ba7eSTrond Myklebust } 30685c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 30695c5fc09aSTrond Myklebust ret = -EACCES; 30705c5fc09aSTrond Myklebust return ret; 30715c5fc09aSTrond Myklebust } 30725c5fc09aSTrond Myklebust 3073549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns, 3074549c7297SChristian Brauner struct inode *inode, 3075549c7297SChristian Brauner int mask) 30761da177e4SLinus Torvalds { 3077b68572e0SNeilBrown const struct cred *cred = current_cred(); 30781da177e4SLinus Torvalds int res = 0; 30791da177e4SLinus Torvalds 308091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 308191d5b470SChuck Lever 3082e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 30831da177e4SLinus Torvalds goto out; 30841da177e4SLinus Torvalds /* Is this sys_access() ? */ 30859cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 30861da177e4SLinus Torvalds goto force_lookup; 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 30891da177e4SLinus Torvalds case S_IFLNK: 30901da177e4SLinus Torvalds goto out; 30911da177e4SLinus Torvalds case S_IFREG: 3092762674f8STrond Myklebust if ((mask & MAY_OPEN) && 3093762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 3094762674f8STrond Myklebust return 0; 30951da177e4SLinus Torvalds break; 30961da177e4SLinus Torvalds case S_IFDIR: 30971da177e4SLinus Torvalds /* 30981da177e4SLinus Torvalds * Optimize away all write operations, since the server 30991da177e4SLinus Torvalds * will check permissions when we perform the op. 31001da177e4SLinus Torvalds */ 31011da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 31021da177e4SLinus Torvalds goto out; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds force_lookup: 31061da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 31071da177e4SLinus Torvalds goto out_notsup; 31081da177e4SLinus Torvalds 31091da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 31101da177e4SLinus Torvalds out: 31115c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 31125c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 3113f696a365SMiklos Szeredi 31141e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 31151e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 31161da177e4SLinus Torvalds return res; 31171da177e4SLinus Torvalds out_notsup: 3118d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 3119d51ac1a8SNeilBrown return -ECHILD; 3120d51ac1a8SNeilBrown 3121720869ebSTrond Myklebust res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE | 3122720869ebSTrond Myklebust NFS_INO_INVALID_OTHER); 31231da177e4SLinus Torvalds if (res == 0) 312447291baaSChristian Brauner res = generic_permission(&init_user_ns, inode, mask); 31251e7cb3dcSChuck Lever goto out; 31261da177e4SLinus Torvalds } 3127ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 3128