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 72580f2367STrond Myklebust #define NFS_INIT_DTSIZE PAGE_SIZE 73580f2367STrond Myklebust 74281f31b2STrond Myklebust static struct nfs_open_dir_context * 75281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir) 76480c2006SBryan Schumaker { 77311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 78480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 79281f31b2STrond Myklebust 80281f31b2STrond Myklebust ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); 81480c2006SBryan Schumaker if (ctx != NULL) { 82311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 83580f2367STrond Myklebust ctx->dtsize = NFS_INIT_DTSIZE; 84311324adSTrond Myklebust spin_lock(&dir->i_lock); 851c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 861c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 87ac46b3d7STrond Myklebust nfs_set_cache_invalid(dir, 88ac46b3d7STrond Myklebust NFS_INO_INVALID_DATA | 89ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 90230bc98fSTrond Myklebust list_add_tail_rcu(&ctx->list, &nfsi->open_files); 91d1e32ea3STrond Myklebust memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf)); 92311324adSTrond Myklebust spin_unlock(&dir->i_lock); 93480c2006SBryan Schumaker return ctx; 94480c2006SBryan Schumaker } 950c030806STrond Myklebust return ERR_PTR(-ENOMEM); 960c030806STrond Myklebust } 97480c2006SBryan Schumaker 98311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 99480c2006SBryan Schumaker { 100311324adSTrond Myklebust spin_lock(&dir->i_lock); 101230bc98fSTrond Myklebust list_del_rcu(&ctx->list); 102311324adSTrond Myklebust spin_unlock(&dir->i_lock); 103230bc98fSTrond Myklebust kfree_rcu(ctx, rcu_head); 104480c2006SBryan Schumaker } 105480c2006SBryan Schumaker 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * Open file 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds static int 1101da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1111da177e4SLinus Torvalds { 112480c2006SBryan Schumaker int res = 0; 113480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1141da177e4SLinus Torvalds 1156de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 116cc0dd2d1SChuck Lever 117cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1181e7cb3dcSChuck Lever 11993b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode); 120480c2006SBryan Schumaker if (IS_ERR(ctx)) { 121480c2006SBryan Schumaker res = PTR_ERR(ctx); 122480c2006SBryan Schumaker goto out; 123480c2006SBryan Schumaker } 124480c2006SBryan Schumaker filp->private_data = ctx; 125480c2006SBryan Schumaker out: 1261da177e4SLinus Torvalds return res; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 129480c2006SBryan Schumaker static int 130480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 131480c2006SBryan Schumaker { 132a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 133480c2006SBryan Schumaker return 0; 134480c2006SBryan Schumaker } 135480c2006SBryan Schumaker 136d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 137d1bacf9eSBryan Schumaker u64 cookie; 138d1bacf9eSBryan Schumaker u64 ino; 139a52a8a6aSTrond Myklebust const char *name; 140a52a8a6aSTrond Myklebust unsigned int name_len; 1410b26a0bfSTrond Myklebust unsigned char d_type; 142d1bacf9eSBryan Schumaker }; 143d1bacf9eSBryan Schumaker 144d1bacf9eSBryan Schumaker struct nfs_cache_array { 145d09e673fSTrond Myklebust u64 change_attr; 146d1bacf9eSBryan Schumaker u64 last_cookie; 147b1e21c97STrond Myklebust unsigned int size; 148b1e21c97STrond Myklebust unsigned char page_full : 1, 149762567b7STrond Myklebust page_is_eof : 1, 150762567b7STrond Myklebust cookies_are_ordered : 1; 1515601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 152d1bacf9eSBryan Schumaker }; 153d1bacf9eSBryan Schumaker 1546c981effSTrond Myklebust struct nfs_readdir_descriptor { 1551da177e4SLinus Torvalds struct file *file; 1561da177e4SLinus Torvalds struct page *page; 15723db8620SAl Viro struct dir_context *ctx; 1581f1d4aa4STrond Myklebust pgoff_t page_index; 159580f2367STrond Myklebust pgoff_t page_index_max; 1602e7a4641STrond Myklebust u64 dir_cookie; 1610aded708STrond Myklebust u64 last_cookie; 1622e7a4641STrond Myklebust u64 dup_cookie; 163f0dd2136STrond Myklebust loff_t current_index; 16459e356a9STrond Myklebust loff_t prev_index; 165d1bacf9eSBryan Schumaker 166b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 167a1147b82STrond Myklebust unsigned long dir_verifier; 1681f4eab7eSNeil Brown unsigned long timestamp; 1694704f0e2STrond Myklebust unsigned long gencount; 1702e7a4641STrond Myklebust unsigned long attr_gencount; 171d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 172580f2367STrond Myklebust unsigned int buffer_fills; 173580f2367STrond Myklebust unsigned int dtsize; 1742e7a4641STrond Myklebust signed char duped; 175a7a3b1e9SBenjamin Coddington bool plus; 176e1d2699bSTrond Myklebust bool eob; 177a7a3b1e9SBenjamin Coddington bool eof; 1786c981effSTrond Myklebust }; 1791da177e4SLinus Torvalds 180580f2367STrond Myklebust static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz) 181580f2367STrond Myklebust { 182580f2367STrond Myklebust struct nfs_server *server = NFS_SERVER(file_inode(desc->file)); 183580f2367STrond Myklebust unsigned int maxsize = server->dtsize; 184580f2367STrond Myklebust 185580f2367STrond Myklebust if (sz > maxsize) 186580f2367STrond Myklebust sz = maxsize; 187580f2367STrond Myklebust if (sz < NFS_MIN_FILE_IO_SIZE) 188580f2367STrond Myklebust sz = NFS_MIN_FILE_IO_SIZE; 189580f2367STrond Myklebust desc->dtsize = sz; 190580f2367STrond Myklebust } 191580f2367STrond Myklebust 192580f2367STrond Myklebust static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc) 193580f2367STrond Myklebust { 194580f2367STrond Myklebust nfs_set_dtsize(desc, desc->dtsize >> 1); 195580f2367STrond Myklebust } 196580f2367STrond Myklebust 197580f2367STrond Myklebust static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc) 198580f2367STrond Myklebust { 199580f2367STrond Myklebust nfs_set_dtsize(desc, desc->dtsize << 1); 200580f2367STrond Myklebust } 201580f2367STrond Myklebust 2021f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array) 2031f1d4aa4STrond Myklebust { 2041f1d4aa4STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 2051f1d4aa4STrond Myklebust } 2061f1d4aa4STrond Myklebust 207d09e673fSTrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie, 208d09e673fSTrond Myklebust u64 change_attr) 2094b310319STrond Myklebust { 2104b310319STrond Myklebust struct nfs_cache_array *array; 2114b310319STrond Myklebust 2124b310319STrond Myklebust array = kmap_atomic(page); 2131f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 214d09e673fSTrond Myklebust array->change_attr = change_attr; 2151f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 216762567b7STrond Myklebust array->cookies_are_ordered = 1; 2174b310319STrond Myklebust kunmap_atomic(array); 2184b310319STrond Myklebust } 2194b310319STrond Myklebust 220d1bacf9eSBryan Schumaker /* 221d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 222d1bacf9eSBryan Schumaker */ 223d1bacf9eSBryan Schumaker static 22411de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 225d1bacf9eSBryan Schumaker { 22611de3b11STrond Myklebust struct nfs_cache_array *array; 227d1bacf9eSBryan Schumaker int i; 2288cd51a0cSTrond Myklebust 2292b86ce2dSCong Wang array = kmap_atomic(page); 230d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 231a52a8a6aSTrond Myklebust kfree(array->array[i].name); 2321f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 2332b86ce2dSCong Wang kunmap_atomic(array); 234d1bacf9eSBryan Schumaker } 235d1bacf9eSBryan Schumaker 23635df59d3STrond Myklebust static struct page * 23735df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags) 23835df59d3STrond Myklebust { 23935df59d3STrond Myklebust struct page *page = alloc_page(gfp_flags); 24035df59d3STrond Myklebust if (page) 241d09e673fSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, 0); 24235df59d3STrond Myklebust return page; 24335df59d3STrond Myklebust } 24435df59d3STrond Myklebust 24535df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page) 24635df59d3STrond Myklebust { 24735df59d3STrond Myklebust if (page) { 24835df59d3STrond Myklebust nfs_readdir_clear_array(page); 24935df59d3STrond Myklebust put_page(page); 25035df59d3STrond Myklebust } 25135df59d3STrond Myklebust } 25235df59d3STrond Myklebust 253b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 254b1e21c97STrond Myklebust { 255b1e21c97STrond Myklebust array->page_is_eof = 1; 256b1e21c97STrond Myklebust array->page_full = 1; 257b1e21c97STrond Myklebust } 258b1e21c97STrond Myklebust 259b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 260b1e21c97STrond Myklebust { 261b1e21c97STrond Myklebust return array->page_full; 262b1e21c97STrond Myklebust } 263b1e21c97STrond Myklebust 264d1bacf9eSBryan Schumaker /* 265d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 266d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 267d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 268d1bacf9eSBryan Schumaker */ 269a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 270d1bacf9eSBryan Schumaker { 271a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 272a52a8a6aSTrond Myklebust 27304e4bd1cSCatalin Marinas /* 27404e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 27504e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 27604e4bd1cSCatalin Marinas */ 277a52a8a6aSTrond Myklebust if (ret != NULL) 278a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 279a52a8a6aSTrond Myklebust return ret; 280d1bacf9eSBryan Schumaker } 281d1bacf9eSBryan Schumaker 2820b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void) 2830b2662b7STrond Myklebust { 2840b2662b7STrond Myklebust return (PAGE_SIZE - sizeof(struct nfs_cache_array)) / 2850b2662b7STrond Myklebust sizeof(struct nfs_cache_array_entry); 2860b2662b7STrond Myklebust } 2870b2662b7STrond Myklebust 288b1e21c97STrond Myklebust /* 289b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 290b1e21c97STrond Myklebust */ 291b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 292b1e21c97STrond Myklebust { 293b1e21c97STrond Myklebust if (array->page_full) 294b1e21c97STrond Myklebust return -ENOSPC; 2950b2662b7STrond Myklebust if (array->size == nfs_readdir_array_maxentries()) { 296b1e21c97STrond Myklebust array->page_full = 1; 297b1e21c97STrond Myklebust return -ENOSPC; 298b1e21c97STrond Myklebust } 299d1bacf9eSBryan Schumaker return 0; 300d1bacf9eSBryan Schumaker } 301d1bacf9eSBryan Schumaker 302d1bacf9eSBryan Schumaker static 303d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 304d1bacf9eSBryan Schumaker { 305a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 3064a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 307a52a8a6aSTrond Myklebust const char *name; 3084a201d6eSTrond Myklebust int ret; 3094a201d6eSTrond Myklebust 310a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 311a52a8a6aSTrond Myklebust if (!name) 312a52a8a6aSTrond Myklebust return -ENOMEM; 3133020093fSTrond Myklebust 314a52a8a6aSTrond Myklebust array = kmap_atomic(page); 315b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 316a52a8a6aSTrond Myklebust if (ret) { 317a52a8a6aSTrond Myklebust kfree(name); 3183020093fSTrond Myklebust goto out; 319a52a8a6aSTrond Myklebust } 3203020093fSTrond Myklebust 321b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 3224a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 3234a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 3240b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 325a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 326a52a8a6aSTrond Myklebust cache_entry->name = name; 327d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 328762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie) 329762567b7STrond Myklebust array->cookies_are_ordered = 0; 3308cd51a0cSTrond Myklebust array->size++; 33147c716cbSTrond Myklebust if (entry->eof != 0) 332b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 3334a201d6eSTrond Myklebust out: 334a52a8a6aSTrond Myklebust kunmap_atomic(array); 3354a201d6eSTrond Myklebust return ret; 336d1bacf9eSBryan Schumaker } 337d1bacf9eSBryan Schumaker 338d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie, 339d09e673fSTrond Myklebust u64 change_attr) 340d09e673fSTrond Myklebust { 341d09e673fSTrond Myklebust struct nfs_cache_array *array = kmap_atomic(page); 342d09e673fSTrond Myklebust int ret = true; 343d09e673fSTrond Myklebust 344d09e673fSTrond Myklebust if (array->change_attr != change_attr) 345d09e673fSTrond Myklebust ret = false; 346d09e673fSTrond Myklebust if (array->size > 0 && array->array[0].cookie != last_cookie) 347d09e673fSTrond Myklebust ret = false; 348d09e673fSTrond Myklebust kunmap_atomic(array); 349d09e673fSTrond Myklebust return ret; 350d09e673fSTrond Myklebust } 351d09e673fSTrond Myklebust 352d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 353d09e673fSTrond Myklebust { 354d09e673fSTrond Myklebust unlock_page(page); 355d09e673fSTrond Myklebust put_page(page); 356d09e673fSTrond Myklebust } 357d09e673fSTrond Myklebust 3581f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 3591f1d4aa4STrond Myklebust pgoff_t index, u64 last_cookie) 3601f1d4aa4STrond Myklebust { 3611f1d4aa4STrond Myklebust struct page *page; 362d09e673fSTrond Myklebust u64 change_attr; 3631f1d4aa4STrond Myklebust 3641f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 365d09e673fSTrond Myklebust if (!page) 366d09e673fSTrond Myklebust return NULL; 367d09e673fSTrond Myklebust change_attr = inode_peek_iversion_raw(mapping->host); 368d09e673fSTrond Myklebust if (PageUptodate(page)) { 369d09e673fSTrond Myklebust if (nfs_readdir_page_validate(page, last_cookie, change_attr)) 370d09e673fSTrond Myklebust return page; 371d09e673fSTrond Myklebust nfs_readdir_clear_array(page); 3721f1d4aa4STrond Myklebust } 373d09e673fSTrond Myklebust nfs_readdir_page_init_array(page, last_cookie, change_attr); 374d09e673fSTrond Myklebust SetPageUptodate(page); 3751f1d4aa4STrond Myklebust return page; 3761f1d4aa4STrond Myklebust } 3771f1d4aa4STrond Myklebust 3780b2662b7STrond Myklebust static loff_t nfs_readdir_page_offset(struct page *page) 3790b2662b7STrond Myklebust { 3800b2662b7STrond Myklebust return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries(); 3810b2662b7STrond Myklebust } 3820b2662b7STrond Myklebust 3831f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 3841f1d4aa4STrond Myklebust { 3851f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3861f1d4aa4STrond Myklebust u64 ret; 3871f1d4aa4STrond Myklebust 3881f1d4aa4STrond Myklebust array = kmap_atomic(page); 3891f1d4aa4STrond Myklebust ret = array->last_cookie; 3901f1d4aa4STrond Myklebust kunmap_atomic(array); 3911f1d4aa4STrond Myklebust return ret; 3921f1d4aa4STrond Myklebust } 3931f1d4aa4STrond Myklebust 3941f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 3951f1d4aa4STrond Myklebust { 3961f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3971f1d4aa4STrond Myklebust bool ret; 3981f1d4aa4STrond Myklebust 3991f1d4aa4STrond Myklebust array = kmap_atomic(page); 4001f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 4011f1d4aa4STrond Myklebust kunmap_atomic(array); 4021f1d4aa4STrond Myklebust return ret; 4031f1d4aa4STrond Myklebust } 4041f1d4aa4STrond Myklebust 405b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 406b1e21c97STrond Myklebust { 407b1e21c97STrond Myklebust struct nfs_cache_array *array; 408b1e21c97STrond Myklebust 409b1e21c97STrond Myklebust array = kmap_atomic(page); 410b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 411b1e21c97STrond Myklebust kunmap_atomic(array); 412b1e21c97STrond Myklebust } 413b1e21c97STrond Myklebust 4143b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 4153b2a09f1STrond Myklebust pgoff_t index, u64 cookie) 4163b2a09f1STrond Myklebust { 4173b2a09f1STrond Myklebust struct page *page; 4183b2a09f1STrond Myklebust 4193b2a09f1STrond Myklebust page = nfs_readdir_page_get_locked(mapping, index, cookie); 4203b2a09f1STrond Myklebust if (page) { 4213b2a09f1STrond Myklebust if (nfs_readdir_page_last_cookie(page) == cookie) 4223b2a09f1STrond Myklebust return page; 4233b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 4243b2a09f1STrond Myklebust } 4253b2a09f1STrond Myklebust return NULL; 4263b2a09f1STrond Myklebust } 4273b2a09f1STrond Myklebust 42859e356a9STrond Myklebust static inline 42959e356a9STrond Myklebust int is_32bit_api(void) 43059e356a9STrond Myklebust { 43159e356a9STrond Myklebust #ifdef CONFIG_COMPAT 43259e356a9STrond Myklebust return in_compat_syscall(); 43359e356a9STrond Myklebust #else 43459e356a9STrond Myklebust return (BITS_PER_LONG == 32); 43559e356a9STrond Myklebust #endif 43659e356a9STrond Myklebust } 43759e356a9STrond Myklebust 43859e356a9STrond Myklebust static 43959e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 44059e356a9STrond Myklebust { 44159e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 44259e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 44359e356a9STrond Myklebust return false; 44459e356a9STrond Myklebust return true; 44559e356a9STrond Myklebust } 44659e356a9STrond Myklebust 447c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array, 448c8f0523bSTrond Myklebust struct nfs_readdir_descriptor *desc) 449c8f0523bSTrond Myklebust { 450c8f0523bSTrond Myklebust if (array->page_full) { 451c8f0523bSTrond Myklebust desc->last_cookie = array->last_cookie; 452c8f0523bSTrond Myklebust desc->current_index += array->size; 453c8f0523bSTrond Myklebust desc->cache_entry_index = 0; 454c8f0523bSTrond Myklebust desc->page_index++; 455c8f0523bSTrond Myklebust } else 456c8f0523bSTrond Myklebust desc->last_cookie = array->array[0].cookie; 457c8f0523bSTrond Myklebust } 458c8f0523bSTrond Myklebust 4596c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 4606c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 461d1bacf9eSBryan Schumaker { 46223db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 463d1bacf9eSBryan Schumaker unsigned int index; 464d1bacf9eSBryan Schumaker 465d1bacf9eSBryan Schumaker if (diff < 0) 466d1bacf9eSBryan Schumaker goto out_eof; 467d1bacf9eSBryan Schumaker if (diff >= array->size) { 468b1e21c97STrond Myklebust if (array->page_is_eof) 469d1bacf9eSBryan Schumaker goto out_eof; 470c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 471d1bacf9eSBryan Schumaker return -EAGAIN; 472d1bacf9eSBryan Schumaker } 473d1bacf9eSBryan Schumaker 474d1bacf9eSBryan Schumaker index = (unsigned int)diff; 4752e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 476d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 477d1bacf9eSBryan Schumaker return 0; 478d1bacf9eSBryan Schumaker out_eof: 4796089dd0dSThomas Meyer desc->eof = true; 480d1bacf9eSBryan Schumaker return -EBADCOOKIE; 481d1bacf9eSBryan Schumaker } 482d1bacf9eSBryan Schumaker 483762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 484762567b7STrond Myklebust u64 cookie) 485762567b7STrond Myklebust { 486762567b7STrond Myklebust if (!array->cookies_are_ordered) 487762567b7STrond Myklebust return true; 488762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */ 489762567b7STrond Myklebust if (cookie >= array->last_cookie) 490762567b7STrond Myklebust return false; 491762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie) 492762567b7STrond Myklebust return false; 493762567b7STrond Myklebust return true; 494762567b7STrond Myklebust } 495762567b7STrond Myklebust 4966c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 4976c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 498d1bacf9eSBryan Schumaker { 499d1bacf9eSBryan Schumaker int i; 5008ef2ce3eSBryan Schumaker loff_t new_pos; 501d1bacf9eSBryan Schumaker int status = -EAGAIN; 502d1bacf9eSBryan Schumaker 503762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 504762567b7STrond Myklebust goto check_eof; 505762567b7STrond Myklebust 506d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 5072e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 508496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 5090c030806STrond Myklebust 5100b2662b7STrond Myklebust new_pos = nfs_readdir_page_offset(desc->page) + i; 511d09e673fSTrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount) { 5122e7a4641STrond Myklebust desc->duped = 0; 5132e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 51459e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 5152e7a4641STrond Myklebust if (desc->duped > 0 5162e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 5170c030806STrond Myklebust if (printk_ratelimit()) { 5186de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 5190c030806STrond Myklebust "Please contact your server vendor. " 520a52a8a6aSTrond Myklebust "The file: %s has duplicate cookie %llu\n", 521a52a8a6aSTrond Myklebust desc->file, array->array[i].name, desc->dir_cookie); 5220c030806STrond Myklebust } 5230c030806STrond Myklebust status = -ELOOP; 5240c030806STrond Myklebust goto out; 5250c030806STrond Myklebust } 5262e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 5272e7a4641STrond Myklebust desc->duped = -1; 5288ef2ce3eSBryan Schumaker } 52959e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 5302e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 53159e356a9STrond Myklebust else 53223db8620SAl Viro desc->ctx->pos = new_pos; 53359e356a9STrond Myklebust desc->prev_index = new_pos; 5348cd51a0cSTrond Myklebust desc->cache_entry_index = i; 53547c716cbSTrond Myklebust return 0; 5368cd51a0cSTrond Myklebust } 5378cd51a0cSTrond Myklebust } 538762567b7STrond Myklebust check_eof: 539b1e21c97STrond Myklebust if (array->page_is_eof) { 540d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 5412e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 5426089dd0dSThomas Meyer desc->eof = true; 543c8f0523bSTrond Myklebust } else 544c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 5450c030806STrond Myklebust out: 546d1bacf9eSBryan Schumaker return status; 547d1bacf9eSBryan Schumaker } 548d1bacf9eSBryan Schumaker 5496c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 550d1bacf9eSBryan Schumaker { 551d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 55247c716cbSTrond Myklebust int status; 553d1bacf9eSBryan Schumaker 554ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 555d1bacf9eSBryan Schumaker 5562e7a4641STrond Myklebust if (desc->dir_cookie == 0) 557d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 558d1bacf9eSBryan Schumaker else 559d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 560d1bacf9eSBryan Schumaker 561ed09222dSTrond Myklebust kunmap_atomic(array); 562d1bacf9eSBryan Schumaker return status; 563d1bacf9eSBryan Schumaker } 564d1bacf9eSBryan Schumaker 565d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 56693b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 567b593c09fSTrond Myklebust __be32 *verf, u64 cookie, 568b593c09fSTrond Myklebust struct page **pages, size_t bufsize, 569b593c09fSTrond Myklebust __be32 *verf_res) 570d1bacf9eSBryan Schumaker { 57182e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 57282e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 57382e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 57482e22a5eSTrond Myklebust .cred = desc->file->f_cred, 575b593c09fSTrond Myklebust .verf = verf, 57682e22a5eSTrond Myklebust .cookie = cookie, 57782e22a5eSTrond Myklebust .pages = pages, 57882e22a5eSTrond Myklebust .page_len = bufsize, 57982e22a5eSTrond Myklebust .plus = desc->plus, 58082e22a5eSTrond Myklebust }; 58182e22a5eSTrond Myklebust struct nfs_readdir_res res = { 58282e22a5eSTrond Myklebust .verf = verf_res, 58382e22a5eSTrond Myklebust }; 5844704f0e2STrond Myklebust unsigned long timestamp, gencount; 5851da177e4SLinus Torvalds int error; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds again: 5881da177e4SLinus Torvalds timestamp = jiffies; 5894704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 590a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 59182e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 5921da177e4SLinus Torvalds if (error < 0) { 5931da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 5941da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 5951da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 59682e22a5eSTrond Myklebust desc->plus = arg.plus = false; 5971da177e4SLinus Torvalds goto again; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds goto error; 6001da177e4SLinus Torvalds } 6011f4eab7eSNeil Brown desc->timestamp = timestamp; 6024704f0e2STrond Myklebust desc->gencount = gencount; 603d1bacf9eSBryan Schumaker error: 604d1bacf9eSBryan Schumaker return error; 605d1bacf9eSBryan Schumaker } 606d1bacf9eSBryan Schumaker 6076c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 608573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 609d1bacf9eSBryan Schumaker { 61059e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 611573c4e1eSChuck Lever int error; 612d1bacf9eSBryan Schumaker 61359e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 614573c4e1eSChuck Lever if (error) 615573c4e1eSChuck Lever return error; 616d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 617d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 618d1bacf9eSBryan Schumaker return 0; 619d1bacf9eSBryan Schumaker } 620d1bacf9eSBryan Schumaker 621fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 622fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 623fa923369STrond Myklebust */ 624d39ab9deSBryan Schumaker static 625d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 626d39ab9deSBryan Schumaker { 627d8fdb47fSTrond Myklebust struct inode *inode; 628fa923369STrond Myklebust struct nfs_inode *nfsi; 629fa923369STrond Myklebust 6302b0143b5SDavid Howells if (d_really_is_negative(dentry)) 6312b0143b5SDavid Howells return 0; 632fa923369STrond Myklebust 633d8fdb47fSTrond Myklebust inode = d_inode(dentry); 634d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 635d8fdb47fSTrond Myklebust return 0; 636d8fdb47fSTrond Myklebust 637d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 6387dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 639d39ab9deSBryan Schumaker return 0; 6407dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 6417dc72d5fSTrond Myklebust return 0; 6427dc72d5fSTrond Myklebust return 1; 643d39ab9deSBryan Schumaker } 644d39ab9deSBryan Schumaker 645230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL) 646230bc98fSTrond Myklebust 647230bc98fSTrond Myklebust static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx, 648230bc98fSTrond Myklebust unsigned int cache_hits, 649230bc98fSTrond Myklebust unsigned int cache_misses) 650d69ee9b8STrond Myklebust { 651d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 652d69ee9b8STrond Myklebust return false; 653230bc98fSTrond Myklebust if (ctx->pos == 0 || 654230bc98fSTrond Myklebust cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD) 655d69ee9b8STrond Myklebust return true; 656d69ee9b8STrond Myklebust return false; 657d69ee9b8STrond Myklebust } 658d69ee9b8STrond Myklebust 659d69ee9b8STrond Myklebust /* 660230bc98fSTrond Myklebust * This function is called by the getattr code to request the 66163519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 662d69ee9b8STrond Myklebust * directory. 663d69ee9b8STrond Myklebust */ 664230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_hit(struct inode *dir) 665d69ee9b8STrond Myklebust { 66663519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 667230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx; 66863519fbcSTrond Myklebust 66963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 670230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) { 671230bc98fSTrond Myklebust rcu_read_lock(); 672230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list) 673230bc98fSTrond Myklebust atomic_inc(&ctx->cache_hits); 674230bc98fSTrond Myklebust rcu_read_unlock(); 675230bc98fSTrond Myklebust } 676d69ee9b8STrond Myklebust } 677d69ee9b8STrond Myklebust 678311324adSTrond Myklebust /* 679311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 680311324adSTrond Myklebust * 681311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 682311324adSTrond Myklebust */ 683230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_miss(struct inode *dir) 684311324adSTrond Myklebust { 68563519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 686230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx; 68763519fbcSTrond Myklebust 68863519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 689230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) { 690230bc98fSTrond Myklebust rcu_read_lock(); 691230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list) 692230bc98fSTrond Myklebust atomic_inc(&ctx->cache_misses); 693230bc98fSTrond Myklebust rcu_read_unlock(); 694311324adSTrond Myklebust } 695311324adSTrond Myklebust } 696311324adSTrond Myklebust 6970b3cc71bSTrond Myklebust static void nfs_lookup_advise_force_readdirplus(struct inode *dir, 6980b3cc71bSTrond Myklebust unsigned int flags) 699230bc98fSTrond Myklebust { 7002c2c3365STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 7012c2c3365STrond Myklebust return; 7020b3cc71bSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL)) 7030b3cc71bSTrond Myklebust return; 704230bc98fSTrond Myklebust nfs_readdir_record_entry_cache_miss(dir); 705230bc98fSTrond Myklebust } 706230bc98fSTrond Myklebust 707d69ee9b8STrond Myklebust static 708a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 709a1147b82STrond Myklebust unsigned long dir_verifier) 710d39ab9deSBryan Schumaker { 71126fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 7129ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 7134a201d6eSTrond Myklebust struct dentry *dentry; 7144a201d6eSTrond Myklebust struct dentry *alias; 715d39ab9deSBryan Schumaker struct inode *inode; 716aa9c2669SDavid Quigley int status; 717d39ab9deSBryan Schumaker 718fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 719fa923369STrond Myklebust return; 7206c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 7216c441c25STrond Myklebust return; 72278d04af4STrond Myklebust if (filename.len == 0) 72378d04af4STrond Myklebust return; 72478d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 72578d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 72678d04af4STrond Myklebust return; 72778d04af4STrond Myklebust /* ...or '/' */ 72878d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 72978d04af4STrond Myklebust return; 7304a201d6eSTrond Myklebust if (filename.name[0] == '.') { 7314a201d6eSTrond Myklebust if (filename.len == 1) 7324a201d6eSTrond Myklebust return; 7334a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 7344a201d6eSTrond Myklebust return; 7354a201d6eSTrond Myklebust } 7368387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 737d39ab9deSBryan Schumaker 7384a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 7399ac3d3e8SAl Viro again: 7409ac3d3e8SAl Viro if (!dentry) { 7419ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 7429ac3d3e8SAl Viro if (IS_ERR(dentry)) 7439ac3d3e8SAl Viro return; 7449ac3d3e8SAl Viro } 7459ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 7466c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 7476c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 7486c441c25STrond Myklebust &entry->fattr->fsid)) 7496c441c25STrond Myklebust goto out; 750d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 7517dc72d5fSTrond Myklebust if (!entry->fh->size) 7527dc72d5fSTrond Myklebust goto out; 753a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 7542b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 755aa9c2669SDavid Quigley if (!status) 756dd225cb3SAnna Schumaker nfs_setsecurity(d_inode(dentry), entry->fattr); 757d39ab9deSBryan Schumaker goto out; 758d39ab9deSBryan Schumaker } else { 7595542aa2fSEric W. Biederman d_invalidate(dentry); 760d39ab9deSBryan Schumaker dput(dentry); 7619ac3d3e8SAl Viro dentry = NULL; 7629ac3d3e8SAl Viro goto again; 763d39ab9deSBryan Schumaker } 764d39ab9deSBryan Schumaker } 7657dc72d5fSTrond Myklebust if (!entry->fh->size) { 7667dc72d5fSTrond Myklebust d_lookup_done(dentry); 7677dc72d5fSTrond Myklebust goto out; 7687dc72d5fSTrond Myklebust } 769d39ab9deSBryan Schumaker 770cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 77141d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 7729ac3d3e8SAl Viro d_lookup_done(dentry); 7739ac3d3e8SAl Viro if (alias) { 774d39ab9deSBryan Schumaker if (IS_ERR(alias)) 775d39ab9deSBryan Schumaker goto out; 7769ac3d3e8SAl Viro dput(dentry); 7779ac3d3e8SAl Viro dentry = alias; 7789ac3d3e8SAl Viro } 779a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 780d39ab9deSBryan Schumaker out: 781d39ab9deSBryan Schumaker dput(dentry); 782d39ab9deSBryan Schumaker } 783d39ab9deSBryan Schumaker 784d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 7853b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 7863b2a09f1STrond Myklebust struct nfs_entry *entry, 7873b2a09f1STrond Myklebust struct page **xdr_pages, 78835df59d3STrond Myklebust unsigned int buflen, 78935df59d3STrond Myklebust struct page **arrays, 79035df59d3STrond Myklebust size_t narrays) 791d1bacf9eSBryan Schumaker { 7923b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 793babddc72SBryan Schumaker struct xdr_stream stream; 794f7da7a12SBenny Halevy struct xdr_buf buf; 79535df59d3STrond Myklebust struct page *scratch, *new, *page = *arrays; 7965c346854STrond Myklebust int status; 797babddc72SBryan Schumaker 7986650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 7996650239aSTrond Myklebust if (scratch == NULL) 8006650239aSTrond Myklebust return -ENOMEM; 801babddc72SBryan Schumaker 802f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 8030ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch); 80499424380SBryan Schumaker 80599424380SBryan Schumaker do { 806b1db9a40SAnna Schumaker if (entry->fattr->label) 807b1db9a40SAnna Schumaker entry->fattr->label->len = NFS4_MAXLABELLEN; 808d33030e2SJeffrey Mitchell 80999424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 810972bcdf2STrond Myklebust if (status != 0) 81199424380SBryan Schumaker break; 8125c346854STrond Myklebust 813a7a3b1e9SBenjamin Coddington if (desc->plus) 814a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 815a1147b82STrond Myklebust desc->dir_verifier); 8168cd51a0cSTrond Myklebust 817db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 8183b2a09f1STrond Myklebust if (status != -ENOSPC) 8193b2a09f1STrond Myklebust continue; 82099424380SBryan Schumaker 82135df59d3STrond Myklebust if (page->mapping != mapping) { 82235df59d3STrond Myklebust if (!--narrays) 8233b2a09f1STrond Myklebust break; 82435df59d3STrond Myklebust new = nfs_readdir_page_array_alloc(entry->prev_cookie, 82535df59d3STrond Myklebust GFP_KERNEL); 82635df59d3STrond Myklebust if (!new) 82735df59d3STrond Myklebust break; 82835df59d3STrond Myklebust arrays++; 82935df59d3STrond Myklebust *arrays = page = new; 830580f2367STrond Myklebust desc->page_index_max++; 83135df59d3STrond Myklebust } else { 83235df59d3STrond Myklebust new = nfs_readdir_page_get_next(mapping, 83335df59d3STrond Myklebust page->index + 1, 8343b2a09f1STrond Myklebust entry->prev_cookie); 8353b2a09f1STrond Myklebust if (!new) 8363b2a09f1STrond Myklebust break; 83735df59d3STrond Myklebust if (page != *arrays) 8383b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8393b2a09f1STrond Myklebust page = new; 840580f2367STrond Myklebust desc->page_index_max = new->index; 84156e4ebf8SBryan Schumaker } 8423b2a09f1STrond Myklebust status = nfs_readdir_add_to_array(entry, page); 843972bcdf2STrond Myklebust } while (!status && !entry->eof); 84456e4ebf8SBryan Schumaker 845972bcdf2STrond Myklebust switch (status) { 846972bcdf2STrond Myklebust case -EBADCOOKIE: 847972bcdf2STrond Myklebust if (entry->eof) { 848b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 8498cd51a0cSTrond Myklebust status = 0; 85056e4ebf8SBryan Schumaker } 851972bcdf2STrond Myklebust break; 852972bcdf2STrond Myklebust case -ENOSPC: 853972bcdf2STrond Myklebust case -EAGAIN: 854972bcdf2STrond Myklebust status = 0; 855972bcdf2STrond Myklebust break; 856972bcdf2STrond Myklebust } 8576650239aSTrond Myklebust 85835df59d3STrond Myklebust if (page != *arrays) 8593b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 8606650239aSTrond Myklebust 8616650239aSTrond Myklebust put_page(scratch); 862d1bacf9eSBryan Schumaker return status; 86356e4ebf8SBryan Schumaker } 86456e4ebf8SBryan Schumaker 8651a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 86656e4ebf8SBryan Schumaker { 8671a34c8c9STrond Myklebust while (npages--) 8681a34c8c9STrond Myklebust put_page(pages[npages]); 8691a34c8c9STrond Myklebust kfree(pages); 87056e4ebf8SBryan Schumaker } 87156e4ebf8SBryan Schumaker 87256e4ebf8SBryan Schumaker /* 873bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 874bf211ca1Szhangliguang * to nfs_readdir_free_pages() 87556e4ebf8SBryan Schumaker */ 8761a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 87756e4ebf8SBryan Schumaker { 8781a34c8c9STrond Myklebust struct page **pages; 8791a34c8c9STrond Myklebust size_t i; 88056e4ebf8SBryan Schumaker 8811a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 8821a34c8c9STrond Myklebust if (!pages) 8831a34c8c9STrond Myklebust return NULL; 88456e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 88556e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 88656e4ebf8SBryan Schumaker if (page == NULL) 88756e4ebf8SBryan Schumaker goto out_freepages; 88856e4ebf8SBryan Schumaker pages[i] = page; 88956e4ebf8SBryan Schumaker } 8901a34c8c9STrond Myklebust return pages; 89156e4ebf8SBryan Schumaker 89256e4ebf8SBryan Schumaker out_freepages: 893c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 8941a34c8c9STrond Myklebust return NULL; 895d1bacf9eSBryan Schumaker } 896d1bacf9eSBryan Schumaker 8976c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 89835df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res, 89935df59d3STrond Myklebust struct page **arrays, size_t narrays) 900d1bacf9eSBryan Schumaker { 9011a34c8c9STrond Myklebust struct page **pages; 90235df59d3STrond Myklebust struct page *page = *arrays; 9036b75cf9eSTrond Myklebust struct nfs_entry *entry; 9041a34c8c9STrond Myklebust size_t array_size; 905b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file); 906580f2367STrond Myklebust unsigned int dtsize = desc->dtsize; 9079ff89c25STrond Myklebust unsigned int pglen; 9088cd51a0cSTrond Myklebust int status = -ENOMEM; 909d1bacf9eSBryan Schumaker 9106b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 9116b75cf9eSTrond Myklebust if (!entry) 9126b75cf9eSTrond Myklebust return -ENOMEM; 9136b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 9146b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 915b1db9a40SAnna Schumaker entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 9166b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 9176b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 918d1bacf9eSBryan Schumaker goto out; 919d1bacf9eSBryan Schumaker 9201a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 9211a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 9221a34c8c9STrond Myklebust if (!pages) 923b1db9a40SAnna Schumaker goto out; 924d1bacf9eSBryan Schumaker 9259ff89c25STrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages, 9269ff89c25STrond Myklebust dtsize, verf_res); 927d1bacf9eSBryan Schumaker if (status < 0) 9289ff89c25STrond Myklebust goto free_pages; 929972bcdf2STrond Myklebust 930ac396128STrond Myklebust pglen = status; 9319ff89c25STrond Myklebust if (pglen != 0) 93235df59d3STrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, pglen, 93335df59d3STrond Myklebust arrays, narrays); 9349ff89c25STrond Myklebust else 9359ff89c25STrond Myklebust nfs_readdir_page_set_eof(page); 936580f2367STrond Myklebust desc->buffer_fills++; 937d1bacf9eSBryan Schumaker 9389ff89c25STrond Myklebust free_pages: 939c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 940d1bacf9eSBryan Schumaker out: 9416b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 9426b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 9436b75cf9eSTrond Myklebust kfree(entry); 944d1bacf9eSBryan Schumaker return status; 945d1bacf9eSBryan Schumaker } 946d1bacf9eSBryan Schumaker 9471f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 9481da177e4SLinus Torvalds { 94909cbfeafSKirill A. Shutemov put_page(desc->page); 9501da177e4SLinus Torvalds desc->page = NULL; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531f1d4aa4STrond Myklebust static void 9541f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 9551da177e4SLinus Torvalds { 9561f1d4aa4STrond Myklebust unlock_page(desc->page); 9571f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 9581f1d4aa4STrond Myklebust } 9591f1d4aa4STrond Myklebust 9601f1d4aa4STrond Myklebust static struct page * 9611f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 9621f1d4aa4STrond Myklebust { 9631f1d4aa4STrond Myklebust return nfs_readdir_page_get_locked(desc->file->f_mapping, 9641f1d4aa4STrond Myklebust desc->page_index, 9651f1d4aa4STrond Myklebust desc->last_cookie); 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /* 969d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 970114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 9711da177e4SLinus Torvalds */ 9726c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 973d1bacf9eSBryan Schumaker { 974227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 975227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 976b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 977d1bacf9eSBryan Schumaker int res; 978d1bacf9eSBryan Schumaker 9791f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 9801f1d4aa4STrond Myklebust if (!desc->page) 9811f1d4aa4STrond Myklebust return -ENOMEM; 9821f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 983580f2367STrond Myklebust /* Grow the dtsize if we had to go back for more pages */ 984580f2367STrond Myklebust if (desc->page_index == desc->page_index_max) 985580f2367STrond Myklebust nfs_grow_dtsize(desc); 986580f2367STrond Myklebust desc->page_index_max = desc->page_index; 987*310e3187STrond Myklebust trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf, 988*310e3187STrond Myklebust desc->last_cookie, 989*310e3187STrond Myklebust desc->page->index, desc->dtsize); 99035df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 99135df59d3STrond Myklebust &desc->page, 1); 9929fff59edSTrond Myklebust if (res < 0) { 9939fff59edSTrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 994*310e3187STrond Myklebust trace_nfs_readdir_cache_fill_done(inode, res); 9959fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) { 9969fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping); 9979fff59edSTrond Myklebust desc->page_index = 0; 9989fff59edSTrond Myklebust return -EAGAIN; 9999fff59edSTrond Myklebust } 10009fff59edSTrond Myklebust return res; 10019fff59edSTrond Myklebust } 1002f892c41cSTrond Myklebust /* 1003f892c41cSTrond Myklebust * Set the cookie verifier if the page cache was empty 1004f892c41cSTrond Myklebust */ 10056c34f05bSTrond Myklebust if (desc->last_cookie == 0 && 10066c34f05bSTrond Myklebust memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) { 1007f892c41cSTrond Myklebust memcpy(nfsi->cookieverf, verf, 1008f892c41cSTrond Myklebust sizeof(nfsi->cookieverf)); 10096c34f05bSTrond Myklebust invalidate_inode_pages2_range(desc->file->f_mapping, 10106c34f05bSTrond Myklebust desc->page_index_max + 1, 10116c34f05bSTrond Myklebust -1); 10126c34f05bSTrond Myklebust } 10131f1d4aa4STrond Myklebust } 1014114de382STrond Myklebust res = nfs_readdir_search_array(desc); 1015ff81dfb5STrond Myklebust if (res == 0) 1016114de382STrond Myklebust return 0; 10171f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1018d1bacf9eSBryan Schumaker return res; 1019d1bacf9eSBryan Schumaker } 1020d1bacf9eSBryan Schumaker 1021d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 10226c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 10231da177e4SLinus Torvalds { 10248cd51a0cSTrond Myklebust int res; 1025d1bacf9eSBryan Schumaker 10269fff59edSTrond Myklebust do { 10270aded708STrond Myklebust if (desc->page_index == 0) { 10288cd51a0cSTrond Myklebust desc->current_index = 0; 102959e356a9STrond Myklebust desc->prev_index = 0; 10300aded708STrond Myklebust desc->last_cookie = 0; 10310aded708STrond Myklebust } 1032114de382STrond Myklebust res = find_and_lock_cache_page(desc); 103347c716cbSTrond Myklebust } while (res == -EAGAIN); 10341da177e4SLinus Torvalds return res; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds /* 10381da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 10391da177e4SLinus Torvalds */ 104013884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc, 104113884ff2STrond Myklebust const __be32 *verf) 10421da177e4SLinus Torvalds { 10431da177e4SLinus Torvalds struct file *file = desc->file; 1044dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 1045c8f0523bSTrond Myklebust unsigned int i; 10468ef2ce3eSBryan Schumaker 10470795bf83SFabian Frederick array = kmap(desc->page); 1048d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 1049ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 10501da177e4SLinus Torvalds 1051ece0b423STrond Myklebust ent = &array->array[i]; 1052a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 105323db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 1054e1d2699bSTrond Myklebust desc->eob = true; 10551da177e4SLinus Torvalds break; 1056ece0b423STrond Myklebust } 105713884ff2STrond Myklebust memcpy(desc->verf, verf, sizeof(desc->verf)); 1058c8f0523bSTrond Myklebust if (i == array->size - 1) { 10592e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 1060c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc); 1061c8f0523bSTrond Myklebust } else { 1062c8f0523bSTrond Myklebust desc->dir_cookie = array->array[i + 1].cookie; 1063c8f0523bSTrond Myklebust desc->last_cookie = array->array[0].cookie; 1064c8f0523bSTrond Myklebust } 106559e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 10662e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 106759e356a9STrond Myklebust else 106859e356a9STrond Myklebust desc->ctx->pos++; 10692e7a4641STrond Myklebust if (desc->duped != 0) 10702e7a4641STrond Myklebust desc->duped = 1; 10718cd51a0cSTrond Myklebust } 1072b1e21c97STrond Myklebust if (array->page_is_eof) 1073e1d2699bSTrond Myklebust desc->eof = !desc->eob; 1074d1bacf9eSBryan Schumaker 10750795bf83SFabian Frederick kunmap(desc->page); 1076dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1077dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds /* 10811da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 10821da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 10831da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 10841da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 10851da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 10861da177e4SLinus Torvalds * 10871da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 10881da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 10891da177e4SLinus Torvalds * we should already have a complete representation of the 10901da177e4SLinus Torvalds * directory in the page cache by the time we get here. 10911da177e4SLinus Torvalds */ 10926c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 10931da177e4SLinus Torvalds { 109435df59d3STrond Myklebust struct page **arrays; 109535df59d3STrond Myklebust size_t i, sz = 512; 1096b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 109735df59d3STrond Myklebust int status = -ENOMEM; 10981da177e4SLinus Torvalds 109935df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 11002e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 11011da177e4SLinus Torvalds 110235df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 110335df59d3STrond Myklebust if (!arrays) 11041da177e4SLinus Torvalds goto out; 110535df59d3STrond Myklebust arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 110635df59d3STrond Myklebust if (!arrays[0]) 110735df59d3STrond Myklebust goto out; 11081da177e4SLinus Torvalds 11097a8e1dc3STrond Myklebust desc->page_index = 0; 1110ce292d8fStrondmy@kernel.org desc->cache_entry_index = 0; 11112e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 11122e7a4641STrond Myklebust desc->duped = 0; 1113580f2367STrond Myklebust desc->page_index_max = 0; 11147a8e1dc3STrond Myklebust 1115*310e3187STrond Myklebust trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie, 1116*310e3187STrond Myklebust -1, desc->dtsize); 1117*310e3187STrond Myklebust 111835df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1119*310e3187STrond Myklebust if (status < 0) { 1120*310e3187STrond Myklebust trace_nfs_readdir_uncached_done(file_inode(desc->file), status); 1121*310e3187STrond Myklebust goto out_free; 1122*310e3187STrond Myklebust } 1123d1bacf9eSBryan Schumaker 1124e1d2699bSTrond Myklebust for (i = 0; !desc->eob && i < sz && arrays[i]; i++) { 112535df59d3STrond Myklebust desc->page = arrays[i]; 112613884ff2STrond Myklebust nfs_do_filldir(desc, verf); 112735df59d3STrond Myklebust } 112835df59d3STrond Myklebust desc->page = NULL; 11291da177e4SLinus Torvalds 1130580f2367STrond Myklebust /* 1131580f2367STrond Myklebust * Grow the dtsize if we have to go back for more pages, 1132580f2367STrond Myklebust * or shrink it if we're reading too many. 1133580f2367STrond Myklebust */ 1134580f2367STrond Myklebust if (!desc->eof) { 1135580f2367STrond Myklebust if (!desc->eob) 1136580f2367STrond Myklebust nfs_grow_dtsize(desc); 1137580f2367STrond Myklebust else if (desc->buffer_fills == 1 && 1138580f2367STrond Myklebust i < (desc->page_index_max >> 1)) 1139580f2367STrond Myklebust nfs_shrink_dtsize(desc); 1140580f2367STrond Myklebust } 1141*310e3187STrond Myklebust out_free: 114235df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++) 114335df59d3STrond Myklebust nfs_readdir_page_array_free(arrays[i]); 11441da177e4SLinus Torvalds out: 1145580f2367STrond Myklebust desc->page_index_max = -1; 114635df59d3STrond Myklebust kfree(arrays); 114735df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 11481da177e4SLinus Torvalds return status; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 1151230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL) 1152230bc98fSTrond Myklebust 1153230bc98fSTrond Myklebust static void nfs_readdir_handle_cache_misses(struct inode *inode, 1154230bc98fSTrond Myklebust struct nfs_readdir_descriptor *desc, 1155230bc98fSTrond Myklebust pgoff_t page_index, 1156230bc98fSTrond Myklebust unsigned int cache_misses) 1157230bc98fSTrond Myklebust { 1158230bc98fSTrond Myklebust if (desc->ctx->pos == 0 || 1159230bc98fSTrond Myklebust cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD) 1160230bc98fSTrond Myklebust return; 1161230bc98fSTrond Myklebust invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1); 1162230bc98fSTrond Myklebust } 1163230bc98fSTrond Myklebust 116400a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 116500a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 116600a92642SOlivier Galibert whole directory. 11671da177e4SLinus Torvalds */ 116823db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 11691da177e4SLinus Torvalds { 1170be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 11712b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 117213884ff2STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 117323db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 11746b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 1175230bc98fSTrond Myklebust unsigned int cache_hits, cache_misses; 1176ff81dfb5STrond Myklebust pgoff_t page_index; 11776b75cf9eSTrond Myklebust int res; 11781da177e4SLinus Torvalds 11796de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 11806de1472fSAl Viro file, (long long)ctx->pos); 118191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 118291d5b470SChuck Lever 11831da177e4SLinus Torvalds /* 118423db8620SAl Viro * ctx->pos points to the dirent entry number. 1185f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 118600a92642SOlivier Galibert * to either find the entry with the appropriate number or 118700a92642SOlivier Galibert * revalidate the cookie. 11881da177e4SLinus Torvalds */ 1189d09e673fSTrond Myklebust nfs_revalidate_mapping(inode, file->f_mapping); 11906b75cf9eSTrond Myklebust 11916b75cf9eSTrond Myklebust res = -ENOMEM; 11926b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 11936b75cf9eSTrond Myklebust if (!desc) 11946b75cf9eSTrond Myklebust goto out; 11956b75cf9eSTrond Myklebust desc->file = file; 11966b75cf9eSTrond Myklebust desc->ctx = ctx; 1197580f2367STrond Myklebust desc->page_index_max = -1; 1198fccca7fcSTrond Myklebust 11992e7a4641STrond Myklebust spin_lock(&file->f_lock); 12002e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 12012e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 12022e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 1203ff81dfb5STrond Myklebust page_index = dir_ctx->page_index; 1204728dd0abSTrond Myklebust desc->page_index = page_index; 1205728dd0abSTrond Myklebust desc->last_cookie = dir_ctx->last_cookie; 12062e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 1207e1d2699bSTrond Myklebust desc->eof = dir_ctx->eof; 1208580f2367STrond Myklebust nfs_set_dtsize(desc, dir_ctx->dtsize); 1209b593c09fSTrond Myklebust memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf)); 1210230bc98fSTrond Myklebust cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0); 1211230bc98fSTrond Myklebust cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0); 12122e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1213565277f6STrond Myklebust 1214e1d2699bSTrond Myklebust if (desc->eof) { 1215e1d2699bSTrond Myklebust res = 0; 1216e1d2699bSTrond Myklebust goto out_free; 1217e1d2699bSTrond Myklebust } 1218e1d2699bSTrond Myklebust 1219230bc98fSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses); 1220230bc98fSTrond Myklebust nfs_readdir_handle_cache_misses(inode, desc, page_index, cache_misses); 1221ff81dfb5STrond Myklebust 122247c716cbSTrond Myklebust do { 12231da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 122400a92642SOlivier Galibert 12251da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1226ece0b423STrond Myklebust res = 0; 12271da177e4SLinus Torvalds /* This means either end of directory */ 12282e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 12291da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 123023db8620SAl Viro res = uncached_readdir(desc); 1231ece0b423STrond Myklebust if (res == 0) 12321da177e4SLinus Torvalds continue; 12339fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) 12349fff59edSTrond Myklebust res = 0; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds break; 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 12391da177e4SLinus Torvalds nfs_zap_caches(inode); 1240baf57a09STrond Myklebust desc->page_index = 0; 1241a7a3b1e9SBenjamin Coddington desc->plus = false; 1242a7a3b1e9SBenjamin Coddington desc->eof = false; 12431da177e4SLinus Torvalds continue; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds if (res < 0) 12461da177e4SLinus Torvalds break; 12471da177e4SLinus Torvalds 124813884ff2STrond Myklebust nfs_do_filldir(desc, nfsi->cookieverf); 12491f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 1250e1d2699bSTrond Myklebust } while (!desc->eob && !desc->eof); 12512e7a4641STrond Myklebust 12522e7a4641STrond Myklebust spin_lock(&file->f_lock); 12532e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 12542e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 1255728dd0abSTrond Myklebust dir_ctx->last_cookie = desc->last_cookie; 12562e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 12572e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 1258ff81dfb5STrond Myklebust dir_ctx->page_index = desc->page_index; 1259e1d2699bSTrond Myklebust dir_ctx->eof = desc->eof; 1260580f2367STrond Myklebust dir_ctx->dtsize = desc->dtsize; 1261b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 12622e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1263e1d2699bSTrond Myklebust out_free: 12646b75cf9eSTrond Myklebust kfree(desc); 12656b75cf9eSTrond Myklebust 1266fccca7fcSTrond Myklebust out: 12676de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 12681da177e4SLinus Torvalds return res; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 1271965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1272f0dd2136STrond Myklebust { 1273480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1274b84e06c5SChuck Lever 12756de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 12766de1472fSAl Viro filp, offset, whence); 1277b84e06c5SChuck Lever 1278965c8e59SAndrew Morton switch (whence) { 1279f0dd2136STrond Myklebust default: 1280b2b1ff3dSTrond Myklebust return -EINVAL; 1281b2b1ff3dSTrond Myklebust case SEEK_SET: 1282b2b1ff3dSTrond Myklebust if (offset < 0) 1283b2b1ff3dSTrond Myklebust return -EINVAL; 128483f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1285b2b1ff3dSTrond Myklebust break; 1286b2b1ff3dSTrond Myklebust case SEEK_CUR: 1287b2b1ff3dSTrond Myklebust if (offset == 0) 1288b2b1ff3dSTrond Myklebust return filp->f_pos; 128983f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1290b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1291b2b1ff3dSTrond Myklebust if (offset < 0) { 129283f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1293b2b1ff3dSTrond Myklebust return -EINVAL; 1294b2b1ff3dSTrond Myklebust } 1295f0dd2136STrond Myklebust } 1296f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1297f0dd2136STrond Myklebust filp->f_pos = offset; 1298728dd0abSTrond Myklebust dir_ctx->page_index = 0; 12999c3f4d98STrond Myklebust if (!nfs_readdir_use_cookie(filp)) 13009c3f4d98STrond Myklebust dir_ctx->dir_cookie = 0; 13019c3f4d98STrond Myklebust else 1302728dd0abSTrond Myklebust dir_ctx->dir_cookie = offset; 1303b593c09fSTrond Myklebust if (offset == 0) 1304b593c09fSTrond Myklebust memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf)); 13058ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1306e1d2699bSTrond Myklebust dir_ctx->eof = false; 1307f0dd2136STrond Myklebust } 130883f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1309f0dd2136STrond Myklebust return offset; 1310f0dd2136STrond Myklebust } 1311f0dd2136STrond Myklebust 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 13141da177e4SLinus Torvalds * is a dummy operation. 13151da177e4SLinus Torvalds */ 131602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 131702c24a82SJosef Bacik int datasync) 13181da177e4SLinus Torvalds { 13196de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 13201e7cb3dcSChuck Lever 132111decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 13221da177e4SLinus Torvalds return 0; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 1325bfc69a45STrond Myklebust /** 1326bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1327302fad7bSTrond Myklebust * @dir: pointer to directory inode 1328bfc69a45STrond Myklebust * 1329bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1330bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1331bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1332bfc69a45STrond Myklebust * 1333efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1334efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1335efeda80dSTrond Myklebust * 1336bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1337bfc69a45STrond Myklebust */ 1338bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1339bfc69a45STrond Myklebust { 1340efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1341bfc69a45STrond Myklebust } 134289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1343bfc69a45STrond Myklebust 1344efeda80dSTrond Myklebust /** 1345efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1346efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1347efeda80dSTrond Myklebust * @verf: previously saved change attribute 1348efeda80dSTrond Myklebust * 1349efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1350efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1351efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1352efeda80dSTrond Myklebust */ 1353efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1354efeda80dSTrond Myklebust { 1355efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1356efeda80dSTrond Myklebust } 1357efeda80dSTrond Myklebust 1358efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1359efeda80dSTrond Myklebust { 1360efeda80dSTrond Myklebust *verf |= 1UL; 1361efeda80dSTrond Myklebust } 1362efeda80dSTrond Myklebust 1363efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1364efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1365efeda80dSTrond Myklebust { 1366efeda80dSTrond Myklebust *verf &= ~1UL; 1367efeda80dSTrond Myklebust } 1368efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1369efeda80dSTrond Myklebust 1370efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1371efeda80dSTrond Myklebust { 1372efeda80dSTrond Myklebust return verf & 1; 1373efeda80dSTrond Myklebust } 1374efeda80dSTrond Myklebust 1375efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1376efeda80dSTrond Myklebust { 1377efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1378efeda80dSTrond Myklebust } 1379efeda80dSTrond Myklebust 1380efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1381efeda80dSTrond Myklebust { 1382efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1383cec08f45STrond Myklebust struct inode *dir = d_inode(dentry->d_parent); 1384efeda80dSTrond Myklebust 1385cec08f45STrond Myklebust if (!nfs_verify_change_attribute(dir, verf)) 1386cec08f45STrond Myklebust return; 1387efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1388efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1389efeda80dSTrond Myklebust dentry->d_time = verf; 1390efeda80dSTrond Myklebust } 1391efeda80dSTrond Myklebust 1392efeda80dSTrond Myklebust /** 1393efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1394efeda80dSTrond Myklebust * @dentry: pointer to dentry 1395efeda80dSTrond Myklebust * @verf: verifier to save 1396efeda80dSTrond Myklebust * 1397efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1398efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1399efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1400efeda80dSTrond Myklebust * look it up again after a directory change. 1401efeda80dSTrond Myklebust */ 1402efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1403efeda80dSTrond Myklebust { 1404efeda80dSTrond Myklebust 1405efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1406efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1407efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1408efeda80dSTrond Myklebust } 1409efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1410efeda80dSTrond Myklebust 1411efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1412efeda80dSTrond Myklebust /** 1413efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1414efeda80dSTrond Myklebust * @inode: pointer to inode 1415efeda80dSTrond Myklebust * 1416efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1417efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1418efeda80dSTrond Myklebust * while holding a delegation. 1419efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1420efeda80dSTrond Myklebust * returned or revoked. 1421efeda80dSTrond Myklebust */ 1422efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1423efeda80dSTrond Myklebust { 1424efeda80dSTrond Myklebust struct dentry *alias; 1425efeda80dSTrond Myklebust 1426efeda80dSTrond Myklebust if (!inode) 1427efeda80dSTrond Myklebust return; 1428efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1429efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1430efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1431efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1432efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1433efeda80dSTrond Myklebust } 1434efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1435efeda80dSTrond Myklebust } 1436efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1437efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1438efeda80dSTrond Myklebust 14398ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry) 14408ce37abdSTrond Myklebust { 14418ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) && 14428ce37abdSTrond Myklebust d_really_is_negative(dentry)) 14438ce37abdSTrond Myklebust return dentry->d_time == inode_peek_iversion_raw(dir); 14448ce37abdSTrond Myklebust return nfs_verify_change_attribute(dir, dentry->d_time); 14458ce37abdSTrond Myklebust } 14468ce37abdSTrond Myklebust 14471da177e4SLinus Torvalds /* 14481da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 14491da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 14501da177e4SLinus Torvalds * and may need to be looked up again. 1451912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 14521da177e4SLinus Torvalds */ 1453912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1454912a108dSNeilBrown int rcu_walk) 14551da177e4SLinus Torvalds { 14561da177e4SLinus Torvalds if (IS_ROOT(dentry)) 14571da177e4SLinus Torvalds return 1; 14584eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 14594eec952eSTrond Myklebust return 0; 14608ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 14616ecc5e8fSTrond Myklebust return 0; 1462f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 14631cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1464912a108dSNeilBrown if (rcu_walk) 1465f2c77f4eSTrond Myklebust return 0; 14661cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 14671cd9cb05STrond Myklebust return 0; 14681cd9cb05STrond Myklebust } 14698ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry)) 1470f2c77f4eSTrond Myklebust return 0; 1471f2c77f4eSTrond Myklebust return 1; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds /* 1475a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1476a12802caSTrond Myklebust * an O_EXCL create using this path component. 1477a12802caSTrond Myklebust */ 1478fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1479a12802caSTrond Myklebust { 1480a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1481a12802caSTrond Myklebust return 0; 1482fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1483a12802caSTrond Myklebust } 1484a12802caSTrond Myklebust 1485a12802caSTrond Myklebust /* 14861d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 14871d6757fbSTrond Myklebust * 14881d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 14891d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 14901d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 14911d6757fbSTrond Myklebust * 14921d6757fbSTrond Myklebust */ 149365a0c149STrond Myklebust static 1494fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 14951da177e4SLinus Torvalds { 14961da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 149765a0c149STrond Myklebust int ret; 14981da177e4SLinus Torvalds 149936d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 15004e99a1ffSTrond Myklebust return 0; 150147921921STrond Myklebust 150247921921STrond Myklebust if (flags & LOOKUP_OPEN) { 150347921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 150447921921STrond Myklebust case S_IFREG: 150547921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 150647921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 150747921921STrond Myklebust goto out; 1508df561f66SGustavo A. R. Silva fallthrough; 150947921921STrond Myklebust case S_IFDIR: 151047921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 151147921921STrond Myklebust break; 151247921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 151347921921STrond Myklebust goto out_force; 151447921921STrond Myklebust } 151547921921STrond Myklebust } 151647921921STrond Myklebust 15171da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1518fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 15191da177e4SLinus Torvalds goto out_force; 152065a0c149STrond Myklebust out: 152143245ecaSOlga Kornievskaia if (inode->i_nlink > 0 || 152243245ecaSOlga Kornievskaia (inode->i_nlink == 0 && 152343245ecaSOlga Kornievskaia test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags))) 152443245ecaSOlga Kornievskaia return 0; 152543245ecaSOlga Kornievskaia else 152643245ecaSOlga Kornievskaia return -ESTALE; 15271da177e4SLinus Torvalds out_force: 15281fa1e384SNeilBrown if (flags & LOOKUP_RCU) 15291fa1e384SNeilBrown return -ECHILD; 153065a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 153165a0c149STrond Myklebust if (ret != 0) 153265a0c149STrond Myklebust return ret; 153365a0c149STrond Myklebust goto out; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 153682e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode) 153782e7ca13STrond Myklebust { 153882e7ca13STrond Myklebust spin_lock(&inode->i_lock); 1539a6a361c4STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 154082e7ca13STrond Myklebust spin_unlock(&inode->i_lock); 154182e7ca13STrond Myklebust } 154282e7ca13STrond Myklebust 15431da177e4SLinus Torvalds /* 15441da177e4SLinus Torvalds * We judge how long we want to trust negative 15451da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 15461da177e4SLinus Torvalds * 15471da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 15481da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1549912a108dSNeilBrown * 1550912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1551912a108dSNeilBrown * suggesting a reval is needed. 15529f6d44d4STrond Myklebust * 15539f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 15549f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 15551da177e4SLinus Torvalds */ 15561da177e4SLinus Torvalds static inline 15571da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1558fa3c56bbSAl Viro unsigned int flags) 15591da177e4SLinus Torvalds { 15609f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 15611da177e4SLinus Torvalds return 0; 15624eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 15634eec952eSTrond Myklebust return 1; 156498ca3ee6STrond Myklebust /* Case insensitive server? Revalidate negative dentries */ 156598ca3ee6STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 156698ca3ee6STrond Myklebust return 1; 1567912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15705ceb9d7fSTrond Myklebust static int 15715ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 15725ceb9d7fSTrond Myklebust struct inode *inode, int error) 15731da177e4SLinus Torvalds { 15745ceb9d7fSTrond Myklebust switch (error) { 15755ceb9d7fSTrond Myklebust case 1: 15762eef8a31STrond Myklebust break; 15775ceb9d7fSTrond Myklebust case 0: 1578a3f432bfSJ. Bruce Fields /* 1579a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1580a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1581a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1582a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1583a3f432bfSJ. Bruce Fields */ 158447397915STrond Myklebust if (inode && IS_ROOT(dentry)) 15852eef8a31STrond Myklebust error = 1; 15862eef8a31STrond Myklebust break; 15875ceb9d7fSTrond Myklebust } 15882eef8a31STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error); 1589e1fb4d05STrond Myklebust return error; 15901da177e4SLinus Torvalds } 15911da177e4SLinus Torvalds 15925ceb9d7fSTrond Myklebust static int 15935ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 15945ceb9d7fSTrond Myklebust unsigned int flags) 15955ceb9d7fSTrond Myklebust { 15965ceb9d7fSTrond Myklebust int ret = 1; 15975ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 15985ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 15995ceb9d7fSTrond Myklebust return -ECHILD; 16005ceb9d7fSTrond Myklebust ret = 0; 16015ceb9d7fSTrond Myklebust } 16025ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 16035ceb9d7fSTrond Myklebust } 16045ceb9d7fSTrond Myklebust 16055ceb9d7fSTrond Myklebust static int 16065ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 16075ceb9d7fSTrond Myklebust struct inode *inode) 16085ceb9d7fSTrond Myklebust { 16095ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16105ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 16115ceb9d7fSTrond Myklebust } 16125ceb9d7fSTrond Myklebust 16130b3cc71bSTrond Myklebust static int nfs_lookup_revalidate_dentry(struct inode *dir, 16140b3cc71bSTrond Myklebust struct dentry *dentry, 16150b3cc71bSTrond Myklebust struct inode *inode, unsigned int flags) 16165ceb9d7fSTrond Myklebust { 16175ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 16185ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 1619a1147b82STrond Myklebust unsigned long dir_verifier; 16205ceb9d7fSTrond Myklebust int ret; 16215ceb9d7fSTrond Myklebust 16220b3cc71bSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 16230b3cc71bSTrond Myklebust 16245ceb9d7fSTrond Myklebust ret = -ENOMEM; 16255ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 16269558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 16279558a007SAnna Schumaker if (fhandle == NULL || fattr == NULL) 16285ceb9d7fSTrond Myklebust goto out; 16295ceb9d7fSTrond Myklebust 1630a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 16319558a007SAnna Schumaker ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 16325ceb9d7fSTrond Myklebust if (ret < 0) { 1633f7b37b8bSTrond Myklebust switch (ret) { 1634f7b37b8bSTrond Myklebust case -ESTALE: 1635f7b37b8bSTrond Myklebust case -ENOENT: 16365ceb9d7fSTrond Myklebust ret = 0; 1637f7b37b8bSTrond Myklebust break; 1638f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1639f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1640f7b37b8bSTrond Myklebust ret = 1; 1641f7b37b8bSTrond Myklebust } 16425ceb9d7fSTrond Myklebust goto out; 16435ceb9d7fSTrond Myklebust } 16440b3cc71bSTrond Myklebust 16450b3cc71bSTrond Myklebust /* Request help from readdirplus */ 16460b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags); 16470b3cc71bSTrond Myklebust 16485ceb9d7fSTrond Myklebust ret = 0; 16495ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 16505ceb9d7fSTrond Myklebust goto out; 16515ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 16525ceb9d7fSTrond Myklebust goto out; 16535ceb9d7fSTrond Myklebust 1654dd225cb3SAnna Schumaker nfs_setsecurity(inode, fattr); 1655a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 16565ceb9d7fSTrond Myklebust 16575ceb9d7fSTrond Myklebust ret = 1; 16585ceb9d7fSTrond Myklebust out: 16595ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 16605ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 166182e7ca13STrond Myklebust 166282e7ca13STrond Myklebust /* 166382e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being 166482e7ca13STrond Myklebust * a match, then we should revalidate the directory cache. 166582e7ca13STrond Myklebust */ 16668ce37abdSTrond Myklebust if (!ret && nfs_dentry_verify_change(dir, dentry)) 166782e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 16685ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 16695ceb9d7fSTrond Myklebust } 16705ceb9d7fSTrond Myklebust 16715ceb9d7fSTrond Myklebust /* 16725ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 16735ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 16745ceb9d7fSTrond Myklebust * lookup. 16755ceb9d7fSTrond Myklebust * 16765ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 16775ceb9d7fSTrond Myklebust * we have an inode! 16785ceb9d7fSTrond Myklebust * 16795ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 16805ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 16815ceb9d7fSTrond Myklebust */ 16825ceb9d7fSTrond Myklebust static int 16835ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 16845ceb9d7fSTrond Myklebust unsigned int flags) 16855ceb9d7fSTrond Myklebust { 16865ceb9d7fSTrond Myklebust struct inode *inode; 16875ceb9d7fSTrond Myklebust int error; 16885ceb9d7fSTrond Myklebust 16895ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 16905ceb9d7fSTrond Myklebust inode = d_inode(dentry); 16915ceb9d7fSTrond Myklebust 16925ceb9d7fSTrond Myklebust if (!inode) 16935ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 16945ceb9d7fSTrond Myklebust 16955ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 16965ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 16975ceb9d7fSTrond Myklebust __func__, dentry); 16985ceb9d7fSTrond Myklebust goto out_bad; 16995ceb9d7fSTrond Myklebust } 17005ceb9d7fSTrond Myklebust 1701efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 17025ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 17035ceb9d7fSTrond Myklebust 17045ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 17055ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 17065ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 17075ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 17085ceb9d7fSTrond Myklebust if (error) { 17095ceb9d7fSTrond Myklebust if (error == -ESTALE) 171082e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 17115ceb9d7fSTrond Myklebust goto out_bad; 17125ceb9d7fSTrond Myklebust } 17135ceb9d7fSTrond Myklebust goto out_valid; 17145ceb9d7fSTrond Myklebust } 17155ceb9d7fSTrond Myklebust 17165ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 17175ceb9d7fSTrond Myklebust return -ECHILD; 17185ceb9d7fSTrond Myklebust 17195ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 17205ceb9d7fSTrond Myklebust goto out_bad; 17215ceb9d7fSTrond Myklebust 17220b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags); 17235ceb9d7fSTrond Myklebust out_valid: 17245ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 17255ceb9d7fSTrond Myklebust out_bad: 17265ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 17275ceb9d7fSTrond Myklebust return -ECHILD; 17285ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 17295ceb9d7fSTrond Myklebust } 17305ceb9d7fSTrond Myklebust 17315ceb9d7fSTrond Myklebust static int 1732c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1733c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 17345ceb9d7fSTrond Myklebust { 17355ceb9d7fSTrond Myklebust struct dentry *parent; 17365ceb9d7fSTrond Myklebust struct inode *dir; 17375ceb9d7fSTrond Myklebust int ret; 17385ceb9d7fSTrond Myklebust 17395ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 17405ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 17415ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 17425ceb9d7fSTrond Myklebust if (!dir) 17435ceb9d7fSTrond Myklebust return -ECHILD; 1744c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 17455ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 17465ceb9d7fSTrond Myklebust return -ECHILD; 17475ceb9d7fSTrond Myklebust } else { 17485ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1749c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 17505ceb9d7fSTrond Myklebust dput(parent); 17515ceb9d7fSTrond Myklebust } 17525ceb9d7fSTrond Myklebust return ret; 17535ceb9d7fSTrond Myklebust } 17545ceb9d7fSTrond Myklebust 1755c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1756c7944ebbSTrond Myklebust { 1757c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1758c7944ebbSTrond Myklebust } 1759c7944ebbSTrond Myklebust 17601da177e4SLinus Torvalds /* 17612b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1762ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1763ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1764ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1765ecf3d1f1SJeff Layton * 1766ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1767ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1768ecf3d1f1SJeff Layton */ 1769ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1770ecf3d1f1SJeff Layton { 17712b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 17729cdd1d3fSTrond Myklebust int error = 0; 1773ecf3d1f1SJeff Layton 1774ecf3d1f1SJeff Layton /* 1775ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1776ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1777ecf3d1f1SJeff Layton * eventually need to do something more here. 1778ecf3d1f1SJeff Layton */ 1779ecf3d1f1SJeff Layton if (!inode) { 17806de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 17816de1472fSAl Viro __func__, dentry); 1782ecf3d1f1SJeff Layton return 1; 1783ecf3d1f1SJeff Layton } 1784ecf3d1f1SJeff Layton 1785ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 17866de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 17876de1472fSAl Viro __func__, dentry); 1788ecf3d1f1SJeff Layton return 0; 1789ecf3d1f1SJeff Layton } 1790ecf3d1f1SJeff Layton 1791b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1792ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1793ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1794ecf3d1f1SJeff Layton return !error; 1795ecf3d1f1SJeff Layton } 1796ecf3d1f1SJeff Layton 1797ecf3d1f1SJeff Layton /* 17981da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 17991da177e4SLinus Torvalds */ 1800fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 18011da177e4SLinus Torvalds { 18026de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 18036de1472fSAl Viro dentry, dentry->d_flags); 18041da177e4SLinus Torvalds 180577f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 18062b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 180777f11192STrond Myklebust return 1; 180877f11192STrond Myklebust 18091da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18101da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 18111da177e4SLinus Torvalds return 1; 18121da177e4SLinus Torvalds } 18131751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 18141da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 18151da177e4SLinus Torvalds * files will be cleaned up during umount */ 18161da177e4SLinus Torvalds return 1; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds return 0; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 18221f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 18231b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 18241b83d707STrond Myklebust { 18251b83d707STrond Myklebust spin_lock(&inode->i_lock); 18261f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 182759a707b0STrond Myklebust if (inode->i_nlink > 0) 182859a707b0STrond Myklebust drop_nlink(inode); 182959a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1830ac46b3d7STrond Myklebust nfs_set_cache_invalid( 1831ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 18321301e421STrond Myklebust NFS_INO_INVALID_NLINK); 18331b83d707STrond Myklebust spin_unlock(&inode->i_lock); 18341b83d707STrond Myklebust } 18351b83d707STrond Myklebust 18361da177e4SLinus Torvalds /* 18371da177e4SLinus Torvalds * Called when the dentry loses inode. 18381da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 18391da177e4SLinus Torvalds */ 18401da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 18411da177e4SLinus Torvalds { 18421da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1843e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 18441f018458STrond Myklebust nfs_drop_nlink(inode); 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds iput(inode); 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 1849b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1850b1942c5fSAl Viro { 1851b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1852b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1853b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1854b1942c5fSAl Viro WARN_ON(1); 1855b1942c5fSAl Viro else 1856b1942c5fSAl Viro kfree(dentry->d_fsdata); 1857b1942c5fSAl Viro } 1858b1942c5fSAl Viro } 1859b1942c5fSAl Viro 1860f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 18611da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1862ecf3d1f1SJeff Layton .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 }; 1868ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 18691da177e4SLinus Torvalds 1870597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 18711da177e4SLinus Torvalds { 18721da177e4SLinus Torvalds struct dentry *res; 18731da177e4SLinus Torvalds struct inode *inode = NULL; 1874e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1875e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 1876a1147b82STrond Myklebust unsigned long dir_verifier; 18771da177e4SLinus Torvalds int error; 18781da177e4SLinus Torvalds 18796de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 188091d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 18811da177e4SLinus Torvalds 1882130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1883130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 18841da177e4SLinus Torvalds 1885fd684071STrond Myklebust /* 1886fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1887fd684071STrond Myklebust * but don't hash the dentry. 1888fd684071STrond Myklebust */ 18899f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1890130f9ab7SAl Viro return NULL; 18911da177e4SLinus Torvalds 1892e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1893e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 18949558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir)); 1895e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1896e1fb4d05STrond Myklebust goto out; 1897e1fb4d05STrond Myklebust 1898a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 18996e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 19009558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 19018ce37abdSTrond Myklebust if (error == -ENOENT) { 19028ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 19038ce37abdSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 19041da177e4SLinus Torvalds goto no_entry; 19058ce37abdSTrond Myklebust } 19061da177e4SLinus Torvalds if (error < 0) { 19071da177e4SLinus Torvalds res = ERR_PTR(error); 19089558a007SAnna Schumaker goto out; 19091da177e4SLinus Torvalds } 1910cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1911bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 191203f28e3aSTrond Myklebust if (IS_ERR(res)) 19139558a007SAnna Schumaker goto out; 191454ceac45SDavid Howells 191563519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 19160b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags); 1917d69ee9b8STrond Myklebust 19181da177e4SLinus Torvalds no_entry: 191941d28bcaSAl Viro res = d_splice_alias(inode, dentry); 19209eaef27bSTrond Myklebust if (res != NULL) { 19219eaef27bSTrond Myklebust if (IS_ERR(res)) 19229558a007SAnna Schumaker goto out; 19231da177e4SLinus Torvalds dentry = res; 19249eaef27bSTrond Myklebust } 1925a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 19261da177e4SLinus Torvalds out: 19279558a007SAnna Schumaker trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res)); 1928e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1929e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 19301da177e4SLinus Torvalds return res; 19311da177e4SLinus Torvalds } 1932ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 19331da177e4SLinus Torvalds 193400bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode) 193500bdadc7STrond Myklebust { 193600bdadc7STrond Myklebust /* Case insensitive server? Revalidate dentries */ 193700bdadc7STrond Myklebust if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE)) 193800bdadc7STrond Myklebust d_prune_aliases(inode); 193900bdadc7STrond Myklebust } 194000bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases); 194100bdadc7STrond Myklebust 194289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 19430b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 19441da177e4SLinus Torvalds 1945f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 19460ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1947b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 19481da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 19491da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 195036d43a43SDavid Howells .d_automount = nfs_d_automount, 1951b1942c5fSAl Viro .d_release = nfs_d_release, 19521da177e4SLinus Torvalds }; 195389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 19541da177e4SLinus Torvalds 19558a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 19568a5e929dSAl Viro { 19578a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 19588a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 19598a5e929dSAl Viro res |= FMODE_READ; 19608a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 19618a5e929dSAl Viro res |= FMODE_WRITE; 19628a5e929dSAl Viro return res; 19638a5e929dSAl Viro } 19648a5e929dSAl Viro 1965532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1966cd9a1c0eSTrond Myklebust { 1967532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1968cd9a1c0eSTrond Myklebust } 1969cd9a1c0eSTrond Myklebust 1970cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1971cd9a1c0eSTrond Myklebust { 1972f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1973cd9a1c0eSTrond Myklebust return 0; 1974cd9a1c0eSTrond Myklebust } 1975cd9a1c0eSTrond Myklebust 1976d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 19770dd2b474SMiklos Szeredi struct dentry *dentry, 1978b452a458SAl Viro struct file *file, unsigned open_flags) 1979cd9a1c0eSTrond Myklebust { 19800dd2b474SMiklos Szeredi int err; 19810dd2b474SMiklos Szeredi 1982be12af3eSAl Viro err = finish_open(file, dentry, do_open); 198330d90494SAl Viro if (err) 1984d9585277SAl Viro goto out; 1985eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 198630d90494SAl Viro nfs_file_set_open_context(file, ctx); 1987eaa2b82cSNeilBrown else 19889821421aSTrond Myklebust err = -EOPENSTALE; 1989cd9a1c0eSTrond Myklebust out: 1990d9585277SAl Viro return err; 1991cd9a1c0eSTrond Myklebust } 1992cd9a1c0eSTrond Myklebust 199373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 199430d90494SAl Viro struct file *file, unsigned open_flags, 199544907d79SAl Viro umode_t mode) 19961da177e4SLinus Torvalds { 1997c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1998cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 19990dd2b474SMiklos Szeredi struct dentry *res; 20000dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 2001f46e0bd3STrond Myklebust struct inode *inode; 20021472b83eSTrond Myklebust unsigned int lookup_flags = 0; 200368eaba4cSTrond Myklebust unsigned long dir_verifier; 2004c94c0953SAl Viro bool switched = false; 200573a09dd9SAl Viro int created = 0; 2006898f635cSTrond Myklebust int err; 20071da177e4SLinus Torvalds 20080dd2b474SMiklos Szeredi /* Expect a negative dentry */ 20092b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 20100dd2b474SMiklos Szeredi 20111e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 20126de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20131e7cb3dcSChuck Lever 20149597c13bSJeff Layton err = nfs_check_flags(open_flags); 20159597c13bSJeff Layton if (err) 20169597c13bSJeff Layton return err; 20179597c13bSJeff Layton 20180dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 20190dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 202000699ad8SAl Viro if (!d_in_lookup(dentry)) { 20210dd2b474SMiklos Szeredi /* 20220dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 20230dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 20240dd2b474SMiklos Szeredi * again 20250dd2b474SMiklos Szeredi */ 2026d9585277SAl Viro return -ENOENT; 20270dd2b474SMiklos Szeredi } 20281472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 20291da177e4SLinus Torvalds goto no_open; 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds 20320dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 2033d9585277SAl Viro return -ENAMETOOLONG; 20341da177e4SLinus Torvalds 20350dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 2036dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 2037dff25ddbSAndreas Gruenbacher 2038dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 2039dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 2040dff25ddbSAndreas Gruenbacher 2041536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 2042dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 20430dd2b474SMiklos Szeredi } 2044536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 2045536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 2046536e43d1STrond Myklebust attr.ia_size = 0; 2047cd9a1c0eSTrond Myklebust } 2048cd9a1c0eSTrond Myklebust 2049c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 2050c94c0953SAl Viro d_drop(dentry); 2051c94c0953SAl Viro switched = true; 2052c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 2053c94c0953SAl Viro &dentry->d_name, &wq); 2054c94c0953SAl Viro if (IS_ERR(dentry)) 2055c94c0953SAl Viro return PTR_ERR(dentry); 2056c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 2057c94c0953SAl Viro return finish_no_open(file, dentry); 2058c94c0953SAl Viro } 2059c94c0953SAl Viro 2060532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 20610dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 20620dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 2063d9585277SAl Viro goto out; 20640dd2b474SMiklos Szeredi 20656e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 206673a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 206773a09dd9SAl Viro if (created) 206873a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 2069275bb307STrond Myklebust if (IS_ERR(inode)) { 20700dd2b474SMiklos Szeredi err = PTR_ERR(inode); 20716e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 20722d9db750STrond Myklebust put_nfs_open_context(ctx); 2073d20cb71dSAl Viro d_drop(dentry); 20740dd2b474SMiklos Szeredi switch (err) { 20751da177e4SLinus Torvalds case -ENOENT: 2076774d9513SPeng Tao d_splice_alias(NULL, dentry); 207768eaba4cSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) 207868eaba4cSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir); 207968eaba4cSTrond Myklebust else 208068eaba4cSTrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 208168eaba4cSTrond Myklebust nfs_set_verifier(dentry, dir_verifier); 20820dd2b474SMiklos Szeredi break; 20831788ea6eSJeff Layton case -EISDIR: 20846f926b5bSTrond Myklebust case -ENOTDIR: 20856f926b5bSTrond Myklebust goto no_open; 20861da177e4SLinus Torvalds case -ELOOP: 20870dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 20881da177e4SLinus Torvalds goto no_open; 20890dd2b474SMiklos Szeredi break; 20901da177e4SLinus Torvalds /* case -EINVAL: */ 20911da177e4SLinus Torvalds default: 20920dd2b474SMiklos Szeredi break; 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds goto out; 20951da177e4SLinus Torvalds } 20960dd2b474SMiklos Szeredi 2097b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 20986e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 20992d9db750STrond Myklebust put_nfs_open_context(ctx); 2100d9585277SAl Viro out: 2101c94c0953SAl Viro if (unlikely(switched)) { 2102c94c0953SAl Viro d_lookup_done(dentry); 2103c94c0953SAl Viro dput(dentry); 2104c94c0953SAl Viro } 2105d9585277SAl Viro return err; 21060dd2b474SMiklos Szeredi 21071da177e4SLinus Torvalds no_open: 21081472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 2109ac795161STrond Myklebust if (!res) { 2110ac795161STrond Myklebust inode = d_inode(dentry); 2111ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2112e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) 2113ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 21141751fc1dSTrond Myklebust else if (inode && S_ISREG(inode->i_mode)) 21151751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2116ac795161STrond Myklebust } else if (!IS_ERR(res)) { 2117ac795161STrond Myklebust inode = d_inode(res); 2118ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode && 2119e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) { 2120ac795161STrond Myklebust dput(res); 2121ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR); 21221751fc1dSTrond Myklebust } else if (inode && S_ISREG(inode->i_mode)) { 21231751fc1dSTrond Myklebust dput(res); 21241751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE); 2125ac795161STrond Myklebust } 2126ac795161STrond Myklebust } 2127c94c0953SAl Viro if (switched) { 2128c94c0953SAl Viro d_lookup_done(dentry); 2129c94c0953SAl Viro if (!res) 2130c94c0953SAl Viro res = dentry; 2131c94c0953SAl Viro else 2132c94c0953SAl Viro dput(dentry); 2133c94c0953SAl Viro } 21340dd2b474SMiklos Szeredi if (IS_ERR(res)) 2135c94c0953SAl Viro return PTR_ERR(res); 2136e45198a6SAl Viro return finish_no_open(file, res); 21371da177e4SLinus Torvalds } 213889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 21391da177e4SLinus Torvalds 2140c7944ebbSTrond Myklebust static int 2141c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 2142c7944ebbSTrond Myklebust unsigned int flags) 21431da177e4SLinus Torvalds { 2144657e94b6SNick Piggin struct inode *inode; 21451da177e4SLinus Torvalds 2146fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 2147c7944ebbSTrond Myklebust goto full_reval; 2148eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 2149c7944ebbSTrond Myklebust goto full_reval; 21502b484297STrond Myklebust 21512b0143b5SDavid Howells inode = d_inode(dentry); 21522b484297STrond Myklebust 21531da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 21541da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 21551da177e4SLinus Torvalds */ 2156c7944ebbSTrond Myklebust if (inode == NULL) 2157c7944ebbSTrond Myklebust goto full_reval; 215849317a7fSNeilBrown 2159efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 2160c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2161216d5d06STrond Myklebust 21621da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 21631da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 2164c7944ebbSTrond Myklebust goto full_reval; 2165c7944ebbSTrond Myklebust 21661da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 2167c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2168c7944ebbSTrond Myklebust goto reval_dentry; 2169c7944ebbSTrond Myklebust 2170c7944ebbSTrond Myklebust /* Check if the directory changed */ 2171c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2172c7944ebbSTrond Myklebust goto reval_dentry; 21731da177e4SLinus Torvalds 21740ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 2175c7944ebbSTrond Myklebust return 1; 2176c7944ebbSTrond Myklebust reval_dentry: 2177c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 2178c7944ebbSTrond Myklebust return -ECHILD; 21790b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags); 21800ef97dcfSMiklos Szeredi 2181c7944ebbSTrond Myklebust full_reval: 2182c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 2183c7944ebbSTrond Myklebust } 2184535918f1STrond Myklebust 2185c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2186c7944ebbSTrond Myklebust { 2187c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 2188c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 2189c0204fd2STrond Myklebust } 2190c0204fd2STrond Myklebust 21911da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 21921da177e4SLinus Torvalds 2193406cd915SBenjamin Coddington struct dentry * 2194406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 2195cc6f3298SAnna Schumaker struct nfs_fattr *fattr) 21961da177e4SLinus Torvalds { 2197fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 21982b0143b5SDavid Howells struct inode *dir = d_inode(parent); 21991da177e4SLinus Torvalds struct inode *inode; 2200b0c6108eSAl Viro struct dentry *d; 2201406cd915SBenjamin Coddington int error; 22021da177e4SLinus Torvalds 2203fab728e1STrond Myklebust d_drop(dentry); 2204fab728e1STrond Myklebust 22051da177e4SLinus Torvalds if (fhandle->size == 0) { 22069558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 22071da177e4SLinus Torvalds if (error) 2208fab728e1STrond Myklebust goto out_error; 22091da177e4SLinus Torvalds } 22105724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 22111da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 22121da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 2213a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 22142ef61e0eSAnna Schumaker fattr, NULL); 22151da177e4SLinus Torvalds if (error < 0) 2216fab728e1STrond Myklebust goto out_error; 22171da177e4SLinus Torvalds } 2218cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 2219b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 2220fab728e1STrond Myklebust out: 2221fab728e1STrond Myklebust dput(parent); 2222406cd915SBenjamin Coddington return d; 2223fab728e1STrond Myklebust out_error: 2224406cd915SBenjamin Coddington d = ERR_PTR(error); 2225406cd915SBenjamin Coddington goto out; 2226406cd915SBenjamin Coddington } 2227406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2228406cd915SBenjamin Coddington 2229406cd915SBenjamin Coddington /* 2230406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 2231406cd915SBenjamin Coddington */ 2232406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2233d91bfc46SAnna Schumaker struct nfs_fattr *fattr) 2234406cd915SBenjamin Coddington { 2235406cd915SBenjamin Coddington struct dentry *d; 2236406cd915SBenjamin Coddington 2237cc6f3298SAnna Schumaker d = nfs_add_or_obtain(dentry, fhandle, fattr); 2238406cd915SBenjamin Coddington if (IS_ERR(d)) 2239406cd915SBenjamin Coddington return PTR_ERR(d); 2240406cd915SBenjamin Coddington 2241406cd915SBenjamin Coddington /* Callers don't care */ 2242406cd915SBenjamin Coddington dput(d); 2243406cd915SBenjamin Coddington return 0; 22441da177e4SLinus Torvalds } 2245ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds /* 22481da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 22491da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 22501da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 22511da177e4SLinus Torvalds * reply path made it appear to have failed. 22521da177e4SLinus Torvalds */ 2253549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2254549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 22551da177e4SLinus Torvalds { 22561da177e4SLinus Torvalds struct iattr attr; 2257ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 22581da177e4SLinus Torvalds int error; 22591da177e4SLinus Torvalds 22601e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 22616de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds attr.ia_mode = mode; 22641da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 22651da177e4SLinus Torvalds 22668b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 22678867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 22688b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 22691da177e4SLinus Torvalds if (error != 0) 22701da177e4SLinus Torvalds goto out_err; 22711da177e4SLinus Torvalds return 0; 22721da177e4SLinus Torvalds out_err: 22731da177e4SLinus Torvalds d_drop(dentry); 22741da177e4SLinus Torvalds return error; 22751da177e4SLinus Torvalds } 2276ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds /* 22791da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 22801da177e4SLinus Torvalds */ 2281597d9289SBryan Schumaker int 2282549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2283549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev) 22841da177e4SLinus Torvalds { 22851da177e4SLinus Torvalds struct iattr attr; 22861da177e4SLinus Torvalds int status; 22871da177e4SLinus Torvalds 22881e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 22896de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds attr.ia_mode = mode; 22921da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 22931da177e4SLinus Torvalds 22941ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 22951da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 22961ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 22971da177e4SLinus Torvalds if (status != 0) 22981da177e4SLinus Torvalds goto out_err; 22991da177e4SLinus Torvalds return 0; 23001da177e4SLinus Torvalds out_err: 23011da177e4SLinus Torvalds d_drop(dentry); 23021da177e4SLinus Torvalds return status; 23031da177e4SLinus Torvalds } 2304ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds /* 23071da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 23081da177e4SLinus Torvalds */ 2309549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2310549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 23111da177e4SLinus Torvalds { 23121da177e4SLinus Torvalds struct iattr attr; 23131da177e4SLinus Torvalds int error; 23141da177e4SLinus Torvalds 23151e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 23166de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 23191da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 23201da177e4SLinus Torvalds 23211ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 23221da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 23231ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 23241da177e4SLinus Torvalds if (error != 0) 23251da177e4SLinus Torvalds goto out_err; 23261da177e4SLinus Torvalds return 0; 23271da177e4SLinus Torvalds out_err: 23281da177e4SLinus Torvalds d_drop(dentry); 23291da177e4SLinus Torvalds return error; 23301da177e4SLinus Torvalds } 2331ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 23321da177e4SLinus Torvalds 2333d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2334d45b9d8bSTrond Myklebust { 2335dc3f4198SAl Viro if (simple_positive(dentry)) 2336d45b9d8bSTrond Myklebust d_delete(dentry); 2337d45b9d8bSTrond Myklebust } 2338d45b9d8bSTrond Myklebust 23399019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir, 23409019fb39STrond Myklebust struct dentry *dentry, int error) 23419019fb39STrond Myklebust { 23429019fb39STrond Myklebust switch (error) { 23439019fb39STrond Myklebust case -ENOENT: 23449019fb39STrond Myklebust d_delete(dentry); 234500bdadc7STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 234600bdadc7STrond Myklebust break; 23479019fb39STrond Myklebust case 0: 234800bdadc7STrond Myklebust nfs_d_prune_case_insensitive_aliases(d_inode(dentry)); 23499019fb39STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 23509019fb39STrond Myklebust } 23519019fb39STrond Myklebust } 23529019fb39STrond Myklebust 2353597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 23541da177e4SLinus Torvalds { 23551da177e4SLinus Torvalds int error; 23561da177e4SLinus Torvalds 23571e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 23586de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 23591da177e4SLinus Torvalds 23601ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 23612b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2362884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 23631da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 23641da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2365ba6c0592STrond Myklebust switch (error) { 2366ba6c0592STrond Myklebust case 0: 23672b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2368ba6c0592STrond Myklebust break; 2369ba6c0592STrond Myklebust case -ENOENT: 2370d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2371ba6c0592STrond Myklebust } 2372884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2373ba6c0592STrond Myklebust } else 2374ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 23759019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 23761ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds return error; 23791da177e4SLinus Torvalds } 2380ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds /* 23831da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 23841da177e4SLinus Torvalds * and after checking that the file has only one user. 23851da177e4SLinus Torvalds * 23861da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 23871da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 23881da177e4SLinus Torvalds */ 23891da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 23901da177e4SLinus Torvalds { 23912b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 23922b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 23931da177e4SLinus Torvalds int error = -EBUSY; 23941da177e4SLinus Torvalds 23956de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 23981da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 23991da177e4SLinus Torvalds error = 0; 24001da177e4SLinus Torvalds goto out; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 24031ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 24041da177e4SLinus Torvalds if (inode != NULL) { 2405912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 24061da177e4SLinus Torvalds if (error == 0) 24071b83d707STrond Myklebust nfs_drop_nlink(inode); 24081da177e4SLinus Torvalds } else 2409912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2410d45b9d8bSTrond Myklebust if (error == -ENOENT) 2411d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 24121ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 24131da177e4SLinus Torvalds out: 24141da177e4SLinus Torvalds return error; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 24181da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 24191da177e4SLinus Torvalds * 24201da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 24211da177e4SLinus Torvalds */ 2422597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 24231da177e4SLinus Torvalds { 24241da177e4SLinus Torvalds int error; 24251da177e4SLinus Torvalds int need_rehash = 0; 24261da177e4SLinus Torvalds 24271e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 24286de1472fSAl Viro dir->i_ino, dentry); 24291da177e4SLinus Torvalds 24301ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 24311da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 243243245ecaSOlga Kornievskaia if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED, 243343245ecaSOlga Kornievskaia &NFS_I(d_inode(dentry))->flags)) { 24341da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2435ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 24362b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 24371da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 24381ca42382STrond Myklebust goto out; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 24411da177e4SLinus Torvalds __d_drop(dentry); 24421da177e4SLinus Torvalds need_rehash = 1; 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 24451da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 24469019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 24479019fb39STrond Myklebust if (need_rehash) 24481da177e4SLinus Torvalds d_rehash(dentry); 24491ca42382STrond Myklebust out: 24501ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 24511da177e4SLinus Torvalds return error; 24521da177e4SLinus Torvalds } 2453ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 24541da177e4SLinus Torvalds 2455873101b3SChuck Lever /* 2456873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2457873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2458873101b3SChuck Lever * using prepare_write/commit_write. 2459873101b3SChuck Lever * 2460873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2461873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2462873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2463873101b3SChuck Lever * symlink request has completed on the server. 2464873101b3SChuck Lever * 2465873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2466873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2467873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2468873101b3SChuck Lever * and move the raw page into its mapping. 2469873101b3SChuck Lever */ 2470549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 2471549c7297SChristian Brauner struct dentry *dentry, const char *symname) 24721da177e4SLinus Torvalds { 2473873101b3SChuck Lever struct page *page; 2474873101b3SChuck Lever char *kaddr; 24751da177e4SLinus Torvalds struct iattr attr; 2476873101b3SChuck Lever unsigned int pathlen = strlen(symname); 24771da177e4SLinus Torvalds int error; 24781da177e4SLinus Torvalds 24791e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 24806de1472fSAl Viro dir->i_ino, dentry, symname); 24811da177e4SLinus Torvalds 2482873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2483873101b3SChuck Lever return -ENAMETOOLONG; 24841da177e4SLinus Torvalds 2485873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2486873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 24871da177e4SLinus Torvalds 2488e8ecde25SAl Viro page = alloc_page(GFP_USER); 248976566991STrond Myklebust if (!page) 2490873101b3SChuck Lever return -ENOMEM; 2491873101b3SChuck Lever 2492e8ecde25SAl Viro kaddr = page_address(page); 2493873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2494873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2495873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2496873101b3SChuck Lever 24971ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 249894a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 24991ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2500873101b3SChuck Lever if (error != 0) { 25011e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2502873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 25036de1472fSAl Viro dentry, symname, error); 25041da177e4SLinus Torvalds d_drop(dentry); 2505873101b3SChuck Lever __free_page(page); 25061da177e4SLinus Torvalds return error; 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 2509342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2510342a67f0STrond Myklebust 2511873101b3SChuck Lever /* 2512873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2513873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2514873101b3SChuck Lever */ 25152b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2516873101b3SChuck Lever GFP_KERNEL)) { 2517873101b3SChuck Lever SetPageUptodate(page); 2518873101b3SChuck Lever unlock_page(page); 2519a0b54addSRafael Aquini /* 2520a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2521a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2522a0b54addSRafael Aquini */ 252309cbfeafSKirill A. Shutemov put_page(page); 2524873101b3SChuck Lever } else 2525873101b3SChuck Lever __free_page(page); 2526873101b3SChuck Lever 2527873101b3SChuck Lever return 0; 2528873101b3SChuck Lever } 2529ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2530873101b3SChuck Lever 2531597d9289SBryan Schumaker int 25321da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 25331da177e4SLinus Torvalds { 25342b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 25351da177e4SLinus Torvalds int error; 25361da177e4SLinus Torvalds 25376de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 25386de1472fSAl Viro old_dentry, dentry); 25391da177e4SLinus Torvalds 25401fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 25419697d234STrond Myklebust d_drop(dentry); 254220497503STrond Myklebust if (S_ISREG(inode->i_mode)) 254320497503STrond Myklebust nfs_sync_inode(inode); 25441da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2545cf809556STrond Myklebust if (error == 0) { 2546342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 25477de9c6eeSAl Viro ihold(inode); 25489697d234STrond Myklebust d_add(dentry, inode); 2549cf809556STrond Myklebust } 25501fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 25511da177e4SLinus Torvalds return error; 25521da177e4SLinus Torvalds } 2553ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds /* 25561da177e4SLinus Torvalds * RENAME 25571da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 25581da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 25591da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 25601da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 25611da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 25621da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 25631da177e4SLinus Torvalds * 25641da177e4SLinus Torvalds * FIXED. 25651da177e4SLinus Torvalds * 25661da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 25671da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 25681da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 25691da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 25701da177e4SLinus Torvalds * using the inode layer 25711da177e4SLinus Torvalds * 25721da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 25731da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 25741da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 25751da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 25761da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 25771da177e4SLinus Torvalds * the rename. 25781da177e4SLinus Torvalds */ 2579549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2580549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 2581549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 25821da177e4SLinus Torvalds { 25832b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 25842b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2585d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 258680a491fdSJeff Layton struct rpc_task *task; 25871da177e4SLinus Torvalds int error = -EBUSY; 25881da177e4SLinus Torvalds 25891cd66c93SMiklos Szeredi if (flags) 25901cd66c93SMiklos Szeredi return -EINVAL; 25911cd66c93SMiklos Szeredi 25926de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 25936de1472fSAl Viro old_dentry, new_dentry, 259484d08fa8SAl Viro d_count(new_dentry)); 25951da177e4SLinus Torvalds 259670ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 25971da177e4SLinus Torvalds /* 259828f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 259928f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 260028f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 260128f79a1aSMiklos Szeredi * the new target. 26021da177e4SLinus Torvalds */ 260327226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 260427226104SMiklos Szeredi /* 260527226104SMiklos Szeredi * To prevent any new references to the target during the 260627226104SMiklos Szeredi * rename, we unhash the dentry in advance. 260727226104SMiklos Szeredi */ 2608d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 260927226104SMiklos Szeredi d_drop(new_dentry); 2610d9f29500SBenjamin Coddington rehash = new_dentry; 2611d9f29500SBenjamin Coddington } 261227226104SMiklos Szeredi 261384d08fa8SAl Viro if (d_count(new_dentry) > 2) { 26141da177e4SLinus Torvalds int err; 261527226104SMiklos Szeredi 26161da177e4SLinus Torvalds /* copy the target dentry's name */ 26171da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 26181da177e4SLinus Torvalds &new_dentry->d_name); 26191da177e4SLinus Torvalds if (!dentry) 26201da177e4SLinus Torvalds goto out; 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds /* silly-rename the existing target ... */ 26231da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 262424e93025SMiklos Szeredi if (err) 26251da177e4SLinus Torvalds goto out; 262624e93025SMiklos Szeredi 262724e93025SMiklos Szeredi new_dentry = dentry; 2628d9f29500SBenjamin Coddington rehash = NULL; 262924e93025SMiklos Szeredi new_inode = NULL; 2630b1e4adf4STrond Myklebust } 263127226104SMiklos Szeredi } 26321da177e4SLinus Torvalds 26336ff9d99bSTrond Myklebust if (S_ISREG(old_inode->i_mode)) 26346ff9d99bSTrond Myklebust nfs_sync_inode(old_inode); 2635d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 263680a491fdSJeff Layton if (IS_ERR(task)) { 263780a491fdSJeff Layton error = PTR_ERR(task); 263880a491fdSJeff Layton goto out; 263980a491fdSJeff Layton } 264080a491fdSJeff Layton 264180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2642818a8dbeSBenjamin Coddington if (error != 0) { 2643818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2644818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2645818a8dbeSBenjamin Coddington smp_wmb(); 2646818a8dbeSBenjamin Coddington } else 264780a491fdSJeff Layton error = task->tk_status; 264880a491fdSJeff Layton rpc_put_task(task); 264959a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 265059a707b0STrond Myklebust if (error == 0) { 265159a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 265259a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2653ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2654ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME | 2655ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 265659a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 265759a707b0STrond Myklebust } 26581da177e4SLinus Torvalds out: 2659d9f29500SBenjamin Coddington if (rehash) 2660d9f29500SBenjamin Coddington d_rehash(rehash); 266170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 266270ded201STrond Myklebust new_dir, new_dentry, error); 2663d9f29500SBenjamin Coddington if (!error) { 2664d9f29500SBenjamin Coddington if (new_inode != NULL) 2665d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2666d9f29500SBenjamin Coddington /* 2667d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2668d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2669d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2670d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2671d9f29500SBenjamin Coddington */ 2672d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2673d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2674d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2675d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2676d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2677d9f29500SBenjamin Coddington 26781da177e4SLinus Torvalds /* new dentry created? */ 26791da177e4SLinus Torvalds if (dentry) 26801da177e4SLinus Torvalds dput(dentry); 26811da177e4SLinus Torvalds return error; 26821da177e4SLinus Torvalds } 2683ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 26841da177e4SLinus Torvalds 2685cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2686cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2687cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2688cfcea3e8STrond Myklebust 2689a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 26903a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 26913a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 26923a505845STrond Myklebust 26931c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 26941c3c07e9STrond Myklebust { 26956238aec8SNeilBrown put_group_info(entry->group_info); 2696f682a398SNeilBrown kfree_rcu(entry, rcu_head); 26974e857c58SPeter Zijlstra smp_mb__before_atomic(); 2698cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 26994e857c58SPeter Zijlstra smp_mb__after_atomic(); 27001c3c07e9STrond Myklebust } 27011c3c07e9STrond Myklebust 27021a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 27031a81bb8aSTrond Myklebust { 27041a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 27051a81bb8aSTrond Myklebust 27061a81bb8aSTrond Myklebust while (!list_empty(head)) { 27071a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 27081a81bb8aSTrond Myklebust list_del(&cache->lru); 27091a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 27101a81bb8aSTrond Myklebust } 27111a81bb8aSTrond Myklebust } 27121a81bb8aSTrond Myklebust 27133a505845STrond Myklebust static unsigned long 27143a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2715979df72eSTrond Myklebust { 2716979df72eSTrond Myklebust LIST_HEAD(head); 2717aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2718979df72eSTrond Myklebust struct nfs_access_entry *cache; 27191ab6c499SDave Chinner long freed = 0; 2720979df72eSTrond Myklebust 2721a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2722aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2723979df72eSTrond Myklebust struct inode *inode; 2724979df72eSTrond Myklebust 2725979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2726979df72eSTrond Myklebust break; 27279c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2728979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2729979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2730979df72eSTrond Myklebust goto remove_lru_entry; 2731979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2732979df72eSTrond Myklebust struct nfs_access_entry, lru); 2733979df72eSTrond Myklebust list_move(&cache->lru, &head); 2734979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 27351ab6c499SDave Chinner freed++; 2736979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2737979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2738979df72eSTrond Myklebust &nfs_access_lru_list); 2739979df72eSTrond Myklebust else { 2740979df72eSTrond Myklebust remove_lru_entry: 2741979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 27424e857c58SPeter Zijlstra smp_mb__before_atomic(); 2743979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 27444e857c58SPeter Zijlstra smp_mb__after_atomic(); 2745979df72eSTrond Myklebust } 274659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2747979df72eSTrond Myklebust } 2748979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 27491a81bb8aSTrond Myklebust nfs_access_free_list(&head); 27501ab6c499SDave Chinner return freed; 27511ab6c499SDave Chinner } 27521ab6c499SDave Chinner 27531ab6c499SDave Chinner unsigned long 27543a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 27553a505845STrond Myklebust { 27563a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 27573a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 27583a505845STrond Myklebust 27593a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 27603a505845STrond Myklebust return SHRINK_STOP; 27613a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 27623a505845STrond Myklebust } 27633a505845STrond Myklebust 27643a505845STrond Myklebust 27653a505845STrond Myklebust unsigned long 27661ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 27671ab6c499SDave Chinner { 276855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2769979df72eSTrond Myklebust } 2770979df72eSTrond Myklebust 27713a505845STrond Myklebust static void 27723a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 27733a505845STrond Myklebust { 27743a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 27753a505845STrond Myklebust unsigned long diff; 27763a505845STrond Myklebust unsigned int nr_to_scan; 27773a505845STrond Myklebust 27783a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 27793a505845STrond Myklebust return; 27803a505845STrond Myklebust nr_to_scan = 100; 27813a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 27823a505845STrond Myklebust if (diff < nr_to_scan) 27833a505845STrond Myklebust nr_to_scan = diff; 27843a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 27853a505845STrond Myklebust } 27863a505845STrond Myklebust 27871a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 27881c3c07e9STrond Myklebust { 27891c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 27901a81bb8aSTrond Myklebust struct rb_node *n; 27911c3c07e9STrond Myklebust struct nfs_access_entry *entry; 27921c3c07e9STrond Myklebust 27931c3c07e9STrond Myklebust /* Unhook entries from the cache */ 27941c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 27951c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 27961c3c07e9STrond Myklebust rb_erase(n, root_node); 27971a81bb8aSTrond Myklebust list_move(&entry->lru, head); 27981c3c07e9STrond Myklebust } 27991c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 28001c3c07e9STrond Myklebust } 28011c3c07e9STrond Myklebust 28021c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 28031c3c07e9STrond Myklebust { 28041a81bb8aSTrond Myklebust LIST_HEAD(head); 28051a81bb8aSTrond Myklebust 28061a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 28071a81bb8aSTrond Myklebust return; 2808cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2809cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 28101a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2811cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2812cfcea3e8STrond Myklebust 28131c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 28141a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 28151a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 28161a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 28171a81bb8aSTrond Myklebust nfs_access_free_list(&head); 28181c3c07e9STrond Myklebust } 28191c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 28201c3c07e9STrond Myklebust 28216238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b) 28226238aec8SNeilBrown { 28236238aec8SNeilBrown struct group_info *ga, *gb; 28246238aec8SNeilBrown int g; 28256238aec8SNeilBrown 28266238aec8SNeilBrown if (uid_lt(a->fsuid, b->fsuid)) 28276238aec8SNeilBrown return -1; 28286238aec8SNeilBrown if (uid_gt(a->fsuid, b->fsuid)) 28296238aec8SNeilBrown return 1; 28306238aec8SNeilBrown 28316238aec8SNeilBrown if (gid_lt(a->fsgid, b->fsgid)) 28326238aec8SNeilBrown return -1; 28336238aec8SNeilBrown if (gid_gt(a->fsgid, b->fsgid)) 28346238aec8SNeilBrown return 1; 28356238aec8SNeilBrown 28366238aec8SNeilBrown ga = a->group_info; 28376238aec8SNeilBrown gb = b->group_info; 28386238aec8SNeilBrown if (ga == gb) 28396238aec8SNeilBrown return 0; 28406238aec8SNeilBrown if (ga == NULL) 28416238aec8SNeilBrown return -1; 28426238aec8SNeilBrown if (gb == NULL) 28436238aec8SNeilBrown return 1; 28446238aec8SNeilBrown if (ga->ngroups < gb->ngroups) 28456238aec8SNeilBrown return -1; 28466238aec8SNeilBrown if (ga->ngroups > gb->ngroups) 28476238aec8SNeilBrown return 1; 28486238aec8SNeilBrown 28496238aec8SNeilBrown for (g = 0; g < ga->ngroups; g++) { 28506238aec8SNeilBrown if (gid_lt(ga->gid[g], gb->gid[g])) 28516238aec8SNeilBrown return -1; 28526238aec8SNeilBrown if (gid_gt(ga->gid[g], gb->gid[g])) 28536238aec8SNeilBrown return 1; 28546238aec8SNeilBrown } 28556238aec8SNeilBrown return 0; 28566238aec8SNeilBrown } 28576238aec8SNeilBrown 2858b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 28591c3c07e9STrond Myklebust { 28601c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 28611c3c07e9STrond Myklebust 28621c3c07e9STrond Myklebust while (n != NULL) { 2863b68572e0SNeilBrown struct nfs_access_entry *entry = 2864b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 28656238aec8SNeilBrown int cmp = access_cmp(cred, entry); 28661c3c07e9STrond Myklebust 2867b68572e0SNeilBrown if (cmp < 0) 28681c3c07e9STrond Myklebust n = n->rb_left; 2869b68572e0SNeilBrown else if (cmp > 0) 28701c3c07e9STrond Myklebust n = n->rb_right; 28711c3c07e9STrond Myklebust else 28721c3c07e9STrond Myklebust return entry; 28731c3c07e9STrond Myklebust } 28741c3c07e9STrond Myklebust return NULL; 28751c3c07e9STrond Myklebust } 28761c3c07e9STrond Myklebust 2877b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block) 28781da177e4SLinus Torvalds { 287955296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 28801c3c07e9STrond Myklebust struct nfs_access_entry *cache; 288157b69181STrond Myklebust bool retry = true; 288257b69181STrond Myklebust int err; 28831da177e4SLinus Torvalds 28841c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 288557b69181STrond Myklebust for(;;) { 28861c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 28871c3c07e9STrond Myklebust goto out_zap; 28881c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 288957b69181STrond Myklebust err = -ENOENT; 28901c3c07e9STrond Myklebust if (cache == NULL) 28911c3c07e9STrond Myklebust goto out; 289257b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 289321c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 289457b69181STrond Myklebust break; 28955c965db8STrond Myklebust if (!retry) 28965c965db8STrond Myklebust break; 289757b69181STrond Myklebust err = -ECHILD; 289857b69181STrond Myklebust if (!may_block) 289957b69181STrond Myklebust goto out; 290057b69181STrond Myklebust spin_unlock(&inode->i_lock); 290157b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 290257b69181STrond Myklebust if (err) 290357b69181STrond Myklebust return err; 290457b69181STrond Myklebust spin_lock(&inode->i_lock); 290557b69181STrond Myklebust retry = false; 290657b69181STrond Myklebust } 2907b5e7b59cSNeilBrown *mask = cache->mask; 2908cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 29091c3c07e9STrond Myklebust err = 0; 29101c3c07e9STrond Myklebust out: 29111c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29121c3c07e9STrond Myklebust return err; 29131c3c07e9STrond Myklebust out_zap: 29141a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 29151a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 29161c3c07e9STrond Myklebust return -ENOENT; 29171c3c07e9STrond Myklebust } 29181c3c07e9STrond Myklebust 2919b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask) 2920f682a398SNeilBrown { 2921f682a398SNeilBrown /* Only check the most recently returned cache entry, 2922f682a398SNeilBrown * but do it without locking. 2923f682a398SNeilBrown */ 2924f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2925f682a398SNeilBrown struct nfs_access_entry *cache; 2926f682a398SNeilBrown int err = -ECHILD; 2927f682a398SNeilBrown struct list_head *lh; 2928f682a398SNeilBrown 2929f682a398SNeilBrown rcu_read_lock(); 2930f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2931f682a398SNeilBrown goto out; 29329f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2933f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2934f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 29356238aec8SNeilBrown access_cmp(cred, cache) != 0) 2936f682a398SNeilBrown cache = NULL; 2937f682a398SNeilBrown if (cache == NULL) 2938f682a398SNeilBrown goto out; 293921c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2940f682a398SNeilBrown goto out; 2941b5e7b59cSNeilBrown *mask = cache->mask; 294221c3ba7eSTrond Myklebust err = 0; 2943f682a398SNeilBrown out: 2944f682a398SNeilBrown rcu_read_unlock(); 2945f682a398SNeilBrown return err; 2946f682a398SNeilBrown } 2947f682a398SNeilBrown 2948b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred, 2949b5e7b59cSNeilBrown u32 *mask, bool may_block) 2950d2ae4f8bSFrank van der Linden { 2951d2ae4f8bSFrank van der Linden int status; 2952d2ae4f8bSFrank van der Linden 2953b5e7b59cSNeilBrown status = nfs_access_get_cached_rcu(inode, cred, mask); 2954d2ae4f8bSFrank van der Linden if (status != 0) 2955b5e7b59cSNeilBrown status = nfs_access_get_cached_locked(inode, cred, mask, 2956d2ae4f8bSFrank van der Linden may_block); 2957d2ae4f8bSFrank van der Linden 2958d2ae4f8bSFrank van der Linden return status; 2959d2ae4f8bSFrank van der Linden } 2960d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2961d2ae4f8bSFrank van der Linden 296273fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode, 296373fbb3faSNeilBrown struct nfs_access_entry *set, 296473fbb3faSNeilBrown const struct cred *cred) 29651c3c07e9STrond Myklebust { 2966cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2967cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 29681c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 29691c3c07e9STrond Myklebust struct rb_node *parent = NULL; 29701c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2971b68572e0SNeilBrown int cmp; 29721c3c07e9STrond Myklebust 29731c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 29741c3c07e9STrond Myklebust while (*p != NULL) { 29751c3c07e9STrond Myklebust parent = *p; 29761c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 29776238aec8SNeilBrown cmp = access_cmp(cred, entry); 29781c3c07e9STrond Myklebust 2979b68572e0SNeilBrown if (cmp < 0) 29801c3c07e9STrond Myklebust p = &parent->rb_left; 2981b68572e0SNeilBrown else if (cmp > 0) 29821c3c07e9STrond Myklebust p = &parent->rb_right; 29831c3c07e9STrond Myklebust else 29841c3c07e9STrond Myklebust goto found; 29851c3c07e9STrond Myklebust } 29861c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 29871c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2988cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 29891c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29901c3c07e9STrond Myklebust return; 29911c3c07e9STrond Myklebust found: 29921c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2993cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2994cfcea3e8STrond Myklebust list_del(&entry->lru); 29951c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 29961c3c07e9STrond Myklebust nfs_access_free_entry(entry); 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 299973fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set, 300073fbb3faSNeilBrown const struct cred *cred) 30011da177e4SLinus Torvalds { 30021c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 30031c3c07e9STrond Myklebust if (cache == NULL) 30041c3c07e9STrond Myklebust return; 30051c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 30066238aec8SNeilBrown cache->fsuid = cred->fsuid; 30076238aec8SNeilBrown cache->fsgid = cred->fsgid; 30086238aec8SNeilBrown cache->group_info = get_group_info(cred->group_info); 30091da177e4SLinus Torvalds cache->mask = set->mask; 30101c3c07e9STrond Myklebust 3011f682a398SNeilBrown /* The above field assignments must be visible 3012f682a398SNeilBrown * before this item appears on the lru. We cannot easily 3013f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 3014f682a398SNeilBrown */ 3015f682a398SNeilBrown smp_wmb(); 301673fbb3faSNeilBrown nfs_access_add_rbtree(inode, cache, cred); 3017cfcea3e8STrond Myklebust 3018cfcea3e8STrond Myklebust /* Update accounting */ 30194e857c58SPeter Zijlstra smp_mb__before_atomic(); 3020cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 30214e857c58SPeter Zijlstra smp_mb__after_atomic(); 3022cfcea3e8STrond Myklebust 3023cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 30241a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 3025cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 30261a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 30271a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 30281a81bb8aSTrond Myklebust &nfs_access_lru_list); 3029cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 3030cfcea3e8STrond Myklebust } 30313a505845STrond Myklebust nfs_access_cache_enforce_limit(); 30321da177e4SLinus Torvalds } 30336168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 30346168f62cSWeston Andros Adamson 30353c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 30363c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 30373c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 30383c181827SAnna Schumaker NFS_ACCESS_DELETE) 30393c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 30403c181827SAnna Schumaker NFS_ACCESS_EXTEND) 3041ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 30423c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 30433c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 304415d4b73aSTrond Myklebust static int 3045ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 304615d4b73aSTrond Myklebust { 304715d4b73aSTrond Myklebust int mask = 0; 304815d4b73aSTrond Myklebust 304915d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 305015d4b73aSTrond Myklebust mask |= MAY_READ; 3051ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 3052ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 305315d4b73aSTrond Myklebust mask |= MAY_WRITE; 3054ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 305515d4b73aSTrond Myklebust mask |= MAY_EXEC; 3056ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 3057ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 3058ecbb903cSTrond Myklebust mask |= MAY_WRITE; 3059ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 306015d4b73aSTrond Myklebust mask |= MAY_EXEC; 3061ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 3062ecbb903cSTrond Myklebust mask |= MAY_WRITE; 306315d4b73aSTrond Myklebust return mask; 306415d4b73aSTrond Myklebust } 306515d4b73aSTrond Myklebust 30666168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 30676168f62cSWeston Andros Adamson { 3068bd8b2441STrond Myklebust entry->mask = access_result; 30696168f62cSWeston Andros Adamson } 30706168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 30711da177e4SLinus Torvalds 3072b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 30731da177e4SLinus Torvalds { 30741da177e4SLinus Torvalds struct nfs_access_entry cache; 307557b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 3076e8194b7dSTrond Myklebust int cache_mask = -1; 30771da177e4SLinus Torvalds int status; 30781da177e4SLinus Torvalds 3079f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 3080f4ce1299STrond Myklebust 3081b5e7b59cSNeilBrown status = nfs_access_get_cached(inode, cred, &cache.mask, may_block); 30821da177e4SLinus Torvalds if (status == 0) 3083f4ce1299STrond Myklebust goto out_cached; 30841da177e4SLinus Torvalds 3085f3324a2aSNeilBrown status = -ECHILD; 308657b69181STrond Myklebust if (!may_block) 3087f3324a2aSNeilBrown goto out; 3088f3324a2aSNeilBrown 30891750d929SAnna Schumaker /* 30901750d929SAnna Schumaker * Determine which access bits we want to ask for... 30911750d929SAnna Schumaker */ 309284631f84STrond Myklebust cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND | 309384631f84STrond Myklebust nfs_access_xattr_mask(NFS_SERVER(inode)); 30941750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 30951750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 30961750d929SAnna Schumaker else 30971750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 309873fbb3faSNeilBrown status = NFS_PROTO(inode)->access(inode, &cache, cred); 3099a71ee337SSuresh Jayaraman if (status != 0) { 3100a71ee337SSuresh Jayaraman if (status == -ESTALE) { 3101a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 310293ce4af7STrond Myklebust nfs_set_inode_stale(inode); 310393ce4af7STrond Myklebust else 310493ce4af7STrond Myklebust nfs_zap_caches(inode); 3105a71ee337SSuresh Jayaraman } 3106f4ce1299STrond Myklebust goto out; 3107a71ee337SSuresh Jayaraman } 310873fbb3faSNeilBrown nfs_access_add_cache(inode, &cache, cred); 3109f4ce1299STrond Myklebust out_cached: 3110ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 3111bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 3112f4ce1299STrond Myklebust status = -EACCES; 31131da177e4SLinus Torvalds out: 3114e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 3115f4ce1299STrond Myklebust return status; 31161da177e4SLinus Torvalds } 31171da177e4SLinus Torvalds 3118af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 3119af22f94aSTrond Myklebust { 3120af22f94aSTrond Myklebust int mask = 0; 3121af22f94aSTrond Myklebust 3122f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 3123f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 3124f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 3125f8d9a897SWeston Andros Adamson } else { 31268a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 3127af22f94aSTrond Myklebust mask |= MAY_READ; 31288a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 3129af22f94aSTrond Myklebust mask |= MAY_WRITE; 3130f8d9a897SWeston Andros Adamson } 3131f8d9a897SWeston Andros Adamson 3132af22f94aSTrond Myklebust return mask; 3133af22f94aSTrond Myklebust } 3134af22f94aSTrond Myklebust 3135b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 3136af22f94aSTrond Myklebust { 3137af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 3138af22f94aSTrond Myklebust } 313989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 3140af22f94aSTrond Myklebust 31415c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 31425c5fc09aSTrond Myklebust { 31435c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 314421c3ba7eSTrond Myklebust int ret = 0; 31455c5fc09aSTrond Myklebust 31463825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 31473825827eSTrond Myklebust return 0; 3148720869ebSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { 31495c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 315021c3ba7eSTrond Myklebust return -ECHILD; 315121c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 315221c3ba7eSTrond Myklebust } 31535c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 31545c5fc09aSTrond Myklebust ret = -EACCES; 31555c5fc09aSTrond Myklebust return ret; 31565c5fc09aSTrond Myklebust } 31575c5fc09aSTrond Myklebust 3158549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns, 3159549c7297SChristian Brauner struct inode *inode, 3160549c7297SChristian Brauner int mask) 31611da177e4SLinus Torvalds { 3162b68572e0SNeilBrown const struct cred *cred = current_cred(); 31631da177e4SLinus Torvalds int res = 0; 31641da177e4SLinus Torvalds 316591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 316691d5b470SChuck Lever 3167e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 31681da177e4SLinus Torvalds goto out; 31691da177e4SLinus Torvalds /* Is this sys_access() ? */ 31709cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 31711da177e4SLinus Torvalds goto force_lookup; 31721da177e4SLinus Torvalds 31731da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 31741da177e4SLinus Torvalds case S_IFLNK: 31751da177e4SLinus Torvalds goto out; 31761da177e4SLinus Torvalds case S_IFREG: 3177762674f8STrond Myklebust if ((mask & MAY_OPEN) && 3178762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 3179762674f8STrond Myklebust return 0; 31801da177e4SLinus Torvalds break; 31811da177e4SLinus Torvalds case S_IFDIR: 31821da177e4SLinus Torvalds /* 31831da177e4SLinus Torvalds * Optimize away all write operations, since the server 31841da177e4SLinus Torvalds * will check permissions when we perform the op. 31851da177e4SLinus Torvalds */ 31861da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 31871da177e4SLinus Torvalds goto out; 31881da177e4SLinus Torvalds } 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds force_lookup: 31911da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 31921da177e4SLinus Torvalds goto out_notsup; 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 31951da177e4SLinus Torvalds out: 31965c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 31975c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 3198f696a365SMiklos Szeredi 31991e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 32001e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 32011da177e4SLinus Torvalds return res; 32021da177e4SLinus Torvalds out_notsup: 3203d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 3204d51ac1a8SNeilBrown return -ECHILD; 3205d51ac1a8SNeilBrown 3206720869ebSTrond Myklebust res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE | 3207720869ebSTrond Myklebust NFS_INO_INVALID_OTHER); 32081da177e4SLinus Torvalds if (res == 0) 320947291baaSChristian Brauner res = generic_permission(&init_user_ns, inode, mask); 32101e7cb3dcSChuck Lever goto out; 32111da177e4SLinus Torvalds } 3212ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 3213