1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/dir.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * nfs directory handling functions 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * 10 Apr 1996 Added silly rename for unlink --okir 101da177e4SLinus Torvalds * 28 Sep 1996 Improved directory cache --okir 111da177e4SLinus Torvalds * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de 121da177e4SLinus Torvalds * Re-implemented silly rename for unlink, newly implemented 131da177e4SLinus Torvalds * silly rename for nfs_rename() following the suggestions 141da177e4SLinus Torvalds * of Olaf Kirch (okir) found in this file. 151da177e4SLinus Torvalds * Following Linus comments on my original hack, this version 161da177e4SLinus Torvalds * depends only on the dcache stuff and doesn't touch the inode 171da177e4SLinus Torvalds * layer (iput() and friends). 181da177e4SLinus Torvalds * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 21ddda8e0aSBryan Schumaker #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/errno.h> 241da177e4SLinus Torvalds #include <linux/stat.h> 251da177e4SLinus Torvalds #include <linux/fcntl.h> 261da177e4SLinus Torvalds #include <linux/string.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/slab.h> 291da177e4SLinus Torvalds #include <linux/mm.h> 301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 311da177e4SLinus Torvalds #include <linux/nfs_fs.h> 321da177e4SLinus Torvalds #include <linux/nfs_mount.h> 331da177e4SLinus Torvalds #include <linux/pagemap.h> 34873101b3SChuck Lever #include <linux/pagevec.h> 351da177e4SLinus Torvalds #include <linux/namei.h> 3654ceac45SDavid Howells #include <linux/mount.h> 37a0b8cab3SMel Gorman #include <linux/swap.h> 38e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include "delegation.h" 4391d5b470SChuck Lever #include "iostat.h" 444c30d56eSAdrian Bunk #include "internal.h" 45cd9a1c0eSTrond Myklebust #include "fscache.h" 461da177e4SLinus Torvalds 47f4ce1299STrond Myklebust #include "nfstrace.h" 48f4ce1299STrond Myklebust 491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 571da177e4SLinus Torvalds 584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 59f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 601da177e4SLinus Torvalds .read = generic_read_dir, 6193a6ab7bSTrond Myklebust .iterate_shared = nfs_readdir, 621da177e4SLinus Torvalds .open = nfs_opendir, 63480c2006SBryan Schumaker .release = nfs_closedir, 641da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 651da177e4SLinus Torvalds }; 661da177e4SLinus Torvalds 6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6811de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 69d1bacf9eSBryan Schumaker }; 70d1bacf9eSBryan Schumaker 7193b8959aSTrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir) 72480c2006SBryan Schumaker { 73311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 74480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 75480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 76480c2006SBryan Schumaker if (ctx != NULL) { 778ef2ce3eSBryan Schumaker ctx->duped = 0; 78311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 79480c2006SBryan Schumaker ctx->dir_cookie = 0; 808ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 81ff81dfb5STrond Myklebust ctx->page_index = 0; 82311324adSTrond Myklebust spin_lock(&dir->i_lock); 831c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 841c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 85ac46b3d7STrond Myklebust nfs_set_cache_invalid(dir, 86ac46b3d7STrond Myklebust NFS_INO_INVALID_DATA | 87ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 88311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 89ff81dfb5STrond Myklebust clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags); 90311324adSTrond Myklebust spin_unlock(&dir->i_lock); 91480c2006SBryan Schumaker return ctx; 92480c2006SBryan Schumaker } 930c030806STrond Myklebust return ERR_PTR(-ENOMEM); 940c030806STrond Myklebust } 95480c2006SBryan Schumaker 96311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 97480c2006SBryan Schumaker { 98311324adSTrond Myklebust spin_lock(&dir->i_lock); 99311324adSTrond Myklebust list_del(&ctx->list); 100311324adSTrond Myklebust spin_unlock(&dir->i_lock); 101480c2006SBryan Schumaker kfree(ctx); 102480c2006SBryan Schumaker } 103480c2006SBryan Schumaker 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * Open file 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds static int 1081da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1091da177e4SLinus Torvalds { 110480c2006SBryan Schumaker int res = 0; 111480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1121da177e4SLinus Torvalds 1136de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 114cc0dd2d1SChuck Lever 115cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1161e7cb3dcSChuck Lever 11793b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode); 118480c2006SBryan Schumaker if (IS_ERR(ctx)) { 119480c2006SBryan Schumaker res = PTR_ERR(ctx); 120480c2006SBryan Schumaker goto out; 121480c2006SBryan Schumaker } 122480c2006SBryan Schumaker filp->private_data = ctx; 123480c2006SBryan Schumaker out: 1241da177e4SLinus Torvalds return res; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127480c2006SBryan Schumaker static int 128480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 129480c2006SBryan Schumaker { 130a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 131480c2006SBryan Schumaker return 0; 132480c2006SBryan Schumaker } 133480c2006SBryan Schumaker 134d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 135d1bacf9eSBryan Schumaker u64 cookie; 136d1bacf9eSBryan Schumaker u64 ino; 137a52a8a6aSTrond Myklebust const char *name; 138a52a8a6aSTrond Myklebust unsigned int name_len; 1390b26a0bfSTrond Myklebust unsigned char d_type; 140d1bacf9eSBryan Schumaker }; 141d1bacf9eSBryan Schumaker 142d1bacf9eSBryan Schumaker struct nfs_cache_array { 143d1bacf9eSBryan Schumaker u64 last_cookie; 144b1e21c97STrond Myklebust unsigned int size; 145b1e21c97STrond Myklebust unsigned char page_full : 1, 146762567b7STrond Myklebust page_is_eof : 1, 147762567b7STrond Myklebust cookies_are_ordered : 1; 1485601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 149d1bacf9eSBryan Schumaker }; 150d1bacf9eSBryan Schumaker 1516c981effSTrond Myklebust struct nfs_readdir_descriptor { 1521da177e4SLinus Torvalds struct file *file; 1531da177e4SLinus Torvalds struct page *page; 15423db8620SAl Viro struct dir_context *ctx; 1551f1d4aa4STrond Myklebust pgoff_t page_index; 1562e7a4641STrond Myklebust u64 dir_cookie; 1570aded708STrond Myklebust u64 last_cookie; 1582e7a4641STrond Myklebust u64 dup_cookie; 159f0dd2136STrond Myklebust loff_t current_index; 16059e356a9STrond Myklebust loff_t prev_index; 161d1bacf9eSBryan Schumaker 162b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 163a1147b82STrond Myklebust unsigned long dir_verifier; 1641f4eab7eSNeil Brown unsigned long timestamp; 1654704f0e2STrond Myklebust unsigned long gencount; 1662e7a4641STrond Myklebust unsigned long attr_gencount; 167d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 1682e7a4641STrond Myklebust signed char duped; 169a7a3b1e9SBenjamin Coddington bool plus; 170a7a3b1e9SBenjamin Coddington bool eof; 1716c981effSTrond Myklebust }; 1721da177e4SLinus Torvalds 1731f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array) 1741f1d4aa4STrond Myklebust { 1751f1d4aa4STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1761f1d4aa4STrond Myklebust } 1771f1d4aa4STrond Myklebust 1781f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie) 1794b310319STrond Myklebust { 1804b310319STrond Myklebust struct nfs_cache_array *array; 1814b310319STrond Myklebust 1824b310319STrond Myklebust array = kmap_atomic(page); 1831f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 1841f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 185762567b7STrond Myklebust array->cookies_are_ordered = 1; 1864b310319STrond Myklebust kunmap_atomic(array); 1874b310319STrond Myklebust } 1884b310319STrond Myklebust 189d1bacf9eSBryan Schumaker /* 190d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 191d1bacf9eSBryan Schumaker */ 192d1bacf9eSBryan Schumaker static 19311de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 194d1bacf9eSBryan Schumaker { 19511de3b11STrond Myklebust struct nfs_cache_array *array; 196d1bacf9eSBryan Schumaker int i; 1978cd51a0cSTrond Myklebust 1982b86ce2dSCong Wang array = kmap_atomic(page); 199d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 200a52a8a6aSTrond Myklebust kfree(array->array[i].name); 2011f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 2022b86ce2dSCong Wang kunmap_atomic(array); 203d1bacf9eSBryan Schumaker } 204d1bacf9eSBryan Schumaker 20535df59d3STrond Myklebust static struct page * 20635df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags) 20735df59d3STrond Myklebust { 20835df59d3STrond Myklebust struct page *page = alloc_page(gfp_flags); 20935df59d3STrond Myklebust if (page) 21035df59d3STrond Myklebust nfs_readdir_page_init_array(page, last_cookie); 21135df59d3STrond Myklebust return page; 21235df59d3STrond Myklebust } 21335df59d3STrond Myklebust 21435df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page) 21535df59d3STrond Myklebust { 21635df59d3STrond Myklebust if (page) { 21735df59d3STrond Myklebust nfs_readdir_clear_array(page); 21835df59d3STrond Myklebust put_page(page); 21935df59d3STrond Myklebust } 22035df59d3STrond Myklebust } 22135df59d3STrond Myklebust 222b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 223b1e21c97STrond Myklebust { 224b1e21c97STrond Myklebust array->page_is_eof = 1; 225b1e21c97STrond Myklebust array->page_full = 1; 226b1e21c97STrond Myklebust } 227b1e21c97STrond Myklebust 228b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 229b1e21c97STrond Myklebust { 230b1e21c97STrond Myklebust return array->page_full; 231b1e21c97STrond Myklebust } 232b1e21c97STrond Myklebust 233d1bacf9eSBryan Schumaker /* 234d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 235d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 236d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 237d1bacf9eSBryan Schumaker */ 238a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 239d1bacf9eSBryan Schumaker { 240a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 241a52a8a6aSTrond Myklebust 24204e4bd1cSCatalin Marinas /* 24304e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 24404e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 24504e4bd1cSCatalin Marinas */ 246a52a8a6aSTrond Myklebust if (ret != NULL) 247a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 248a52a8a6aSTrond Myklebust return ret; 249d1bacf9eSBryan Schumaker } 250d1bacf9eSBryan Schumaker 251b1e21c97STrond Myklebust /* 252b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 253b1e21c97STrond Myklebust */ 254b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 255b1e21c97STrond Myklebust { 256b1e21c97STrond Myklebust struct nfs_cache_array_entry *cache_entry; 257b1e21c97STrond Myklebust 258b1e21c97STrond Myklebust if (array->page_full) 259b1e21c97STrond Myklebust return -ENOSPC; 260b1e21c97STrond Myklebust cache_entry = &array->array[array->size + 1]; 261b1e21c97STrond Myklebust if ((char *)cache_entry - (char *)array > PAGE_SIZE) { 262b1e21c97STrond Myklebust array->page_full = 1; 263b1e21c97STrond Myklebust return -ENOSPC; 264b1e21c97STrond Myklebust } 265d1bacf9eSBryan Schumaker return 0; 266d1bacf9eSBryan Schumaker } 267d1bacf9eSBryan Schumaker 268d1bacf9eSBryan Schumaker static 269d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 270d1bacf9eSBryan Schumaker { 271a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 2724a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 273a52a8a6aSTrond Myklebust const char *name; 2744a201d6eSTrond Myklebust int ret; 2754a201d6eSTrond Myklebust 276a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 277a52a8a6aSTrond Myklebust if (!name) 278a52a8a6aSTrond Myklebust return -ENOMEM; 2793020093fSTrond Myklebust 280a52a8a6aSTrond Myklebust array = kmap_atomic(page); 281b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 282a52a8a6aSTrond Myklebust if (ret) { 283a52a8a6aSTrond Myklebust kfree(name); 2843020093fSTrond Myklebust goto out; 285a52a8a6aSTrond Myklebust } 2863020093fSTrond Myklebust 287b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 2884a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2894a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2900b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 291a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 292a52a8a6aSTrond Myklebust cache_entry->name = name; 293d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 294762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie) 295762567b7STrond Myklebust array->cookies_are_ordered = 0; 2968cd51a0cSTrond Myklebust array->size++; 29747c716cbSTrond Myklebust if (entry->eof != 0) 298b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 2994a201d6eSTrond Myklebust out: 300a52a8a6aSTrond Myklebust kunmap_atomic(array); 3014a201d6eSTrond Myklebust return ret; 302d1bacf9eSBryan Schumaker } 303d1bacf9eSBryan Schumaker 3041f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 3051f1d4aa4STrond Myklebust pgoff_t index, u64 last_cookie) 3061f1d4aa4STrond Myklebust { 3071f1d4aa4STrond Myklebust struct page *page; 3081f1d4aa4STrond Myklebust 3091f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 3101f1d4aa4STrond Myklebust if (page && !PageUptodate(page)) { 3111f1d4aa4STrond Myklebust nfs_readdir_page_init_array(page, last_cookie); 3121f1d4aa4STrond Myklebust if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0) 3131f1d4aa4STrond Myklebust nfs_zap_mapping(mapping->host, mapping); 3141f1d4aa4STrond Myklebust SetPageUptodate(page); 3151f1d4aa4STrond Myklebust } 3161f1d4aa4STrond Myklebust 3171f1d4aa4STrond Myklebust return page; 3181f1d4aa4STrond Myklebust } 3191f1d4aa4STrond Myklebust 3201f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 3211f1d4aa4STrond Myklebust { 3221f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3231f1d4aa4STrond Myklebust u64 ret; 3241f1d4aa4STrond Myklebust 3251f1d4aa4STrond Myklebust array = kmap_atomic(page); 3261f1d4aa4STrond Myklebust ret = array->last_cookie; 3271f1d4aa4STrond Myklebust kunmap_atomic(array); 3281f1d4aa4STrond Myklebust return ret; 3291f1d4aa4STrond Myklebust } 3301f1d4aa4STrond Myklebust 3311f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 3321f1d4aa4STrond Myklebust { 3331f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3341f1d4aa4STrond Myklebust bool ret; 3351f1d4aa4STrond Myklebust 3361f1d4aa4STrond Myklebust array = kmap_atomic(page); 3371f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 3381f1d4aa4STrond Myklebust kunmap_atomic(array); 3391f1d4aa4STrond Myklebust return ret; 3401f1d4aa4STrond Myklebust } 3411f1d4aa4STrond Myklebust 342b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 343b1e21c97STrond Myklebust { 344b1e21c97STrond Myklebust struct nfs_cache_array *array; 345b1e21c97STrond Myklebust 346b1e21c97STrond Myklebust array = kmap_atomic(page); 347b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 348b1e21c97STrond Myklebust kunmap_atomic(array); 349b1e21c97STrond Myklebust } 350b1e21c97STrond Myklebust 3513b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 3523b2a09f1STrond Myklebust { 3533b2a09f1STrond Myklebust unlock_page(page); 3543b2a09f1STrond Myklebust put_page(page); 3553b2a09f1STrond Myklebust } 3563b2a09f1STrond Myklebust 3573b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 3583b2a09f1STrond Myklebust pgoff_t index, u64 cookie) 3593b2a09f1STrond Myklebust { 3603b2a09f1STrond Myklebust struct page *page; 3613b2a09f1STrond Myklebust 3623b2a09f1STrond Myklebust page = nfs_readdir_page_get_locked(mapping, index, cookie); 3633b2a09f1STrond Myklebust if (page) { 3643b2a09f1STrond Myklebust if (nfs_readdir_page_last_cookie(page) == cookie) 3653b2a09f1STrond Myklebust return page; 3663b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 3673b2a09f1STrond Myklebust } 3683b2a09f1STrond Myklebust return NULL; 3693b2a09f1STrond Myklebust } 3703b2a09f1STrond Myklebust 37159e356a9STrond Myklebust static inline 37259e356a9STrond Myklebust int is_32bit_api(void) 37359e356a9STrond Myklebust { 37459e356a9STrond Myklebust #ifdef CONFIG_COMPAT 37559e356a9STrond Myklebust return in_compat_syscall(); 37659e356a9STrond Myklebust #else 37759e356a9STrond Myklebust return (BITS_PER_LONG == 32); 37859e356a9STrond Myklebust #endif 37959e356a9STrond Myklebust } 38059e356a9STrond Myklebust 38159e356a9STrond Myklebust static 38259e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 38359e356a9STrond Myklebust { 38459e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 38559e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 38659e356a9STrond Myklebust return false; 38759e356a9STrond Myklebust return true; 38859e356a9STrond Myklebust } 38959e356a9STrond Myklebust 3906c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 3916c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 392d1bacf9eSBryan Schumaker { 39323db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 394d1bacf9eSBryan Schumaker unsigned int index; 395d1bacf9eSBryan Schumaker 396d1bacf9eSBryan Schumaker if (diff < 0) 397d1bacf9eSBryan Schumaker goto out_eof; 398d1bacf9eSBryan Schumaker if (diff >= array->size) { 399b1e21c97STrond Myklebust if (array->page_is_eof) 400d1bacf9eSBryan Schumaker goto out_eof; 401d1bacf9eSBryan Schumaker return -EAGAIN; 402d1bacf9eSBryan Schumaker } 403d1bacf9eSBryan Schumaker 404d1bacf9eSBryan Schumaker index = (unsigned int)diff; 4052e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 406d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 407d1bacf9eSBryan Schumaker return 0; 408d1bacf9eSBryan Schumaker out_eof: 4096089dd0dSThomas Meyer desc->eof = true; 410d1bacf9eSBryan Schumaker return -EBADCOOKIE; 411d1bacf9eSBryan Schumaker } 412d1bacf9eSBryan Schumaker 4134db72b40SJeff Layton static bool 4144db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 4154db72b40SJeff Layton { 4162929bc33STrond Myklebust if (nfsi->cache_validity & (NFS_INO_INVALID_CHANGE | 4172929bc33STrond Myklebust NFS_INO_INVALID_DATA)) 4184db72b40SJeff Layton return false; 4194db72b40SJeff Layton smp_rmb(); 4204db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 4214db72b40SJeff Layton } 4224db72b40SJeff Layton 423762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 424762567b7STrond Myklebust u64 cookie) 425762567b7STrond Myklebust { 426762567b7STrond Myklebust if (!array->cookies_are_ordered) 427762567b7STrond Myklebust return true; 428762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */ 429762567b7STrond Myklebust if (cookie >= array->last_cookie) 430762567b7STrond Myklebust return false; 431762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie) 432762567b7STrond Myklebust return false; 433762567b7STrond Myklebust return true; 434762567b7STrond Myklebust } 435762567b7STrond Myklebust 4366c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 4376c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 438d1bacf9eSBryan Schumaker { 439d1bacf9eSBryan Schumaker int i; 4408ef2ce3eSBryan Schumaker loff_t new_pos; 441d1bacf9eSBryan Schumaker int status = -EAGAIN; 442d1bacf9eSBryan Schumaker 443762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 444762567b7STrond Myklebust goto check_eof; 445762567b7STrond Myklebust 446d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 4472e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 448496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 4490c030806STrond Myklebust 4508ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 4512e7a4641STrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount || 4524db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 4532e7a4641STrond Myklebust desc->duped = 0; 4542e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 45559e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 4562e7a4641STrond Myklebust if (desc->duped > 0 4572e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 4580c030806STrond Myklebust if (printk_ratelimit()) { 4596de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 4600c030806STrond Myklebust "Please contact your server vendor. " 461a52a8a6aSTrond Myklebust "The file: %s has duplicate cookie %llu\n", 462a52a8a6aSTrond Myklebust desc->file, array->array[i].name, desc->dir_cookie); 4630c030806STrond Myklebust } 4640c030806STrond Myklebust status = -ELOOP; 4650c030806STrond Myklebust goto out; 4660c030806STrond Myklebust } 4672e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 4682e7a4641STrond Myklebust desc->duped = -1; 4698ef2ce3eSBryan Schumaker } 47059e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 4712e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 47259e356a9STrond Myklebust else 47323db8620SAl Viro desc->ctx->pos = new_pos; 47459e356a9STrond Myklebust desc->prev_index = new_pos; 4758cd51a0cSTrond Myklebust desc->cache_entry_index = i; 47647c716cbSTrond Myklebust return 0; 4778cd51a0cSTrond Myklebust } 4788cd51a0cSTrond Myklebust } 479762567b7STrond Myklebust check_eof: 480b1e21c97STrond Myklebust if (array->page_is_eof) { 481d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 4822e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 4836089dd0dSThomas Meyer desc->eof = true; 484d1bacf9eSBryan Schumaker } 4850c030806STrond Myklebust out: 486d1bacf9eSBryan Schumaker return status; 487d1bacf9eSBryan Schumaker } 488d1bacf9eSBryan Schumaker 4896c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 490d1bacf9eSBryan Schumaker { 491d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 49247c716cbSTrond Myklebust int status; 493d1bacf9eSBryan Schumaker 494ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 495d1bacf9eSBryan Schumaker 4962e7a4641STrond Myklebust if (desc->dir_cookie == 0) 497d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 498d1bacf9eSBryan Schumaker else 499d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 500d1bacf9eSBryan Schumaker 50147c716cbSTrond Myklebust if (status == -EAGAIN) { 5020aded708STrond Myklebust desc->last_cookie = array->last_cookie; 503e47c085aSTrond Myklebust desc->current_index += array->size; 50447c716cbSTrond Myklebust desc->page_index++; 50547c716cbSTrond Myklebust } 506ed09222dSTrond Myklebust kunmap_atomic(array); 507d1bacf9eSBryan Schumaker return status; 508d1bacf9eSBryan Schumaker } 509d1bacf9eSBryan Schumaker 510d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 51193b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 512b593c09fSTrond Myklebust __be32 *verf, u64 cookie, 513b593c09fSTrond Myklebust struct page **pages, size_t bufsize, 514b593c09fSTrond Myklebust __be32 *verf_res) 515d1bacf9eSBryan Schumaker { 51682e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 51782e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 51882e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 51982e22a5eSTrond Myklebust .cred = desc->file->f_cred, 520b593c09fSTrond Myklebust .verf = verf, 52182e22a5eSTrond Myklebust .cookie = cookie, 52282e22a5eSTrond Myklebust .pages = pages, 52382e22a5eSTrond Myklebust .page_len = bufsize, 52482e22a5eSTrond Myklebust .plus = desc->plus, 52582e22a5eSTrond Myklebust }; 52682e22a5eSTrond Myklebust struct nfs_readdir_res res = { 52782e22a5eSTrond Myklebust .verf = verf_res, 52882e22a5eSTrond Myklebust }; 5294704f0e2STrond Myklebust unsigned long timestamp, gencount; 5301da177e4SLinus Torvalds int error; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds again: 5331da177e4SLinus Torvalds timestamp = jiffies; 5344704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 535a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 53682e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 5371da177e4SLinus Torvalds if (error < 0) { 5381da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 5391da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 5401da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 5413a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 54282e22a5eSTrond Myklebust desc->plus = arg.plus = false; 5431da177e4SLinus Torvalds goto again; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds goto error; 5461da177e4SLinus Torvalds } 5471f4eab7eSNeil Brown desc->timestamp = timestamp; 5484704f0e2STrond Myklebust desc->gencount = gencount; 549d1bacf9eSBryan Schumaker error: 550d1bacf9eSBryan Schumaker return error; 551d1bacf9eSBryan Schumaker } 552d1bacf9eSBryan Schumaker 5536c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 554573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 555d1bacf9eSBryan Schumaker { 55659e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 557573c4e1eSChuck Lever int error; 558d1bacf9eSBryan Schumaker 55959e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 560573c4e1eSChuck Lever if (error) 561573c4e1eSChuck Lever return error; 562d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 563d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 564d1bacf9eSBryan Schumaker return 0; 565d1bacf9eSBryan Schumaker } 566d1bacf9eSBryan Schumaker 567fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 568fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 569fa923369STrond Myklebust */ 570d39ab9deSBryan Schumaker static 571d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 572d39ab9deSBryan Schumaker { 573d8fdb47fSTrond Myklebust struct inode *inode; 574fa923369STrond Myklebust struct nfs_inode *nfsi; 575fa923369STrond Myklebust 5762b0143b5SDavid Howells if (d_really_is_negative(dentry)) 5772b0143b5SDavid Howells return 0; 578fa923369STrond Myklebust 579d8fdb47fSTrond Myklebust inode = d_inode(dentry); 580d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 581d8fdb47fSTrond Myklebust return 0; 582d8fdb47fSTrond Myklebust 583d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 5847dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 585d39ab9deSBryan Schumaker return 0; 5867dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 5877dc72d5fSTrond Myklebust return 0; 5887dc72d5fSTrond Myklebust return 1; 589d39ab9deSBryan Schumaker } 590d39ab9deSBryan Schumaker 591d39ab9deSBryan Schumaker static 59223db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 593d69ee9b8STrond Myklebust { 594d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 595d69ee9b8STrond Myklebust return false; 596d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 597d69ee9b8STrond Myklebust return true; 59823db8620SAl Viro if (ctx->pos == 0) 599d69ee9b8STrond Myklebust return true; 600d69ee9b8STrond Myklebust return false; 601d69ee9b8STrond Myklebust } 602d69ee9b8STrond Myklebust 603d69ee9b8STrond Myklebust /* 60463519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 60563519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 606d69ee9b8STrond Myklebust * directory. 607d69ee9b8STrond Myklebust */ 608d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 609d69ee9b8STrond Myklebust { 61063519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 61163519fbcSTrond Myklebust 61263519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 61363519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 61463519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 615d69ee9b8STrond Myklebust } 616d69ee9b8STrond Myklebust 617311324adSTrond Myklebust /* 618311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 619311324adSTrond Myklebust * 620311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 621311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 622311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 623311324adSTrond Myklebust * cache flush. 624311324adSTrond Myklebust */ 625311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 626311324adSTrond Myklebust { 62763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 62863519fbcSTrond Myklebust 62963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 63063519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 63163519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 632ff81dfb5STrond Myklebust set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags); 633311324adSTrond Myklebust } 634311324adSTrond Myklebust } 635311324adSTrond Myklebust 636d69ee9b8STrond Myklebust static 637a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 638a1147b82STrond Myklebust unsigned long dir_verifier) 639d39ab9deSBryan Schumaker { 64026fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 6419ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 6424a201d6eSTrond Myklebust struct dentry *dentry; 6434a201d6eSTrond Myklebust struct dentry *alias; 644d39ab9deSBryan Schumaker struct inode *inode; 645aa9c2669SDavid Quigley int status; 646d39ab9deSBryan Schumaker 647fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 648fa923369STrond Myklebust return; 6496c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 6506c441c25STrond Myklebust return; 65178d04af4STrond Myklebust if (filename.len == 0) 65278d04af4STrond Myklebust return; 65378d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 65478d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 65578d04af4STrond Myklebust return; 65678d04af4STrond Myklebust /* ...or '/' */ 65778d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 65878d04af4STrond Myklebust return; 6594a201d6eSTrond Myklebust if (filename.name[0] == '.') { 6604a201d6eSTrond Myklebust if (filename.len == 1) 6614a201d6eSTrond Myklebust return; 6624a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 6634a201d6eSTrond Myklebust return; 6644a201d6eSTrond Myklebust } 6658387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 666d39ab9deSBryan Schumaker 6674a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 6689ac3d3e8SAl Viro again: 6699ac3d3e8SAl Viro if (!dentry) { 6709ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 6719ac3d3e8SAl Viro if (IS_ERR(dentry)) 6729ac3d3e8SAl Viro return; 6739ac3d3e8SAl Viro } 6749ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 6756c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 6766c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 6776c441c25STrond Myklebust &entry->fattr->fsid)) 6786c441c25STrond Myklebust goto out; 679d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 6807dc72d5fSTrond Myklebust if (!entry->fh->size) 6817dc72d5fSTrond Myklebust goto out; 682a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 6832b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 684aa9c2669SDavid Quigley if (!status) 685dd225cb3SAnna Schumaker nfs_setsecurity(d_inode(dentry), entry->fattr); 686d39ab9deSBryan Schumaker goto out; 687d39ab9deSBryan Schumaker } else { 6885542aa2fSEric W. Biederman d_invalidate(dentry); 689d39ab9deSBryan Schumaker dput(dentry); 6909ac3d3e8SAl Viro dentry = NULL; 6919ac3d3e8SAl Viro goto again; 692d39ab9deSBryan Schumaker } 693d39ab9deSBryan Schumaker } 6947dc72d5fSTrond Myklebust if (!entry->fh->size) { 6957dc72d5fSTrond Myklebust d_lookup_done(dentry); 6967dc72d5fSTrond Myklebust goto out; 6977dc72d5fSTrond Myklebust } 698d39ab9deSBryan Schumaker 699cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 70041d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 7019ac3d3e8SAl Viro d_lookup_done(dentry); 7029ac3d3e8SAl Viro if (alias) { 703d39ab9deSBryan Schumaker if (IS_ERR(alias)) 704d39ab9deSBryan Schumaker goto out; 7059ac3d3e8SAl Viro dput(dentry); 7069ac3d3e8SAl Viro dentry = alias; 7079ac3d3e8SAl Viro } 708a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 709d39ab9deSBryan Schumaker out: 710d39ab9deSBryan Schumaker dput(dentry); 711d39ab9deSBryan Schumaker } 712d39ab9deSBryan Schumaker 713d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 7143b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 7153b2a09f1STrond Myklebust struct nfs_entry *entry, 7163b2a09f1STrond Myklebust struct page **xdr_pages, 71735df59d3STrond Myklebust unsigned int buflen, 71835df59d3STrond Myklebust struct page **arrays, 71935df59d3STrond Myklebust size_t narrays) 720d1bacf9eSBryan Schumaker { 7213b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 722babddc72SBryan Schumaker struct xdr_stream stream; 723f7da7a12SBenny Halevy struct xdr_buf buf; 72435df59d3STrond Myklebust struct page *scratch, *new, *page = *arrays; 7255c346854STrond Myklebust int status; 726babddc72SBryan Schumaker 7276650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 7286650239aSTrond Myklebust if (scratch == NULL) 7296650239aSTrond Myklebust return -ENOMEM; 730babddc72SBryan Schumaker 731f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 7320ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch); 73399424380SBryan Schumaker 73499424380SBryan Schumaker do { 735b1db9a40SAnna Schumaker if (entry->fattr->label) 736b1db9a40SAnna Schumaker entry->fattr->label->len = NFS4_MAXLABELLEN; 737d33030e2SJeffrey Mitchell 73899424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 739972bcdf2STrond Myklebust if (status != 0) 74099424380SBryan Schumaker break; 7415c346854STrond Myklebust 742a7a3b1e9SBenjamin Coddington if (desc->plus) 743a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 744a1147b82STrond Myklebust desc->dir_verifier); 7458cd51a0cSTrond Myklebust 746db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 7473b2a09f1STrond Myklebust if (status != -ENOSPC) 7483b2a09f1STrond Myklebust continue; 74999424380SBryan Schumaker 75035df59d3STrond Myklebust if (page->mapping != mapping) { 75135df59d3STrond Myklebust if (!--narrays) 7523b2a09f1STrond Myklebust break; 75335df59d3STrond Myklebust new = nfs_readdir_page_array_alloc(entry->prev_cookie, 75435df59d3STrond Myklebust GFP_KERNEL); 75535df59d3STrond Myklebust if (!new) 75635df59d3STrond Myklebust break; 75735df59d3STrond Myklebust arrays++; 75835df59d3STrond Myklebust *arrays = page = new; 75935df59d3STrond Myklebust } else { 76035df59d3STrond Myklebust new = nfs_readdir_page_get_next(mapping, 76135df59d3STrond Myklebust page->index + 1, 7623b2a09f1STrond Myklebust entry->prev_cookie); 7633b2a09f1STrond Myklebust if (!new) 7643b2a09f1STrond Myklebust break; 76535df59d3STrond Myklebust if (page != *arrays) 7663b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7673b2a09f1STrond Myklebust page = new; 76856e4ebf8SBryan Schumaker } 7693b2a09f1STrond Myklebust status = nfs_readdir_add_to_array(entry, page); 770972bcdf2STrond Myklebust } while (!status && !entry->eof); 77156e4ebf8SBryan Schumaker 772972bcdf2STrond Myklebust switch (status) { 773972bcdf2STrond Myklebust case -EBADCOOKIE: 774972bcdf2STrond Myklebust if (entry->eof) { 775b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 7768cd51a0cSTrond Myklebust status = 0; 77756e4ebf8SBryan Schumaker } 778972bcdf2STrond Myklebust break; 779972bcdf2STrond Myklebust case -ENOSPC: 780972bcdf2STrond Myklebust case -EAGAIN: 781972bcdf2STrond Myklebust status = 0; 782972bcdf2STrond Myklebust break; 783972bcdf2STrond Myklebust } 7846650239aSTrond Myklebust 78535df59d3STrond Myklebust if (page != *arrays) 7863b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7876650239aSTrond Myklebust 7886650239aSTrond Myklebust put_page(scratch); 789d1bacf9eSBryan Schumaker return status; 79056e4ebf8SBryan Schumaker } 79156e4ebf8SBryan Schumaker 7921a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 79356e4ebf8SBryan Schumaker { 7941a34c8c9STrond Myklebust while (npages--) 7951a34c8c9STrond Myklebust put_page(pages[npages]); 7961a34c8c9STrond Myklebust kfree(pages); 79756e4ebf8SBryan Schumaker } 79856e4ebf8SBryan Schumaker 79956e4ebf8SBryan Schumaker /* 800bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 801bf211ca1Szhangliguang * to nfs_readdir_free_pages() 80256e4ebf8SBryan Schumaker */ 8031a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 80456e4ebf8SBryan Schumaker { 8051a34c8c9STrond Myklebust struct page **pages; 8061a34c8c9STrond Myklebust size_t i; 80756e4ebf8SBryan Schumaker 8081a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 8091a34c8c9STrond Myklebust if (!pages) 8101a34c8c9STrond Myklebust return NULL; 81156e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 81256e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 81356e4ebf8SBryan Schumaker if (page == NULL) 81456e4ebf8SBryan Schumaker goto out_freepages; 81556e4ebf8SBryan Schumaker pages[i] = page; 81656e4ebf8SBryan Schumaker } 8171a34c8c9STrond Myklebust return pages; 81856e4ebf8SBryan Schumaker 81956e4ebf8SBryan Schumaker out_freepages: 820c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 8211a34c8c9STrond Myklebust return NULL; 822d1bacf9eSBryan Schumaker } 823d1bacf9eSBryan Schumaker 8246c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 82535df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res, 82635df59d3STrond Myklebust struct page **arrays, size_t narrays) 827d1bacf9eSBryan Schumaker { 8281a34c8c9STrond Myklebust struct page **pages; 82935df59d3STrond Myklebust struct page *page = *arrays; 8306b75cf9eSTrond Myklebust struct nfs_entry *entry; 8311a34c8c9STrond Myklebust size_t array_size; 832b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file); 8331a34c8c9STrond Myklebust size_t dtsize = NFS_SERVER(inode)->dtsize; 8348cd51a0cSTrond Myklebust int status = -ENOMEM; 835d1bacf9eSBryan Schumaker 8366b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 8376b75cf9eSTrond Myklebust if (!entry) 8386b75cf9eSTrond Myklebust return -ENOMEM; 8396b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 8406b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 841b1db9a40SAnna Schumaker entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 8426b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 8436b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 844d1bacf9eSBryan Schumaker goto out; 845d1bacf9eSBryan Schumaker 8461a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 8471a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 8481a34c8c9STrond Myklebust if (!pages) 849b1db9a40SAnna Schumaker goto out; 850d1bacf9eSBryan Schumaker 851d1bacf9eSBryan Schumaker do { 852ac396128STrond Myklebust unsigned int pglen; 853b593c09fSTrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, 854b593c09fSTrond Myklebust pages, dtsize, 855b593c09fSTrond Myklebust verf_res); 856d1bacf9eSBryan Schumaker if (status < 0) 857d1bacf9eSBryan Schumaker break; 858972bcdf2STrond Myklebust 859ac396128STrond Myklebust pglen = status; 860972bcdf2STrond Myklebust if (pglen == 0) { 861972bcdf2STrond Myklebust nfs_readdir_page_set_eof(page); 8628cd51a0cSTrond Myklebust break; 8638cd51a0cSTrond Myklebust } 864972bcdf2STrond Myklebust 865ee3707aeSNagendra S Tomar verf_arg = verf_res; 866ee3707aeSNagendra S Tomar 86735df59d3STrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, pglen, 86835df59d3STrond Myklebust arrays, narrays); 869e762a639STrond Myklebust } while (!status && nfs_readdir_page_needs_filling(page)); 870d1bacf9eSBryan Schumaker 871c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 872d1bacf9eSBryan Schumaker out: 8736b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 8746b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 8756b75cf9eSTrond Myklebust kfree(entry); 876d1bacf9eSBryan Schumaker return status; 877d1bacf9eSBryan Schumaker } 878d1bacf9eSBryan Schumaker 8791f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 8801da177e4SLinus Torvalds { 88109cbfeafSKirill A. Shutemov put_page(desc->page); 8821da177e4SLinus Torvalds desc->page = NULL; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851f1d4aa4STrond Myklebust static void 8861f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 8871da177e4SLinus Torvalds { 8881f1d4aa4STrond Myklebust unlock_page(desc->page); 8891f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 8901f1d4aa4STrond Myklebust } 8911f1d4aa4STrond Myklebust 8921f1d4aa4STrond Myklebust static struct page * 8931f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 8941f1d4aa4STrond Myklebust { 8951f1d4aa4STrond Myklebust return nfs_readdir_page_get_locked(desc->file->f_mapping, 8961f1d4aa4STrond Myklebust desc->page_index, 8971f1d4aa4STrond Myklebust desc->last_cookie); 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds /* 901d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 902114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 9031da177e4SLinus Torvalds */ 9046c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 905d1bacf9eSBryan Schumaker { 906227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 907227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 908b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 909d1bacf9eSBryan Schumaker int res; 910d1bacf9eSBryan Schumaker 9111f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 9121f1d4aa4STrond Myklebust if (!desc->page) 9131f1d4aa4STrond Myklebust return -ENOMEM; 9141f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 91535df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 91635df59d3STrond Myklebust &desc->page, 1); 9179fff59edSTrond Myklebust if (res < 0) { 9189fff59edSTrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 9199fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) { 9209fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping); 9219fff59edSTrond Myklebust desc->page_index = 0; 9229fff59edSTrond Myklebust return -EAGAIN; 9239fff59edSTrond Myklebust } 9249fff59edSTrond Myklebust return res; 9259fff59edSTrond Myklebust } 926f892c41cSTrond Myklebust /* 927f892c41cSTrond Myklebust * Set the cookie verifier if the page cache was empty 928f892c41cSTrond Myklebust */ 929f892c41cSTrond Myklebust if (desc->page_index == 0) 930f892c41cSTrond Myklebust memcpy(nfsi->cookieverf, verf, 931f892c41cSTrond Myklebust sizeof(nfsi->cookieverf)); 9321f1d4aa4STrond Myklebust } 933114de382STrond Myklebust res = nfs_readdir_search_array(desc); 934ff81dfb5STrond Myklebust if (res == 0) 935114de382STrond Myklebust return 0; 9361f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 937d1bacf9eSBryan Schumaker return res; 938d1bacf9eSBryan Schumaker } 939d1bacf9eSBryan Schumaker 940794092c5STrond Myklebust static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc) 941794092c5STrond Myklebust { 942794092c5STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 943794092c5STrond Myklebust struct inode *dir = file_inode(desc->file); 944794092c5STrond Myklebust unsigned int dtsize = NFS_SERVER(dir)->dtsize; 945794092c5STrond Myklebust loff_t size = i_size_read(dir); 946794092c5STrond Myklebust 947794092c5STrond Myklebust /* 948794092c5STrond Myklebust * Default to uncached readdir if the page cache is empty, and 949794092c5STrond Myklebust * we're looking for a non-zero cookie in a large directory. 950794092c5STrond Myklebust */ 951794092c5STrond Myklebust return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize; 952794092c5STrond Myklebust } 953794092c5STrond Myklebust 954d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 9556c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 9561da177e4SLinus Torvalds { 9578cd51a0cSTrond Myklebust int res; 958d1bacf9eSBryan Schumaker 959794092c5STrond Myklebust if (nfs_readdir_dont_search_cache(desc)) 960794092c5STrond Myklebust return -EBADCOOKIE; 961794092c5STrond Myklebust 9629fff59edSTrond Myklebust do { 9630aded708STrond Myklebust if (desc->page_index == 0) { 9648cd51a0cSTrond Myklebust desc->current_index = 0; 96559e356a9STrond Myklebust desc->prev_index = 0; 9660aded708STrond Myklebust desc->last_cookie = 0; 9670aded708STrond Myklebust } 968114de382STrond Myklebust res = find_and_lock_cache_page(desc); 96947c716cbSTrond Myklebust } while (res == -EAGAIN); 9701da177e4SLinus Torvalds return res; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* 9741da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 9751da177e4SLinus Torvalds */ 97613884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc, 97713884ff2STrond Myklebust const __be32 *verf) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds struct file *file = desc->file; 980dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 981dbeaf8c9STrond Myklebust unsigned int i = 0; 9828ef2ce3eSBryan Schumaker 9830795bf83SFabian Frederick array = kmap(desc->page); 984d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 985ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 9861da177e4SLinus Torvalds 987ece0b423STrond Myklebust ent = &array->array[i]; 988a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 98923db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 9906089dd0dSThomas Meyer desc->eof = true; 9911da177e4SLinus Torvalds break; 992ece0b423STrond Myklebust } 99313884ff2STrond Myklebust memcpy(desc->verf, verf, sizeof(desc->verf)); 994d1bacf9eSBryan Schumaker if (i < (array->size-1)) 9952e7a4641STrond Myklebust desc->dir_cookie = array->array[i+1].cookie; 996d1bacf9eSBryan Schumaker else 9972e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 99859e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 9992e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 100059e356a9STrond Myklebust else 100159e356a9STrond Myklebust desc->ctx->pos++; 10022e7a4641STrond Myklebust if (desc->duped != 0) 10032e7a4641STrond Myklebust desc->duped = 1; 10048cd51a0cSTrond Myklebust } 1005b1e21c97STrond Myklebust if (array->page_is_eof) 10066089dd0dSThomas Meyer desc->eof = true; 1007d1bacf9eSBryan Schumaker 10080795bf83SFabian Frederick kunmap(desc->page); 1009dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1010dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds /* 10141da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 10151da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 10161da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 10171da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 10181da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 10191da177e4SLinus Torvalds * 10201da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 10211da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 10221da177e4SLinus Torvalds * we should already have a complete representation of the 10231da177e4SLinus Torvalds * directory in the page cache by the time we get here. 10241da177e4SLinus Torvalds */ 10256c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 10261da177e4SLinus Torvalds { 102735df59d3STrond Myklebust struct page **arrays; 102835df59d3STrond Myklebust size_t i, sz = 512; 1029b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 103035df59d3STrond Myklebust int status = -ENOMEM; 10311da177e4SLinus Torvalds 103235df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 10332e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 10341da177e4SLinus Torvalds 103535df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 103635df59d3STrond Myklebust if (!arrays) 10371da177e4SLinus Torvalds goto out; 103835df59d3STrond Myklebust arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 103935df59d3STrond Myklebust if (!arrays[0]) 104035df59d3STrond Myklebust goto out; 10411da177e4SLinus Torvalds 10427a8e1dc3STrond Myklebust desc->page_index = 0; 10432e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 10442e7a4641STrond Myklebust desc->duped = 0; 10457a8e1dc3STrond Myklebust 104635df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1047d1bacf9eSBryan Schumaker 104835df59d3STrond Myklebust for (i = 0; !desc->eof && i < sz && arrays[i]; i++) { 104935df59d3STrond Myklebust desc->page = arrays[i]; 105013884ff2STrond Myklebust nfs_do_filldir(desc, verf); 105135df59d3STrond Myklebust } 105235df59d3STrond Myklebust desc->page = NULL; 10531da177e4SLinus Torvalds 105435df59d3STrond Myklebust 105535df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++) 105635df59d3STrond Myklebust nfs_readdir_page_array_free(arrays[i]); 10571da177e4SLinus Torvalds out: 105835df59d3STrond Myklebust kfree(arrays); 105935df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 10601da177e4SLinus Torvalds return status; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 106300a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 106400a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 106500a92642SOlivier Galibert whole directory. 10661da177e4SLinus Torvalds */ 106723db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 10681da177e4SLinus Torvalds { 1069be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 10702b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 107113884ff2STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 107223db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 10736b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 1074ff81dfb5STrond Myklebust pgoff_t page_index; 10756b75cf9eSTrond Myklebust int res; 10761da177e4SLinus Torvalds 10776de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 10786de1472fSAl Viro file, (long long)ctx->pos); 107991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 108091d5b470SChuck Lever 10811da177e4SLinus Torvalds /* 108223db8620SAl Viro * ctx->pos points to the dirent entry number. 1083f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 108400a92642SOlivier Galibert * to either find the entry with the appropriate number or 108500a92642SOlivier Galibert * revalidate the cookie. 10861da177e4SLinus Torvalds */ 10876b75cf9eSTrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) { 108823db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 1089fccca7fcSTrond Myklebust if (res < 0) 1090fccca7fcSTrond Myklebust goto out; 10916b75cf9eSTrond Myklebust } 10926b75cf9eSTrond Myklebust 10936b75cf9eSTrond Myklebust res = -ENOMEM; 10946b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 10956b75cf9eSTrond Myklebust if (!desc) 10966b75cf9eSTrond Myklebust goto out; 10976b75cf9eSTrond Myklebust desc->file = file; 10986b75cf9eSTrond Myklebust desc->ctx = ctx; 10996b75cf9eSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx); 1100fccca7fcSTrond Myklebust 11012e7a4641STrond Myklebust spin_lock(&file->f_lock); 11022e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 11032e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 11042e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 1105ff81dfb5STrond Myklebust page_index = dir_ctx->page_index; 11062e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 1107b593c09fSTrond Myklebust memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf)); 11082e7a4641STrond Myklebust spin_unlock(&file->f_lock); 1109565277f6STrond Myklebust 1110ff81dfb5STrond Myklebust if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) && 1111ff81dfb5STrond Myklebust list_is_singular(&nfsi->open_files)) 1112ff81dfb5STrond Myklebust invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1); 1113ff81dfb5STrond Myklebust 111447c716cbSTrond Myklebust do { 11151da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 111600a92642SOlivier Galibert 11171da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1118ece0b423STrond Myklebust res = 0; 11191da177e4SLinus Torvalds /* This means either end of directory */ 11202e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 11211da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 112223db8620SAl Viro res = uncached_readdir(desc); 1123ece0b423STrond Myklebust if (res == 0) 11241da177e4SLinus Torvalds continue; 11259fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) 11269fff59edSTrond Myklebust res = 0; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds break; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 113113884ff2STrond Myklebust clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 11321da177e4SLinus Torvalds nfs_zap_caches(inode); 1133baf57a09STrond Myklebust desc->page_index = 0; 1134a7a3b1e9SBenjamin Coddington desc->plus = false; 1135a7a3b1e9SBenjamin Coddington desc->eof = false; 11361da177e4SLinus Torvalds continue; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds if (res < 0) 11391da177e4SLinus Torvalds break; 11401da177e4SLinus Torvalds 114113884ff2STrond Myklebust nfs_do_filldir(desc, nfsi->cookieverf); 11421f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 114347c716cbSTrond Myklebust } while (!desc->eof); 11442e7a4641STrond Myklebust 11452e7a4641STrond Myklebust spin_lock(&file->f_lock); 11462e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 11472e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 11482e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 11492e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 1150ff81dfb5STrond Myklebust dir_ctx->page_index = desc->page_index; 1151b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 11522e7a4641STrond Myklebust spin_unlock(&file->f_lock); 11532e7a4641STrond Myklebust 11546b75cf9eSTrond Myklebust kfree(desc); 11556b75cf9eSTrond Myklebust 1156fccca7fcSTrond Myklebust out: 11576de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 11581da177e4SLinus Torvalds return res; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 1161965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1162f0dd2136STrond Myklebust { 1163480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1164b84e06c5SChuck Lever 11656de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 11666de1472fSAl Viro filp, offset, whence); 1167b84e06c5SChuck Lever 1168965c8e59SAndrew Morton switch (whence) { 1169f0dd2136STrond Myklebust default: 1170b2b1ff3dSTrond Myklebust return -EINVAL; 1171b2b1ff3dSTrond Myklebust case SEEK_SET: 1172b2b1ff3dSTrond Myklebust if (offset < 0) 1173b2b1ff3dSTrond Myklebust return -EINVAL; 117483f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1175b2b1ff3dSTrond Myklebust break; 1176b2b1ff3dSTrond Myklebust case SEEK_CUR: 1177b2b1ff3dSTrond Myklebust if (offset == 0) 1178b2b1ff3dSTrond Myklebust return filp->f_pos; 117983f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1180b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1181b2b1ff3dSTrond Myklebust if (offset < 0) { 118283f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1183b2b1ff3dSTrond Myklebust return -EINVAL; 1184b2b1ff3dSTrond Myklebust } 1185f0dd2136STrond Myklebust } 1186f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1187f0dd2136STrond Myklebust filp->f_pos = offset; 118859e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 118959e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 119059e356a9STrond Myklebust else 1191480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 1192b593c09fSTrond Myklebust if (offset == 0) 1193b593c09fSTrond Myklebust memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf)); 11948ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1195f0dd2136STrond Myklebust } 119683f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1197f0dd2136STrond Myklebust return offset; 1198f0dd2136STrond Myklebust } 1199f0dd2136STrond Myklebust 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 12021da177e4SLinus Torvalds * is a dummy operation. 12031da177e4SLinus Torvalds */ 120402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 120502c24a82SJosef Bacik int datasync) 12061da177e4SLinus Torvalds { 12076de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 12081e7cb3dcSChuck Lever 120911decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 12101da177e4SLinus Torvalds return 0; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 1213bfc69a45STrond Myklebust /** 1214bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1215302fad7bSTrond Myklebust * @dir: pointer to directory inode 1216bfc69a45STrond Myklebust * 1217bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1218bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1219bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1220bfc69a45STrond Myklebust * 1221efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1222efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1223efeda80dSTrond Myklebust * 1224bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1225bfc69a45STrond Myklebust */ 1226bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1227bfc69a45STrond Myklebust { 1228efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1229bfc69a45STrond Myklebust } 123089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1231bfc69a45STrond Myklebust 1232efeda80dSTrond Myklebust /** 1233efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1234efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1235efeda80dSTrond Myklebust * @verf: previously saved change attribute 1236efeda80dSTrond Myklebust * 1237efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1238efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1239efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1240efeda80dSTrond Myklebust */ 1241efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1242efeda80dSTrond Myklebust { 1243efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1244efeda80dSTrond Myklebust } 1245efeda80dSTrond Myklebust 1246efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1247efeda80dSTrond Myklebust { 1248efeda80dSTrond Myklebust *verf |= 1UL; 1249efeda80dSTrond Myklebust } 1250efeda80dSTrond Myklebust 1251efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1252efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1253efeda80dSTrond Myklebust { 1254efeda80dSTrond Myklebust *verf &= ~1UL; 1255efeda80dSTrond Myklebust } 1256efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1257efeda80dSTrond Myklebust 1258efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1259efeda80dSTrond Myklebust { 1260efeda80dSTrond Myklebust return verf & 1; 1261efeda80dSTrond Myklebust } 1262efeda80dSTrond Myklebust 1263efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1264efeda80dSTrond Myklebust { 1265efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1266efeda80dSTrond Myklebust } 1267efeda80dSTrond Myklebust 1268efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1269efeda80dSTrond Myklebust { 1270efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1271cec08f45STrond Myklebust struct inode *dir = d_inode(dentry->d_parent); 1272efeda80dSTrond Myklebust 1273cec08f45STrond Myklebust if (!nfs_verify_change_attribute(dir, verf)) 1274cec08f45STrond Myklebust return; 1275efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1276efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1277efeda80dSTrond Myklebust dentry->d_time = verf; 1278efeda80dSTrond Myklebust } 1279efeda80dSTrond Myklebust 1280efeda80dSTrond Myklebust /** 1281efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1282efeda80dSTrond Myklebust * @dentry: pointer to dentry 1283efeda80dSTrond Myklebust * @verf: verifier to save 1284efeda80dSTrond Myklebust * 1285efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1286efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1287efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1288efeda80dSTrond Myklebust * look it up again after a directory change. 1289efeda80dSTrond Myklebust */ 1290efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1291efeda80dSTrond Myklebust { 1292efeda80dSTrond Myklebust 1293efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1294efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1295efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1296efeda80dSTrond Myklebust } 1297efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1298efeda80dSTrond Myklebust 1299efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1300efeda80dSTrond Myklebust /** 1301efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1302efeda80dSTrond Myklebust * @inode: pointer to inode 1303efeda80dSTrond Myklebust * 1304efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1305efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1306efeda80dSTrond Myklebust * while holding a delegation. 1307efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1308efeda80dSTrond Myklebust * returned or revoked. 1309efeda80dSTrond Myklebust */ 1310efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1311efeda80dSTrond Myklebust { 1312efeda80dSTrond Myklebust struct dentry *alias; 1313efeda80dSTrond Myklebust 1314efeda80dSTrond Myklebust if (!inode) 1315efeda80dSTrond Myklebust return; 1316efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1317efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1318efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1319efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1320efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1321efeda80dSTrond Myklebust } 1322efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1323efeda80dSTrond Myklebust } 1324efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1325efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1326efeda80dSTrond Myklebust 13271da177e4SLinus Torvalds /* 13281da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 13291da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 13301da177e4SLinus Torvalds * and may need to be looked up again. 1331912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 13321da177e4SLinus Torvalds */ 1333912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1334912a108dSNeilBrown int rcu_walk) 13351da177e4SLinus Torvalds { 13361da177e4SLinus Torvalds if (IS_ROOT(dentry)) 13371da177e4SLinus Torvalds return 1; 13384eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 13394eec952eSTrond Myklebust return 0; 1340f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 13416ecc5e8fSTrond Myklebust return 0; 1342f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 13431cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1344912a108dSNeilBrown if (rcu_walk) 1345f2c77f4eSTrond Myklebust return 0; 13461cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 13471cd9cb05STrond Myklebust return 0; 13481cd9cb05STrond Myklebust } 1349f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1350f2c77f4eSTrond Myklebust return 0; 1351f2c77f4eSTrond Myklebust return 1; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* 1355a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1356a12802caSTrond Myklebust * an O_EXCL create using this path component. 1357a12802caSTrond Myklebust */ 1358fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1359a12802caSTrond Myklebust { 1360a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1361a12802caSTrond Myklebust return 0; 1362fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1363a12802caSTrond Myklebust } 1364a12802caSTrond Myklebust 1365a12802caSTrond Myklebust /* 13661d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 13671d6757fbSTrond Myklebust * 13681d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 13691d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 13701d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 13711d6757fbSTrond Myklebust * 13721d6757fbSTrond Myklebust */ 137365a0c149STrond Myklebust static 1374fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 13751da177e4SLinus Torvalds { 13761da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 137765a0c149STrond Myklebust int ret; 13781da177e4SLinus Torvalds 137936d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 13804e99a1ffSTrond Myklebust return 0; 138147921921STrond Myklebust 138247921921STrond Myklebust if (flags & LOOKUP_OPEN) { 138347921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 138447921921STrond Myklebust case S_IFREG: 138547921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 138647921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 138747921921STrond Myklebust goto out; 1388df561f66SGustavo A. R. Silva fallthrough; 138947921921STrond Myklebust case S_IFDIR: 139047921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 139147921921STrond Myklebust break; 139247921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 139347921921STrond Myklebust goto out_force; 139447921921STrond Myklebust } 139547921921STrond Myklebust } 139647921921STrond Myklebust 13971da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1398fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 13991da177e4SLinus Torvalds goto out_force; 140065a0c149STrond Myklebust out: 1401a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 14021da177e4SLinus Torvalds out_force: 14031fa1e384SNeilBrown if (flags & LOOKUP_RCU) 14041fa1e384SNeilBrown return -ECHILD; 140565a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 140665a0c149STrond Myklebust if (ret != 0) 140765a0c149STrond Myklebust return ret; 140865a0c149STrond Myklebust goto out; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 141182e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode) 141282e7ca13STrond Myklebust { 141382e7ca13STrond Myklebust spin_lock(&inode->i_lock); 1414a6a361c4STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 141582e7ca13STrond Myklebust spin_unlock(&inode->i_lock); 141682e7ca13STrond Myklebust } 141782e7ca13STrond Myklebust 14181da177e4SLinus Torvalds /* 14191da177e4SLinus Torvalds * We judge how long we want to trust negative 14201da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 14211da177e4SLinus Torvalds * 14221da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 14231da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1424912a108dSNeilBrown * 1425912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1426912a108dSNeilBrown * suggesting a reval is needed. 14279f6d44d4STrond Myklebust * 14289f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 14299f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 14301da177e4SLinus Torvalds */ 14311da177e4SLinus Torvalds static inline 14321da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1433fa3c56bbSAl Viro unsigned int flags) 14341da177e4SLinus Torvalds { 14359f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 14361da177e4SLinus Torvalds return 0; 14374eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 14384eec952eSTrond Myklebust return 1; 1439912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14425ceb9d7fSTrond Myklebust static int 14435ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 14445ceb9d7fSTrond Myklebust struct inode *inode, int error) 14451da177e4SLinus Torvalds { 14465ceb9d7fSTrond Myklebust switch (error) { 14475ceb9d7fSTrond Myklebust case 1: 14486de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 14496de1472fSAl Viro __func__, dentry); 14501da177e4SLinus Torvalds return 1; 14515ceb9d7fSTrond Myklebust case 0: 1452a3f432bfSJ. Bruce Fields /* 1453a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1454a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1455a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1456a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1457a3f432bfSJ. Bruce Fields */ 145847397915STrond Myklebust if (inode && IS_ROOT(dentry)) 14595ceb9d7fSTrond Myklebust return 1; 14606de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 14616de1472fSAl Viro __func__, dentry); 14621da177e4SLinus Torvalds return 0; 14635ceb9d7fSTrond Myklebust } 14646de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 14656de1472fSAl Viro __func__, dentry, error); 1466e1fb4d05STrond Myklebust return error; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14695ceb9d7fSTrond Myklebust static int 14705ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 14715ceb9d7fSTrond Myklebust unsigned int flags) 14725ceb9d7fSTrond Myklebust { 14735ceb9d7fSTrond Myklebust int ret = 1; 14745ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 14755ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 14765ceb9d7fSTrond Myklebust return -ECHILD; 14775ceb9d7fSTrond Myklebust ret = 0; 14785ceb9d7fSTrond Myklebust } 14795ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 14805ceb9d7fSTrond Myklebust } 14815ceb9d7fSTrond Myklebust 14825ceb9d7fSTrond Myklebust static int 14835ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 14845ceb9d7fSTrond Myklebust struct inode *inode) 14855ceb9d7fSTrond Myklebust { 14865ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14875ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 14885ceb9d7fSTrond Myklebust } 14895ceb9d7fSTrond Myklebust 14905ceb9d7fSTrond Myklebust static int 14915ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 14925ceb9d7fSTrond Myklebust struct inode *inode) 14935ceb9d7fSTrond Myklebust { 14945ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 14955ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 1496a1147b82STrond Myklebust unsigned long dir_verifier; 14975ceb9d7fSTrond Myklebust int ret; 14985ceb9d7fSTrond Myklebust 14995ceb9d7fSTrond Myklebust ret = -ENOMEM; 15005ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 15019558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 15029558a007SAnna Schumaker if (fhandle == NULL || fattr == NULL) 15035ceb9d7fSTrond Myklebust goto out; 15045ceb9d7fSTrond Myklebust 1505a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 15069558a007SAnna Schumaker ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 15075ceb9d7fSTrond Myklebust if (ret < 0) { 1508f7b37b8bSTrond Myklebust switch (ret) { 1509f7b37b8bSTrond Myklebust case -ESTALE: 1510f7b37b8bSTrond Myklebust case -ENOENT: 15115ceb9d7fSTrond Myklebust ret = 0; 1512f7b37b8bSTrond Myklebust break; 1513f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1514f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1515f7b37b8bSTrond Myklebust ret = 1; 1516f7b37b8bSTrond Myklebust } 15175ceb9d7fSTrond Myklebust goto out; 15185ceb9d7fSTrond Myklebust } 15195ceb9d7fSTrond Myklebust ret = 0; 15205ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 15215ceb9d7fSTrond Myklebust goto out; 15225ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 15235ceb9d7fSTrond Myklebust goto out; 15245ceb9d7fSTrond Myklebust 1525dd225cb3SAnna Schumaker nfs_setsecurity(inode, fattr); 1526a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 15275ceb9d7fSTrond Myklebust 15285ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 15295ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 15305ceb9d7fSTrond Myklebust ret = 1; 15315ceb9d7fSTrond Myklebust out: 15325ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 15335ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 153482e7ca13STrond Myklebust 153582e7ca13STrond Myklebust /* 153682e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being 153782e7ca13STrond Myklebust * a match, then we should revalidate the directory cache. 153882e7ca13STrond Myklebust */ 153982e7ca13STrond Myklebust if (!ret && nfs_verify_change_attribute(dir, dentry->d_time)) 154082e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 15415ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 15425ceb9d7fSTrond Myklebust } 15435ceb9d7fSTrond Myklebust 15445ceb9d7fSTrond Myklebust /* 15455ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 15465ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 15475ceb9d7fSTrond Myklebust * lookup. 15485ceb9d7fSTrond Myklebust * 15495ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 15505ceb9d7fSTrond Myklebust * we have an inode! 15515ceb9d7fSTrond Myklebust * 15525ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 15535ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 15545ceb9d7fSTrond Myklebust */ 15555ceb9d7fSTrond Myklebust static int 15565ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 15575ceb9d7fSTrond Myklebust unsigned int flags) 15585ceb9d7fSTrond Myklebust { 15595ceb9d7fSTrond Myklebust struct inode *inode; 15605ceb9d7fSTrond Myklebust int error; 15615ceb9d7fSTrond Myklebust 15625ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 15635ceb9d7fSTrond Myklebust inode = d_inode(dentry); 15645ceb9d7fSTrond Myklebust 15655ceb9d7fSTrond Myklebust if (!inode) 15665ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 15675ceb9d7fSTrond Myklebust 15685ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 15695ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 15705ceb9d7fSTrond Myklebust __func__, dentry); 15715ceb9d7fSTrond Myklebust goto out_bad; 15725ceb9d7fSTrond Myklebust } 15735ceb9d7fSTrond Myklebust 1574efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 15755ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 15765ceb9d7fSTrond Myklebust 15775ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 15785ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 15795ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 15805ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 15815ceb9d7fSTrond Myklebust if (error) { 15825ceb9d7fSTrond Myklebust if (error == -ESTALE) 158382e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 15845ceb9d7fSTrond Myklebust goto out_bad; 15855ceb9d7fSTrond Myklebust } 15865ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 15875ceb9d7fSTrond Myklebust goto out_valid; 15885ceb9d7fSTrond Myklebust } 15895ceb9d7fSTrond Myklebust 15905ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 15915ceb9d7fSTrond Myklebust return -ECHILD; 15925ceb9d7fSTrond Myklebust 15935ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 15945ceb9d7fSTrond Myklebust goto out_bad; 15955ceb9d7fSTrond Myklebust 15965ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 15975ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 15985ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 15995ceb9d7fSTrond Myklebust return error; 16005ceb9d7fSTrond Myklebust out_valid: 16015ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 16025ceb9d7fSTrond Myklebust out_bad: 16035ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 16045ceb9d7fSTrond Myklebust return -ECHILD; 16055ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 16065ceb9d7fSTrond Myklebust } 16075ceb9d7fSTrond Myklebust 16085ceb9d7fSTrond Myklebust static int 1609c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1610c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 16115ceb9d7fSTrond Myklebust { 16125ceb9d7fSTrond Myklebust struct dentry *parent; 16135ceb9d7fSTrond Myklebust struct inode *dir; 16145ceb9d7fSTrond Myklebust int ret; 16155ceb9d7fSTrond Myklebust 16165ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 16175ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 16185ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 16195ceb9d7fSTrond Myklebust if (!dir) 16205ceb9d7fSTrond Myklebust return -ECHILD; 1621c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 16225ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 16235ceb9d7fSTrond Myklebust return -ECHILD; 16245ceb9d7fSTrond Myklebust } else { 16255ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1626c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 16275ceb9d7fSTrond Myklebust dput(parent); 16285ceb9d7fSTrond Myklebust } 16295ceb9d7fSTrond Myklebust return ret; 16305ceb9d7fSTrond Myklebust } 16315ceb9d7fSTrond Myklebust 1632c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1633c7944ebbSTrond Myklebust { 1634c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1635c7944ebbSTrond Myklebust } 1636c7944ebbSTrond Myklebust 16371da177e4SLinus Torvalds /* 16382b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1639ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1640ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1641ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1642ecf3d1f1SJeff Layton * 1643ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1644ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1645ecf3d1f1SJeff Layton */ 1646ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1647ecf3d1f1SJeff Layton { 16482b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 16499cdd1d3fSTrond Myklebust int error = 0; 1650ecf3d1f1SJeff Layton 1651ecf3d1f1SJeff Layton /* 1652ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1653ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1654ecf3d1f1SJeff Layton * eventually need to do something more here. 1655ecf3d1f1SJeff Layton */ 1656ecf3d1f1SJeff Layton if (!inode) { 16576de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 16586de1472fSAl Viro __func__, dentry); 1659ecf3d1f1SJeff Layton return 1; 1660ecf3d1f1SJeff Layton } 1661ecf3d1f1SJeff Layton 1662ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 16636de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 16646de1472fSAl Viro __func__, dentry); 1665ecf3d1f1SJeff Layton return 0; 1666ecf3d1f1SJeff Layton } 1667ecf3d1f1SJeff Layton 1668b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1669ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1670ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1671ecf3d1f1SJeff Layton return !error; 1672ecf3d1f1SJeff Layton } 1673ecf3d1f1SJeff Layton 1674ecf3d1f1SJeff Layton /* 16751da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 16761da177e4SLinus Torvalds */ 1677fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 16781da177e4SLinus Torvalds { 16796de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 16806de1472fSAl Viro dentry, dentry->d_flags); 16811da177e4SLinus Torvalds 168277f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 16832b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 168477f11192STrond Myklebust return 1; 168577f11192STrond Myklebust 16861da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 16871da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 16881da177e4SLinus Torvalds return 1; 16891da177e4SLinus Torvalds } 16901751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 16911da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 16921da177e4SLinus Torvalds * files will be cleaned up during umount */ 16931da177e4SLinus Torvalds return 1; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds return 0; 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 17001b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 17011b83d707STrond Myklebust { 17021b83d707STrond Myklebust spin_lock(&inode->i_lock); 17031f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 170459a707b0STrond Myklebust if (inode->i_nlink > 0) 170559a707b0STrond Myklebust drop_nlink(inode); 170659a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1707ac46b3d7STrond Myklebust nfs_set_cache_invalid( 1708ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 17091301e421STrond Myklebust NFS_INO_INVALID_NLINK); 17101b83d707STrond Myklebust spin_unlock(&inode->i_lock); 17111b83d707STrond Myklebust } 17121b83d707STrond Myklebust 17131da177e4SLinus Torvalds /* 17141da177e4SLinus Torvalds * Called when the dentry loses inode. 17151da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 17161da177e4SLinus Torvalds */ 17171da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 17181da177e4SLinus Torvalds { 17191da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1720e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 17211f018458STrond Myklebust nfs_drop_nlink(inode); 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds iput(inode); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 1726b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1727b1942c5fSAl Viro { 1728b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1729b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1730b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1731b1942c5fSAl Viro WARN_ON(1); 1732b1942c5fSAl Viro else 1733b1942c5fSAl Viro kfree(dentry->d_fsdata); 1734b1942c5fSAl Viro } 1735b1942c5fSAl Viro } 1736b1942c5fSAl Viro 1737f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 17381da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1739ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 17401da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 17411da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 174236d43a43SDavid Howells .d_automount = nfs_d_automount, 1743b1942c5fSAl Viro .d_release = nfs_d_release, 17441da177e4SLinus Torvalds }; 1745ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 17461da177e4SLinus Torvalds 1747597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 17481da177e4SLinus Torvalds { 17491da177e4SLinus Torvalds struct dentry *res; 17501da177e4SLinus Torvalds struct inode *inode = NULL; 1751e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1752e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 1753a1147b82STrond Myklebust unsigned long dir_verifier; 17541da177e4SLinus Torvalds int error; 17551da177e4SLinus Torvalds 17566de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 175791d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 17581da177e4SLinus Torvalds 1759130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1760130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 17611da177e4SLinus Torvalds 1762fd684071STrond Myklebust /* 1763fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1764fd684071STrond Myklebust * but don't hash the dentry. 1765fd684071STrond Myklebust */ 17669f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1767130f9ab7SAl Viro return NULL; 17681da177e4SLinus Torvalds 1769e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1770e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 17719558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir)); 1772e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1773e1fb4d05STrond Myklebust goto out; 1774e1fb4d05STrond Myklebust 1775a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 17766e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 17779558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 17781da177e4SLinus Torvalds if (error == -ENOENT) 17791da177e4SLinus Torvalds goto no_entry; 17801da177e4SLinus Torvalds if (error < 0) { 17811da177e4SLinus Torvalds res = ERR_PTR(error); 17829558a007SAnna Schumaker goto out; 17831da177e4SLinus Torvalds } 1784cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1785bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 178603f28e3aSTrond Myklebust if (IS_ERR(res)) 17879558a007SAnna Schumaker goto out; 178854ceac45SDavid Howells 178963519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 179063519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1791d69ee9b8STrond Myklebust 17921da177e4SLinus Torvalds no_entry: 179341d28bcaSAl Viro res = d_splice_alias(inode, dentry); 17949eaef27bSTrond Myklebust if (res != NULL) { 17959eaef27bSTrond Myklebust if (IS_ERR(res)) 17969558a007SAnna Schumaker goto out; 17971da177e4SLinus Torvalds dentry = res; 17989eaef27bSTrond Myklebust } 1799a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 18001da177e4SLinus Torvalds out: 18019558a007SAnna Schumaker trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res)); 1802e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1803e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 18041da177e4SLinus Torvalds return res; 18051da177e4SLinus Torvalds } 1806ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 18071da177e4SLinus Torvalds 180889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 18090b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 18101da177e4SLinus Torvalds 1811f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 18120ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1813b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 18141da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 18151da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 181636d43a43SDavid Howells .d_automount = nfs_d_automount, 1817b1942c5fSAl Viro .d_release = nfs_d_release, 18181da177e4SLinus Torvalds }; 181989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 18201da177e4SLinus Torvalds 18218a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 18228a5e929dSAl Viro { 18238a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 18248a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 18258a5e929dSAl Viro res |= FMODE_READ; 18268a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 18278a5e929dSAl Viro res |= FMODE_WRITE; 18288a5e929dSAl Viro return res; 18298a5e929dSAl Viro } 18308a5e929dSAl Viro 1831532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1832cd9a1c0eSTrond Myklebust { 1833532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1834cd9a1c0eSTrond Myklebust } 1835cd9a1c0eSTrond Myklebust 1836cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1837cd9a1c0eSTrond Myklebust { 1838f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1839cd9a1c0eSTrond Myklebust return 0; 1840cd9a1c0eSTrond Myklebust } 1841cd9a1c0eSTrond Myklebust 1842d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 18430dd2b474SMiklos Szeredi struct dentry *dentry, 1844b452a458SAl Viro struct file *file, unsigned open_flags) 1845cd9a1c0eSTrond Myklebust { 18460dd2b474SMiklos Szeredi int err; 18470dd2b474SMiklos Szeredi 1848be12af3eSAl Viro err = finish_open(file, dentry, do_open); 184930d90494SAl Viro if (err) 1850d9585277SAl Viro goto out; 1851eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 185230d90494SAl Viro nfs_file_set_open_context(file, ctx); 1853eaa2b82cSNeilBrown else 18549821421aSTrond Myklebust err = -EOPENSTALE; 1855cd9a1c0eSTrond Myklebust out: 1856d9585277SAl Viro return err; 1857cd9a1c0eSTrond Myklebust } 1858cd9a1c0eSTrond Myklebust 185973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 186030d90494SAl Viro struct file *file, unsigned open_flags, 186144907d79SAl Viro umode_t mode) 18621da177e4SLinus Torvalds { 1863c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1864cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 18650dd2b474SMiklos Szeredi struct dentry *res; 18660dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1867f46e0bd3STrond Myklebust struct inode *inode; 18681472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1869c94c0953SAl Viro bool switched = false; 187073a09dd9SAl Viro int created = 0; 1871898f635cSTrond Myklebust int err; 18721da177e4SLinus Torvalds 18730dd2b474SMiklos Szeredi /* Expect a negative dentry */ 18742b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 18750dd2b474SMiklos Szeredi 18761e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 18776de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18781e7cb3dcSChuck Lever 18799597c13bSJeff Layton err = nfs_check_flags(open_flags); 18809597c13bSJeff Layton if (err) 18819597c13bSJeff Layton return err; 18829597c13bSJeff Layton 18830dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 18840dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 188500699ad8SAl Viro if (!d_in_lookup(dentry)) { 18860dd2b474SMiklos Szeredi /* 18870dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 18880dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 18890dd2b474SMiklos Szeredi * again 18900dd2b474SMiklos Szeredi */ 1891d9585277SAl Viro return -ENOENT; 18920dd2b474SMiklos Szeredi } 18931472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 18941da177e4SLinus Torvalds goto no_open; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18970dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1898d9585277SAl Viro return -ENAMETOOLONG; 18991da177e4SLinus Torvalds 19000dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1901dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1902dff25ddbSAndreas Gruenbacher 1903dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1904dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1905dff25ddbSAndreas Gruenbacher 1906536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1907dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 19080dd2b474SMiklos Szeredi } 1909536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1910536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1911536e43d1STrond Myklebust attr.ia_size = 0; 1912cd9a1c0eSTrond Myklebust } 1913cd9a1c0eSTrond Myklebust 1914c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1915c94c0953SAl Viro d_drop(dentry); 1916c94c0953SAl Viro switched = true; 1917c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1918c94c0953SAl Viro &dentry->d_name, &wq); 1919c94c0953SAl Viro if (IS_ERR(dentry)) 1920c94c0953SAl Viro return PTR_ERR(dentry); 1921c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1922c94c0953SAl Viro return finish_no_open(file, dentry); 1923c94c0953SAl Viro } 1924c94c0953SAl Viro 1925532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 19260dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 19270dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1928d9585277SAl Viro goto out; 19290dd2b474SMiklos Szeredi 19306e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 193173a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 193273a09dd9SAl Viro if (created) 193373a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1934275bb307STrond Myklebust if (IS_ERR(inode)) { 19350dd2b474SMiklos Szeredi err = PTR_ERR(inode); 19366e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 19372d9db750STrond Myklebust put_nfs_open_context(ctx); 1938d20cb71dSAl Viro d_drop(dentry); 19390dd2b474SMiklos Szeredi switch (err) { 19401da177e4SLinus Torvalds case -ENOENT: 1941774d9513SPeng Tao d_splice_alias(NULL, dentry); 1942809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19430dd2b474SMiklos Szeredi break; 19441788ea6eSJeff Layton case -EISDIR: 19456f926b5bSTrond Myklebust case -ENOTDIR: 19466f926b5bSTrond Myklebust goto no_open; 19471da177e4SLinus Torvalds case -ELOOP: 19480dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 19491da177e4SLinus Torvalds goto no_open; 19500dd2b474SMiklos Szeredi break; 19511da177e4SLinus Torvalds /* case -EINVAL: */ 19521da177e4SLinus Torvalds default: 19530dd2b474SMiklos Szeredi break; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds goto out; 19561da177e4SLinus Torvalds } 19570dd2b474SMiklos Szeredi 1958b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 19596e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 19602d9db750STrond Myklebust put_nfs_open_context(ctx); 1961d9585277SAl Viro out: 1962c94c0953SAl Viro if (unlikely(switched)) { 1963c94c0953SAl Viro d_lookup_done(dentry); 1964c94c0953SAl Viro dput(dentry); 1965c94c0953SAl Viro } 1966d9585277SAl Viro return err; 19670dd2b474SMiklos Szeredi 19681da177e4SLinus Torvalds no_open: 19691472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1970c94c0953SAl Viro if (switched) { 1971c94c0953SAl Viro d_lookup_done(dentry); 1972c94c0953SAl Viro if (!res) 1973c94c0953SAl Viro res = dentry; 1974c94c0953SAl Viro else 1975c94c0953SAl Viro dput(dentry); 1976c94c0953SAl Viro } 19770dd2b474SMiklos Szeredi if (IS_ERR(res)) 1978c94c0953SAl Viro return PTR_ERR(res); 1979e45198a6SAl Viro return finish_no_open(file, res); 19801da177e4SLinus Torvalds } 198189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 19821da177e4SLinus Torvalds 1983c7944ebbSTrond Myklebust static int 1984c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1985c7944ebbSTrond Myklebust unsigned int flags) 19861da177e4SLinus Torvalds { 1987657e94b6SNick Piggin struct inode *inode; 19881da177e4SLinus Torvalds 1989fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1990c7944ebbSTrond Myklebust goto full_reval; 1991eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1992c7944ebbSTrond Myklebust goto full_reval; 19932b484297STrond Myklebust 19942b0143b5SDavid Howells inode = d_inode(dentry); 19952b484297STrond Myklebust 19961da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 19971da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 19981da177e4SLinus Torvalds */ 1999c7944ebbSTrond Myklebust if (inode == NULL) 2000c7944ebbSTrond Myklebust goto full_reval; 200149317a7fSNeilBrown 2002efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 2003c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2004216d5d06STrond Myklebust 20051da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 20061da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 2007c7944ebbSTrond Myklebust goto full_reval; 2008c7944ebbSTrond Myklebust 20091da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 2010c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2011c7944ebbSTrond Myklebust goto reval_dentry; 2012c7944ebbSTrond Myklebust 2013c7944ebbSTrond Myklebust /* Check if the directory changed */ 2014c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2015c7944ebbSTrond Myklebust goto reval_dentry; 20161da177e4SLinus Torvalds 20170ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 2018c7944ebbSTrond Myklebust return 1; 2019c7944ebbSTrond Myklebust reval_dentry: 2020c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 2021c7944ebbSTrond Myklebust return -ECHILD; 202242f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 20230ef97dcfSMiklos Szeredi 2024c7944ebbSTrond Myklebust full_reval: 2025c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 2026c7944ebbSTrond Myklebust } 2027535918f1STrond Myklebust 2028c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2029c7944ebbSTrond Myklebust { 2030c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 2031c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 2032c0204fd2STrond Myklebust } 2033c0204fd2STrond Myklebust 20341da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 20351da177e4SLinus Torvalds 2036406cd915SBenjamin Coddington struct dentry * 2037406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 2038cc6f3298SAnna Schumaker struct nfs_fattr *fattr) 20391da177e4SLinus Torvalds { 2040fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 20412b0143b5SDavid Howells struct inode *dir = d_inode(parent); 20421da177e4SLinus Torvalds struct inode *inode; 2043b0c6108eSAl Viro struct dentry *d; 2044406cd915SBenjamin Coddington int error; 20451da177e4SLinus Torvalds 2046fab728e1STrond Myklebust d_drop(dentry); 2047fab728e1STrond Myklebust 20481da177e4SLinus Torvalds if (fhandle->size == 0) { 20499558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr); 20501da177e4SLinus Torvalds if (error) 2051fab728e1STrond Myklebust goto out_error; 20521da177e4SLinus Torvalds } 20535724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 20541da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 20551da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 2056a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 20572ef61e0eSAnna Schumaker fattr, NULL); 20581da177e4SLinus Torvalds if (error < 0) 2059fab728e1STrond Myklebust goto out_error; 20601da177e4SLinus Torvalds } 2061cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 2062b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 2063fab728e1STrond Myklebust out: 2064fab728e1STrond Myklebust dput(parent); 2065406cd915SBenjamin Coddington return d; 2066fab728e1STrond Myklebust out_error: 2067406cd915SBenjamin Coddington d = ERR_PTR(error); 2068406cd915SBenjamin Coddington goto out; 2069406cd915SBenjamin Coddington } 2070406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2071406cd915SBenjamin Coddington 2072406cd915SBenjamin Coddington /* 2073406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 2074406cd915SBenjamin Coddington */ 2075406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2076d91bfc46SAnna Schumaker struct nfs_fattr *fattr) 2077406cd915SBenjamin Coddington { 2078406cd915SBenjamin Coddington struct dentry *d; 2079406cd915SBenjamin Coddington 2080cc6f3298SAnna Schumaker d = nfs_add_or_obtain(dentry, fhandle, fattr); 2081406cd915SBenjamin Coddington if (IS_ERR(d)) 2082406cd915SBenjamin Coddington return PTR_ERR(d); 2083406cd915SBenjamin Coddington 2084406cd915SBenjamin Coddington /* Callers don't care */ 2085406cd915SBenjamin Coddington dput(d); 2086406cd915SBenjamin Coddington return 0; 20871da177e4SLinus Torvalds } 2088ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds /* 20911da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 20921da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 20931da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 20941da177e4SLinus Torvalds * reply path made it appear to have failed. 20951da177e4SLinus Torvalds */ 2096549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2097549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds struct iattr attr; 2100ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 21011da177e4SLinus Torvalds int error; 21021da177e4SLinus Torvalds 21031e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 21046de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds attr.ia_mode = mode; 21071da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21081da177e4SLinus Torvalds 21098b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 21108867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 21118b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 21121da177e4SLinus Torvalds if (error != 0) 21131da177e4SLinus Torvalds goto out_err; 21141da177e4SLinus Torvalds return 0; 21151da177e4SLinus Torvalds out_err: 21161da177e4SLinus Torvalds d_drop(dentry); 21171da177e4SLinus Torvalds return error; 21181da177e4SLinus Torvalds } 2119ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* 21221da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 21231da177e4SLinus Torvalds */ 2124597d9289SBryan Schumaker int 2125549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2126549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev) 21271da177e4SLinus Torvalds { 21281da177e4SLinus Torvalds struct iattr attr; 21291da177e4SLinus Torvalds int status; 21301da177e4SLinus Torvalds 21311e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 21326de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds attr.ia_mode = mode; 21351da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21361da177e4SLinus Torvalds 21371ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 21381da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 21391ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 21401da177e4SLinus Torvalds if (status != 0) 21411da177e4SLinus Torvalds goto out_err; 21421da177e4SLinus Torvalds return 0; 21431da177e4SLinus Torvalds out_err: 21441da177e4SLinus Torvalds d_drop(dentry); 21451da177e4SLinus Torvalds return status; 21461da177e4SLinus Torvalds } 2147ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds /* 21501da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 21511da177e4SLinus Torvalds */ 2152549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2153549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 21541da177e4SLinus Torvalds { 21551da177e4SLinus Torvalds struct iattr attr; 21561da177e4SLinus Torvalds int error; 21571da177e4SLinus Torvalds 21581e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 21596de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21621da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 21631da177e4SLinus Torvalds 21641ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 21651da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 21661ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 21671da177e4SLinus Torvalds if (error != 0) 21681da177e4SLinus Torvalds goto out_err; 21691da177e4SLinus Torvalds return 0; 21701da177e4SLinus Torvalds out_err: 21711da177e4SLinus Torvalds d_drop(dentry); 21721da177e4SLinus Torvalds return error; 21731da177e4SLinus Torvalds } 2174ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 21751da177e4SLinus Torvalds 2176d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2177d45b9d8bSTrond Myklebust { 2178dc3f4198SAl Viro if (simple_positive(dentry)) 2179d45b9d8bSTrond Myklebust d_delete(dentry); 2180d45b9d8bSTrond Myklebust } 2181d45b9d8bSTrond Myklebust 21829019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir, 21839019fb39STrond Myklebust struct dentry *dentry, int error) 21849019fb39STrond Myklebust { 21859019fb39STrond Myklebust switch (error) { 21869019fb39STrond Myklebust case -ENOENT: 21879019fb39STrond Myklebust d_delete(dentry); 21889019fb39STrond Myklebust fallthrough; 21899019fb39STrond Myklebust case 0: 21909019fb39STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 21919019fb39STrond Myklebust } 21929019fb39STrond Myklebust } 21939019fb39STrond Myklebust 2194597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 21951da177e4SLinus Torvalds { 21961da177e4SLinus Torvalds int error; 21971da177e4SLinus Torvalds 21981e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 21996de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 22001da177e4SLinus Torvalds 22011ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 22022b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2203884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 22041da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 22051da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2206ba6c0592STrond Myklebust switch (error) { 2207ba6c0592STrond Myklebust case 0: 22082b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2209ba6c0592STrond Myklebust break; 2210ba6c0592STrond Myklebust case -ENOENT: 2211d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2212ba6c0592STrond Myklebust } 2213884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2214ba6c0592STrond Myklebust } else 2215ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 22169019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 22171ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds return error; 22201da177e4SLinus Torvalds } 2221ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds /* 22241da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 22251da177e4SLinus Torvalds * and after checking that the file has only one user. 22261da177e4SLinus Torvalds * 22271da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 22281da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 22291da177e4SLinus Torvalds */ 22301da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 22311da177e4SLinus Torvalds { 22322b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 22332b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 22341da177e4SLinus Torvalds int error = -EBUSY; 22351da177e4SLinus Torvalds 22366de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 22391da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 22401da177e4SLinus Torvalds error = 0; 22411da177e4SLinus Torvalds goto out; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds 22441ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 22451da177e4SLinus Torvalds if (inode != NULL) { 2246912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 22471da177e4SLinus Torvalds if (error == 0) 22481b83d707STrond Myklebust nfs_drop_nlink(inode); 22491da177e4SLinus Torvalds } else 2250912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2251d45b9d8bSTrond Myklebust if (error == -ENOENT) 2252d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 22531ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 22541da177e4SLinus Torvalds out: 22551da177e4SLinus Torvalds return error; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 22591da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 22601da177e4SLinus Torvalds * 22611da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 22621da177e4SLinus Torvalds */ 2263597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 22641da177e4SLinus Torvalds { 22651da177e4SLinus Torvalds int error; 22661da177e4SLinus Torvalds int need_rehash = 0; 22671da177e4SLinus Torvalds 22681e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 22696de1472fSAl Viro dir->i_ino, dentry); 22701da177e4SLinus Torvalds 22711ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 22721da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 227384d08fa8SAl Viro if (d_count(dentry) > 1) { 22741da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2275ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 22762b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 22771da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 22781ca42382STrond Myklebust goto out; 22791da177e4SLinus Torvalds } 22801da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 22811da177e4SLinus Torvalds __d_drop(dentry); 22821da177e4SLinus Torvalds need_rehash = 1; 22831da177e4SLinus Torvalds } 22841da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 22851da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 22869019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error); 22879019fb39STrond Myklebust if (need_rehash) 22881da177e4SLinus Torvalds d_rehash(dentry); 22891ca42382STrond Myklebust out: 22901ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 22911da177e4SLinus Torvalds return error; 22921da177e4SLinus Torvalds } 2293ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 22941da177e4SLinus Torvalds 2295873101b3SChuck Lever /* 2296873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2297873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2298873101b3SChuck Lever * using prepare_write/commit_write. 2299873101b3SChuck Lever * 2300873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2301873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2302873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2303873101b3SChuck Lever * symlink request has completed on the server. 2304873101b3SChuck Lever * 2305873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2306873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2307873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2308873101b3SChuck Lever * and move the raw page into its mapping. 2309873101b3SChuck Lever */ 2310549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 2311549c7297SChristian Brauner struct dentry *dentry, const char *symname) 23121da177e4SLinus Torvalds { 2313873101b3SChuck Lever struct page *page; 2314873101b3SChuck Lever char *kaddr; 23151da177e4SLinus Torvalds struct iattr attr; 2316873101b3SChuck Lever unsigned int pathlen = strlen(symname); 23171da177e4SLinus Torvalds int error; 23181da177e4SLinus Torvalds 23191e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 23206de1472fSAl Viro dir->i_ino, dentry, symname); 23211da177e4SLinus Torvalds 2322873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2323873101b3SChuck Lever return -ENAMETOOLONG; 23241da177e4SLinus Torvalds 2325873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2326873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 23271da177e4SLinus Torvalds 2328e8ecde25SAl Viro page = alloc_page(GFP_USER); 232976566991STrond Myklebust if (!page) 2330873101b3SChuck Lever return -ENOMEM; 2331873101b3SChuck Lever 2332e8ecde25SAl Viro kaddr = page_address(page); 2333873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2334873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2335873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2336873101b3SChuck Lever 23371ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 233894a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 23391ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2340873101b3SChuck Lever if (error != 0) { 23411e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2342873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 23436de1472fSAl Viro dentry, symname, error); 23441da177e4SLinus Torvalds d_drop(dentry); 2345873101b3SChuck Lever __free_page(page); 23461da177e4SLinus Torvalds return error; 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 2349342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2350342a67f0STrond Myklebust 2351873101b3SChuck Lever /* 2352873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2353873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2354873101b3SChuck Lever */ 23552b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2356873101b3SChuck Lever GFP_KERNEL)) { 2357873101b3SChuck Lever SetPageUptodate(page); 2358873101b3SChuck Lever unlock_page(page); 2359a0b54addSRafael Aquini /* 2360a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2361a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2362a0b54addSRafael Aquini */ 236309cbfeafSKirill A. Shutemov put_page(page); 2364873101b3SChuck Lever } else 2365873101b3SChuck Lever __free_page(page); 2366873101b3SChuck Lever 2367873101b3SChuck Lever return 0; 2368873101b3SChuck Lever } 2369ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2370873101b3SChuck Lever 2371597d9289SBryan Schumaker int 23721da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 23731da177e4SLinus Torvalds { 23742b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 23751da177e4SLinus Torvalds int error; 23761da177e4SLinus Torvalds 23776de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 23786de1472fSAl Viro old_dentry, dentry); 23791da177e4SLinus Torvalds 23801fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 23819697d234STrond Myklebust d_drop(dentry); 238220497503STrond Myklebust if (S_ISREG(inode->i_mode)) 238320497503STrond Myklebust nfs_sync_inode(inode); 23841da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2385cf809556STrond Myklebust if (error == 0) { 2386342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 23877de9c6eeSAl Viro ihold(inode); 23889697d234STrond Myklebust d_add(dentry, inode); 2389cf809556STrond Myklebust } 23901fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 23911da177e4SLinus Torvalds return error; 23921da177e4SLinus Torvalds } 2393ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds /* 23961da177e4SLinus Torvalds * RENAME 23971da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 23981da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 23991da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 24001da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 24011da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 24021da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 24031da177e4SLinus Torvalds * 24041da177e4SLinus Torvalds * FIXED. 24051da177e4SLinus Torvalds * 24061da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 24071da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 24081da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 24091da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 24101da177e4SLinus Torvalds * using the inode layer 24111da177e4SLinus Torvalds * 24121da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 24131da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 24141da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 24151da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 24161da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 24171da177e4SLinus Torvalds * the rename. 24181da177e4SLinus Torvalds */ 2419549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2420549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 2421549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 24221da177e4SLinus Torvalds { 24232b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 24242b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2425d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 242680a491fdSJeff Layton struct rpc_task *task; 24271da177e4SLinus Torvalds int error = -EBUSY; 24281da177e4SLinus Torvalds 24291cd66c93SMiklos Szeredi if (flags) 24301cd66c93SMiklos Szeredi return -EINVAL; 24311cd66c93SMiklos Szeredi 24326de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 24336de1472fSAl Viro old_dentry, new_dentry, 243484d08fa8SAl Viro d_count(new_dentry)); 24351da177e4SLinus Torvalds 243670ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 24371da177e4SLinus Torvalds /* 243828f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 243928f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 244028f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 244128f79a1aSMiklos Szeredi * the new target. 24421da177e4SLinus Torvalds */ 244327226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 244427226104SMiklos Szeredi /* 244527226104SMiklos Szeredi * To prevent any new references to the target during the 244627226104SMiklos Szeredi * rename, we unhash the dentry in advance. 244727226104SMiklos Szeredi */ 2448d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 244927226104SMiklos Szeredi d_drop(new_dentry); 2450d9f29500SBenjamin Coddington rehash = new_dentry; 2451d9f29500SBenjamin Coddington } 245227226104SMiklos Szeredi 245384d08fa8SAl Viro if (d_count(new_dentry) > 2) { 24541da177e4SLinus Torvalds int err; 245527226104SMiklos Szeredi 24561da177e4SLinus Torvalds /* copy the target dentry's name */ 24571da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 24581da177e4SLinus Torvalds &new_dentry->d_name); 24591da177e4SLinus Torvalds if (!dentry) 24601da177e4SLinus Torvalds goto out; 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds /* silly-rename the existing target ... */ 24631da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 246424e93025SMiklos Szeredi if (err) 24651da177e4SLinus Torvalds goto out; 246624e93025SMiklos Szeredi 246724e93025SMiklos Szeredi new_dentry = dentry; 2468d9f29500SBenjamin Coddington rehash = NULL; 246924e93025SMiklos Szeredi new_inode = NULL; 2470b1e4adf4STrond Myklebust } 247127226104SMiklos Szeredi } 24721da177e4SLinus Torvalds 2473*6ff9d99bSTrond Myklebust if (S_ISREG(old_inode->i_mode)) 2474*6ff9d99bSTrond Myklebust nfs_sync_inode(old_inode); 2475d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 247680a491fdSJeff Layton if (IS_ERR(task)) { 247780a491fdSJeff Layton error = PTR_ERR(task); 247880a491fdSJeff Layton goto out; 247980a491fdSJeff Layton } 248080a491fdSJeff Layton 248180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2482818a8dbeSBenjamin Coddington if (error != 0) { 2483818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2484818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2485818a8dbeSBenjamin Coddington smp_wmb(); 2486818a8dbeSBenjamin Coddington } else 248780a491fdSJeff Layton error = task->tk_status; 248880a491fdSJeff Layton rpc_put_task(task); 248959a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 249059a707b0STrond Myklebust if (error == 0) { 249159a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 249259a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2493ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2494ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME | 2495ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 249659a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 249759a707b0STrond Myklebust } 24981da177e4SLinus Torvalds out: 2499d9f29500SBenjamin Coddington if (rehash) 2500d9f29500SBenjamin Coddington d_rehash(rehash); 250170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 250270ded201STrond Myklebust new_dir, new_dentry, error); 2503d9f29500SBenjamin Coddington if (!error) { 2504d9f29500SBenjamin Coddington if (new_inode != NULL) 2505d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2506d9f29500SBenjamin Coddington /* 2507d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2508d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2509d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2510d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2511d9f29500SBenjamin Coddington */ 2512d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2513d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2514d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2515d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2516d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2517d9f29500SBenjamin Coddington 25181da177e4SLinus Torvalds /* new dentry created? */ 25191da177e4SLinus Torvalds if (dentry) 25201da177e4SLinus Torvalds dput(dentry); 25211da177e4SLinus Torvalds return error; 25221da177e4SLinus Torvalds } 2523ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 25241da177e4SLinus Torvalds 2525cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2526cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2527cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2528cfcea3e8STrond Myklebust 2529a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 25303a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 25313a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 25323a505845STrond Myklebust 25331c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 25341c3c07e9STrond Myklebust { 25356238aec8SNeilBrown put_group_info(entry->group_info); 2536f682a398SNeilBrown kfree_rcu(entry, rcu_head); 25374e857c58SPeter Zijlstra smp_mb__before_atomic(); 2538cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 25394e857c58SPeter Zijlstra smp_mb__after_atomic(); 25401c3c07e9STrond Myklebust } 25411c3c07e9STrond Myklebust 25421a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 25431a81bb8aSTrond Myklebust { 25441a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 25451a81bb8aSTrond Myklebust 25461a81bb8aSTrond Myklebust while (!list_empty(head)) { 25471a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 25481a81bb8aSTrond Myklebust list_del(&cache->lru); 25491a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 25501a81bb8aSTrond Myklebust } 25511a81bb8aSTrond Myklebust } 25521a81bb8aSTrond Myklebust 25533a505845STrond Myklebust static unsigned long 25543a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2555979df72eSTrond Myklebust { 2556979df72eSTrond Myklebust LIST_HEAD(head); 2557aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2558979df72eSTrond Myklebust struct nfs_access_entry *cache; 25591ab6c499SDave Chinner long freed = 0; 2560979df72eSTrond Myklebust 2561a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2562aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2563979df72eSTrond Myklebust struct inode *inode; 2564979df72eSTrond Myklebust 2565979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2566979df72eSTrond Myklebust break; 25679c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2568979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2569979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2570979df72eSTrond Myklebust goto remove_lru_entry; 2571979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2572979df72eSTrond Myklebust struct nfs_access_entry, lru); 2573979df72eSTrond Myklebust list_move(&cache->lru, &head); 2574979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 25751ab6c499SDave Chinner freed++; 2576979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2577979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2578979df72eSTrond Myklebust &nfs_access_lru_list); 2579979df72eSTrond Myklebust else { 2580979df72eSTrond Myklebust remove_lru_entry: 2581979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 25824e857c58SPeter Zijlstra smp_mb__before_atomic(); 2583979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 25844e857c58SPeter Zijlstra smp_mb__after_atomic(); 2585979df72eSTrond Myklebust } 258659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2587979df72eSTrond Myklebust } 2588979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 25891a81bb8aSTrond Myklebust nfs_access_free_list(&head); 25901ab6c499SDave Chinner return freed; 25911ab6c499SDave Chinner } 25921ab6c499SDave Chinner 25931ab6c499SDave Chinner unsigned long 25943a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 25953a505845STrond Myklebust { 25963a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 25973a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 25983a505845STrond Myklebust 25993a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 26003a505845STrond Myklebust return SHRINK_STOP; 26013a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 26023a505845STrond Myklebust } 26033a505845STrond Myklebust 26043a505845STrond Myklebust 26053a505845STrond Myklebust unsigned long 26061ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 26071ab6c499SDave Chinner { 260855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2609979df72eSTrond Myklebust } 2610979df72eSTrond Myklebust 26113a505845STrond Myklebust static void 26123a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 26133a505845STrond Myklebust { 26143a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 26153a505845STrond Myklebust unsigned long diff; 26163a505845STrond Myklebust unsigned int nr_to_scan; 26173a505845STrond Myklebust 26183a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 26193a505845STrond Myklebust return; 26203a505845STrond Myklebust nr_to_scan = 100; 26213a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 26223a505845STrond Myklebust if (diff < nr_to_scan) 26233a505845STrond Myklebust nr_to_scan = diff; 26243a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 26253a505845STrond Myklebust } 26263a505845STrond Myklebust 26271a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 26281c3c07e9STrond Myklebust { 26291c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 26301a81bb8aSTrond Myklebust struct rb_node *n; 26311c3c07e9STrond Myklebust struct nfs_access_entry *entry; 26321c3c07e9STrond Myklebust 26331c3c07e9STrond Myklebust /* Unhook entries from the cache */ 26341c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 26351c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 26361c3c07e9STrond Myklebust rb_erase(n, root_node); 26371a81bb8aSTrond Myklebust list_move(&entry->lru, head); 26381c3c07e9STrond Myklebust } 26391c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 26401c3c07e9STrond Myklebust } 26411c3c07e9STrond Myklebust 26421c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 26431c3c07e9STrond Myklebust { 26441a81bb8aSTrond Myklebust LIST_HEAD(head); 26451a81bb8aSTrond Myklebust 26461a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 26471a81bb8aSTrond Myklebust return; 2648cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2649cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 26501a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2651cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2652cfcea3e8STrond Myklebust 26531c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 26541a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 26551a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 26561a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 26571a81bb8aSTrond Myklebust nfs_access_free_list(&head); 26581c3c07e9STrond Myklebust } 26591c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 26601c3c07e9STrond Myklebust 26616238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b) 26626238aec8SNeilBrown { 26636238aec8SNeilBrown struct group_info *ga, *gb; 26646238aec8SNeilBrown int g; 26656238aec8SNeilBrown 26666238aec8SNeilBrown if (uid_lt(a->fsuid, b->fsuid)) 26676238aec8SNeilBrown return -1; 26686238aec8SNeilBrown if (uid_gt(a->fsuid, b->fsuid)) 26696238aec8SNeilBrown return 1; 26706238aec8SNeilBrown 26716238aec8SNeilBrown if (gid_lt(a->fsgid, b->fsgid)) 26726238aec8SNeilBrown return -1; 26736238aec8SNeilBrown if (gid_gt(a->fsgid, b->fsgid)) 26746238aec8SNeilBrown return 1; 26756238aec8SNeilBrown 26766238aec8SNeilBrown ga = a->group_info; 26776238aec8SNeilBrown gb = b->group_info; 26786238aec8SNeilBrown if (ga == gb) 26796238aec8SNeilBrown return 0; 26806238aec8SNeilBrown if (ga == NULL) 26816238aec8SNeilBrown return -1; 26826238aec8SNeilBrown if (gb == NULL) 26836238aec8SNeilBrown return 1; 26846238aec8SNeilBrown if (ga->ngroups < gb->ngroups) 26856238aec8SNeilBrown return -1; 26866238aec8SNeilBrown if (ga->ngroups > gb->ngroups) 26876238aec8SNeilBrown return 1; 26886238aec8SNeilBrown 26896238aec8SNeilBrown for (g = 0; g < ga->ngroups; g++) { 26906238aec8SNeilBrown if (gid_lt(ga->gid[g], gb->gid[g])) 26916238aec8SNeilBrown return -1; 26926238aec8SNeilBrown if (gid_gt(ga->gid[g], gb->gid[g])) 26936238aec8SNeilBrown return 1; 26946238aec8SNeilBrown } 26956238aec8SNeilBrown return 0; 26966238aec8SNeilBrown } 26976238aec8SNeilBrown 2698b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 26991c3c07e9STrond Myklebust { 27001c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 27011c3c07e9STrond Myklebust 27021c3c07e9STrond Myklebust while (n != NULL) { 2703b68572e0SNeilBrown struct nfs_access_entry *entry = 2704b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 27056238aec8SNeilBrown int cmp = access_cmp(cred, entry); 27061c3c07e9STrond Myklebust 2707b68572e0SNeilBrown if (cmp < 0) 27081c3c07e9STrond Myklebust n = n->rb_left; 2709b68572e0SNeilBrown else if (cmp > 0) 27101c3c07e9STrond Myklebust n = n->rb_right; 27111c3c07e9STrond Myklebust else 27121c3c07e9STrond Myklebust return entry; 27131c3c07e9STrond Myklebust } 27141c3c07e9STrond Myklebust return NULL; 27151c3c07e9STrond Myklebust } 27161c3c07e9STrond Myklebust 2717b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block) 27181da177e4SLinus Torvalds { 271955296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 27201c3c07e9STrond Myklebust struct nfs_access_entry *cache; 272157b69181STrond Myklebust bool retry = true; 272257b69181STrond Myklebust int err; 27231da177e4SLinus Torvalds 27241c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 272557b69181STrond Myklebust for(;;) { 27261c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 27271c3c07e9STrond Myklebust goto out_zap; 27281c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 272957b69181STrond Myklebust err = -ENOENT; 27301c3c07e9STrond Myklebust if (cache == NULL) 27311c3c07e9STrond Myklebust goto out; 273257b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 273321c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 273457b69181STrond Myklebust break; 27355c965db8STrond Myklebust if (!retry) 27365c965db8STrond Myklebust break; 273757b69181STrond Myklebust err = -ECHILD; 273857b69181STrond Myklebust if (!may_block) 273957b69181STrond Myklebust goto out; 274057b69181STrond Myklebust spin_unlock(&inode->i_lock); 274157b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 274257b69181STrond Myklebust if (err) 274357b69181STrond Myklebust return err; 274457b69181STrond Myklebust spin_lock(&inode->i_lock); 274557b69181STrond Myklebust retry = false; 274657b69181STrond Myklebust } 2747b5e7b59cSNeilBrown *mask = cache->mask; 2748cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 27491c3c07e9STrond Myklebust err = 0; 27501c3c07e9STrond Myklebust out: 27511c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 27521c3c07e9STrond Myklebust return err; 27531c3c07e9STrond Myklebust out_zap: 27541a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 27551a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 27561c3c07e9STrond Myklebust return -ENOENT; 27571c3c07e9STrond Myklebust } 27581c3c07e9STrond Myklebust 2759b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask) 2760f682a398SNeilBrown { 2761f682a398SNeilBrown /* Only check the most recently returned cache entry, 2762f682a398SNeilBrown * but do it without locking. 2763f682a398SNeilBrown */ 2764f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2765f682a398SNeilBrown struct nfs_access_entry *cache; 2766f682a398SNeilBrown int err = -ECHILD; 2767f682a398SNeilBrown struct list_head *lh; 2768f682a398SNeilBrown 2769f682a398SNeilBrown rcu_read_lock(); 2770f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2771f682a398SNeilBrown goto out; 27729f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2773f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2774f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 27756238aec8SNeilBrown access_cmp(cred, cache) != 0) 2776f682a398SNeilBrown cache = NULL; 2777f682a398SNeilBrown if (cache == NULL) 2778f682a398SNeilBrown goto out; 277921c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2780f682a398SNeilBrown goto out; 2781b5e7b59cSNeilBrown *mask = cache->mask; 278221c3ba7eSTrond Myklebust err = 0; 2783f682a398SNeilBrown out: 2784f682a398SNeilBrown rcu_read_unlock(); 2785f682a398SNeilBrown return err; 2786f682a398SNeilBrown } 2787f682a398SNeilBrown 2788b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred, 2789b5e7b59cSNeilBrown u32 *mask, bool may_block) 2790d2ae4f8bSFrank van der Linden { 2791d2ae4f8bSFrank van der Linden int status; 2792d2ae4f8bSFrank van der Linden 2793b5e7b59cSNeilBrown status = nfs_access_get_cached_rcu(inode, cred, mask); 2794d2ae4f8bSFrank van der Linden if (status != 0) 2795b5e7b59cSNeilBrown status = nfs_access_get_cached_locked(inode, cred, mask, 2796d2ae4f8bSFrank van der Linden may_block); 2797d2ae4f8bSFrank van der Linden 2798d2ae4f8bSFrank van der Linden return status; 2799d2ae4f8bSFrank van der Linden } 2800d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2801d2ae4f8bSFrank van der Linden 280273fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode, 280373fbb3faSNeilBrown struct nfs_access_entry *set, 280473fbb3faSNeilBrown const struct cred *cred) 28051c3c07e9STrond Myklebust { 2806cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2807cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 28081c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 28091c3c07e9STrond Myklebust struct rb_node *parent = NULL; 28101c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2811b68572e0SNeilBrown int cmp; 28121c3c07e9STrond Myklebust 28131c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 28141c3c07e9STrond Myklebust while (*p != NULL) { 28151c3c07e9STrond Myklebust parent = *p; 28161c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 28176238aec8SNeilBrown cmp = access_cmp(cred, entry); 28181c3c07e9STrond Myklebust 2819b68572e0SNeilBrown if (cmp < 0) 28201c3c07e9STrond Myklebust p = &parent->rb_left; 2821b68572e0SNeilBrown else if (cmp > 0) 28221c3c07e9STrond Myklebust p = &parent->rb_right; 28231c3c07e9STrond Myklebust else 28241c3c07e9STrond Myklebust goto found; 28251c3c07e9STrond Myklebust } 28261c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 28271c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2828cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 28291c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 28301c3c07e9STrond Myklebust return; 28311c3c07e9STrond Myklebust found: 28321c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2833cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2834cfcea3e8STrond Myklebust list_del(&entry->lru); 28351c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 28361c3c07e9STrond Myklebust nfs_access_free_entry(entry); 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 283973fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set, 284073fbb3faSNeilBrown const struct cred *cred) 28411da177e4SLinus Torvalds { 28421c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 28431c3c07e9STrond Myklebust if (cache == NULL) 28441c3c07e9STrond Myklebust return; 28451c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 28466238aec8SNeilBrown cache->fsuid = cred->fsuid; 28476238aec8SNeilBrown cache->fsgid = cred->fsgid; 28486238aec8SNeilBrown cache->group_info = get_group_info(cred->group_info); 28491da177e4SLinus Torvalds cache->mask = set->mask; 28501c3c07e9STrond Myklebust 2851f682a398SNeilBrown /* The above field assignments must be visible 2852f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2853f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2854f682a398SNeilBrown */ 2855f682a398SNeilBrown smp_wmb(); 285673fbb3faSNeilBrown nfs_access_add_rbtree(inode, cache, cred); 2857cfcea3e8STrond Myklebust 2858cfcea3e8STrond Myklebust /* Update accounting */ 28594e857c58SPeter Zijlstra smp_mb__before_atomic(); 2860cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 28614e857c58SPeter Zijlstra smp_mb__after_atomic(); 2862cfcea3e8STrond Myklebust 2863cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 28641a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2865cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 28661a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 28671a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 28681a81bb8aSTrond Myklebust &nfs_access_lru_list); 2869cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2870cfcea3e8STrond Myklebust } 28713a505845STrond Myklebust nfs_access_cache_enforce_limit(); 28721da177e4SLinus Torvalds } 28736168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 28746168f62cSWeston Andros Adamson 28753c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 28763c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 28773c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 28783c181827SAnna Schumaker NFS_ACCESS_DELETE) 28793c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 28803c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2881ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 28823c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 28833c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 288415d4b73aSTrond Myklebust static int 2885ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 288615d4b73aSTrond Myklebust { 288715d4b73aSTrond Myklebust int mask = 0; 288815d4b73aSTrond Myklebust 288915d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 289015d4b73aSTrond Myklebust mask |= MAY_READ; 2891ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2892ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 289315d4b73aSTrond Myklebust mask |= MAY_WRITE; 2894ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 289515d4b73aSTrond Myklebust mask |= MAY_EXEC; 2896ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2897ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2898ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2899ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 290015d4b73aSTrond Myklebust mask |= MAY_EXEC; 2901ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2902ecbb903cSTrond Myklebust mask |= MAY_WRITE; 290315d4b73aSTrond Myklebust return mask; 290415d4b73aSTrond Myklebust } 290515d4b73aSTrond Myklebust 29066168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 29076168f62cSWeston Andros Adamson { 2908bd8b2441STrond Myklebust entry->mask = access_result; 29096168f62cSWeston Andros Adamson } 29106168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 29111da177e4SLinus Torvalds 2912b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 29131da177e4SLinus Torvalds { 29141da177e4SLinus Torvalds struct nfs_access_entry cache; 291557b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2916e8194b7dSTrond Myklebust int cache_mask = -1; 29171da177e4SLinus Torvalds int status; 29181da177e4SLinus Torvalds 2919f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2920f4ce1299STrond Myklebust 2921b5e7b59cSNeilBrown status = nfs_access_get_cached(inode, cred, &cache.mask, may_block); 29221da177e4SLinus Torvalds if (status == 0) 2923f4ce1299STrond Myklebust goto out_cached; 29241da177e4SLinus Torvalds 2925f3324a2aSNeilBrown status = -ECHILD; 292657b69181STrond Myklebust if (!may_block) 2927f3324a2aSNeilBrown goto out; 2928f3324a2aSNeilBrown 29291750d929SAnna Schumaker /* 29301750d929SAnna Schumaker * Determine which access bits we want to ask for... 29311750d929SAnna Schumaker */ 29321750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 293372832a24SFrank van der Linden if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 293472832a24SFrank van der Linden cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 293572832a24SFrank van der Linden NFS_ACCESS_XALIST; 293672832a24SFrank van der Linden } 29371750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 29381750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 29391750d929SAnna Schumaker else 29401750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 294173fbb3faSNeilBrown status = NFS_PROTO(inode)->access(inode, &cache, cred); 2942a71ee337SSuresh Jayaraman if (status != 0) { 2943a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2944a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 294593ce4af7STrond Myklebust nfs_set_inode_stale(inode); 294693ce4af7STrond Myklebust else 294793ce4af7STrond Myklebust nfs_zap_caches(inode); 2948a71ee337SSuresh Jayaraman } 2949f4ce1299STrond Myklebust goto out; 2950a71ee337SSuresh Jayaraman } 295173fbb3faSNeilBrown nfs_access_add_cache(inode, &cache, cred); 2952f4ce1299STrond Myklebust out_cached: 2953ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2954bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2955f4ce1299STrond Myklebust status = -EACCES; 29561da177e4SLinus Torvalds out: 2957e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2958f4ce1299STrond Myklebust return status; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 2961af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2962af22f94aSTrond Myklebust { 2963af22f94aSTrond Myklebust int mask = 0; 2964af22f94aSTrond Myklebust 2965f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2966f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2967f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2968f8d9a897SWeston Andros Adamson } else { 29698a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2970af22f94aSTrond Myklebust mask |= MAY_READ; 29718a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2972af22f94aSTrond Myklebust mask |= MAY_WRITE; 2973f8d9a897SWeston Andros Adamson } 2974f8d9a897SWeston Andros Adamson 2975af22f94aSTrond Myklebust return mask; 2976af22f94aSTrond Myklebust } 2977af22f94aSTrond Myklebust 2978b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2979af22f94aSTrond Myklebust { 2980af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2981af22f94aSTrond Myklebust } 298289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2983af22f94aSTrond Myklebust 29845c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 29855c5fc09aSTrond Myklebust { 29865c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 298721c3ba7eSTrond Myklebust int ret = 0; 29885c5fc09aSTrond Myklebust 29893825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 29903825827eSTrond Myklebust return 0; 2991720869ebSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { 29925c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 299321c3ba7eSTrond Myklebust return -ECHILD; 299421c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 299521c3ba7eSTrond Myklebust } 29965c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 29975c5fc09aSTrond Myklebust ret = -EACCES; 29985c5fc09aSTrond Myklebust return ret; 29995c5fc09aSTrond Myklebust } 30005c5fc09aSTrond Myklebust 3001549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns, 3002549c7297SChristian Brauner struct inode *inode, 3003549c7297SChristian Brauner int mask) 30041da177e4SLinus Torvalds { 3005b68572e0SNeilBrown const struct cred *cred = current_cred(); 30061da177e4SLinus Torvalds int res = 0; 30071da177e4SLinus Torvalds 300891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 300991d5b470SChuck Lever 3010e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 30111da177e4SLinus Torvalds goto out; 30121da177e4SLinus Torvalds /* Is this sys_access() ? */ 30139cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 30141da177e4SLinus Torvalds goto force_lookup; 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 30171da177e4SLinus Torvalds case S_IFLNK: 30181da177e4SLinus Torvalds goto out; 30191da177e4SLinus Torvalds case S_IFREG: 3020762674f8STrond Myklebust if ((mask & MAY_OPEN) && 3021762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 3022762674f8STrond Myklebust return 0; 30231da177e4SLinus Torvalds break; 30241da177e4SLinus Torvalds case S_IFDIR: 30251da177e4SLinus Torvalds /* 30261da177e4SLinus Torvalds * Optimize away all write operations, since the server 30271da177e4SLinus Torvalds * will check permissions when we perform the op. 30281da177e4SLinus Torvalds */ 30291da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 30301da177e4SLinus Torvalds goto out; 30311da177e4SLinus Torvalds } 30321da177e4SLinus Torvalds 30331da177e4SLinus Torvalds force_lookup: 30341da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 30351da177e4SLinus Torvalds goto out_notsup; 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 30381da177e4SLinus Torvalds out: 30395c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 30405c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 3041f696a365SMiklos Szeredi 30421e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 30431e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 30441da177e4SLinus Torvalds return res; 30451da177e4SLinus Torvalds out_notsup: 3046d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 3047d51ac1a8SNeilBrown return -ECHILD; 3048d51ac1a8SNeilBrown 3049720869ebSTrond Myklebust res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE | 3050720869ebSTrond Myklebust NFS_INO_INVALID_OTHER); 30511da177e4SLinus Torvalds if (res == 0) 305247291baaSChristian Brauner res = generic_permission(&init_user_ns, inode, mask); 30531e7cb3dcSChuck Lever goto out; 30541da177e4SLinus Torvalds } 3055ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 3056