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; 81311324adSTrond Myklebust spin_lock(&dir->i_lock); 821c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 831c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 84ac46b3d7STrond Myklebust nfs_set_cache_invalid(dir, 85ac46b3d7STrond Myklebust NFS_INO_INVALID_DATA | 86ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 87311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 88311324adSTrond Myklebust spin_unlock(&dir->i_lock); 89480c2006SBryan Schumaker return ctx; 90480c2006SBryan Schumaker } 910c030806STrond Myklebust return ERR_PTR(-ENOMEM); 920c030806STrond Myklebust } 93480c2006SBryan Schumaker 94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 95480c2006SBryan Schumaker { 96311324adSTrond Myklebust spin_lock(&dir->i_lock); 97311324adSTrond Myklebust list_del(&ctx->list); 98311324adSTrond Myklebust spin_unlock(&dir->i_lock); 99480c2006SBryan Schumaker kfree(ctx); 100480c2006SBryan Schumaker } 101480c2006SBryan Schumaker 1021da177e4SLinus Torvalds /* 1031da177e4SLinus Torvalds * Open file 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds static int 1061da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1071da177e4SLinus Torvalds { 108480c2006SBryan Schumaker int res = 0; 109480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1101da177e4SLinus Torvalds 1116de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 112cc0dd2d1SChuck Lever 113cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1141e7cb3dcSChuck Lever 11593b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode); 116480c2006SBryan Schumaker if (IS_ERR(ctx)) { 117480c2006SBryan Schumaker res = PTR_ERR(ctx); 118480c2006SBryan Schumaker goto out; 119480c2006SBryan Schumaker } 120480c2006SBryan Schumaker filp->private_data = ctx; 121480c2006SBryan Schumaker out: 1221da177e4SLinus Torvalds return res; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 125480c2006SBryan Schumaker static int 126480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 127480c2006SBryan Schumaker { 128a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 129480c2006SBryan Schumaker return 0; 130480c2006SBryan Schumaker } 131480c2006SBryan Schumaker 132d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 133d1bacf9eSBryan Schumaker u64 cookie; 134d1bacf9eSBryan Schumaker u64 ino; 135a52a8a6aSTrond Myklebust const char *name; 136a52a8a6aSTrond Myklebust unsigned int name_len; 1370b26a0bfSTrond Myklebust unsigned char d_type; 138d1bacf9eSBryan Schumaker }; 139d1bacf9eSBryan Schumaker 140d1bacf9eSBryan Schumaker struct nfs_cache_array { 141d1bacf9eSBryan Schumaker u64 last_cookie; 142b1e21c97STrond Myklebust unsigned int size; 143b1e21c97STrond Myklebust unsigned char page_full : 1, 144762567b7STrond Myklebust page_is_eof : 1, 145762567b7STrond Myklebust cookies_are_ordered : 1; 1465601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 147d1bacf9eSBryan Schumaker }; 148d1bacf9eSBryan Schumaker 1496c981effSTrond Myklebust struct nfs_readdir_descriptor { 1501da177e4SLinus Torvalds struct file *file; 1511da177e4SLinus Torvalds struct page *page; 15223db8620SAl Viro struct dir_context *ctx; 1531f1d4aa4STrond Myklebust pgoff_t page_index; 1542e7a4641STrond Myklebust u64 dir_cookie; 1550aded708STrond Myklebust u64 last_cookie; 1562e7a4641STrond Myklebust u64 dup_cookie; 157f0dd2136STrond Myklebust loff_t current_index; 15859e356a9STrond Myklebust loff_t prev_index; 159d1bacf9eSBryan Schumaker 160b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 161a1147b82STrond Myklebust unsigned long dir_verifier; 1621f4eab7eSNeil Brown unsigned long timestamp; 1634704f0e2STrond Myklebust unsigned long gencount; 1642e7a4641STrond Myklebust unsigned long attr_gencount; 165d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 1662e7a4641STrond Myklebust signed char duped; 167a7a3b1e9SBenjamin Coddington bool plus; 168a7a3b1e9SBenjamin Coddington bool eof; 1696c981effSTrond Myklebust }; 1701da177e4SLinus Torvalds 1711f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array) 1721f1d4aa4STrond Myklebust { 1731f1d4aa4STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1741f1d4aa4STrond Myklebust } 1751f1d4aa4STrond Myklebust 1761f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie) 1774b310319STrond Myklebust { 1784b310319STrond Myklebust struct nfs_cache_array *array; 1794b310319STrond Myklebust 1804b310319STrond Myklebust array = kmap_atomic(page); 1811f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 1821f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 183762567b7STrond Myklebust array->cookies_are_ordered = 1; 1844b310319STrond Myklebust kunmap_atomic(array); 1854b310319STrond Myklebust } 1864b310319STrond Myklebust 187d1bacf9eSBryan Schumaker /* 188d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 189d1bacf9eSBryan Schumaker */ 190d1bacf9eSBryan Schumaker static 19111de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 192d1bacf9eSBryan Schumaker { 19311de3b11STrond Myklebust struct nfs_cache_array *array; 194d1bacf9eSBryan Schumaker int i; 1958cd51a0cSTrond Myklebust 1962b86ce2dSCong Wang array = kmap_atomic(page); 197d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 198a52a8a6aSTrond Myklebust kfree(array->array[i].name); 1991f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 2002b86ce2dSCong Wang kunmap_atomic(array); 201d1bacf9eSBryan Schumaker } 202d1bacf9eSBryan Schumaker 20335df59d3STrond Myklebust static struct page * 20435df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags) 20535df59d3STrond Myklebust { 20635df59d3STrond Myklebust struct page *page = alloc_page(gfp_flags); 20735df59d3STrond Myklebust if (page) 20835df59d3STrond Myklebust nfs_readdir_page_init_array(page, last_cookie); 20935df59d3STrond Myklebust return page; 21035df59d3STrond Myklebust } 21135df59d3STrond Myklebust 21235df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page) 21335df59d3STrond Myklebust { 21435df59d3STrond Myklebust if (page) { 21535df59d3STrond Myklebust nfs_readdir_clear_array(page); 21635df59d3STrond Myklebust put_page(page); 21735df59d3STrond Myklebust } 21835df59d3STrond Myklebust } 21935df59d3STrond Myklebust 220b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 221b1e21c97STrond Myklebust { 222b1e21c97STrond Myklebust array->page_is_eof = 1; 223b1e21c97STrond Myklebust array->page_full = 1; 224b1e21c97STrond Myklebust } 225b1e21c97STrond Myklebust 226b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 227b1e21c97STrond Myklebust { 228b1e21c97STrond Myklebust return array->page_full; 229b1e21c97STrond Myklebust } 230b1e21c97STrond Myklebust 231d1bacf9eSBryan Schumaker /* 232d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 233d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 234d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 235d1bacf9eSBryan Schumaker */ 236a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 237d1bacf9eSBryan Schumaker { 238a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 239a52a8a6aSTrond Myklebust 24004e4bd1cSCatalin Marinas /* 24104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 24204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 24304e4bd1cSCatalin Marinas */ 244a52a8a6aSTrond Myklebust if (ret != NULL) 245a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 246a52a8a6aSTrond Myklebust return ret; 247d1bacf9eSBryan Schumaker } 248d1bacf9eSBryan Schumaker 249b1e21c97STrond Myklebust /* 250b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 251b1e21c97STrond Myklebust */ 252b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 253b1e21c97STrond Myklebust { 254b1e21c97STrond Myklebust struct nfs_cache_array_entry *cache_entry; 255b1e21c97STrond Myklebust 256b1e21c97STrond Myklebust if (array->page_full) 257b1e21c97STrond Myklebust return -ENOSPC; 258b1e21c97STrond Myklebust cache_entry = &array->array[array->size + 1]; 259b1e21c97STrond Myklebust if ((char *)cache_entry - (char *)array > PAGE_SIZE) { 260b1e21c97STrond Myklebust array->page_full = 1; 261b1e21c97STrond Myklebust return -ENOSPC; 262b1e21c97STrond Myklebust } 263d1bacf9eSBryan Schumaker return 0; 264d1bacf9eSBryan Schumaker } 265d1bacf9eSBryan Schumaker 266d1bacf9eSBryan Schumaker static 267d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 268d1bacf9eSBryan Schumaker { 269a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 2704a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 271a52a8a6aSTrond Myklebust const char *name; 2724a201d6eSTrond Myklebust int ret; 2734a201d6eSTrond Myklebust 274a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 275a52a8a6aSTrond Myklebust if (!name) 276a52a8a6aSTrond Myklebust return -ENOMEM; 2773020093fSTrond Myklebust 278a52a8a6aSTrond Myklebust array = kmap_atomic(page); 279b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 280a52a8a6aSTrond Myklebust if (ret) { 281a52a8a6aSTrond Myklebust kfree(name); 2823020093fSTrond Myklebust goto out; 283a52a8a6aSTrond Myklebust } 2843020093fSTrond Myklebust 285b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 2864a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2874a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2880b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 289a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 290a52a8a6aSTrond Myklebust cache_entry->name = name; 291d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 292762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie) 293762567b7STrond Myklebust array->cookies_are_ordered = 0; 2948cd51a0cSTrond Myklebust array->size++; 29547c716cbSTrond Myklebust if (entry->eof != 0) 296b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 2974a201d6eSTrond Myklebust out: 298a52a8a6aSTrond Myklebust kunmap_atomic(array); 2994a201d6eSTrond Myklebust return ret; 300d1bacf9eSBryan Schumaker } 301d1bacf9eSBryan Schumaker 3021f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 3031f1d4aa4STrond Myklebust pgoff_t index, u64 last_cookie) 3041f1d4aa4STrond Myklebust { 3051f1d4aa4STrond Myklebust struct page *page; 3061f1d4aa4STrond Myklebust 3071f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 3081f1d4aa4STrond Myklebust if (page && !PageUptodate(page)) { 3091f1d4aa4STrond Myklebust nfs_readdir_page_init_array(page, last_cookie); 3101f1d4aa4STrond Myklebust if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0) 3111f1d4aa4STrond Myklebust nfs_zap_mapping(mapping->host, mapping); 3121f1d4aa4STrond Myklebust SetPageUptodate(page); 3131f1d4aa4STrond Myklebust } 3141f1d4aa4STrond Myklebust 3151f1d4aa4STrond Myklebust return page; 3161f1d4aa4STrond Myklebust } 3171f1d4aa4STrond Myklebust 3181f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 3191f1d4aa4STrond Myklebust { 3201f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3211f1d4aa4STrond Myklebust u64 ret; 3221f1d4aa4STrond Myklebust 3231f1d4aa4STrond Myklebust array = kmap_atomic(page); 3241f1d4aa4STrond Myklebust ret = array->last_cookie; 3251f1d4aa4STrond Myklebust kunmap_atomic(array); 3261f1d4aa4STrond Myklebust return ret; 3271f1d4aa4STrond Myklebust } 3281f1d4aa4STrond Myklebust 3291f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 3301f1d4aa4STrond Myklebust { 3311f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3321f1d4aa4STrond Myklebust bool ret; 3331f1d4aa4STrond Myklebust 3341f1d4aa4STrond Myklebust array = kmap_atomic(page); 3351f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 3361f1d4aa4STrond Myklebust kunmap_atomic(array); 3371f1d4aa4STrond Myklebust return ret; 3381f1d4aa4STrond Myklebust } 3391f1d4aa4STrond Myklebust 340b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 341b1e21c97STrond Myklebust { 342b1e21c97STrond Myklebust struct nfs_cache_array *array; 343b1e21c97STrond Myklebust 344b1e21c97STrond Myklebust array = kmap_atomic(page); 345b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 346b1e21c97STrond Myklebust kunmap_atomic(array); 347b1e21c97STrond Myklebust } 348b1e21c97STrond Myklebust 3493b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 3503b2a09f1STrond Myklebust { 3513b2a09f1STrond Myklebust unlock_page(page); 3523b2a09f1STrond Myklebust put_page(page); 3533b2a09f1STrond Myklebust } 3543b2a09f1STrond Myklebust 3553b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 3563b2a09f1STrond Myklebust pgoff_t index, u64 cookie) 3573b2a09f1STrond Myklebust { 3583b2a09f1STrond Myklebust struct page *page; 3593b2a09f1STrond Myklebust 3603b2a09f1STrond Myklebust page = nfs_readdir_page_get_locked(mapping, index, cookie); 3613b2a09f1STrond Myklebust if (page) { 3623b2a09f1STrond Myklebust if (nfs_readdir_page_last_cookie(page) == cookie) 3633b2a09f1STrond Myklebust return page; 3643b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 3653b2a09f1STrond Myklebust } 3663b2a09f1STrond Myklebust return NULL; 3673b2a09f1STrond Myklebust } 3683b2a09f1STrond Myklebust 36959e356a9STrond Myklebust static inline 37059e356a9STrond Myklebust int is_32bit_api(void) 37159e356a9STrond Myklebust { 37259e356a9STrond Myklebust #ifdef CONFIG_COMPAT 37359e356a9STrond Myklebust return in_compat_syscall(); 37459e356a9STrond Myklebust #else 37559e356a9STrond Myklebust return (BITS_PER_LONG == 32); 37659e356a9STrond Myklebust #endif 37759e356a9STrond Myklebust } 37859e356a9STrond Myklebust 37959e356a9STrond Myklebust static 38059e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 38159e356a9STrond Myklebust { 38259e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 38359e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 38459e356a9STrond Myklebust return false; 38559e356a9STrond Myklebust return true; 38659e356a9STrond Myklebust } 38759e356a9STrond Myklebust 3886c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 3896c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 390d1bacf9eSBryan Schumaker { 39123db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 392d1bacf9eSBryan Schumaker unsigned int index; 393d1bacf9eSBryan Schumaker 394d1bacf9eSBryan Schumaker if (diff < 0) 395d1bacf9eSBryan Schumaker goto out_eof; 396d1bacf9eSBryan Schumaker if (diff >= array->size) { 397b1e21c97STrond Myklebust if (array->page_is_eof) 398d1bacf9eSBryan Schumaker goto out_eof; 399d1bacf9eSBryan Schumaker return -EAGAIN; 400d1bacf9eSBryan Schumaker } 401d1bacf9eSBryan Schumaker 402d1bacf9eSBryan Schumaker index = (unsigned int)diff; 4032e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 404d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 405d1bacf9eSBryan Schumaker return 0; 406d1bacf9eSBryan Schumaker out_eof: 4076089dd0dSThomas Meyer desc->eof = true; 408d1bacf9eSBryan Schumaker return -EBADCOOKIE; 409d1bacf9eSBryan Schumaker } 410d1bacf9eSBryan Schumaker 4114db72b40SJeff Layton static bool 4124db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 4134db72b40SJeff Layton { 4144db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 4154db72b40SJeff Layton return false; 4164db72b40SJeff Layton smp_rmb(); 4174db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 4184db72b40SJeff Layton } 4194db72b40SJeff Layton 420762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 421762567b7STrond Myklebust u64 cookie) 422762567b7STrond Myklebust { 423762567b7STrond Myklebust if (!array->cookies_are_ordered) 424762567b7STrond Myklebust return true; 425762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */ 426762567b7STrond Myklebust if (cookie >= array->last_cookie) 427762567b7STrond Myklebust return false; 428762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie) 429762567b7STrond Myklebust return false; 430762567b7STrond Myklebust return true; 431762567b7STrond Myklebust } 432762567b7STrond Myklebust 4336c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 4346c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 435d1bacf9eSBryan Schumaker { 436d1bacf9eSBryan Schumaker int i; 4378ef2ce3eSBryan Schumaker loff_t new_pos; 438d1bacf9eSBryan Schumaker int status = -EAGAIN; 439d1bacf9eSBryan Schumaker 440762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 441762567b7STrond Myklebust goto check_eof; 442762567b7STrond Myklebust 443d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 4442e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 445496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 4460c030806STrond Myklebust 4478ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 4482e7a4641STrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount || 4494db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 4502e7a4641STrond Myklebust desc->duped = 0; 4512e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 45259e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 4532e7a4641STrond Myklebust if (desc->duped > 0 4542e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 4550c030806STrond Myklebust if (printk_ratelimit()) { 4566de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 4570c030806STrond Myklebust "Please contact your server vendor. " 458a52a8a6aSTrond Myklebust "The file: %s has duplicate cookie %llu\n", 459a52a8a6aSTrond Myklebust desc->file, array->array[i].name, desc->dir_cookie); 4600c030806STrond Myklebust } 4610c030806STrond Myklebust status = -ELOOP; 4620c030806STrond Myklebust goto out; 4630c030806STrond Myklebust } 4642e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 4652e7a4641STrond Myklebust desc->duped = -1; 4668ef2ce3eSBryan Schumaker } 46759e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 4682e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 46959e356a9STrond Myklebust else 47023db8620SAl Viro desc->ctx->pos = new_pos; 47159e356a9STrond Myklebust desc->prev_index = new_pos; 4728cd51a0cSTrond Myklebust desc->cache_entry_index = i; 47347c716cbSTrond Myklebust return 0; 4748cd51a0cSTrond Myklebust } 4758cd51a0cSTrond Myklebust } 476762567b7STrond Myklebust check_eof: 477b1e21c97STrond Myklebust if (array->page_is_eof) { 478d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 4792e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 4806089dd0dSThomas Meyer desc->eof = true; 481d1bacf9eSBryan Schumaker } 4820c030806STrond Myklebust out: 483d1bacf9eSBryan Schumaker return status; 484d1bacf9eSBryan Schumaker } 485d1bacf9eSBryan Schumaker 4866c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 487d1bacf9eSBryan Schumaker { 488d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 48947c716cbSTrond Myklebust int status; 490d1bacf9eSBryan Schumaker 491ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 492d1bacf9eSBryan Schumaker 4932e7a4641STrond Myklebust if (desc->dir_cookie == 0) 494d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 495d1bacf9eSBryan Schumaker else 496d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 497d1bacf9eSBryan Schumaker 49847c716cbSTrond Myklebust if (status == -EAGAIN) { 4990aded708STrond Myklebust desc->last_cookie = array->last_cookie; 500e47c085aSTrond Myklebust desc->current_index += array->size; 50147c716cbSTrond Myklebust desc->page_index++; 50247c716cbSTrond Myklebust } 503ed09222dSTrond Myklebust kunmap_atomic(array); 504d1bacf9eSBryan Schumaker return status; 505d1bacf9eSBryan Schumaker } 506d1bacf9eSBryan Schumaker 507d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 50893b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 509b593c09fSTrond Myklebust __be32 *verf, u64 cookie, 510b593c09fSTrond Myklebust struct page **pages, size_t bufsize, 511b593c09fSTrond Myklebust __be32 *verf_res) 512d1bacf9eSBryan Schumaker { 51382e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 51482e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 51582e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 51682e22a5eSTrond Myklebust .cred = desc->file->f_cred, 517b593c09fSTrond Myklebust .verf = verf, 51882e22a5eSTrond Myklebust .cookie = cookie, 51982e22a5eSTrond Myklebust .pages = pages, 52082e22a5eSTrond Myklebust .page_len = bufsize, 52182e22a5eSTrond Myklebust .plus = desc->plus, 52282e22a5eSTrond Myklebust }; 52382e22a5eSTrond Myklebust struct nfs_readdir_res res = { 52482e22a5eSTrond Myklebust .verf = verf_res, 52582e22a5eSTrond Myklebust }; 5264704f0e2STrond Myklebust unsigned long timestamp, gencount; 5271da177e4SLinus Torvalds int error; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds again: 5301da177e4SLinus Torvalds timestamp = jiffies; 5314704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 532a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 53382e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 5341da177e4SLinus Torvalds if (error < 0) { 5351da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 5361da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 5371da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 5383a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 53982e22a5eSTrond Myklebust desc->plus = arg.plus = false; 5401da177e4SLinus Torvalds goto again; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds goto error; 5431da177e4SLinus Torvalds } 5441f4eab7eSNeil Brown desc->timestamp = timestamp; 5454704f0e2STrond Myklebust desc->gencount = gencount; 546d1bacf9eSBryan Schumaker error: 547d1bacf9eSBryan Schumaker return error; 548d1bacf9eSBryan Schumaker } 549d1bacf9eSBryan Schumaker 5506c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 551573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 552d1bacf9eSBryan Schumaker { 55359e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 554573c4e1eSChuck Lever int error; 555d1bacf9eSBryan Schumaker 55659e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 557573c4e1eSChuck Lever if (error) 558573c4e1eSChuck Lever return error; 559d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 560d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 561d1bacf9eSBryan Schumaker return 0; 562d1bacf9eSBryan Schumaker } 563d1bacf9eSBryan Schumaker 564fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 565fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 566fa923369STrond Myklebust */ 567d39ab9deSBryan Schumaker static 568d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 569d39ab9deSBryan Schumaker { 570d8fdb47fSTrond Myklebust struct inode *inode; 571fa923369STrond Myklebust struct nfs_inode *nfsi; 572fa923369STrond Myklebust 5732b0143b5SDavid Howells if (d_really_is_negative(dentry)) 5742b0143b5SDavid Howells return 0; 575fa923369STrond Myklebust 576d8fdb47fSTrond Myklebust inode = d_inode(dentry); 577d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 578d8fdb47fSTrond Myklebust return 0; 579d8fdb47fSTrond Myklebust 580d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 5817dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 582d39ab9deSBryan Schumaker return 0; 5837dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 5847dc72d5fSTrond Myklebust return 0; 5857dc72d5fSTrond Myklebust return 1; 586d39ab9deSBryan Schumaker } 587d39ab9deSBryan Schumaker 588d39ab9deSBryan Schumaker static 58923db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 590d69ee9b8STrond Myklebust { 591d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 592d69ee9b8STrond Myklebust return false; 593d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 594d69ee9b8STrond Myklebust return true; 59523db8620SAl Viro if (ctx->pos == 0) 596d69ee9b8STrond Myklebust return true; 597d69ee9b8STrond Myklebust return false; 598d69ee9b8STrond Myklebust } 599d69ee9b8STrond Myklebust 600d69ee9b8STrond Myklebust /* 60163519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 60263519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 603d69ee9b8STrond Myklebust * directory. 604d69ee9b8STrond Myklebust */ 605d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 606d69ee9b8STrond Myklebust { 60763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 60863519fbcSTrond Myklebust 60963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 61063519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 61163519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 612d69ee9b8STrond Myklebust } 613d69ee9b8STrond Myklebust 614311324adSTrond Myklebust /* 615311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 616311324adSTrond Myklebust * 617311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 618311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 619311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 620311324adSTrond Myklebust * cache flush. 621311324adSTrond Myklebust */ 622311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 623311324adSTrond Myklebust { 62463519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 62563519fbcSTrond Myklebust 62663519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 62763519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 62863519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 629227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 630227823d2SDai Ngo nfsi->page_index + 1, -1); 631311324adSTrond Myklebust } 632311324adSTrond Myklebust } 633311324adSTrond Myklebust 634d69ee9b8STrond Myklebust static 635a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 636a1147b82STrond Myklebust unsigned long dir_verifier) 637d39ab9deSBryan Schumaker { 63826fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 6399ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 6404a201d6eSTrond Myklebust struct dentry *dentry; 6414a201d6eSTrond Myklebust struct dentry *alias; 642d39ab9deSBryan Schumaker struct inode *inode; 643aa9c2669SDavid Quigley int status; 644d39ab9deSBryan Schumaker 645fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 646fa923369STrond Myklebust return; 6476c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 6486c441c25STrond Myklebust return; 64978d04af4STrond Myklebust if (filename.len == 0) 65078d04af4STrond Myklebust return; 65178d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 65278d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 65378d04af4STrond Myklebust return; 65478d04af4STrond Myklebust /* ...or '/' */ 65578d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 65678d04af4STrond Myklebust return; 6574a201d6eSTrond Myklebust if (filename.name[0] == '.') { 6584a201d6eSTrond Myklebust if (filename.len == 1) 6594a201d6eSTrond Myklebust return; 6604a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 6614a201d6eSTrond Myklebust return; 6624a201d6eSTrond Myklebust } 6638387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 664d39ab9deSBryan Schumaker 6654a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 6669ac3d3e8SAl Viro again: 6679ac3d3e8SAl Viro if (!dentry) { 6689ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 6699ac3d3e8SAl Viro if (IS_ERR(dentry)) 6709ac3d3e8SAl Viro return; 6719ac3d3e8SAl Viro } 6729ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 6736c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 6746c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 6756c441c25STrond Myklebust &entry->fattr->fsid)) 6766c441c25STrond Myklebust goto out; 677d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 6787dc72d5fSTrond Myklebust if (!entry->fh->size) 6797dc72d5fSTrond Myklebust goto out; 680a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 6812b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 682aa9c2669SDavid Quigley if (!status) 6832b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 684d39ab9deSBryan Schumaker goto out; 685d39ab9deSBryan Schumaker } else { 6865542aa2fSEric W. Biederman d_invalidate(dentry); 687d39ab9deSBryan Schumaker dput(dentry); 6889ac3d3e8SAl Viro dentry = NULL; 6899ac3d3e8SAl Viro goto again; 690d39ab9deSBryan Schumaker } 691d39ab9deSBryan Schumaker } 6927dc72d5fSTrond Myklebust if (!entry->fh->size) { 6937dc72d5fSTrond Myklebust d_lookup_done(dentry); 6947dc72d5fSTrond Myklebust goto out; 6957dc72d5fSTrond Myklebust } 696d39ab9deSBryan Schumaker 6971775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 69841d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 6999ac3d3e8SAl Viro d_lookup_done(dentry); 7009ac3d3e8SAl Viro if (alias) { 701d39ab9deSBryan Schumaker if (IS_ERR(alias)) 702d39ab9deSBryan Schumaker goto out; 7039ac3d3e8SAl Viro dput(dentry); 7049ac3d3e8SAl Viro dentry = alias; 7059ac3d3e8SAl Viro } 706a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 707d39ab9deSBryan Schumaker out: 708d39ab9deSBryan Schumaker dput(dentry); 709d39ab9deSBryan Schumaker } 710d39ab9deSBryan Schumaker 711d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 7123b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 7133b2a09f1STrond Myklebust struct nfs_entry *entry, 7143b2a09f1STrond Myklebust struct page **xdr_pages, 71535df59d3STrond Myklebust unsigned int buflen, 71635df59d3STrond Myklebust struct page **arrays, 71735df59d3STrond Myklebust size_t narrays) 718d1bacf9eSBryan Schumaker { 7193b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 720babddc72SBryan Schumaker struct xdr_stream stream; 721f7da7a12SBenny Halevy struct xdr_buf buf; 72235df59d3STrond Myklebust struct page *scratch, *new, *page = *arrays; 7235c346854STrond Myklebust int status; 724babddc72SBryan Schumaker 7256650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 7266650239aSTrond Myklebust if (scratch == NULL) 7276650239aSTrond Myklebust return -ENOMEM; 728babddc72SBryan Schumaker 729f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 7300ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch); 73199424380SBryan Schumaker 73299424380SBryan Schumaker do { 733d33030e2SJeffrey Mitchell if (entry->label) 734d33030e2SJeffrey Mitchell entry->label->len = NFS4_MAXLABELLEN; 735d33030e2SJeffrey Mitchell 73699424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 737972bcdf2STrond Myklebust if (status != 0) 73899424380SBryan Schumaker break; 7395c346854STrond Myklebust 740a7a3b1e9SBenjamin Coddington if (desc->plus) 741a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 742a1147b82STrond Myklebust desc->dir_verifier); 7438cd51a0cSTrond Myklebust 744db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 7453b2a09f1STrond Myklebust if (status != -ENOSPC) 7463b2a09f1STrond Myklebust continue; 74799424380SBryan Schumaker 74835df59d3STrond Myklebust if (page->mapping != mapping) { 74935df59d3STrond Myklebust if (!--narrays) 7503b2a09f1STrond Myklebust break; 75135df59d3STrond Myklebust new = nfs_readdir_page_array_alloc(entry->prev_cookie, 75235df59d3STrond Myklebust GFP_KERNEL); 75335df59d3STrond Myklebust if (!new) 75435df59d3STrond Myklebust break; 75535df59d3STrond Myklebust arrays++; 75635df59d3STrond Myklebust *arrays = page = new; 75735df59d3STrond Myklebust } else { 75835df59d3STrond Myklebust new = nfs_readdir_page_get_next(mapping, 75935df59d3STrond Myklebust page->index + 1, 7603b2a09f1STrond Myklebust entry->prev_cookie); 7613b2a09f1STrond Myklebust if (!new) 7623b2a09f1STrond Myklebust break; 76335df59d3STrond Myklebust if (page != *arrays) 7643b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7653b2a09f1STrond Myklebust page = new; 76656e4ebf8SBryan Schumaker } 7673b2a09f1STrond Myklebust status = nfs_readdir_add_to_array(entry, page); 768972bcdf2STrond Myklebust } while (!status && !entry->eof); 76956e4ebf8SBryan Schumaker 770972bcdf2STrond Myklebust switch (status) { 771972bcdf2STrond Myklebust case -EBADCOOKIE: 772972bcdf2STrond Myklebust if (entry->eof) { 773b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 7748cd51a0cSTrond Myklebust status = 0; 77556e4ebf8SBryan Schumaker } 776972bcdf2STrond Myklebust break; 777972bcdf2STrond Myklebust case -ENOSPC: 778972bcdf2STrond Myklebust case -EAGAIN: 779972bcdf2STrond Myklebust status = 0; 780972bcdf2STrond Myklebust break; 781972bcdf2STrond Myklebust } 7826650239aSTrond Myklebust 78335df59d3STrond Myklebust if (page != *arrays) 7843b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7856650239aSTrond Myklebust 7866650239aSTrond Myklebust put_page(scratch); 787d1bacf9eSBryan Schumaker return status; 78856e4ebf8SBryan Schumaker } 78956e4ebf8SBryan Schumaker 7901a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 79156e4ebf8SBryan Schumaker { 7921a34c8c9STrond Myklebust while (npages--) 7931a34c8c9STrond Myklebust put_page(pages[npages]); 7941a34c8c9STrond Myklebust kfree(pages); 79556e4ebf8SBryan Schumaker } 79656e4ebf8SBryan Schumaker 79756e4ebf8SBryan Schumaker /* 798bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 799bf211ca1Szhangliguang * to nfs_readdir_free_pages() 80056e4ebf8SBryan Schumaker */ 8011a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 80256e4ebf8SBryan Schumaker { 8031a34c8c9STrond Myklebust struct page **pages; 8041a34c8c9STrond Myklebust size_t i; 80556e4ebf8SBryan Schumaker 8061a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 8071a34c8c9STrond Myklebust if (!pages) 8081a34c8c9STrond Myklebust return NULL; 80956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 81056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 81156e4ebf8SBryan Schumaker if (page == NULL) 81256e4ebf8SBryan Schumaker goto out_freepages; 81356e4ebf8SBryan Schumaker pages[i] = page; 81456e4ebf8SBryan Schumaker } 8151a34c8c9STrond Myklebust return pages; 81656e4ebf8SBryan Schumaker 81756e4ebf8SBryan Schumaker out_freepages: 818c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 8191a34c8c9STrond Myklebust return NULL; 820d1bacf9eSBryan Schumaker } 821d1bacf9eSBryan Schumaker 8226c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 82335df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res, 82435df59d3STrond Myklebust struct page **arrays, size_t narrays) 825d1bacf9eSBryan Schumaker { 8261a34c8c9STrond Myklebust struct page **pages; 82735df59d3STrond Myklebust struct page *page = *arrays; 8286b75cf9eSTrond Myklebust struct nfs_entry *entry; 8291a34c8c9STrond Myklebust size_t array_size; 830b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file); 8311a34c8c9STrond Myklebust size_t dtsize = NFS_SERVER(inode)->dtsize; 8328cd51a0cSTrond Myklebust int status = -ENOMEM; 833d1bacf9eSBryan Schumaker 8346b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 8356b75cf9eSTrond Myklebust if (!entry) 8366b75cf9eSTrond Myklebust return -ENOMEM; 8376b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 8386b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 8396b75cf9eSTrond Myklebust entry->fattr = nfs_alloc_fattr(); 8406b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 8416b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 842d1bacf9eSBryan Schumaker goto out; 843d1bacf9eSBryan Schumaker 8446b75cf9eSTrond Myklebust entry->label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 8456b75cf9eSTrond Myklebust if (IS_ERR(entry->label)) { 8466b75cf9eSTrond Myklebust status = PTR_ERR(entry->label); 84714c43f76SDavid Quigley goto out; 84814c43f76SDavid Quigley } 84914c43f76SDavid Quigley 8501a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 8511a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 8521a34c8c9STrond Myklebust if (!pages) 853e762a639STrond Myklebust goto out_release_label; 854d1bacf9eSBryan Schumaker 855d1bacf9eSBryan Schumaker do { 856ac396128STrond Myklebust unsigned int pglen; 857b593c09fSTrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, 858b593c09fSTrond Myklebust pages, dtsize, 859b593c09fSTrond Myklebust verf_res); 860d1bacf9eSBryan Schumaker if (status < 0) 861d1bacf9eSBryan Schumaker break; 862972bcdf2STrond Myklebust 863ac396128STrond Myklebust pglen = status; 864972bcdf2STrond Myklebust if (pglen == 0) { 865972bcdf2STrond Myklebust nfs_readdir_page_set_eof(page); 8668cd51a0cSTrond Myklebust break; 8678cd51a0cSTrond Myklebust } 868972bcdf2STrond Myklebust 869*ee3707aeSNagendra S Tomar verf_arg = verf_res; 870*ee3707aeSNagendra S Tomar 87135df59d3STrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, pglen, 87235df59d3STrond Myklebust arrays, narrays); 873e762a639STrond Myklebust } while (!status && nfs_readdir_page_needs_filling(page)); 874d1bacf9eSBryan Schumaker 875c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 876e762a639STrond Myklebust out_release_label: 8776b75cf9eSTrond Myklebust nfs4_label_free(entry->label); 878d1bacf9eSBryan Schumaker out: 8796b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 8806b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 8816b75cf9eSTrond Myklebust kfree(entry); 882d1bacf9eSBryan Schumaker return status; 883d1bacf9eSBryan Schumaker } 884d1bacf9eSBryan Schumaker 8851f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 8861da177e4SLinus Torvalds { 88709cbfeafSKirill A. Shutemov put_page(desc->page); 8881da177e4SLinus Torvalds desc->page = NULL; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911f1d4aa4STrond Myklebust static void 8921f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 8931da177e4SLinus Torvalds { 8941f1d4aa4STrond Myklebust unlock_page(desc->page); 8951f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 8961f1d4aa4STrond Myklebust } 8971f1d4aa4STrond Myklebust 8981f1d4aa4STrond Myklebust static struct page * 8991f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 9001f1d4aa4STrond Myklebust { 9011f1d4aa4STrond Myklebust return nfs_readdir_page_get_locked(desc->file->f_mapping, 9021f1d4aa4STrond Myklebust desc->page_index, 9031f1d4aa4STrond Myklebust desc->last_cookie); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 907d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 908114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 9091da177e4SLinus Torvalds */ 9106c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 911d1bacf9eSBryan Schumaker { 912227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 913227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 914b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 915d1bacf9eSBryan Schumaker int res; 916d1bacf9eSBryan Schumaker 9171f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 9181f1d4aa4STrond Myklebust if (!desc->page) 9191f1d4aa4STrond Myklebust return -ENOMEM; 9201f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 92135df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 92235df59d3STrond Myklebust &desc->page, 1); 9239fff59edSTrond Myklebust if (res < 0) { 9249fff59edSTrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 9259fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) { 9269fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping); 9279fff59edSTrond Myklebust desc->page_index = 0; 9289fff59edSTrond Myklebust return -EAGAIN; 9299fff59edSTrond Myklebust } 9309fff59edSTrond Myklebust return res; 9319fff59edSTrond Myklebust } 932b593c09fSTrond Myklebust memcpy(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf)); 9331f1d4aa4STrond Myklebust } 934114de382STrond Myklebust res = nfs_readdir_search_array(desc); 935227823d2SDai Ngo if (res == 0) { 936227823d2SDai Ngo nfsi->page_index = desc->page_index; 937114de382STrond Myklebust return 0; 938114de382STrond Myklebust } 9391f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 940d1bacf9eSBryan Schumaker return res; 941d1bacf9eSBryan Schumaker } 942d1bacf9eSBryan Schumaker 943794092c5STrond Myklebust static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc) 944794092c5STrond Myklebust { 945794092c5STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 946794092c5STrond Myklebust struct inode *dir = file_inode(desc->file); 947794092c5STrond Myklebust unsigned int dtsize = NFS_SERVER(dir)->dtsize; 948794092c5STrond Myklebust loff_t size = i_size_read(dir); 949794092c5STrond Myklebust 950794092c5STrond Myklebust /* 951794092c5STrond Myklebust * Default to uncached readdir if the page cache is empty, and 952794092c5STrond Myklebust * we're looking for a non-zero cookie in a large directory. 953794092c5STrond Myklebust */ 954794092c5STrond Myklebust return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize; 955794092c5STrond Myklebust } 956794092c5STrond Myklebust 957d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 9586c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 9591da177e4SLinus Torvalds { 9608cd51a0cSTrond Myklebust int res; 961d1bacf9eSBryan Schumaker 962794092c5STrond Myklebust if (nfs_readdir_dont_search_cache(desc)) 963794092c5STrond Myklebust return -EBADCOOKIE; 964794092c5STrond Myklebust 9659fff59edSTrond Myklebust do { 9660aded708STrond Myklebust if (desc->page_index == 0) { 9678cd51a0cSTrond Myklebust desc->current_index = 0; 96859e356a9STrond Myklebust desc->prev_index = 0; 9690aded708STrond Myklebust desc->last_cookie = 0; 9700aded708STrond Myklebust } 971114de382STrond Myklebust res = find_and_lock_cache_page(desc); 97247c716cbSTrond Myklebust } while (res == -EAGAIN); 9731da177e4SLinus Torvalds return res; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 9781da177e4SLinus Torvalds */ 979dbeaf8c9STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc) 9801da177e4SLinus Torvalds { 9811da177e4SLinus Torvalds struct file *file = desc->file; 982b593c09fSTrond Myklebust struct nfs_inode *nfsi = NFS_I(file_inode(file)); 983dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 984dbeaf8c9STrond Myklebust unsigned int i = 0; 9858ef2ce3eSBryan Schumaker 9860795bf83SFabian Frederick array = kmap(desc->page); 987d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 988ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 9891da177e4SLinus Torvalds 990ece0b423STrond Myklebust ent = &array->array[i]; 991a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 99223db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 9936089dd0dSThomas Meyer desc->eof = true; 9941da177e4SLinus Torvalds break; 995ece0b423STrond Myklebust } 996b593c09fSTrond Myklebust memcpy(desc->verf, nfsi->cookieverf, sizeof(desc->verf)); 997d1bacf9eSBryan Schumaker if (i < (array->size-1)) 9982e7a4641STrond Myklebust desc->dir_cookie = array->array[i+1].cookie; 999d1bacf9eSBryan Schumaker else 10002e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 100159e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 10022e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 100359e356a9STrond Myklebust else 100459e356a9STrond Myklebust desc->ctx->pos++; 10052e7a4641STrond Myklebust if (desc->duped != 0) 10062e7a4641STrond Myklebust desc->duped = 1; 10078cd51a0cSTrond Myklebust } 1008b1e21c97STrond Myklebust if (array->page_is_eof) 10096089dd0dSThomas Meyer desc->eof = true; 1010d1bacf9eSBryan Schumaker 10110795bf83SFabian Frederick kunmap(desc->page); 1012dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1013dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* 10171da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 10181da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 10191da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 10201da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 10211da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 10221da177e4SLinus Torvalds * 10231da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 10241da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 10251da177e4SLinus Torvalds * we should already have a complete representation of the 10261da177e4SLinus Torvalds * directory in the page cache by the time we get here. 10271da177e4SLinus Torvalds */ 10286c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 10291da177e4SLinus Torvalds { 103035df59d3STrond Myklebust struct page **arrays; 103135df59d3STrond Myklebust size_t i, sz = 512; 1032b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE]; 103335df59d3STrond Myklebust int status = -ENOMEM; 10341da177e4SLinus Torvalds 103535df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 10362e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 10371da177e4SLinus Torvalds 103835df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 103935df59d3STrond Myklebust if (!arrays) 10401da177e4SLinus Torvalds goto out; 104135df59d3STrond Myklebust arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 104235df59d3STrond Myklebust if (!arrays[0]) 104335df59d3STrond Myklebust goto out; 10441da177e4SLinus Torvalds 10457a8e1dc3STrond Myklebust desc->page_index = 0; 10462e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 10472e7a4641STrond Myklebust desc->duped = 0; 10487a8e1dc3STrond Myklebust 104935df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1050d1bacf9eSBryan Schumaker 105135df59d3STrond Myklebust for (i = 0; !desc->eof && i < sz && arrays[i]; i++) { 105235df59d3STrond Myklebust desc->page = arrays[i]; 1053dbeaf8c9STrond Myklebust nfs_do_filldir(desc); 105435df59d3STrond Myklebust } 105535df59d3STrond Myklebust desc->page = NULL; 10561da177e4SLinus Torvalds 105735df59d3STrond Myklebust 105835df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++) 105935df59d3STrond Myklebust nfs_readdir_page_array_free(arrays[i]); 10601da177e4SLinus Torvalds out: 106135df59d3STrond Myklebust kfree(arrays); 106235df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 10631da177e4SLinus Torvalds return status; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 106600a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 106700a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 106800a92642SOlivier Galibert whole directory. 10691da177e4SLinus Torvalds */ 107023db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 10711da177e4SLinus Torvalds { 1072be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 10732b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 107423db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 10756b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 10766b75cf9eSTrond Myklebust int res; 10771da177e4SLinus Torvalds 10786de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 10796de1472fSAl Viro file, (long long)ctx->pos); 108091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 108191d5b470SChuck Lever 10821da177e4SLinus Torvalds /* 108323db8620SAl Viro * ctx->pos points to the dirent entry number. 1084f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 108500a92642SOlivier Galibert * to either find the entry with the appropriate number or 108600a92642SOlivier Galibert * revalidate the cookie. 10871da177e4SLinus Torvalds */ 10886b75cf9eSTrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) { 108923db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 1090fccca7fcSTrond Myklebust if (res < 0) 1091fccca7fcSTrond Myklebust goto out; 10926b75cf9eSTrond Myklebust } 10936b75cf9eSTrond Myklebust 10946b75cf9eSTrond Myklebust res = -ENOMEM; 10956b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 10966b75cf9eSTrond Myklebust if (!desc) 10976b75cf9eSTrond Myklebust goto out; 10986b75cf9eSTrond Myklebust desc->file = file; 10996b75cf9eSTrond Myklebust desc->ctx = ctx; 11006b75cf9eSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx); 1101fccca7fcSTrond Myklebust 11022e7a4641STrond Myklebust spin_lock(&file->f_lock); 11032e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 11042e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 11052e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 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 111047c716cbSTrond Myklebust do { 11111da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 111200a92642SOlivier Galibert 11131da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1114ece0b423STrond Myklebust res = 0; 11151da177e4SLinus Torvalds /* This means either end of directory */ 11162e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 11171da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 111823db8620SAl Viro res = uncached_readdir(desc); 1119ece0b423STrond Myklebust if (res == 0) 11201da177e4SLinus Torvalds continue; 11219fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) 11229fff59edSTrond Myklebust res = 0; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds break; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 11273a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 11281da177e4SLinus Torvalds nfs_zap_caches(inode); 1129baf57a09STrond Myklebust desc->page_index = 0; 1130a7a3b1e9SBenjamin Coddington desc->plus = false; 1131a7a3b1e9SBenjamin Coddington desc->eof = false; 11321da177e4SLinus Torvalds continue; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds if (res < 0) 11351da177e4SLinus Torvalds break; 11361da177e4SLinus Torvalds 1137dbeaf8c9STrond Myklebust nfs_do_filldir(desc); 11381f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 113947c716cbSTrond Myklebust } while (!desc->eof); 11402e7a4641STrond Myklebust 11412e7a4641STrond Myklebust spin_lock(&file->f_lock); 11422e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 11432e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 11442e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 11452e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 1146b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 11472e7a4641STrond Myklebust spin_unlock(&file->f_lock); 11482e7a4641STrond Myklebust 11496b75cf9eSTrond Myklebust kfree(desc); 11506b75cf9eSTrond Myklebust 1151fccca7fcSTrond Myklebust out: 11526de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 11531da177e4SLinus Torvalds return res; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 1156965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1157f0dd2136STrond Myklebust { 1158480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1159b84e06c5SChuck Lever 11606de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 11616de1472fSAl Viro filp, offset, whence); 1162b84e06c5SChuck Lever 1163965c8e59SAndrew Morton switch (whence) { 1164f0dd2136STrond Myklebust default: 1165b2b1ff3dSTrond Myklebust return -EINVAL; 1166b2b1ff3dSTrond Myklebust case SEEK_SET: 1167b2b1ff3dSTrond Myklebust if (offset < 0) 1168b2b1ff3dSTrond Myklebust return -EINVAL; 116983f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1170b2b1ff3dSTrond Myklebust break; 1171b2b1ff3dSTrond Myklebust case SEEK_CUR: 1172b2b1ff3dSTrond Myklebust if (offset == 0) 1173b2b1ff3dSTrond Myklebust return filp->f_pos; 117483f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1175b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1176b2b1ff3dSTrond Myklebust if (offset < 0) { 117783f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1178b2b1ff3dSTrond Myklebust return -EINVAL; 1179b2b1ff3dSTrond Myklebust } 1180f0dd2136STrond Myklebust } 1181f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1182f0dd2136STrond Myklebust filp->f_pos = offset; 118359e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 118459e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 118559e356a9STrond Myklebust else 1186480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 1187b593c09fSTrond Myklebust if (offset == 0) 1188b593c09fSTrond Myklebust memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf)); 11898ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1190f0dd2136STrond Myklebust } 119183f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1192f0dd2136STrond Myklebust return offset; 1193f0dd2136STrond Myklebust } 1194f0dd2136STrond Myklebust 11951da177e4SLinus Torvalds /* 11961da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 11971da177e4SLinus Torvalds * is a dummy operation. 11981da177e4SLinus Torvalds */ 119902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 120002c24a82SJosef Bacik int datasync) 12011da177e4SLinus Torvalds { 12026de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 12031e7cb3dcSChuck Lever 120411decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 12051da177e4SLinus Torvalds return 0; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 1208bfc69a45STrond Myklebust /** 1209bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1210302fad7bSTrond Myklebust * @dir: pointer to directory inode 1211bfc69a45STrond Myklebust * 1212bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1213bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1214bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1215bfc69a45STrond Myklebust * 1216efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1217efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1218efeda80dSTrond Myklebust * 1219bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1220bfc69a45STrond Myklebust */ 1221bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1222bfc69a45STrond Myklebust { 1223efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1224bfc69a45STrond Myklebust } 122589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1226bfc69a45STrond Myklebust 1227efeda80dSTrond Myklebust /** 1228efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1229efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1230efeda80dSTrond Myklebust * @verf: previously saved change attribute 1231efeda80dSTrond Myklebust * 1232efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1233efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1234efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1235efeda80dSTrond Myklebust */ 1236efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1237efeda80dSTrond Myklebust { 1238efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1239efeda80dSTrond Myklebust } 1240efeda80dSTrond Myklebust 1241efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1242efeda80dSTrond Myklebust { 1243efeda80dSTrond Myklebust *verf |= 1UL; 1244efeda80dSTrond Myklebust } 1245efeda80dSTrond Myklebust 1246efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1247efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1248efeda80dSTrond Myklebust { 1249efeda80dSTrond Myklebust *verf &= ~1UL; 1250efeda80dSTrond Myklebust } 1251efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1252efeda80dSTrond Myklebust 1253efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1254efeda80dSTrond Myklebust { 1255efeda80dSTrond Myklebust return verf & 1; 1256efeda80dSTrond Myklebust } 1257efeda80dSTrond Myklebust 1258efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1259efeda80dSTrond Myklebust { 1260efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1261efeda80dSTrond Myklebust } 1262efeda80dSTrond Myklebust 1263efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1264efeda80dSTrond Myklebust { 1265efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1266efeda80dSTrond Myklebust 1267efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1268efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1269efeda80dSTrond Myklebust goto out; 1270efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1271efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1272efeda80dSTrond Myklebust out: 1273efeda80dSTrond Myklebust dentry->d_time = verf; 1274efeda80dSTrond Myklebust } 1275efeda80dSTrond Myklebust 1276efeda80dSTrond Myklebust /** 1277efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1278efeda80dSTrond Myklebust * @dentry: pointer to dentry 1279efeda80dSTrond Myklebust * @verf: verifier to save 1280efeda80dSTrond Myklebust * 1281efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1282efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1283efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1284efeda80dSTrond Myklebust * look it up again after a directory change. 1285efeda80dSTrond Myklebust */ 1286efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1287efeda80dSTrond Myklebust { 1288efeda80dSTrond Myklebust 1289efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1290efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1291efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1292efeda80dSTrond Myklebust } 1293efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1294efeda80dSTrond Myklebust 1295efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1296efeda80dSTrond Myklebust /** 1297efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1298efeda80dSTrond Myklebust * @inode: pointer to inode 1299efeda80dSTrond Myklebust * 1300efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1301efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1302efeda80dSTrond Myklebust * while holding a delegation. 1303efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1304efeda80dSTrond Myklebust * returned or revoked. 1305efeda80dSTrond Myklebust */ 1306efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1307efeda80dSTrond Myklebust { 1308efeda80dSTrond Myklebust struct dentry *alias; 1309efeda80dSTrond Myklebust 1310efeda80dSTrond Myklebust if (!inode) 1311efeda80dSTrond Myklebust return; 1312efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1313efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1314efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1315efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1316efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1317efeda80dSTrond Myklebust } 1318efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1319efeda80dSTrond Myklebust } 1320efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1321efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1322efeda80dSTrond Myklebust 13231da177e4SLinus Torvalds /* 13241da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 13251da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 13261da177e4SLinus Torvalds * and may need to be looked up again. 1327912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 13281da177e4SLinus Torvalds */ 1329912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1330912a108dSNeilBrown int rcu_walk) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds if (IS_ROOT(dentry)) 13331da177e4SLinus Torvalds return 1; 13344eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 13354eec952eSTrond Myklebust return 0; 1336f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 13376ecc5e8fSTrond Myklebust return 0; 1338f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 13391cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1340912a108dSNeilBrown if (rcu_walk) 1341f2c77f4eSTrond Myklebust return 0; 13421cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 13431cd9cb05STrond Myklebust return 0; 13441cd9cb05STrond Myklebust } 1345f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1346f2c77f4eSTrond Myklebust return 0; 1347f2c77f4eSTrond Myklebust return 1; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds /* 1351a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1352a12802caSTrond Myklebust * an O_EXCL create using this path component. 1353a12802caSTrond Myklebust */ 1354fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1355a12802caSTrond Myklebust { 1356a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1357a12802caSTrond Myklebust return 0; 1358fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1359a12802caSTrond Myklebust } 1360a12802caSTrond Myklebust 1361a12802caSTrond Myklebust /* 13621d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 13631d6757fbSTrond Myklebust * 13641d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 13651d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 13661d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 13671d6757fbSTrond Myklebust * 13681d6757fbSTrond Myklebust */ 136965a0c149STrond Myklebust static 1370fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 13711da177e4SLinus Torvalds { 13721da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 137365a0c149STrond Myklebust int ret; 13741da177e4SLinus Torvalds 137536d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 13764e99a1ffSTrond Myklebust return 0; 137747921921STrond Myklebust 137847921921STrond Myklebust if (flags & LOOKUP_OPEN) { 137947921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 138047921921STrond Myklebust case S_IFREG: 138147921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 138247921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 138347921921STrond Myklebust goto out; 1384df561f66SGustavo A. R. Silva fallthrough; 138547921921STrond Myklebust case S_IFDIR: 138647921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 138747921921STrond Myklebust break; 138847921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 138947921921STrond Myklebust goto out_force; 139047921921STrond Myklebust } 139147921921STrond Myklebust } 139247921921STrond Myklebust 13931da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1394fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 13951da177e4SLinus Torvalds goto out_force; 139665a0c149STrond Myklebust out: 1397a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 13981da177e4SLinus Torvalds out_force: 13991fa1e384SNeilBrown if (flags & LOOKUP_RCU) 14001fa1e384SNeilBrown return -ECHILD; 140165a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 140265a0c149STrond Myklebust if (ret != 0) 140365a0c149STrond Myklebust return ret; 140465a0c149STrond Myklebust goto out; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 140782e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode) 140882e7ca13STrond Myklebust { 140982e7ca13STrond Myklebust spin_lock(&inode->i_lock); 1410fd6d3feeSTrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE); 141182e7ca13STrond Myklebust spin_unlock(&inode->i_lock); 141282e7ca13STrond Myklebust } 141382e7ca13STrond Myklebust 14141da177e4SLinus Torvalds /* 14151da177e4SLinus Torvalds * We judge how long we want to trust negative 14161da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 14171da177e4SLinus Torvalds * 14181da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 14191da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1420912a108dSNeilBrown * 1421912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1422912a108dSNeilBrown * suggesting a reval is needed. 14239f6d44d4STrond Myklebust * 14249f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 14259f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 14261da177e4SLinus Torvalds */ 14271da177e4SLinus Torvalds static inline 14281da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1429fa3c56bbSAl Viro unsigned int flags) 14301da177e4SLinus Torvalds { 14319f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 14321da177e4SLinus Torvalds return 0; 14334eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 14344eec952eSTrond Myklebust return 1; 1435912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14385ceb9d7fSTrond Myklebust static int 14395ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 14405ceb9d7fSTrond Myklebust struct inode *inode, int error) 14411da177e4SLinus Torvalds { 14425ceb9d7fSTrond Myklebust switch (error) { 14435ceb9d7fSTrond Myklebust case 1: 14446de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 14456de1472fSAl Viro __func__, dentry); 14461da177e4SLinus Torvalds return 1; 14475ceb9d7fSTrond Myklebust case 0: 1448a3f432bfSJ. Bruce Fields /* 1449a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1450a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1451a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1452a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1453a3f432bfSJ. Bruce Fields */ 145447397915STrond Myklebust if (inode && IS_ROOT(dentry)) 14555ceb9d7fSTrond Myklebust return 1; 14566de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 14576de1472fSAl Viro __func__, dentry); 14581da177e4SLinus Torvalds return 0; 14595ceb9d7fSTrond Myklebust } 14606de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 14616de1472fSAl Viro __func__, dentry, error); 1462e1fb4d05STrond Myklebust return error; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 14655ceb9d7fSTrond Myklebust static int 14665ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 14675ceb9d7fSTrond Myklebust unsigned int flags) 14685ceb9d7fSTrond Myklebust { 14695ceb9d7fSTrond Myklebust int ret = 1; 14705ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 14715ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 14725ceb9d7fSTrond Myklebust return -ECHILD; 14735ceb9d7fSTrond Myklebust ret = 0; 14745ceb9d7fSTrond Myklebust } 14755ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 14765ceb9d7fSTrond Myklebust } 14775ceb9d7fSTrond Myklebust 14785ceb9d7fSTrond Myklebust static int 14795ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 14805ceb9d7fSTrond Myklebust struct inode *inode) 14815ceb9d7fSTrond Myklebust { 14825ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14835ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 14845ceb9d7fSTrond Myklebust } 14855ceb9d7fSTrond Myklebust 14865ceb9d7fSTrond Myklebust static int 14875ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 14885ceb9d7fSTrond Myklebust struct inode *inode) 14895ceb9d7fSTrond Myklebust { 14905ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 14915ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 14925ceb9d7fSTrond Myklebust struct nfs4_label *label; 1493a1147b82STrond Myklebust unsigned long dir_verifier; 14945ceb9d7fSTrond Myklebust int ret; 14955ceb9d7fSTrond Myklebust 14965ceb9d7fSTrond Myklebust ret = -ENOMEM; 14975ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 14985ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 14995ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 15005ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 15015ceb9d7fSTrond Myklebust goto out; 15025ceb9d7fSTrond Myklebust 1503a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1504f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 15055ceb9d7fSTrond Myklebust if (ret < 0) { 1506f7b37b8bSTrond Myklebust switch (ret) { 1507f7b37b8bSTrond Myklebust case -ESTALE: 1508f7b37b8bSTrond Myklebust case -ENOENT: 15095ceb9d7fSTrond Myklebust ret = 0; 1510f7b37b8bSTrond Myklebust break; 1511f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1512f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1513f7b37b8bSTrond Myklebust ret = 1; 1514f7b37b8bSTrond Myklebust } 15155ceb9d7fSTrond Myklebust goto out; 15165ceb9d7fSTrond Myklebust } 15175ceb9d7fSTrond Myklebust ret = 0; 15185ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 15195ceb9d7fSTrond Myklebust goto out; 15205ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 15215ceb9d7fSTrond Myklebust goto out; 15225ceb9d7fSTrond Myklebust 15235ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1524a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 15255ceb9d7fSTrond Myklebust 15265ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 15275ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 15285ceb9d7fSTrond Myklebust ret = 1; 15295ceb9d7fSTrond Myklebust out: 15305ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 15315ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 15325ceb9d7fSTrond Myklebust nfs4_label_free(label); 153382e7ca13STrond Myklebust 153482e7ca13STrond Myklebust /* 153582e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being 153682e7ca13STrond Myklebust * a match, then we should revalidate the directory cache. 153782e7ca13STrond Myklebust */ 153882e7ca13STrond Myklebust if (!ret && nfs_verify_change_attribute(dir, dentry->d_time)) 153982e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 15405ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 15415ceb9d7fSTrond Myklebust } 15425ceb9d7fSTrond Myklebust 15435ceb9d7fSTrond Myklebust /* 15445ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 15455ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 15465ceb9d7fSTrond Myklebust * lookup. 15475ceb9d7fSTrond Myklebust * 15485ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 15495ceb9d7fSTrond Myklebust * we have an inode! 15505ceb9d7fSTrond Myklebust * 15515ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 15525ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 15535ceb9d7fSTrond Myklebust */ 15545ceb9d7fSTrond Myklebust static int 15555ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 15565ceb9d7fSTrond Myklebust unsigned int flags) 15575ceb9d7fSTrond Myklebust { 15585ceb9d7fSTrond Myklebust struct inode *inode; 15595ceb9d7fSTrond Myklebust int error; 15605ceb9d7fSTrond Myklebust 15615ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 15625ceb9d7fSTrond Myklebust inode = d_inode(dentry); 15635ceb9d7fSTrond Myklebust 15645ceb9d7fSTrond Myklebust if (!inode) 15655ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 15665ceb9d7fSTrond Myklebust 15675ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 15685ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 15695ceb9d7fSTrond Myklebust __func__, dentry); 15705ceb9d7fSTrond Myklebust goto out_bad; 15715ceb9d7fSTrond Myklebust } 15725ceb9d7fSTrond Myklebust 1573efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 15745ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 15755ceb9d7fSTrond Myklebust 15765ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 15775ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 15785ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 15795ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 15805ceb9d7fSTrond Myklebust if (error) { 15815ceb9d7fSTrond Myklebust if (error == -ESTALE) 158282e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir); 15835ceb9d7fSTrond Myklebust goto out_bad; 15845ceb9d7fSTrond Myklebust } 15855ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 15865ceb9d7fSTrond Myklebust goto out_valid; 15875ceb9d7fSTrond Myklebust } 15885ceb9d7fSTrond Myklebust 15895ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 15905ceb9d7fSTrond Myklebust return -ECHILD; 15915ceb9d7fSTrond Myklebust 15925ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 15935ceb9d7fSTrond Myklebust goto out_bad; 15945ceb9d7fSTrond Myklebust 15955ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 15965ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 15975ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 15985ceb9d7fSTrond Myklebust return error; 15995ceb9d7fSTrond Myklebust out_valid: 16005ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 16015ceb9d7fSTrond Myklebust out_bad: 16025ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 16035ceb9d7fSTrond Myklebust return -ECHILD; 16045ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 16055ceb9d7fSTrond Myklebust } 16065ceb9d7fSTrond Myklebust 16075ceb9d7fSTrond Myklebust static int 1608c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1609c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 16105ceb9d7fSTrond Myklebust { 16115ceb9d7fSTrond Myklebust struct dentry *parent; 16125ceb9d7fSTrond Myklebust struct inode *dir; 16135ceb9d7fSTrond Myklebust int ret; 16145ceb9d7fSTrond Myklebust 16155ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 16165ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 16175ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 16185ceb9d7fSTrond Myklebust if (!dir) 16195ceb9d7fSTrond Myklebust return -ECHILD; 1620c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 16215ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 16225ceb9d7fSTrond Myklebust return -ECHILD; 16235ceb9d7fSTrond Myklebust } else { 16245ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1625c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 16265ceb9d7fSTrond Myklebust dput(parent); 16275ceb9d7fSTrond Myklebust } 16285ceb9d7fSTrond Myklebust return ret; 16295ceb9d7fSTrond Myklebust } 16305ceb9d7fSTrond Myklebust 1631c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1632c7944ebbSTrond Myklebust { 1633c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1634c7944ebbSTrond Myklebust } 1635c7944ebbSTrond Myklebust 16361da177e4SLinus Torvalds /* 16372b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1638ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1639ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1640ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1641ecf3d1f1SJeff Layton * 1642ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1643ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1644ecf3d1f1SJeff Layton */ 1645ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1646ecf3d1f1SJeff Layton { 16472b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 16489cdd1d3fSTrond Myklebust int error = 0; 1649ecf3d1f1SJeff Layton 1650ecf3d1f1SJeff Layton /* 1651ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1652ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1653ecf3d1f1SJeff Layton * eventually need to do something more here. 1654ecf3d1f1SJeff Layton */ 1655ecf3d1f1SJeff Layton if (!inode) { 16566de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 16576de1472fSAl Viro __func__, dentry); 1658ecf3d1f1SJeff Layton return 1; 1659ecf3d1f1SJeff Layton } 1660ecf3d1f1SJeff Layton 1661ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 16626de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 16636de1472fSAl Viro __func__, dentry); 1664ecf3d1f1SJeff Layton return 0; 1665ecf3d1f1SJeff Layton } 1666ecf3d1f1SJeff Layton 1667b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1668ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1669ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1670ecf3d1f1SJeff Layton return !error; 1671ecf3d1f1SJeff Layton } 1672ecf3d1f1SJeff Layton 1673ecf3d1f1SJeff Layton /* 16741da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 16751da177e4SLinus Torvalds */ 1676fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 16771da177e4SLinus Torvalds { 16786de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 16796de1472fSAl Viro dentry, dentry->d_flags); 16801da177e4SLinus Torvalds 168177f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 16822b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 168377f11192STrond Myklebust return 1; 168477f11192STrond Myklebust 16851da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 16861da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 16871da177e4SLinus Torvalds return 1; 16881da177e4SLinus Torvalds } 16891751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 16901da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 16911da177e4SLinus Torvalds * files will be cleaned up during umount */ 16921da177e4SLinus Torvalds return 1; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds return 0; 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 16981f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 16991b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 17001b83d707STrond Myklebust { 17011b83d707STrond Myklebust spin_lock(&inode->i_lock); 17021f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 170359a707b0STrond Myklebust if (inode->i_nlink > 0) 170459a707b0STrond Myklebust drop_nlink(inode); 170559a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1706ac46b3d7STrond Myklebust nfs_set_cache_invalid( 1707ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 1708ac46b3d7STrond Myklebust NFS_INO_INVALID_OTHER | NFS_INO_REVAL_FORCED); 17091b83d707STrond Myklebust spin_unlock(&inode->i_lock); 17101b83d707STrond Myklebust } 17111b83d707STrond Myklebust 17121da177e4SLinus Torvalds /* 17131da177e4SLinus Torvalds * Called when the dentry loses inode. 17141da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 17151da177e4SLinus Torvalds */ 17161da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 17171da177e4SLinus Torvalds { 171883672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 171983672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 1720ac46b3d7STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); 172183672d39SNeil Brown 17221da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1723e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 17241f018458STrond Myklebust nfs_drop_nlink(inode); 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds iput(inode); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 1729b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1730b1942c5fSAl Viro { 1731b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1732b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1733b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1734b1942c5fSAl Viro WARN_ON(1); 1735b1942c5fSAl Viro else 1736b1942c5fSAl Viro kfree(dentry->d_fsdata); 1737b1942c5fSAl Viro } 1738b1942c5fSAl Viro } 1739b1942c5fSAl Viro 1740f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 17411da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1742ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 17431da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 17441da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 174536d43a43SDavid Howells .d_automount = nfs_d_automount, 1746b1942c5fSAl Viro .d_release = nfs_d_release, 17471da177e4SLinus Torvalds }; 1748ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 17491da177e4SLinus Torvalds 1750597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 17511da177e4SLinus Torvalds { 17521da177e4SLinus Torvalds struct dentry *res; 17531da177e4SLinus Torvalds struct inode *inode = NULL; 1754e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1755e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 17561775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1757a1147b82STrond Myklebust unsigned long dir_verifier; 17581da177e4SLinus Torvalds int error; 17591da177e4SLinus Torvalds 17606de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 176191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 17621da177e4SLinus Torvalds 1763130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1764130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 17651da177e4SLinus Torvalds 1766fd684071STrond Myklebust /* 1767fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1768fd684071STrond Myklebust * but don't hash the dentry. 1769fd684071STrond Myklebust */ 17709f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1771130f9ab7SAl Viro return NULL; 17721da177e4SLinus Torvalds 1773e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1774e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1775e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1776e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1777e1fb4d05STrond Myklebust goto out; 1778e1fb4d05STrond Myklebust 177914c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 178014c43f76SDavid Quigley if (IS_ERR(label)) 178114c43f76SDavid Quigley goto out; 178214c43f76SDavid Quigley 1783a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 17846e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1785f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 17861da177e4SLinus Torvalds if (error == -ENOENT) 17871da177e4SLinus Torvalds goto no_entry; 17881da177e4SLinus Torvalds if (error < 0) { 17891da177e4SLinus Torvalds res = ERR_PTR(error); 1790bf130914SAl Viro goto out_label; 17911da177e4SLinus Torvalds } 17921775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1793bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 179403f28e3aSTrond Myklebust if (IS_ERR(res)) 1795bf130914SAl Viro goto out_label; 179654ceac45SDavid Howells 179763519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 179863519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1799d69ee9b8STrond Myklebust 18001da177e4SLinus Torvalds no_entry: 180141d28bcaSAl Viro res = d_splice_alias(inode, dentry); 18029eaef27bSTrond Myklebust if (res != NULL) { 18039eaef27bSTrond Myklebust if (IS_ERR(res)) 1804bf130914SAl Viro goto out_label; 18051da177e4SLinus Torvalds dentry = res; 18069eaef27bSTrond Myklebust } 1807a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1808bf130914SAl Viro out_label: 18096e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 181014c43f76SDavid Quigley nfs4_label_free(label); 18111da177e4SLinus Torvalds out: 1812e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1813e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 18141da177e4SLinus Torvalds return res; 18151da177e4SLinus Torvalds } 1816ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 18171da177e4SLinus Torvalds 181889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 18190b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 18201da177e4SLinus Torvalds 1821f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 18220ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1823b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 18241da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 18251da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 182636d43a43SDavid Howells .d_automount = nfs_d_automount, 1827b1942c5fSAl Viro .d_release = nfs_d_release, 18281da177e4SLinus Torvalds }; 182989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 18301da177e4SLinus Torvalds 18318a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 18328a5e929dSAl Viro { 18338a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 18348a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 18358a5e929dSAl Viro res |= FMODE_READ; 18368a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 18378a5e929dSAl Viro res |= FMODE_WRITE; 18388a5e929dSAl Viro return res; 18398a5e929dSAl Viro } 18408a5e929dSAl Viro 1841532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1842cd9a1c0eSTrond Myklebust { 1843532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1844cd9a1c0eSTrond Myklebust } 1845cd9a1c0eSTrond Myklebust 1846cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1847cd9a1c0eSTrond Myklebust { 1848f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1849cd9a1c0eSTrond Myklebust return 0; 1850cd9a1c0eSTrond Myklebust } 1851cd9a1c0eSTrond Myklebust 1852d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 18530dd2b474SMiklos Szeredi struct dentry *dentry, 1854b452a458SAl Viro struct file *file, unsigned open_flags) 1855cd9a1c0eSTrond Myklebust { 18560dd2b474SMiklos Szeredi int err; 18570dd2b474SMiklos Szeredi 1858be12af3eSAl Viro err = finish_open(file, dentry, do_open); 185930d90494SAl Viro if (err) 1860d9585277SAl Viro goto out; 1861eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 186230d90494SAl Viro nfs_file_set_open_context(file, ctx); 1863eaa2b82cSNeilBrown else 18649821421aSTrond Myklebust err = -EOPENSTALE; 1865cd9a1c0eSTrond Myklebust out: 1866d9585277SAl Viro return err; 1867cd9a1c0eSTrond Myklebust } 1868cd9a1c0eSTrond Myklebust 186973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 187030d90494SAl Viro struct file *file, unsigned open_flags, 187144907d79SAl Viro umode_t mode) 18721da177e4SLinus Torvalds { 1873c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1874cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 18750dd2b474SMiklos Szeredi struct dentry *res; 18760dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1877f46e0bd3STrond Myklebust struct inode *inode; 18781472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1879c94c0953SAl Viro bool switched = false; 188073a09dd9SAl Viro int created = 0; 1881898f635cSTrond Myklebust int err; 18821da177e4SLinus Torvalds 18830dd2b474SMiklos Szeredi /* Expect a negative dentry */ 18842b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 18850dd2b474SMiklos Szeredi 18861e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 18876de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18881e7cb3dcSChuck Lever 18899597c13bSJeff Layton err = nfs_check_flags(open_flags); 18909597c13bSJeff Layton if (err) 18919597c13bSJeff Layton return err; 18929597c13bSJeff Layton 18930dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 18940dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 189500699ad8SAl Viro if (!d_in_lookup(dentry)) { 18960dd2b474SMiklos Szeredi /* 18970dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 18980dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 18990dd2b474SMiklos Szeredi * again 19000dd2b474SMiklos Szeredi */ 1901d9585277SAl Viro return -ENOENT; 19020dd2b474SMiklos Szeredi } 19031472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 19041da177e4SLinus Torvalds goto no_open; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 19070dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1908d9585277SAl Viro return -ENAMETOOLONG; 19091da177e4SLinus Torvalds 19100dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1911dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1912dff25ddbSAndreas Gruenbacher 1913dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1914dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1915dff25ddbSAndreas Gruenbacher 1916536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1917dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 19180dd2b474SMiklos Szeredi } 1919536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1920536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1921536e43d1STrond Myklebust attr.ia_size = 0; 1922cd9a1c0eSTrond Myklebust } 1923cd9a1c0eSTrond Myklebust 1924c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1925c94c0953SAl Viro d_drop(dentry); 1926c94c0953SAl Viro switched = true; 1927c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1928c94c0953SAl Viro &dentry->d_name, &wq); 1929c94c0953SAl Viro if (IS_ERR(dentry)) 1930c94c0953SAl Viro return PTR_ERR(dentry); 1931c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1932c94c0953SAl Viro return finish_no_open(file, dentry); 1933c94c0953SAl Viro } 1934c94c0953SAl Viro 1935532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 19360dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 19370dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1938d9585277SAl Viro goto out; 19390dd2b474SMiklos Szeredi 19406e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 194173a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 194273a09dd9SAl Viro if (created) 194373a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1944275bb307STrond Myklebust if (IS_ERR(inode)) { 19450dd2b474SMiklos Szeredi err = PTR_ERR(inode); 19466e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 19472d9db750STrond Myklebust put_nfs_open_context(ctx); 1948d20cb71dSAl Viro d_drop(dentry); 19490dd2b474SMiklos Szeredi switch (err) { 19501da177e4SLinus Torvalds case -ENOENT: 1951774d9513SPeng Tao d_splice_alias(NULL, dentry); 1952809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19530dd2b474SMiklos Szeredi break; 19541788ea6eSJeff Layton case -EISDIR: 19556f926b5bSTrond Myklebust case -ENOTDIR: 19566f926b5bSTrond Myklebust goto no_open; 19571da177e4SLinus Torvalds case -ELOOP: 19580dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 19591da177e4SLinus Torvalds goto no_open; 19600dd2b474SMiklos Szeredi break; 19611da177e4SLinus Torvalds /* case -EINVAL: */ 19621da177e4SLinus Torvalds default: 19630dd2b474SMiklos Szeredi break; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds goto out; 19661da177e4SLinus Torvalds } 19670dd2b474SMiklos Szeredi 1968b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 19696e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 19702d9db750STrond Myklebust put_nfs_open_context(ctx); 1971d9585277SAl Viro out: 1972c94c0953SAl Viro if (unlikely(switched)) { 1973c94c0953SAl Viro d_lookup_done(dentry); 1974c94c0953SAl Viro dput(dentry); 1975c94c0953SAl Viro } 1976d9585277SAl Viro return err; 19770dd2b474SMiklos Szeredi 19781da177e4SLinus Torvalds no_open: 19791472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1980c94c0953SAl Viro if (switched) { 1981c94c0953SAl Viro d_lookup_done(dentry); 1982c94c0953SAl Viro if (!res) 1983c94c0953SAl Viro res = dentry; 1984c94c0953SAl Viro else 1985c94c0953SAl Viro dput(dentry); 1986c94c0953SAl Viro } 19870dd2b474SMiklos Szeredi if (IS_ERR(res)) 1988c94c0953SAl Viro return PTR_ERR(res); 1989e45198a6SAl Viro return finish_no_open(file, res); 19901da177e4SLinus Torvalds } 199189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 19921da177e4SLinus Torvalds 1993c7944ebbSTrond Myklebust static int 1994c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1995c7944ebbSTrond Myklebust unsigned int flags) 19961da177e4SLinus Torvalds { 1997657e94b6SNick Piggin struct inode *inode; 19981da177e4SLinus Torvalds 1999fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 2000c7944ebbSTrond Myklebust goto full_reval; 2001eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 2002c7944ebbSTrond Myklebust goto full_reval; 20032b484297STrond Myklebust 20042b0143b5SDavid Howells inode = d_inode(dentry); 20052b484297STrond Myklebust 20061da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 20071da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 20081da177e4SLinus Torvalds */ 2009c7944ebbSTrond Myklebust if (inode == NULL) 2010c7944ebbSTrond Myklebust goto full_reval; 201149317a7fSNeilBrown 2012efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 2013c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2014216d5d06STrond Myklebust 20151da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 20161da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 2017c7944ebbSTrond Myklebust goto full_reval; 2018c7944ebbSTrond Myklebust 20191da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 2020c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2021c7944ebbSTrond Myklebust goto reval_dentry; 2022c7944ebbSTrond Myklebust 2023c7944ebbSTrond Myklebust /* Check if the directory changed */ 2024c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2025c7944ebbSTrond Myklebust goto reval_dentry; 20261da177e4SLinus Torvalds 20270ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 2028c7944ebbSTrond Myklebust return 1; 2029c7944ebbSTrond Myklebust reval_dentry: 2030c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 2031c7944ebbSTrond Myklebust return -ECHILD; 203242f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 20330ef97dcfSMiklos Szeredi 2034c7944ebbSTrond Myklebust full_reval: 2035c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 2036c7944ebbSTrond Myklebust } 2037535918f1STrond Myklebust 2038c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2039c7944ebbSTrond Myklebust { 2040c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 2041c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 2042c0204fd2STrond Myklebust } 2043c0204fd2STrond Myklebust 20441da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 20451da177e4SLinus Torvalds 2046406cd915SBenjamin Coddington struct dentry * 2047406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 20481775fd3eSDavid Quigley struct nfs_fattr *fattr, 20491775fd3eSDavid Quigley struct nfs4_label *label) 20501da177e4SLinus Torvalds { 2051fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 20522b0143b5SDavid Howells struct inode *dir = d_inode(parent); 20531da177e4SLinus Torvalds struct inode *inode; 2054b0c6108eSAl Viro struct dentry *d; 2055406cd915SBenjamin Coddington int error; 20561da177e4SLinus Torvalds 2057fab728e1STrond Myklebust d_drop(dentry); 2058fab728e1STrond Myklebust 20591da177e4SLinus Torvalds if (fhandle->size == 0) { 2060f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 20611da177e4SLinus Torvalds if (error) 2062fab728e1STrond Myklebust goto out_error; 20631da177e4SLinus Torvalds } 20645724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 20651da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 20661da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 2067a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 2068a841b54dSTrond Myklebust fattr, NULL, NULL); 20691da177e4SLinus Torvalds if (error < 0) 2070fab728e1STrond Myklebust goto out_error; 20711da177e4SLinus Torvalds } 20721775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 2073b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 2074fab728e1STrond Myklebust out: 2075fab728e1STrond Myklebust dput(parent); 2076406cd915SBenjamin Coddington return d; 2077fab728e1STrond Myklebust out_error: 2078406cd915SBenjamin Coddington d = ERR_PTR(error); 2079406cd915SBenjamin Coddington goto out; 2080406cd915SBenjamin Coddington } 2081406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2082406cd915SBenjamin Coddington 2083406cd915SBenjamin Coddington /* 2084406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 2085406cd915SBenjamin Coddington */ 2086406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2087406cd915SBenjamin Coddington struct nfs_fattr *fattr, 2088406cd915SBenjamin Coddington struct nfs4_label *label) 2089406cd915SBenjamin Coddington { 2090406cd915SBenjamin Coddington struct dentry *d; 2091406cd915SBenjamin Coddington 2092406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 2093406cd915SBenjamin Coddington if (IS_ERR(d)) 2094406cd915SBenjamin Coddington return PTR_ERR(d); 2095406cd915SBenjamin Coddington 2096406cd915SBenjamin Coddington /* Callers don't care */ 2097406cd915SBenjamin Coddington dput(d); 2098406cd915SBenjamin Coddington return 0; 20991da177e4SLinus Torvalds } 2100ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds /* 21031da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 21041da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 21051da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 21061da177e4SLinus Torvalds * reply path made it appear to have failed. 21071da177e4SLinus Torvalds */ 2108549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2109549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 21101da177e4SLinus Torvalds { 21111da177e4SLinus Torvalds struct iattr attr; 2112ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 21131da177e4SLinus Torvalds int error; 21141da177e4SLinus Torvalds 21151e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 21166de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds attr.ia_mode = mode; 21191da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21201da177e4SLinus Torvalds 21218b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 21228867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 21238b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 21241da177e4SLinus Torvalds if (error != 0) 21251da177e4SLinus Torvalds goto out_err; 21261da177e4SLinus Torvalds return 0; 21271da177e4SLinus Torvalds out_err: 21281da177e4SLinus Torvalds d_drop(dentry); 21291da177e4SLinus Torvalds return error; 21301da177e4SLinus Torvalds } 2131ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds /* 21341da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 21351da177e4SLinus Torvalds */ 2136597d9289SBryan Schumaker int 2137549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2138549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev) 21391da177e4SLinus Torvalds { 21401da177e4SLinus Torvalds struct iattr attr; 21411da177e4SLinus Torvalds int status; 21421da177e4SLinus Torvalds 21431e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 21446de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds attr.ia_mode = mode; 21471da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21481da177e4SLinus Torvalds 21491ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 21501da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 21511ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 21521da177e4SLinus Torvalds if (status != 0) 21531da177e4SLinus Torvalds goto out_err; 21541da177e4SLinus Torvalds return 0; 21551da177e4SLinus Torvalds out_err: 21561da177e4SLinus Torvalds d_drop(dentry); 21571da177e4SLinus Torvalds return status; 21581da177e4SLinus Torvalds } 2159ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds /* 21621da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 21631da177e4SLinus Torvalds */ 2164549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2165549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 21661da177e4SLinus Torvalds { 21671da177e4SLinus Torvalds struct iattr attr; 21681da177e4SLinus Torvalds int error; 21691da177e4SLinus Torvalds 21701e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 21716de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 21741da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 21751da177e4SLinus Torvalds 21761ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 21771da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 21781ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 21791da177e4SLinus Torvalds if (error != 0) 21801da177e4SLinus Torvalds goto out_err; 21811da177e4SLinus Torvalds return 0; 21821da177e4SLinus Torvalds out_err: 21831da177e4SLinus Torvalds d_drop(dentry); 21841da177e4SLinus Torvalds return error; 21851da177e4SLinus Torvalds } 2186ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 21871da177e4SLinus Torvalds 2188d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2189d45b9d8bSTrond Myklebust { 2190dc3f4198SAl Viro if (simple_positive(dentry)) 2191d45b9d8bSTrond Myklebust d_delete(dentry); 2192d45b9d8bSTrond Myklebust } 2193d45b9d8bSTrond 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); 22161ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds return error; 22191da177e4SLinus Torvalds } 2220ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds /* 22231da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 22241da177e4SLinus Torvalds * and after checking that the file has only one user. 22251da177e4SLinus Torvalds * 22261da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 22271da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 22281da177e4SLinus Torvalds */ 22291da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 22301da177e4SLinus Torvalds { 22312b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 22322b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 22331da177e4SLinus Torvalds int error = -EBUSY; 22341da177e4SLinus Torvalds 22356de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 22381da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 22391da177e4SLinus Torvalds error = 0; 22401da177e4SLinus Torvalds goto out; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22431ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 22441da177e4SLinus Torvalds if (inode != NULL) { 2245912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 22461da177e4SLinus Torvalds if (error == 0) 22471b83d707STrond Myklebust nfs_drop_nlink(inode); 22481da177e4SLinus Torvalds } else 2249912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2250d45b9d8bSTrond Myklebust if (error == -ENOENT) 2251d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 22521ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 22531da177e4SLinus Torvalds out: 22541da177e4SLinus Torvalds return error; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 22581da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 22591da177e4SLinus Torvalds * 22601da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 22611da177e4SLinus Torvalds */ 2262597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 22631da177e4SLinus Torvalds { 22641da177e4SLinus Torvalds int error; 22651da177e4SLinus Torvalds int need_rehash = 0; 22661da177e4SLinus Torvalds 22671e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 22686de1472fSAl Viro dir->i_ino, dentry); 22691da177e4SLinus Torvalds 22701ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 22711da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 227284d08fa8SAl Viro if (d_count(dentry) > 1) { 22731da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2274ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 22752b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 22761da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 22771ca42382STrond Myklebust goto out; 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 22801da177e4SLinus Torvalds __d_drop(dentry); 22811da177e4SLinus Torvalds need_rehash = 1; 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 22841da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2285d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 22861da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 22871da177e4SLinus Torvalds } else 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 2349873101b3SChuck Lever /* 2350873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2351873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2352873101b3SChuck Lever */ 23532b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2354873101b3SChuck Lever GFP_KERNEL)) { 2355873101b3SChuck Lever SetPageUptodate(page); 2356873101b3SChuck Lever unlock_page(page); 2357a0b54addSRafael Aquini /* 2358a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2359a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2360a0b54addSRafael Aquini */ 236109cbfeafSKirill A. Shutemov put_page(page); 2362873101b3SChuck Lever } else 2363873101b3SChuck Lever __free_page(page); 2364873101b3SChuck Lever 2365873101b3SChuck Lever return 0; 2366873101b3SChuck Lever } 2367ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2368873101b3SChuck Lever 2369597d9289SBryan Schumaker int 23701da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 23711da177e4SLinus Torvalds { 23722b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 23731da177e4SLinus Torvalds int error; 23741da177e4SLinus Torvalds 23756de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 23766de1472fSAl Viro old_dentry, dentry); 23771da177e4SLinus Torvalds 23781fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 23799697d234STrond Myklebust d_drop(dentry); 23801da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2381cf809556STrond Myklebust if (error == 0) { 23827de9c6eeSAl Viro ihold(inode); 23839697d234STrond Myklebust d_add(dentry, inode); 2384cf809556STrond Myklebust } 23851fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 23861da177e4SLinus Torvalds return error; 23871da177e4SLinus Torvalds } 2388ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds /* 23911da177e4SLinus Torvalds * RENAME 23921da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 23931da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 23941da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 23951da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 23961da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 23971da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 23981da177e4SLinus Torvalds * 23991da177e4SLinus Torvalds * FIXED. 24001da177e4SLinus Torvalds * 24011da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 24021da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 24031da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 24041da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 24051da177e4SLinus Torvalds * using the inode layer 24061da177e4SLinus Torvalds * 24071da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 24081da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 24091da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 24101da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 24111da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 24121da177e4SLinus Torvalds * the rename. 24131da177e4SLinus Torvalds */ 2414549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2415549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 2416549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 24171da177e4SLinus Torvalds { 24182b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 24192b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2420d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 242180a491fdSJeff Layton struct rpc_task *task; 24221da177e4SLinus Torvalds int error = -EBUSY; 24231da177e4SLinus Torvalds 24241cd66c93SMiklos Szeredi if (flags) 24251cd66c93SMiklos Szeredi return -EINVAL; 24261cd66c93SMiklos Szeredi 24276de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 24286de1472fSAl Viro old_dentry, new_dentry, 242984d08fa8SAl Viro d_count(new_dentry)); 24301da177e4SLinus Torvalds 243170ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 24321da177e4SLinus Torvalds /* 243328f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 243428f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 243528f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 243628f79a1aSMiklos Szeredi * the new target. 24371da177e4SLinus Torvalds */ 243827226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 243927226104SMiklos Szeredi /* 244027226104SMiklos Szeredi * To prevent any new references to the target during the 244127226104SMiklos Szeredi * rename, we unhash the dentry in advance. 244227226104SMiklos Szeredi */ 2443d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 244427226104SMiklos Szeredi d_drop(new_dentry); 2445d9f29500SBenjamin Coddington rehash = new_dentry; 2446d9f29500SBenjamin Coddington } 244727226104SMiklos Szeredi 244884d08fa8SAl Viro if (d_count(new_dentry) > 2) { 24491da177e4SLinus Torvalds int err; 245027226104SMiklos Szeredi 24511da177e4SLinus Torvalds /* copy the target dentry's name */ 24521da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 24531da177e4SLinus Torvalds &new_dentry->d_name); 24541da177e4SLinus Torvalds if (!dentry) 24551da177e4SLinus Torvalds goto out; 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds /* silly-rename the existing target ... */ 24581da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 245924e93025SMiklos Szeredi if (err) 24601da177e4SLinus Torvalds goto out; 246124e93025SMiklos Szeredi 246224e93025SMiklos Szeredi new_dentry = dentry; 2463d9f29500SBenjamin Coddington rehash = NULL; 246424e93025SMiklos Szeredi new_inode = NULL; 2465b1e4adf4STrond Myklebust } 246627226104SMiklos Szeredi } 24671da177e4SLinus Torvalds 2468d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 246980a491fdSJeff Layton if (IS_ERR(task)) { 247080a491fdSJeff Layton error = PTR_ERR(task); 247180a491fdSJeff Layton goto out; 247280a491fdSJeff Layton } 247380a491fdSJeff Layton 247480a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2475818a8dbeSBenjamin Coddington if (error != 0) { 2476818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2477818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2478818a8dbeSBenjamin Coddington smp_wmb(); 2479818a8dbeSBenjamin Coddington } else 248080a491fdSJeff Layton error = task->tk_status; 248180a491fdSJeff Layton rpc_put_task(task); 248259a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 248359a707b0STrond Myklebust if (error == 0) { 248459a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 248559a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2486ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2487ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME | 2488ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED); 248959a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 249059a707b0STrond Myklebust } 24911da177e4SLinus Torvalds out: 2492d9f29500SBenjamin Coddington if (rehash) 2493d9f29500SBenjamin Coddington d_rehash(rehash); 249470ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 249570ded201STrond Myklebust new_dir, new_dentry, error); 2496d9f29500SBenjamin Coddington if (!error) { 2497d9f29500SBenjamin Coddington if (new_inode != NULL) 2498d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2499d9f29500SBenjamin Coddington /* 2500d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2501d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2502d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2503d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2504d9f29500SBenjamin Coddington */ 2505d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2506d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2507d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2508d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2509d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2510d9f29500SBenjamin Coddington 25111da177e4SLinus Torvalds /* new dentry created? */ 25121da177e4SLinus Torvalds if (dentry) 25131da177e4SLinus Torvalds dput(dentry); 25141da177e4SLinus Torvalds return error; 25151da177e4SLinus Torvalds } 2516ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 25171da177e4SLinus Torvalds 2518cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2519cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2520cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2521cfcea3e8STrond Myklebust 2522a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 25233a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 25243a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 25253a505845STrond Myklebust 25261c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 25271c3c07e9STrond Myklebust { 2528b68572e0SNeilBrown put_cred(entry->cred); 2529f682a398SNeilBrown kfree_rcu(entry, rcu_head); 25304e857c58SPeter Zijlstra smp_mb__before_atomic(); 2531cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 25324e857c58SPeter Zijlstra smp_mb__after_atomic(); 25331c3c07e9STrond Myklebust } 25341c3c07e9STrond Myklebust 25351a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 25361a81bb8aSTrond Myklebust { 25371a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 25381a81bb8aSTrond Myklebust 25391a81bb8aSTrond Myklebust while (!list_empty(head)) { 25401a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 25411a81bb8aSTrond Myklebust list_del(&cache->lru); 25421a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 25431a81bb8aSTrond Myklebust } 25441a81bb8aSTrond Myklebust } 25451a81bb8aSTrond Myklebust 25463a505845STrond Myklebust static unsigned long 25473a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2548979df72eSTrond Myklebust { 2549979df72eSTrond Myklebust LIST_HEAD(head); 2550aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2551979df72eSTrond Myklebust struct nfs_access_entry *cache; 25521ab6c499SDave Chinner long freed = 0; 2553979df72eSTrond Myklebust 2554a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2555aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2556979df72eSTrond Myklebust struct inode *inode; 2557979df72eSTrond Myklebust 2558979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2559979df72eSTrond Myklebust break; 25609c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2561979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2562979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2563979df72eSTrond Myklebust goto remove_lru_entry; 2564979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2565979df72eSTrond Myklebust struct nfs_access_entry, lru); 2566979df72eSTrond Myklebust list_move(&cache->lru, &head); 2567979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 25681ab6c499SDave Chinner freed++; 2569979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2570979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2571979df72eSTrond Myklebust &nfs_access_lru_list); 2572979df72eSTrond Myklebust else { 2573979df72eSTrond Myklebust remove_lru_entry: 2574979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 25754e857c58SPeter Zijlstra smp_mb__before_atomic(); 2576979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 25774e857c58SPeter Zijlstra smp_mb__after_atomic(); 2578979df72eSTrond Myklebust } 257959844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2580979df72eSTrond Myklebust } 2581979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 25821a81bb8aSTrond Myklebust nfs_access_free_list(&head); 25831ab6c499SDave Chinner return freed; 25841ab6c499SDave Chinner } 25851ab6c499SDave Chinner 25861ab6c499SDave Chinner unsigned long 25873a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 25883a505845STrond Myklebust { 25893a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 25903a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 25913a505845STrond Myklebust 25923a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 25933a505845STrond Myklebust return SHRINK_STOP; 25943a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 25953a505845STrond Myklebust } 25963a505845STrond Myklebust 25973a505845STrond Myklebust 25983a505845STrond Myklebust unsigned long 25991ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 26001ab6c499SDave Chinner { 260155f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2602979df72eSTrond Myklebust } 2603979df72eSTrond Myklebust 26043a505845STrond Myklebust static void 26053a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 26063a505845STrond Myklebust { 26073a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 26083a505845STrond Myklebust unsigned long diff; 26093a505845STrond Myklebust unsigned int nr_to_scan; 26103a505845STrond Myklebust 26113a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 26123a505845STrond Myklebust return; 26133a505845STrond Myklebust nr_to_scan = 100; 26143a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 26153a505845STrond Myklebust if (diff < nr_to_scan) 26163a505845STrond Myklebust nr_to_scan = diff; 26173a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 26183a505845STrond Myklebust } 26193a505845STrond Myklebust 26201a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 26211c3c07e9STrond Myklebust { 26221c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 26231a81bb8aSTrond Myklebust struct rb_node *n; 26241c3c07e9STrond Myklebust struct nfs_access_entry *entry; 26251c3c07e9STrond Myklebust 26261c3c07e9STrond Myklebust /* Unhook entries from the cache */ 26271c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 26281c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 26291c3c07e9STrond Myklebust rb_erase(n, root_node); 26301a81bb8aSTrond Myklebust list_move(&entry->lru, head); 26311c3c07e9STrond Myklebust } 26321c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 26331c3c07e9STrond Myklebust } 26341c3c07e9STrond Myklebust 26351c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 26361c3c07e9STrond Myklebust { 26371a81bb8aSTrond Myklebust LIST_HEAD(head); 26381a81bb8aSTrond Myklebust 26391a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 26401a81bb8aSTrond Myklebust return; 2641cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2642cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 26431a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2644cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2645cfcea3e8STrond Myklebust 26461c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 26471a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 26481a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 26491a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 26501a81bb8aSTrond Myklebust nfs_access_free_list(&head); 26511c3c07e9STrond Myklebust } 26521c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 26531c3c07e9STrond Myklebust 2654b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 26551c3c07e9STrond Myklebust { 26561c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 26571c3c07e9STrond Myklebust 26581c3c07e9STrond Myklebust while (n != NULL) { 2659b68572e0SNeilBrown struct nfs_access_entry *entry = 2660b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2661b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 26621c3c07e9STrond Myklebust 2663b68572e0SNeilBrown if (cmp < 0) 26641c3c07e9STrond Myklebust n = n->rb_left; 2665b68572e0SNeilBrown else if (cmp > 0) 26661c3c07e9STrond Myklebust n = n->rb_right; 26671c3c07e9STrond Myklebust else 26681c3c07e9STrond Myklebust return entry; 26691c3c07e9STrond Myklebust } 26701c3c07e9STrond Myklebust return NULL; 26711c3c07e9STrond Myklebust } 26721c3c07e9STrond Myklebust 2673d2ae4f8bSFrank van der Linden static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 26741da177e4SLinus Torvalds { 267555296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 26761c3c07e9STrond Myklebust struct nfs_access_entry *cache; 267757b69181STrond Myklebust bool retry = true; 267857b69181STrond Myklebust int err; 26791da177e4SLinus Torvalds 26801c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 268157b69181STrond Myklebust for(;;) { 26821c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 26831c3c07e9STrond Myklebust goto out_zap; 26841c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 268557b69181STrond Myklebust err = -ENOENT; 26861c3c07e9STrond Myklebust if (cache == NULL) 26871c3c07e9STrond Myklebust goto out; 268857b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 268921c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 269057b69181STrond Myklebust break; 26915c965db8STrond Myklebust if (!retry) 26925c965db8STrond Myklebust break; 269357b69181STrond Myklebust err = -ECHILD; 269457b69181STrond Myklebust if (!may_block) 269557b69181STrond Myklebust goto out; 269657b69181STrond Myklebust spin_unlock(&inode->i_lock); 269757b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 269857b69181STrond Myklebust if (err) 269957b69181STrond Myklebust return err; 270057b69181STrond Myklebust spin_lock(&inode->i_lock); 270157b69181STrond Myklebust retry = false; 270257b69181STrond Myklebust } 27031c3c07e9STrond Myklebust res->cred = cache->cred; 27041c3c07e9STrond Myklebust res->mask = cache->mask; 2705cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 27061c3c07e9STrond Myklebust err = 0; 27071c3c07e9STrond Myklebust out: 27081c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 27091c3c07e9STrond Myklebust return err; 27101c3c07e9STrond Myklebust out_zap: 27111a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 27121a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 27131c3c07e9STrond Myklebust return -ENOENT; 27141c3c07e9STrond Myklebust } 27151c3c07e9STrond Myklebust 2716b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2717f682a398SNeilBrown { 2718f682a398SNeilBrown /* Only check the most recently returned cache entry, 2719f682a398SNeilBrown * but do it without locking. 2720f682a398SNeilBrown */ 2721f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2722f682a398SNeilBrown struct nfs_access_entry *cache; 2723f682a398SNeilBrown int err = -ECHILD; 2724f682a398SNeilBrown struct list_head *lh; 2725f682a398SNeilBrown 2726f682a398SNeilBrown rcu_read_lock(); 2727f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2728f682a398SNeilBrown goto out; 27299f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2730f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2731f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 27329a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2733f682a398SNeilBrown cache = NULL; 2734f682a398SNeilBrown if (cache == NULL) 2735f682a398SNeilBrown goto out; 273621c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2737f682a398SNeilBrown goto out; 2738f682a398SNeilBrown res->cred = cache->cred; 2739f682a398SNeilBrown res->mask = cache->mask; 274021c3ba7eSTrond Myklebust err = 0; 2741f682a398SNeilBrown out: 2742f682a398SNeilBrown rcu_read_unlock(); 2743f682a398SNeilBrown return err; 2744f682a398SNeilBrown } 2745f682a398SNeilBrown 2746d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct 2747d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block) 2748d2ae4f8bSFrank van der Linden { 2749d2ae4f8bSFrank van der Linden int status; 2750d2ae4f8bSFrank van der Linden 2751d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_rcu(inode, cred, res); 2752d2ae4f8bSFrank van der Linden if (status != 0) 2753d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_locked(inode, cred, res, 2754d2ae4f8bSFrank van der Linden may_block); 2755d2ae4f8bSFrank van der Linden 2756d2ae4f8bSFrank van der Linden return status; 2757d2ae4f8bSFrank van der Linden } 2758d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2759d2ae4f8bSFrank van der Linden 27601c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 27611c3c07e9STrond Myklebust { 2762cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2763cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 27641c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 27651c3c07e9STrond Myklebust struct rb_node *parent = NULL; 27661c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2767b68572e0SNeilBrown int cmp; 27681c3c07e9STrond Myklebust 27691c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 27701c3c07e9STrond Myklebust while (*p != NULL) { 27711c3c07e9STrond Myklebust parent = *p; 27721c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2773b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 27741c3c07e9STrond Myklebust 2775b68572e0SNeilBrown if (cmp < 0) 27761c3c07e9STrond Myklebust p = &parent->rb_left; 2777b68572e0SNeilBrown else if (cmp > 0) 27781c3c07e9STrond Myklebust p = &parent->rb_right; 27791c3c07e9STrond Myklebust else 27801c3c07e9STrond Myklebust goto found; 27811c3c07e9STrond Myklebust } 27821c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 27831c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2784cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 27851c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 27861c3c07e9STrond Myklebust return; 27871c3c07e9STrond Myklebust found: 27881c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2789cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2790cfcea3e8STrond Myklebust list_del(&entry->lru); 27911c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 27921c3c07e9STrond Myklebust nfs_access_free_entry(entry); 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 27956168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 27961da177e4SLinus Torvalds { 27971c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 27981c3c07e9STrond Myklebust if (cache == NULL) 27991c3c07e9STrond Myklebust return; 28001c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2801b68572e0SNeilBrown cache->cred = get_cred(set->cred); 28021da177e4SLinus Torvalds cache->mask = set->mask; 28031c3c07e9STrond Myklebust 2804f682a398SNeilBrown /* The above field assignments must be visible 2805f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2806f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2807f682a398SNeilBrown */ 2808f682a398SNeilBrown smp_wmb(); 28091c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2810cfcea3e8STrond Myklebust 2811cfcea3e8STrond Myklebust /* Update accounting */ 28124e857c58SPeter Zijlstra smp_mb__before_atomic(); 2813cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 28144e857c58SPeter Zijlstra smp_mb__after_atomic(); 2815cfcea3e8STrond Myklebust 2816cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 28171a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2818cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 28191a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 28201a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 28211a81bb8aSTrond Myklebust &nfs_access_lru_list); 2822cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2823cfcea3e8STrond Myklebust } 28243a505845STrond Myklebust nfs_access_cache_enforce_limit(); 28251da177e4SLinus Torvalds } 28266168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 28276168f62cSWeston Andros Adamson 28283c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 28293c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 28303c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 28313c181827SAnna Schumaker NFS_ACCESS_DELETE) 28323c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 28333c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2834ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 28353c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 28363c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 283715d4b73aSTrond Myklebust static int 2838ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 283915d4b73aSTrond Myklebust { 284015d4b73aSTrond Myklebust int mask = 0; 284115d4b73aSTrond Myklebust 284215d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 284315d4b73aSTrond Myklebust mask |= MAY_READ; 2844ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2845ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 284615d4b73aSTrond Myklebust mask |= MAY_WRITE; 2847ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 284815d4b73aSTrond Myklebust mask |= MAY_EXEC; 2849ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2850ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2851ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2852ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 285315d4b73aSTrond Myklebust mask |= MAY_EXEC; 2854ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2855ecbb903cSTrond Myklebust mask |= MAY_WRITE; 285615d4b73aSTrond Myklebust return mask; 285715d4b73aSTrond Myklebust } 285815d4b73aSTrond Myklebust 28596168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 28606168f62cSWeston Andros Adamson { 2861bd8b2441STrond Myklebust entry->mask = access_result; 28626168f62cSWeston Andros Adamson } 28636168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 28641da177e4SLinus Torvalds 2865b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 28661da177e4SLinus Torvalds { 28671da177e4SLinus Torvalds struct nfs_access_entry cache; 286857b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2869e8194b7dSTrond Myklebust int cache_mask = -1; 28701da177e4SLinus Torvalds int status; 28711da177e4SLinus Torvalds 2872f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2873f4ce1299STrond Myklebust 287457b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 28751da177e4SLinus Torvalds if (status == 0) 2876f4ce1299STrond Myklebust goto out_cached; 28771da177e4SLinus Torvalds 2878f3324a2aSNeilBrown status = -ECHILD; 287957b69181STrond Myklebust if (!may_block) 2880f3324a2aSNeilBrown goto out; 2881f3324a2aSNeilBrown 28821750d929SAnna Schumaker /* 28831750d929SAnna Schumaker * Determine which access bits we want to ask for... 28841750d929SAnna Schumaker */ 28851750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 288672832a24SFrank van der Linden if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 288772832a24SFrank van der Linden cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 288872832a24SFrank van der Linden NFS_ACCESS_XALIST; 288972832a24SFrank van der Linden } 28901750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 28911750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 28921750d929SAnna Schumaker else 28931750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 28941da177e4SLinus Torvalds cache.cred = cred; 28951da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2896a71ee337SSuresh Jayaraman if (status != 0) { 2897a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2898a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 289993ce4af7STrond Myklebust nfs_set_inode_stale(inode); 290093ce4af7STrond Myklebust else 290193ce4af7STrond Myklebust nfs_zap_caches(inode); 2902a71ee337SSuresh Jayaraman } 2903f4ce1299STrond Myklebust goto out; 2904a71ee337SSuresh Jayaraman } 29051da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2906f4ce1299STrond Myklebust out_cached: 2907ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2908bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2909f4ce1299STrond Myklebust status = -EACCES; 29101da177e4SLinus Torvalds out: 2911e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2912f4ce1299STrond Myklebust return status; 29131da177e4SLinus Torvalds } 29141da177e4SLinus Torvalds 2915af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2916af22f94aSTrond Myklebust { 2917af22f94aSTrond Myklebust int mask = 0; 2918af22f94aSTrond Myklebust 2919f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2920f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2921f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2922f8d9a897SWeston Andros Adamson } else { 29238a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2924af22f94aSTrond Myklebust mask |= MAY_READ; 29258a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2926af22f94aSTrond Myklebust mask |= MAY_WRITE; 2927f8d9a897SWeston Andros Adamson } 2928f8d9a897SWeston Andros Adamson 2929af22f94aSTrond Myklebust return mask; 2930af22f94aSTrond Myklebust } 2931af22f94aSTrond Myklebust 2932b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2933af22f94aSTrond Myklebust { 2934af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2935af22f94aSTrond Myklebust } 293689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2937af22f94aSTrond Myklebust 29385c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 29395c5fc09aSTrond Myklebust { 29405c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 294121c3ba7eSTrond Myklebust int ret = 0; 29425c5fc09aSTrond Myklebust 29433825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 29443825827eSTrond Myklebust return 0; 2945cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 29465c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 294721c3ba7eSTrond Myklebust return -ECHILD; 294821c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 294921c3ba7eSTrond Myklebust } 29505c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 29515c5fc09aSTrond Myklebust ret = -EACCES; 29525c5fc09aSTrond Myklebust return ret; 29535c5fc09aSTrond Myklebust } 29545c5fc09aSTrond Myklebust 2955549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns, 2956549c7297SChristian Brauner struct inode *inode, 2957549c7297SChristian Brauner int mask) 29581da177e4SLinus Torvalds { 2959b68572e0SNeilBrown const struct cred *cred = current_cred(); 29601da177e4SLinus Torvalds int res = 0; 29611da177e4SLinus Torvalds 296291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 296391d5b470SChuck Lever 2964e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 29651da177e4SLinus Torvalds goto out; 29661da177e4SLinus Torvalds /* Is this sys_access() ? */ 29679cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 29681da177e4SLinus Torvalds goto force_lookup; 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 29711da177e4SLinus Torvalds case S_IFLNK: 29721da177e4SLinus Torvalds goto out; 29731da177e4SLinus Torvalds case S_IFREG: 2974762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2975762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2976762674f8STrond Myklebust return 0; 29771da177e4SLinus Torvalds break; 29781da177e4SLinus Torvalds case S_IFDIR: 29791da177e4SLinus Torvalds /* 29801da177e4SLinus Torvalds * Optimize away all write operations, since the server 29811da177e4SLinus Torvalds * will check permissions when we perform the op. 29821da177e4SLinus Torvalds */ 29831da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 29841da177e4SLinus Torvalds goto out; 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds force_lookup: 29881da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 29891da177e4SLinus Torvalds goto out_notsup; 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 29921da177e4SLinus Torvalds out: 29935c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 29945c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2995f696a365SMiklos Szeredi 29961e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 29971e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 29981da177e4SLinus Torvalds return res; 29991da177e4SLinus Torvalds out_notsup: 3000d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 3001d51ac1a8SNeilBrown return -ECHILD; 3002d51ac1a8SNeilBrown 30031da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 30041da177e4SLinus Torvalds if (res == 0) 300547291baaSChristian Brauner res = generic_permission(&init_user_ns, inode, mask); 30061e7cb3dcSChuck Lever goto out; 30071da177e4SLinus Torvalds } 3008ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds /* 30111da177e4SLinus Torvalds * Local variables: 30121da177e4SLinus Torvalds * version-control: t 30131da177e4SLinus Torvalds * kept-new-versions: 5 30141da177e4SLinus Torvalds * End: 30151da177e4SLinus Torvalds */ 3016