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)) 841c341b77STrond Myklebust nfsi->cache_validity |= NFS_INO_INVALID_DATA | 851c341b77STrond Myklebust NFS_INO_REVAL_FORCED; 86311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 87311324adSTrond Myklebust spin_unlock(&dir->i_lock); 88480c2006SBryan Schumaker return ctx; 89480c2006SBryan Schumaker } 900c030806STrond Myklebust return ERR_PTR(-ENOMEM); 910c030806STrond Myklebust } 92480c2006SBryan Schumaker 93311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 94480c2006SBryan Schumaker { 95311324adSTrond Myklebust spin_lock(&dir->i_lock); 96311324adSTrond Myklebust list_del(&ctx->list); 97311324adSTrond Myklebust spin_unlock(&dir->i_lock); 98480c2006SBryan Schumaker kfree(ctx); 99480c2006SBryan Schumaker } 100480c2006SBryan Schumaker 1011da177e4SLinus Torvalds /* 1021da177e4SLinus Torvalds * Open file 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds static int 1051da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1061da177e4SLinus Torvalds { 107480c2006SBryan Schumaker int res = 0; 108480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1091da177e4SLinus Torvalds 1106de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 111cc0dd2d1SChuck Lever 112cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1131e7cb3dcSChuck Lever 11493b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode); 115480c2006SBryan Schumaker if (IS_ERR(ctx)) { 116480c2006SBryan Schumaker res = PTR_ERR(ctx); 117480c2006SBryan Schumaker goto out; 118480c2006SBryan Schumaker } 119480c2006SBryan Schumaker filp->private_data = ctx; 120480c2006SBryan Schumaker out: 1211da177e4SLinus Torvalds return res; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 124480c2006SBryan Schumaker static int 125480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 126480c2006SBryan Schumaker { 127a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 128480c2006SBryan Schumaker return 0; 129480c2006SBryan Schumaker } 130480c2006SBryan Schumaker 131d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 132d1bacf9eSBryan Schumaker u64 cookie; 133d1bacf9eSBryan Schumaker u64 ino; 134a52a8a6aSTrond Myklebust const char *name; 135a52a8a6aSTrond Myklebust unsigned int name_len; 1360b26a0bfSTrond Myklebust unsigned char d_type; 137d1bacf9eSBryan Schumaker }; 138d1bacf9eSBryan Schumaker 139d1bacf9eSBryan Schumaker struct nfs_cache_array { 140d1bacf9eSBryan Schumaker u64 last_cookie; 141b1e21c97STrond Myklebust unsigned int size; 142b1e21c97STrond Myklebust unsigned char page_full : 1, 143b1e21c97STrond Myklebust page_is_eof : 1; 1445601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 145d1bacf9eSBryan Schumaker }; 146d1bacf9eSBryan Schumaker 1476c981effSTrond Myklebust struct nfs_readdir_descriptor { 1481da177e4SLinus Torvalds struct file *file; 1491da177e4SLinus Torvalds struct page *page; 15023db8620SAl Viro struct dir_context *ctx; 1511f1d4aa4STrond Myklebust pgoff_t page_index; 1522e7a4641STrond Myklebust u64 dir_cookie; 1530aded708STrond Myklebust u64 last_cookie; 1542e7a4641STrond Myklebust u64 dup_cookie; 155f0dd2136STrond Myklebust loff_t current_index; 15659e356a9STrond Myklebust loff_t prev_index; 157d1bacf9eSBryan Schumaker 158a1147b82STrond Myklebust unsigned long dir_verifier; 1591f4eab7eSNeil Brown unsigned long timestamp; 1604704f0e2STrond Myklebust unsigned long gencount; 1612e7a4641STrond Myklebust unsigned long attr_gencount; 162d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 1632e7a4641STrond Myklebust signed char duped; 164a7a3b1e9SBenjamin Coddington bool plus; 165a7a3b1e9SBenjamin Coddington bool eof; 1666c981effSTrond Myklebust }; 1671da177e4SLinus Torvalds 1681f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array) 1691f1d4aa4STrond Myklebust { 1701f1d4aa4STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1711f1d4aa4STrond Myklebust } 1721f1d4aa4STrond Myklebust 1731f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie) 1744b310319STrond Myklebust { 1754b310319STrond Myklebust struct nfs_cache_array *array; 1764b310319STrond Myklebust 1774b310319STrond Myklebust array = kmap_atomic(page); 1781f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 1791f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 1804b310319STrond Myklebust kunmap_atomic(array); 1814b310319STrond Myklebust } 1824b310319STrond Myklebust 183d1bacf9eSBryan Schumaker /* 184d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 185d1bacf9eSBryan Schumaker */ 186d1bacf9eSBryan Schumaker static 18711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 188d1bacf9eSBryan Schumaker { 18911de3b11STrond Myklebust struct nfs_cache_array *array; 190d1bacf9eSBryan Schumaker int i; 1918cd51a0cSTrond Myklebust 1922b86ce2dSCong Wang array = kmap_atomic(page); 193d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 194a52a8a6aSTrond Myklebust kfree(array->array[i].name); 1951f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 1962b86ce2dSCong Wang kunmap_atomic(array); 197d1bacf9eSBryan Schumaker } 198d1bacf9eSBryan Schumaker 199b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 200b1e21c97STrond Myklebust { 201b1e21c97STrond Myklebust array->page_is_eof = 1; 202b1e21c97STrond Myklebust array->page_full = 1; 203b1e21c97STrond Myklebust } 204b1e21c97STrond Myklebust 205b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 206b1e21c97STrond Myklebust { 207b1e21c97STrond Myklebust return array->page_full; 208b1e21c97STrond Myklebust } 209b1e21c97STrond Myklebust 210d1bacf9eSBryan Schumaker /* 211d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 212d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 213d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 214d1bacf9eSBryan Schumaker */ 215a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 216d1bacf9eSBryan Schumaker { 217a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 218a52a8a6aSTrond Myklebust 21904e4bd1cSCatalin Marinas /* 22004e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 22104e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 22204e4bd1cSCatalin Marinas */ 223a52a8a6aSTrond Myklebust if (ret != NULL) 224a52a8a6aSTrond Myklebust kmemleak_not_leak(ret); 225a52a8a6aSTrond Myklebust return ret; 226d1bacf9eSBryan Schumaker } 227d1bacf9eSBryan Schumaker 228b1e21c97STrond Myklebust /* 229b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 230b1e21c97STrond Myklebust */ 231b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 232b1e21c97STrond Myklebust { 233b1e21c97STrond Myklebust struct nfs_cache_array_entry *cache_entry; 234b1e21c97STrond Myklebust 235b1e21c97STrond Myklebust if (array->page_full) 236b1e21c97STrond Myklebust return -ENOSPC; 237b1e21c97STrond Myklebust cache_entry = &array->array[array->size + 1]; 238b1e21c97STrond Myklebust if ((char *)cache_entry - (char *)array > PAGE_SIZE) { 239b1e21c97STrond Myklebust array->page_full = 1; 240b1e21c97STrond Myklebust return -ENOSPC; 241b1e21c97STrond Myklebust } 242b1e21c97STrond Myklebust return 0; 243b1e21c97STrond Myklebust } 244b1e21c97STrond Myklebust 245d1bacf9eSBryan Schumaker static 246d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 247d1bacf9eSBryan Schumaker { 248a52a8a6aSTrond Myklebust struct nfs_cache_array *array; 2494a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 250a52a8a6aSTrond Myklebust const char *name; 2514a201d6eSTrond Myklebust int ret; 2524a201d6eSTrond Myklebust 253a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len); 254a52a8a6aSTrond Myklebust if (!name) 255a52a8a6aSTrond Myklebust return -ENOMEM; 256a52a8a6aSTrond Myklebust 257a52a8a6aSTrond Myklebust array = kmap_atomic(page); 258b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 259a52a8a6aSTrond Myklebust if (ret) { 260a52a8a6aSTrond Myklebust kfree(name); 2613020093fSTrond Myklebust goto out; 262a52a8a6aSTrond Myklebust } 2633020093fSTrond Myklebust 264b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 2654a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2664a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2670b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 268a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len; 269a52a8a6aSTrond Myklebust cache_entry->name = name; 270d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2718cd51a0cSTrond Myklebust array->size++; 27247c716cbSTrond Myklebust if (entry->eof != 0) 273b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 2744a201d6eSTrond Myklebust out: 275a52a8a6aSTrond Myklebust kunmap_atomic(array); 2764a201d6eSTrond Myklebust return ret; 277d1bacf9eSBryan Schumaker } 278d1bacf9eSBryan Schumaker 2791f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 2801f1d4aa4STrond Myklebust pgoff_t index, u64 last_cookie) 2811f1d4aa4STrond Myklebust { 2821f1d4aa4STrond Myklebust struct page *page; 2831f1d4aa4STrond Myklebust 2841f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 2851f1d4aa4STrond Myklebust if (page && !PageUptodate(page)) { 2861f1d4aa4STrond Myklebust nfs_readdir_page_init_array(page, last_cookie); 2871f1d4aa4STrond Myklebust if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0) 2881f1d4aa4STrond Myklebust nfs_zap_mapping(mapping->host, mapping); 2891f1d4aa4STrond Myklebust SetPageUptodate(page); 2901f1d4aa4STrond Myklebust } 2911f1d4aa4STrond Myklebust 2921f1d4aa4STrond Myklebust return page; 2931f1d4aa4STrond Myklebust } 2941f1d4aa4STrond Myklebust 2951f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 2961f1d4aa4STrond Myklebust { 2971f1d4aa4STrond Myklebust struct nfs_cache_array *array; 2981f1d4aa4STrond Myklebust u64 ret; 2991f1d4aa4STrond Myklebust 3001f1d4aa4STrond Myklebust array = kmap_atomic(page); 3011f1d4aa4STrond Myklebust ret = array->last_cookie; 3021f1d4aa4STrond Myklebust kunmap_atomic(array); 3031f1d4aa4STrond Myklebust return ret; 3041f1d4aa4STrond Myklebust } 3051f1d4aa4STrond Myklebust 3061f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 3071f1d4aa4STrond Myklebust { 3081f1d4aa4STrond Myklebust struct nfs_cache_array *array; 3091f1d4aa4STrond Myklebust bool ret; 3101f1d4aa4STrond Myklebust 3111f1d4aa4STrond Myklebust array = kmap_atomic(page); 3121f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 3131f1d4aa4STrond Myklebust kunmap_atomic(array); 3141f1d4aa4STrond Myklebust return ret; 3151f1d4aa4STrond Myklebust } 3161f1d4aa4STrond Myklebust 317b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 318b1e21c97STrond Myklebust { 319b1e21c97STrond Myklebust struct nfs_cache_array *array; 320b1e21c97STrond Myklebust 321b1e21c97STrond Myklebust array = kmap_atomic(page); 322b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 323b1e21c97STrond Myklebust kunmap_atomic(array); 324b1e21c97STrond Myklebust } 325b1e21c97STrond Myklebust 3263b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page) 3273b2a09f1STrond Myklebust { 3283b2a09f1STrond Myklebust unlock_page(page); 3293b2a09f1STrond Myklebust put_page(page); 3303b2a09f1STrond Myklebust } 3313b2a09f1STrond Myklebust 3323b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 3333b2a09f1STrond Myklebust pgoff_t index, u64 cookie) 3343b2a09f1STrond Myklebust { 3353b2a09f1STrond Myklebust struct page *page; 3363b2a09f1STrond Myklebust 3373b2a09f1STrond Myklebust page = nfs_readdir_page_get_locked(mapping, index, cookie); 3383b2a09f1STrond Myklebust if (page) { 3393b2a09f1STrond Myklebust if (nfs_readdir_page_last_cookie(page) == cookie) 3403b2a09f1STrond Myklebust return page; 3413b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 3423b2a09f1STrond Myklebust } 3433b2a09f1STrond Myklebust return NULL; 3443b2a09f1STrond Myklebust } 3453b2a09f1STrond Myklebust 34659e356a9STrond Myklebust static inline 34759e356a9STrond Myklebust int is_32bit_api(void) 34859e356a9STrond Myklebust { 34959e356a9STrond Myklebust #ifdef CONFIG_COMPAT 35059e356a9STrond Myklebust return in_compat_syscall(); 35159e356a9STrond Myklebust #else 35259e356a9STrond Myklebust return (BITS_PER_LONG == 32); 35359e356a9STrond Myklebust #endif 35459e356a9STrond Myklebust } 35559e356a9STrond Myklebust 35659e356a9STrond Myklebust static 35759e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 35859e356a9STrond Myklebust { 35959e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 36059e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 36159e356a9STrond Myklebust return false; 36259e356a9STrond Myklebust return true; 36359e356a9STrond Myklebust } 36459e356a9STrond Myklebust 3656c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 3666c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 367d1bacf9eSBryan Schumaker { 36823db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 369d1bacf9eSBryan Schumaker unsigned int index; 370d1bacf9eSBryan Schumaker 371d1bacf9eSBryan Schumaker if (diff < 0) 372d1bacf9eSBryan Schumaker goto out_eof; 373d1bacf9eSBryan Schumaker if (diff >= array->size) { 374b1e21c97STrond Myklebust if (array->page_is_eof) 375d1bacf9eSBryan Schumaker goto out_eof; 376d1bacf9eSBryan Schumaker return -EAGAIN; 377d1bacf9eSBryan Schumaker } 378d1bacf9eSBryan Schumaker 379d1bacf9eSBryan Schumaker index = (unsigned int)diff; 3802e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 381d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 382d1bacf9eSBryan Schumaker return 0; 383d1bacf9eSBryan Schumaker out_eof: 3846089dd0dSThomas Meyer desc->eof = true; 385d1bacf9eSBryan Schumaker return -EBADCOOKIE; 386d1bacf9eSBryan Schumaker } 387d1bacf9eSBryan Schumaker 3884db72b40SJeff Layton static bool 3894db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 3904db72b40SJeff Layton { 3914db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 3924db72b40SJeff Layton return false; 3934db72b40SJeff Layton smp_rmb(); 3944db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 3954db72b40SJeff Layton } 3964db72b40SJeff Layton 3976c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 3986c981effSTrond Myklebust struct nfs_readdir_descriptor *desc) 399d1bacf9eSBryan Schumaker { 400d1bacf9eSBryan Schumaker int i; 4018ef2ce3eSBryan Schumaker loff_t new_pos; 402d1bacf9eSBryan Schumaker int status = -EAGAIN; 403d1bacf9eSBryan Schumaker 404d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 4052e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 406496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 4070c030806STrond Myklebust 4088ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 4092e7a4641STrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount || 4104db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 4112e7a4641STrond Myklebust desc->duped = 0; 4122e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 41359e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 4142e7a4641STrond Myklebust if (desc->duped > 0 4152e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 4160c030806STrond Myklebust if (printk_ratelimit()) { 4176de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 4180c030806STrond Myklebust "Please contact your server vendor. " 419a52a8a6aSTrond Myklebust "The file: %s has duplicate cookie %llu\n", 420a52a8a6aSTrond Myklebust desc->file, array->array[i].name, desc->dir_cookie); 4210c030806STrond Myklebust } 4220c030806STrond Myklebust status = -ELOOP; 4230c030806STrond Myklebust goto out; 4240c030806STrond Myklebust } 4252e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 4262e7a4641STrond Myklebust desc->duped = -1; 4278ef2ce3eSBryan Schumaker } 42859e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 4292e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 43059e356a9STrond Myklebust else 43123db8620SAl Viro desc->ctx->pos = new_pos; 43259e356a9STrond Myklebust desc->prev_index = new_pos; 4338cd51a0cSTrond Myklebust desc->cache_entry_index = i; 43447c716cbSTrond Myklebust return 0; 4358cd51a0cSTrond Myklebust } 4368cd51a0cSTrond Myklebust } 437b1e21c97STrond Myklebust if (array->page_is_eof) { 438d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 4392e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 4406089dd0dSThomas Meyer desc->eof = true; 441d1bacf9eSBryan Schumaker } 4420c030806STrond Myklebust out: 443d1bacf9eSBryan Schumaker return status; 444d1bacf9eSBryan Schumaker } 445d1bacf9eSBryan Schumaker 4466c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 447d1bacf9eSBryan Schumaker { 448d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 44947c716cbSTrond Myklebust int status; 450d1bacf9eSBryan Schumaker 451ed09222dSTrond Myklebust array = kmap_atomic(desc->page); 452d1bacf9eSBryan Schumaker 4532e7a4641STrond Myklebust if (desc->dir_cookie == 0) 454d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 455d1bacf9eSBryan Schumaker else 456d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 457d1bacf9eSBryan Schumaker 45847c716cbSTrond Myklebust if (status == -EAGAIN) { 4590aded708STrond Myklebust desc->last_cookie = array->last_cookie; 460e47c085aSTrond Myklebust desc->current_index += array->size; 46147c716cbSTrond Myklebust desc->page_index++; 46247c716cbSTrond Myklebust } 463ed09222dSTrond Myklebust kunmap_atomic(array); 464d1bacf9eSBryan Schumaker return status; 465d1bacf9eSBryan Schumaker } 466d1bacf9eSBryan Schumaker 467d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 46893b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 46993b8959aSTrond Myklebust u64 cookie, struct page **pages, 47093b8959aSTrond Myklebust size_t bufsize) 471d1bacf9eSBryan Schumaker { 472*82e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file); 473*82e22a5eSTrond Myklebust __be32 verf_res[2]; 474*82e22a5eSTrond Myklebust struct nfs_readdir_arg arg = { 475*82e22a5eSTrond Myklebust .dentry = file_dentry(desc->file), 476*82e22a5eSTrond Myklebust .cred = desc->file->f_cred, 477*82e22a5eSTrond Myklebust .verf = NFS_I(inode)->cookieverf, 478*82e22a5eSTrond Myklebust .cookie = cookie, 479*82e22a5eSTrond Myklebust .pages = pages, 480*82e22a5eSTrond Myklebust .page_len = bufsize, 481*82e22a5eSTrond Myklebust .plus = desc->plus, 482*82e22a5eSTrond Myklebust }; 483*82e22a5eSTrond Myklebust struct nfs_readdir_res res = { 484*82e22a5eSTrond Myklebust .verf = verf_res, 485*82e22a5eSTrond Myklebust }; 4864704f0e2STrond Myklebust unsigned long timestamp, gencount; 4871da177e4SLinus Torvalds int error; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds again: 4901da177e4SLinus Torvalds timestamp = jiffies; 4914704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 492a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 493*82e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res); 4941da177e4SLinus Torvalds if (error < 0) { 4951da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 4961da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 4971da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 4983a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 499*82e22a5eSTrond Myklebust desc->plus = arg.plus = false; 5001da177e4SLinus Torvalds goto again; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds goto error; 5031da177e4SLinus Torvalds } 5041f4eab7eSNeil Brown desc->timestamp = timestamp; 5054704f0e2STrond Myklebust desc->gencount = gencount; 506*82e22a5eSTrond Myklebust memcpy(NFS_I(inode)->cookieverf, res.verf, 507*82e22a5eSTrond Myklebust sizeof(NFS_I(inode)->cookieverf)); 508d1bacf9eSBryan Schumaker error: 509d1bacf9eSBryan Schumaker return error; 510d1bacf9eSBryan Schumaker } 511d1bacf9eSBryan Schumaker 5126c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc, 513573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 514d1bacf9eSBryan Schumaker { 51559e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 516573c4e1eSChuck Lever int error; 517d1bacf9eSBryan Schumaker 51859e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 519573c4e1eSChuck Lever if (error) 520573c4e1eSChuck Lever return error; 521d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 522d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 523d1bacf9eSBryan Schumaker return 0; 524d1bacf9eSBryan Schumaker } 525d1bacf9eSBryan Schumaker 526fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 527fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 528fa923369STrond Myklebust */ 529d39ab9deSBryan Schumaker static 530d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 531d39ab9deSBryan Schumaker { 532d8fdb47fSTrond Myklebust struct inode *inode; 533fa923369STrond Myklebust struct nfs_inode *nfsi; 534fa923369STrond Myklebust 5352b0143b5SDavid Howells if (d_really_is_negative(dentry)) 5362b0143b5SDavid Howells return 0; 537fa923369STrond Myklebust 538d8fdb47fSTrond Myklebust inode = d_inode(dentry); 539d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 540d8fdb47fSTrond Myklebust return 0; 541d8fdb47fSTrond Myklebust 542d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 5437dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 544d39ab9deSBryan Schumaker return 0; 5457dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 5467dc72d5fSTrond Myklebust return 0; 5477dc72d5fSTrond Myklebust return 1; 548d39ab9deSBryan Schumaker } 549d39ab9deSBryan Schumaker 550d39ab9deSBryan Schumaker static 55123db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 552d69ee9b8STrond Myklebust { 553d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 554d69ee9b8STrond Myklebust return false; 555d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 556d69ee9b8STrond Myklebust return true; 55723db8620SAl Viro if (ctx->pos == 0) 558d69ee9b8STrond Myklebust return true; 559d69ee9b8STrond Myklebust return false; 560d69ee9b8STrond Myklebust } 561d69ee9b8STrond Myklebust 562d69ee9b8STrond Myklebust /* 56363519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 56463519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 565d69ee9b8STrond Myklebust * directory. 566d69ee9b8STrond Myklebust */ 567d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 568d69ee9b8STrond Myklebust { 56963519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 57063519fbcSTrond Myklebust 57163519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 57263519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 57363519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 574d69ee9b8STrond Myklebust } 575d69ee9b8STrond Myklebust 576311324adSTrond Myklebust /* 577311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 578311324adSTrond Myklebust * 579311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 580311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 581311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 582311324adSTrond Myklebust * cache flush. 583311324adSTrond Myklebust */ 584311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 585311324adSTrond Myklebust { 58663519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 58763519fbcSTrond Myklebust 58863519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 58963519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 59063519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 591227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 592227823d2SDai Ngo nfsi->page_index + 1, -1); 593311324adSTrond Myklebust } 594311324adSTrond Myklebust } 595311324adSTrond Myklebust 596d69ee9b8STrond Myklebust static 597a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 598a1147b82STrond Myklebust unsigned long dir_verifier) 599d39ab9deSBryan Schumaker { 60026fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 6019ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 6024a201d6eSTrond Myklebust struct dentry *dentry; 6034a201d6eSTrond Myklebust struct dentry *alias; 604d39ab9deSBryan Schumaker struct inode *inode; 605aa9c2669SDavid Quigley int status; 606d39ab9deSBryan Schumaker 607fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 608fa923369STrond Myklebust return; 6096c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 6106c441c25STrond Myklebust return; 61178d04af4STrond Myklebust if (filename.len == 0) 61278d04af4STrond Myklebust return; 61378d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 61478d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 61578d04af4STrond Myklebust return; 61678d04af4STrond Myklebust /* ...or '/' */ 61778d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 61878d04af4STrond Myklebust return; 6194a201d6eSTrond Myklebust if (filename.name[0] == '.') { 6204a201d6eSTrond Myklebust if (filename.len == 1) 6214a201d6eSTrond Myklebust return; 6224a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 6234a201d6eSTrond Myklebust return; 6244a201d6eSTrond Myklebust } 6258387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 626d39ab9deSBryan Schumaker 6274a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 6289ac3d3e8SAl Viro again: 6299ac3d3e8SAl Viro if (!dentry) { 6309ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 6319ac3d3e8SAl Viro if (IS_ERR(dentry)) 6329ac3d3e8SAl Viro return; 6339ac3d3e8SAl Viro } 6349ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 6356c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 6366c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 6376c441c25STrond Myklebust &entry->fattr->fsid)) 6386c441c25STrond Myklebust goto out; 639d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 6407dc72d5fSTrond Myklebust if (!entry->fh->size) 6417dc72d5fSTrond Myklebust goto out; 642a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 6432b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 644aa9c2669SDavid Quigley if (!status) 6452b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 646d39ab9deSBryan Schumaker goto out; 647d39ab9deSBryan Schumaker } else { 6485542aa2fSEric W. Biederman d_invalidate(dentry); 649d39ab9deSBryan Schumaker dput(dentry); 6509ac3d3e8SAl Viro dentry = NULL; 6519ac3d3e8SAl Viro goto again; 652d39ab9deSBryan Schumaker } 653d39ab9deSBryan Schumaker } 6547dc72d5fSTrond Myklebust if (!entry->fh->size) { 6557dc72d5fSTrond Myklebust d_lookup_done(dentry); 6567dc72d5fSTrond Myklebust goto out; 6577dc72d5fSTrond Myklebust } 658d39ab9deSBryan Schumaker 6591775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 66041d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 6619ac3d3e8SAl Viro d_lookup_done(dentry); 6629ac3d3e8SAl Viro if (alias) { 663d39ab9deSBryan Schumaker if (IS_ERR(alias)) 664d39ab9deSBryan Schumaker goto out; 6659ac3d3e8SAl Viro dput(dentry); 6669ac3d3e8SAl Viro dentry = alias; 6679ac3d3e8SAl Viro } 668a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 669d39ab9deSBryan Schumaker out: 670d39ab9deSBryan Schumaker dput(dentry); 671d39ab9deSBryan Schumaker } 672d39ab9deSBryan Schumaker 673d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 6743b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 6753b2a09f1STrond Myklebust struct nfs_entry *entry, 6763b2a09f1STrond Myklebust struct page **xdr_pages, 6773b2a09f1STrond Myklebust struct page *fillme, unsigned int buflen) 678d1bacf9eSBryan Schumaker { 6793b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping; 680babddc72SBryan Schumaker struct xdr_stream stream; 681f7da7a12SBenny Halevy struct xdr_buf buf; 6823b2a09f1STrond Myklebust struct page *scratch, *new, *page = fillme; 6835c346854STrond Myklebust int status; 684babddc72SBryan Schumaker 6856650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 6866650239aSTrond Myklebust if (scratch == NULL) 6876650239aSTrond Myklebust return -ENOMEM; 688babddc72SBryan Schumaker 689f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 6906650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 69199424380SBryan Schumaker 69299424380SBryan Schumaker do { 693d33030e2SJeffrey Mitchell if (entry->label) 694d33030e2SJeffrey Mitchell entry->label->len = NFS4_MAXLABELLEN; 695d33030e2SJeffrey Mitchell 69699424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 697972bcdf2STrond Myklebust if (status != 0) 69899424380SBryan Schumaker break; 6995c346854STrond Myklebust 700a7a3b1e9SBenjamin Coddington if (desc->plus) 701a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 702a1147b82STrond Myklebust desc->dir_verifier); 7038cd51a0cSTrond Myklebust 704db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 7053b2a09f1STrond Myklebust if (status != -ENOSPC) 7063b2a09f1STrond Myklebust continue; 7073b2a09f1STrond Myklebust 7083b2a09f1STrond Myklebust if (page->mapping != mapping) 7093b2a09f1STrond Myklebust break; 7103b2a09f1STrond Myklebust new = nfs_readdir_page_get_next(mapping, page->index + 1, 7113b2a09f1STrond Myklebust entry->prev_cookie); 7123b2a09f1STrond Myklebust if (!new) 7133b2a09f1STrond Myklebust break; 7143b2a09f1STrond Myklebust if (page != fillme) 7153b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7163b2a09f1STrond Myklebust page = new; 7173b2a09f1STrond Myklebust status = nfs_readdir_add_to_array(entry, page); 718972bcdf2STrond Myklebust } while (!status && !entry->eof); 71999424380SBryan Schumaker 720972bcdf2STrond Myklebust switch (status) { 721972bcdf2STrond Myklebust case -EBADCOOKIE: 722972bcdf2STrond Myklebust if (entry->eof) { 723b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 72499424380SBryan Schumaker status = 0; 72556e4ebf8SBryan Schumaker } 726972bcdf2STrond Myklebust break; 727972bcdf2STrond Myklebust case -ENOSPC: 728972bcdf2STrond Myklebust case -EAGAIN: 729972bcdf2STrond Myklebust status = 0; 730972bcdf2STrond Myklebust break; 731972bcdf2STrond Myklebust } 7326650239aSTrond Myklebust 7333b2a09f1STrond Myklebust if (page != fillme) 7343b2a09f1STrond Myklebust nfs_readdir_page_unlock_and_put(page); 7353b2a09f1STrond Myklebust 7366650239aSTrond Myklebust put_page(scratch); 7378cd51a0cSTrond Myklebust return status; 7388cd51a0cSTrond Myklebust } 73956e4ebf8SBryan Schumaker 7401a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages) 74156e4ebf8SBryan Schumaker { 7421a34c8c9STrond Myklebust while (npages--) 7431a34c8c9STrond Myklebust put_page(pages[npages]); 7441a34c8c9STrond Myklebust kfree(pages); 74556e4ebf8SBryan Schumaker } 74656e4ebf8SBryan Schumaker 74756e4ebf8SBryan Schumaker /* 748bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 749bf211ca1Szhangliguang * to nfs_readdir_free_pages() 75056e4ebf8SBryan Schumaker */ 7511a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages) 75256e4ebf8SBryan Schumaker { 7531a34c8c9STrond Myklebust struct page **pages; 7541a34c8c9STrond Myklebust size_t i; 75556e4ebf8SBryan Schumaker 7561a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 7571a34c8c9STrond Myklebust if (!pages) 7581a34c8c9STrond Myklebust return NULL; 75956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 76056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 76156e4ebf8SBryan Schumaker if (page == NULL) 76256e4ebf8SBryan Schumaker goto out_freepages; 76356e4ebf8SBryan Schumaker pages[i] = page; 76456e4ebf8SBryan Schumaker } 7651a34c8c9STrond Myklebust return pages; 76656e4ebf8SBryan Schumaker 76756e4ebf8SBryan Schumaker out_freepages: 768c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 7691a34c8c9STrond Myklebust return NULL; 770d1bacf9eSBryan Schumaker } 771d1bacf9eSBryan Schumaker 7726c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 7736c981effSTrond Myklebust struct page *page, struct inode *inode) 774d1bacf9eSBryan Schumaker { 7751a34c8c9STrond Myklebust struct page **pages; 7766b75cf9eSTrond Myklebust struct nfs_entry *entry; 7771a34c8c9STrond Myklebust size_t array_size; 7781a34c8c9STrond Myklebust size_t dtsize = NFS_SERVER(inode)->dtsize; 7798cd51a0cSTrond Myklebust int status = -ENOMEM; 780d1bacf9eSBryan Schumaker 7816b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL); 7826b75cf9eSTrond Myklebust if (!entry) 7836b75cf9eSTrond Myklebust return -ENOMEM; 7846b75cf9eSTrond Myklebust entry->cookie = nfs_readdir_page_last_cookie(page); 7856b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle(); 7866b75cf9eSTrond Myklebust entry->fattr = nfs_alloc_fattr(); 7876b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode); 7886b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL) 789d1bacf9eSBryan Schumaker goto out; 790d1bacf9eSBryan Schumaker 7916b75cf9eSTrond Myklebust entry->label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 7926b75cf9eSTrond Myklebust if (IS_ERR(entry->label)) { 7936b75cf9eSTrond Myklebust status = PTR_ERR(entry->label); 79414c43f76SDavid Quigley goto out; 79514c43f76SDavid Quigley } 79614c43f76SDavid Quigley 7971a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 7981a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size); 7991a34c8c9STrond Myklebust if (!pages) 800e762a639STrond Myklebust goto out_release_label; 8011a34c8c9STrond Myklebust 802d1bacf9eSBryan Schumaker do { 803ac396128STrond Myklebust unsigned int pglen; 8046b75cf9eSTrond Myklebust status = nfs_readdir_xdr_filler(desc, entry->cookie, 80593b8959aSTrond Myklebust pages, dtsize); 806d1bacf9eSBryan Schumaker if (status < 0) 807d1bacf9eSBryan Schumaker break; 808972bcdf2STrond Myklebust 809ac396128STrond Myklebust pglen = status; 810972bcdf2STrond Myklebust if (pglen == 0) { 811972bcdf2STrond Myklebust nfs_readdir_page_set_eof(page); 8128cd51a0cSTrond Myklebust break; 8138cd51a0cSTrond Myklebust } 814972bcdf2STrond Myklebust 8156b75cf9eSTrond Myklebust status = nfs_readdir_page_filler(desc, entry, pages, page, pglen); 816e762a639STrond Myklebust } while (!status && nfs_readdir_page_needs_filling(page)); 817d1bacf9eSBryan Schumaker 818c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 819e762a639STrond Myklebust out_release_label: 8206b75cf9eSTrond Myklebust nfs4_label_free(entry->label); 821d1bacf9eSBryan Schumaker out: 8226b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr); 8236b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh); 8246b75cf9eSTrond Myklebust kfree(entry); 825d1bacf9eSBryan Schumaker return status; 826d1bacf9eSBryan Schumaker } 827d1bacf9eSBryan Schumaker 8281f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 8291da177e4SLinus Torvalds { 83009cbfeafSKirill A. Shutemov put_page(desc->page); 8311da177e4SLinus Torvalds desc->page = NULL; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341f1d4aa4STrond Myklebust static void 8351f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 8361da177e4SLinus Torvalds { 8371f1d4aa4STrond Myklebust unlock_page(desc->page); 8381f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 8391f1d4aa4STrond Myklebust } 8401f1d4aa4STrond Myklebust 8411f1d4aa4STrond Myklebust static struct page * 8421f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 8431f1d4aa4STrond Myklebust { 8441f1d4aa4STrond Myklebust return nfs_readdir_page_get_locked(desc->file->f_mapping, 8451f1d4aa4STrond Myklebust desc->page_index, 8461f1d4aa4STrond Myklebust desc->last_cookie); 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* 850d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 851114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 8521da177e4SLinus Torvalds */ 8536c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 854d1bacf9eSBryan Schumaker { 855227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 856227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 857d1bacf9eSBryan Schumaker int res; 858d1bacf9eSBryan Schumaker 8591f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 8601f1d4aa4STrond Myklebust if (!desc->page) 8611f1d4aa4STrond Myklebust return -ENOMEM; 8621f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 8631f1d4aa4STrond Myklebust res = nfs_readdir_xdr_to_array(desc, desc->page, inode); 8641f1d4aa4STrond Myklebust if (res < 0) 865114de382STrond Myklebust goto error; 8661f1d4aa4STrond Myklebust } 867114de382STrond Myklebust res = nfs_readdir_search_array(desc); 868227823d2SDai Ngo if (res == 0) { 869227823d2SDai Ngo nfsi->page_index = desc->page_index; 870114de382STrond Myklebust return 0; 871114de382STrond Myklebust } 872114de382STrond Myklebust error: 8731f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 874d1bacf9eSBryan Schumaker return res; 875d1bacf9eSBryan Schumaker } 876d1bacf9eSBryan Schumaker 877d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 8786c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 8791da177e4SLinus Torvalds { 8808cd51a0cSTrond Myklebust int res; 881d1bacf9eSBryan Schumaker 8820aded708STrond Myklebust if (desc->page_index == 0) { 8838cd51a0cSTrond Myklebust desc->current_index = 0; 88459e356a9STrond Myklebust desc->prev_index = 0; 8850aded708STrond Myklebust desc->last_cookie = 0; 8860aded708STrond Myklebust } 88747c716cbSTrond Myklebust do { 888114de382STrond Myklebust res = find_and_lock_cache_page(desc); 88947c716cbSTrond Myklebust } while (res == -EAGAIN); 8901da177e4SLinus Torvalds return res; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds /* 8941da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 8951da177e4SLinus Torvalds */ 896dbeaf8c9STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc) 8971da177e4SLinus Torvalds { 8981da177e4SLinus Torvalds struct file *file = desc->file; 899dbeaf8c9STrond Myklebust struct nfs_cache_array *array; 900dbeaf8c9STrond Myklebust unsigned int i = 0; 9018ef2ce3eSBryan Schumaker 9020795bf83SFabian Frederick array = kmap(desc->page); 903d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 904ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 9051da177e4SLinus Torvalds 906ece0b423STrond Myklebust ent = &array->array[i]; 907a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len, 90823db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 9096089dd0dSThomas Meyer desc->eof = true; 9101da177e4SLinus Torvalds break; 911ece0b423STrond Myklebust } 912d1bacf9eSBryan Schumaker if (i < (array->size-1)) 9132e7a4641STrond Myklebust desc->dir_cookie = array->array[i+1].cookie; 914d1bacf9eSBryan Schumaker else 9152e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 91659e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 9172e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 91859e356a9STrond Myklebust else 91959e356a9STrond Myklebust desc->ctx->pos++; 9202e7a4641STrond Myklebust if (desc->duped != 0) 9212e7a4641STrond Myklebust desc->duped = 1; 9228cd51a0cSTrond Myklebust } 923b1e21c97STrond Myklebust if (array->page_is_eof) 9246089dd0dSThomas Meyer desc->eof = true; 925d1bacf9eSBryan Schumaker 9260795bf83SFabian Frederick kunmap(desc->page); 927dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 928dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie); 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds /* 9321da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 9331da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 9341da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 9351da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 9361da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 9371da177e4SLinus Torvalds * 9381da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 9391da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 9401da177e4SLinus Torvalds * we should already have a complete representation of the 9411da177e4SLinus Torvalds * directory in the page cache by the time we get here. 9421da177e4SLinus Torvalds */ 9436c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds struct page *page = NULL; 9461da177e4SLinus Torvalds int status; 947496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 9481da177e4SLinus Torvalds 9491e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 9502e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 9531da177e4SLinus Torvalds if (!page) { 9541da177e4SLinus Torvalds status = -ENOMEM; 9551da177e4SLinus Torvalds goto out; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 9587a8e1dc3STrond Myklebust desc->page_index = 0; 9592e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 9607a8e1dc3STrond Myklebust desc->page = page; 9612e7a4641STrond Myklebust desc->duped = 0; 9627a8e1dc3STrond Myklebust 9631f1d4aa4STrond Myklebust nfs_readdir_page_init_array(page, desc->dir_cookie); 96485f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 96585f8607eSTrond Myklebust if (status < 0) 966d1bacf9eSBryan Schumaker goto out_release; 967d1bacf9eSBryan Schumaker 968dbeaf8c9STrond Myklebust nfs_do_filldir(desc); 9691da177e4SLinus Torvalds 970114de382STrond Myklebust out_release: 971114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 9721f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 9731da177e4SLinus Torvalds out: 9741e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 9753110ff80SHarvey Harrison __func__, status); 9761da177e4SLinus Torvalds return status; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 97900a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 98000a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 98100a92642SOlivier Galibert whole directory. 9821da177e4SLinus Torvalds */ 98323db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 9841da177e4SLinus Torvalds { 985be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 9862b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 98723db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 9886b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc; 9896b75cf9eSTrond Myklebust int res; 9901da177e4SLinus Torvalds 9916de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9926de1472fSAl Viro file, (long long)ctx->pos); 99391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 99491d5b470SChuck Lever 9951da177e4SLinus Torvalds /* 99623db8620SAl Viro * ctx->pos points to the dirent entry number. 997f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 99800a92642SOlivier Galibert * to either find the entry with the appropriate number or 99900a92642SOlivier Galibert * revalidate the cookie. 10001da177e4SLinus Torvalds */ 10016b75cf9eSTrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) { 100223db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 1003fccca7fcSTrond Myklebust if (res < 0) 1004fccca7fcSTrond Myklebust goto out; 10056b75cf9eSTrond Myklebust } 10066b75cf9eSTrond Myklebust 10076b75cf9eSTrond Myklebust res = -ENOMEM; 10086b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL); 10096b75cf9eSTrond Myklebust if (!desc) 10106b75cf9eSTrond Myklebust goto out; 10116b75cf9eSTrond Myklebust desc->file = file; 10126b75cf9eSTrond Myklebust desc->ctx = ctx; 10136b75cf9eSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx); 1014fccca7fcSTrond Myklebust 10152e7a4641STrond Myklebust spin_lock(&file->f_lock); 10162e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 10172e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 10182e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 10192e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 10202e7a4641STrond Myklebust spin_unlock(&file->f_lock); 10212e7a4641STrond Myklebust 102247c716cbSTrond Myklebust do { 10231da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 102400a92642SOlivier Galibert 10251da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 1026ece0b423STrond Myklebust res = 0; 10271da177e4SLinus Torvalds /* This means either end of directory */ 10282e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 10291da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 103023db8620SAl Viro res = uncached_readdir(desc); 1031ece0b423STrond Myklebust if (res == 0) 10321da177e4SLinus Torvalds continue; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds break; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 10373a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 10381da177e4SLinus Torvalds nfs_zap_caches(inode); 1039baf57a09STrond Myklebust desc->page_index = 0; 1040a7a3b1e9SBenjamin Coddington desc->plus = false; 1041a7a3b1e9SBenjamin Coddington desc->eof = false; 10421da177e4SLinus Torvalds continue; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds if (res < 0) 10451da177e4SLinus Torvalds break; 10461da177e4SLinus Torvalds 1047dbeaf8c9STrond Myklebust nfs_do_filldir(desc); 10481f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 104947c716cbSTrond Myklebust } while (!desc->eof); 10502e7a4641STrond Myklebust 10512e7a4641STrond Myklebust spin_lock(&file->f_lock); 10522e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 10532e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 10542e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 10552e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 10562e7a4641STrond Myklebust spin_unlock(&file->f_lock); 10572e7a4641STrond Myklebust 10586b75cf9eSTrond Myklebust kfree(desc); 10596b75cf9eSTrond Myklebust 1060fccca7fcSTrond Myklebust out: 10616de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 10621da177e4SLinus Torvalds return res; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 1065965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1066f0dd2136STrond Myklebust { 1067480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1068b84e06c5SChuck Lever 10696de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 10706de1472fSAl Viro filp, offset, whence); 1071b84e06c5SChuck Lever 1072965c8e59SAndrew Morton switch (whence) { 1073f0dd2136STrond Myklebust default: 1074b2b1ff3dSTrond Myklebust return -EINVAL; 1075b2b1ff3dSTrond Myklebust case SEEK_SET: 1076b2b1ff3dSTrond Myklebust if (offset < 0) 1077b2b1ff3dSTrond Myklebust return -EINVAL; 107883f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1079b2b1ff3dSTrond Myklebust break; 1080b2b1ff3dSTrond Myklebust case SEEK_CUR: 1081b2b1ff3dSTrond Myklebust if (offset == 0) 1082b2b1ff3dSTrond Myklebust return filp->f_pos; 108383f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1084b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1085b2b1ff3dSTrond Myklebust if (offset < 0) { 108683f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1087b2b1ff3dSTrond Myklebust return -EINVAL; 1088b2b1ff3dSTrond Myklebust } 1089f0dd2136STrond Myklebust } 1090f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1091f0dd2136STrond Myklebust filp->f_pos = offset; 109259e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 109359e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 109459e356a9STrond Myklebust else 1095480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 10968ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1097f0dd2136STrond Myklebust } 109883f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1099f0dd2136STrond Myklebust return offset; 1100f0dd2136STrond Myklebust } 1101f0dd2136STrond Myklebust 11021da177e4SLinus Torvalds /* 11031da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 11041da177e4SLinus Torvalds * is a dummy operation. 11051da177e4SLinus Torvalds */ 110602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 110702c24a82SJosef Bacik int datasync) 11081da177e4SLinus Torvalds { 11096de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 11101e7cb3dcSChuck Lever 111111decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 11121da177e4SLinus Torvalds return 0; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 1115bfc69a45STrond Myklebust /** 1116bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1117302fad7bSTrond Myklebust * @dir: pointer to directory inode 1118bfc69a45STrond Myklebust * 1119bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1120bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1121bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1122bfc69a45STrond Myklebust * 1123efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1124efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1125efeda80dSTrond Myklebust * 1126bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1127bfc69a45STrond Myklebust */ 1128bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1129bfc69a45STrond Myklebust { 1130efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1131bfc69a45STrond Myklebust } 113289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1133bfc69a45STrond Myklebust 1134efeda80dSTrond Myklebust /** 1135efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1136efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1137efeda80dSTrond Myklebust * @verf: previously saved change attribute 1138efeda80dSTrond Myklebust * 1139efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1140efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1141efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1142efeda80dSTrond Myklebust */ 1143efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1144efeda80dSTrond Myklebust { 1145efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1146efeda80dSTrond Myklebust } 1147efeda80dSTrond Myklebust 1148efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1149efeda80dSTrond Myklebust { 1150efeda80dSTrond Myklebust *verf |= 1UL; 1151efeda80dSTrond Myklebust } 1152efeda80dSTrond Myklebust 1153efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1154efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1155efeda80dSTrond Myklebust { 1156efeda80dSTrond Myklebust *verf &= ~1UL; 1157efeda80dSTrond Myklebust } 1158efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1159efeda80dSTrond Myklebust 1160efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1161efeda80dSTrond Myklebust { 1162efeda80dSTrond Myklebust return verf & 1; 1163efeda80dSTrond Myklebust } 1164efeda80dSTrond Myklebust 1165efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1166efeda80dSTrond Myklebust { 1167efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1168efeda80dSTrond Myklebust } 1169efeda80dSTrond Myklebust 1170efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1171efeda80dSTrond Myklebust { 1172efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1173efeda80dSTrond Myklebust 1174efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1175efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1176efeda80dSTrond Myklebust goto out; 1177efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1178efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1179efeda80dSTrond Myklebust out: 1180efeda80dSTrond Myklebust dentry->d_time = verf; 1181efeda80dSTrond Myklebust } 1182efeda80dSTrond Myklebust 1183efeda80dSTrond Myklebust /** 1184efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1185efeda80dSTrond Myklebust * @dentry: pointer to dentry 1186efeda80dSTrond Myklebust * @verf: verifier to save 1187efeda80dSTrond Myklebust * 1188efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1189efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1190efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1191efeda80dSTrond Myklebust * look it up again after a directory change. 1192efeda80dSTrond Myklebust */ 1193efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1194efeda80dSTrond Myklebust { 1195efeda80dSTrond Myklebust 1196efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1197efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1198efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1199efeda80dSTrond Myklebust } 1200efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1201efeda80dSTrond Myklebust 1202efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1203efeda80dSTrond Myklebust /** 1204efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1205efeda80dSTrond Myklebust * @inode: pointer to inode 1206efeda80dSTrond Myklebust * 1207efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1208efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1209efeda80dSTrond Myklebust * while holding a delegation. 1210efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1211efeda80dSTrond Myklebust * returned or revoked. 1212efeda80dSTrond Myklebust */ 1213efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1214efeda80dSTrond Myklebust { 1215efeda80dSTrond Myklebust struct dentry *alias; 1216efeda80dSTrond Myklebust 1217efeda80dSTrond Myklebust if (!inode) 1218efeda80dSTrond Myklebust return; 1219efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1220efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1221efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1222efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1223efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1224efeda80dSTrond Myklebust } 1225efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1226efeda80dSTrond Myklebust } 1227efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1228efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1229efeda80dSTrond Myklebust 12301da177e4SLinus Torvalds /* 12311da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 12321da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 12331da177e4SLinus Torvalds * and may need to be looked up again. 1234912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 12351da177e4SLinus Torvalds */ 1236912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1237912a108dSNeilBrown int rcu_walk) 12381da177e4SLinus Torvalds { 12391da177e4SLinus Torvalds if (IS_ROOT(dentry)) 12401da177e4SLinus Torvalds return 1; 12414eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 12424eec952eSTrond Myklebust return 0; 1243f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 12446ecc5e8fSTrond Myklebust return 0; 1245f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 12461cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1247912a108dSNeilBrown if (rcu_walk) 1248f2c77f4eSTrond Myklebust return 0; 12491cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 12501cd9cb05STrond Myklebust return 0; 12511cd9cb05STrond Myklebust } 1252f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1253f2c77f4eSTrond Myklebust return 0; 1254f2c77f4eSTrond Myklebust return 1; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* 1258a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1259a12802caSTrond Myklebust * an O_EXCL create using this path component. 1260a12802caSTrond Myklebust */ 1261fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1262a12802caSTrond Myklebust { 1263a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1264a12802caSTrond Myklebust return 0; 1265fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1266a12802caSTrond Myklebust } 1267a12802caSTrond Myklebust 1268a12802caSTrond Myklebust /* 12691d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 12701d6757fbSTrond Myklebust * 12711d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 12721d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 12731d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 12741d6757fbSTrond Myklebust * 12751d6757fbSTrond Myklebust */ 127665a0c149STrond Myklebust static 1277fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 128065a0c149STrond Myklebust int ret; 12811da177e4SLinus Torvalds 128236d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 12834e99a1ffSTrond Myklebust return 0; 128447921921STrond Myklebust 128547921921STrond Myklebust if (flags & LOOKUP_OPEN) { 128647921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 128747921921STrond Myklebust case S_IFREG: 128847921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 128947921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 129047921921STrond Myklebust goto out; 1291df561f66SGustavo A. R. Silva fallthrough; 129247921921STrond Myklebust case S_IFDIR: 129347921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 129447921921STrond Myklebust break; 129547921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 129647921921STrond Myklebust goto out_force; 129747921921STrond Myklebust } 129847921921STrond Myklebust } 129947921921STrond Myklebust 13001da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1301fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 13021da177e4SLinus Torvalds goto out_force; 130365a0c149STrond Myklebust out: 1304a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 13051da177e4SLinus Torvalds out_force: 13061fa1e384SNeilBrown if (flags & LOOKUP_RCU) 13071fa1e384SNeilBrown return -ECHILD; 130865a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 130965a0c149STrond Myklebust if (ret != 0) 131065a0c149STrond Myklebust return ret; 131165a0c149STrond Myklebust goto out; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* 13151da177e4SLinus Torvalds * We judge how long we want to trust negative 13161da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 13171da177e4SLinus Torvalds * 13181da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 13191da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1320912a108dSNeilBrown * 1321912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1322912a108dSNeilBrown * suggesting a reval is needed. 13239f6d44d4STrond Myklebust * 13249f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 13259f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds static inline 13281da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1329fa3c56bbSAl Viro unsigned int flags) 13301da177e4SLinus Torvalds { 13319f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 13321da177e4SLinus Torvalds return 0; 13334eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 13344eec952eSTrond Myklebust return 1; 1335912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13385ceb9d7fSTrond Myklebust static int 13395ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 13405ceb9d7fSTrond Myklebust struct inode *inode, int error) 13411da177e4SLinus Torvalds { 13425ceb9d7fSTrond Myklebust switch (error) { 13435ceb9d7fSTrond Myklebust case 1: 13446de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 13456de1472fSAl Viro __func__, dentry); 13461da177e4SLinus Torvalds return 1; 13475ceb9d7fSTrond Myklebust case 0: 1348a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 13491da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 13501da177e4SLinus Torvalds /* Purge readdir caches. */ 13511da177e4SLinus Torvalds nfs_zap_caches(inode); 1352a3f432bfSJ. Bruce Fields /* 1353a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1354a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1355a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1356a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1357a3f432bfSJ. Bruce Fields */ 1358a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 13595ceb9d7fSTrond Myklebust return 1; 13601da177e4SLinus Torvalds } 13616de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 13626de1472fSAl Viro __func__, dentry); 13631da177e4SLinus Torvalds return 0; 13645ceb9d7fSTrond Myklebust } 13656de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 13666de1472fSAl Viro __func__, dentry, error); 1367e1fb4d05STrond Myklebust return error; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13705ceb9d7fSTrond Myklebust static int 13715ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 13725ceb9d7fSTrond Myklebust unsigned int flags) 13735ceb9d7fSTrond Myklebust { 13745ceb9d7fSTrond Myklebust int ret = 1; 13755ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 13765ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13775ceb9d7fSTrond Myklebust return -ECHILD; 13785ceb9d7fSTrond Myklebust ret = 0; 13795ceb9d7fSTrond Myklebust } 13805ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 13815ceb9d7fSTrond Myklebust } 13825ceb9d7fSTrond Myklebust 13835ceb9d7fSTrond Myklebust static int 13845ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 13855ceb9d7fSTrond Myklebust struct inode *inode) 13865ceb9d7fSTrond Myklebust { 13875ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 13885ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 13895ceb9d7fSTrond Myklebust } 13905ceb9d7fSTrond Myklebust 13915ceb9d7fSTrond Myklebust static int 13925ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 13935ceb9d7fSTrond Myklebust struct inode *inode) 13945ceb9d7fSTrond Myklebust { 13955ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 13965ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 13975ceb9d7fSTrond Myklebust struct nfs4_label *label; 1398a1147b82STrond Myklebust unsigned long dir_verifier; 13995ceb9d7fSTrond Myklebust int ret; 14005ceb9d7fSTrond Myklebust 14015ceb9d7fSTrond Myklebust ret = -ENOMEM; 14025ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 14035ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 14045ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 14055ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 14065ceb9d7fSTrond Myklebust goto out; 14075ceb9d7fSTrond Myklebust 1408a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1409f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 14105ceb9d7fSTrond Myklebust if (ret < 0) { 1411f7b37b8bSTrond Myklebust switch (ret) { 1412f7b37b8bSTrond Myklebust case -ESTALE: 1413f7b37b8bSTrond Myklebust case -ENOENT: 14145ceb9d7fSTrond Myklebust ret = 0; 1415f7b37b8bSTrond Myklebust break; 1416f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1417f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1418f7b37b8bSTrond Myklebust ret = 1; 1419f7b37b8bSTrond Myklebust } 14205ceb9d7fSTrond Myklebust goto out; 14215ceb9d7fSTrond Myklebust } 14225ceb9d7fSTrond Myklebust ret = 0; 14235ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 14245ceb9d7fSTrond Myklebust goto out; 14255ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 14265ceb9d7fSTrond Myklebust goto out; 14275ceb9d7fSTrond Myklebust 14285ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1429a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 14305ceb9d7fSTrond Myklebust 14315ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 14325ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 14335ceb9d7fSTrond Myklebust ret = 1; 14345ceb9d7fSTrond Myklebust out: 14355ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 14365ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 14375ceb9d7fSTrond Myklebust nfs4_label_free(label); 14385ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 14395ceb9d7fSTrond Myklebust } 14405ceb9d7fSTrond Myklebust 14415ceb9d7fSTrond Myklebust /* 14425ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 14435ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 14445ceb9d7fSTrond Myklebust * lookup. 14455ceb9d7fSTrond Myklebust * 14465ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 14475ceb9d7fSTrond Myklebust * we have an inode! 14485ceb9d7fSTrond Myklebust * 14495ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 14505ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 14515ceb9d7fSTrond Myklebust */ 14525ceb9d7fSTrond Myklebust static int 14535ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 14545ceb9d7fSTrond Myklebust unsigned int flags) 14555ceb9d7fSTrond Myklebust { 14565ceb9d7fSTrond Myklebust struct inode *inode; 14575ceb9d7fSTrond Myklebust int error; 14585ceb9d7fSTrond Myklebust 14595ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 14605ceb9d7fSTrond Myklebust inode = d_inode(dentry); 14615ceb9d7fSTrond Myklebust 14625ceb9d7fSTrond Myklebust if (!inode) 14635ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 14645ceb9d7fSTrond Myklebust 14655ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 14665ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 14675ceb9d7fSTrond Myklebust __func__, dentry); 14685ceb9d7fSTrond Myklebust goto out_bad; 14695ceb9d7fSTrond Myklebust } 14705ceb9d7fSTrond Myklebust 1471efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 14725ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 14735ceb9d7fSTrond Myklebust 14745ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 14755ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 14765ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 14775ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 14785ceb9d7fSTrond Myklebust if (error) { 14795ceb9d7fSTrond Myklebust if (error == -ESTALE) 14805ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 14815ceb9d7fSTrond Myklebust goto out_bad; 14825ceb9d7fSTrond Myklebust } 14835ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 14845ceb9d7fSTrond Myklebust goto out_valid; 14855ceb9d7fSTrond Myklebust } 14865ceb9d7fSTrond Myklebust 14875ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 14885ceb9d7fSTrond Myklebust return -ECHILD; 14895ceb9d7fSTrond Myklebust 14905ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 14915ceb9d7fSTrond Myklebust goto out_bad; 14925ceb9d7fSTrond Myklebust 14935ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 14945ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 14955ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 14965ceb9d7fSTrond Myklebust return error; 14975ceb9d7fSTrond Myklebust out_valid: 14985ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 14995ceb9d7fSTrond Myklebust out_bad: 15005ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 15015ceb9d7fSTrond Myklebust return -ECHILD; 15025ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 15035ceb9d7fSTrond Myklebust } 15045ceb9d7fSTrond Myklebust 15055ceb9d7fSTrond Myklebust static int 1506c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1507c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 15085ceb9d7fSTrond Myklebust { 15095ceb9d7fSTrond Myklebust struct dentry *parent; 15105ceb9d7fSTrond Myklebust struct inode *dir; 15115ceb9d7fSTrond Myklebust int ret; 15125ceb9d7fSTrond Myklebust 15135ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 15145ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 15155ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 15165ceb9d7fSTrond Myklebust if (!dir) 15175ceb9d7fSTrond Myklebust return -ECHILD; 1518c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 15195ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 15205ceb9d7fSTrond Myklebust return -ECHILD; 15215ceb9d7fSTrond Myklebust } else { 15225ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1523c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 15245ceb9d7fSTrond Myklebust dput(parent); 15255ceb9d7fSTrond Myklebust } 15265ceb9d7fSTrond Myklebust return ret; 15275ceb9d7fSTrond Myklebust } 15285ceb9d7fSTrond Myklebust 1529c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1530c7944ebbSTrond Myklebust { 1531c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1532c7944ebbSTrond Myklebust } 1533c7944ebbSTrond Myklebust 15341da177e4SLinus Torvalds /* 15352b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1536ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1537ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1538ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1539ecf3d1f1SJeff Layton * 1540ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1541ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1542ecf3d1f1SJeff Layton */ 1543ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1544ecf3d1f1SJeff Layton { 15452b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 15469cdd1d3fSTrond Myklebust int error = 0; 1547ecf3d1f1SJeff Layton 1548ecf3d1f1SJeff Layton /* 1549ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1550ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1551ecf3d1f1SJeff Layton * eventually need to do something more here. 1552ecf3d1f1SJeff Layton */ 1553ecf3d1f1SJeff Layton if (!inode) { 15546de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 15556de1472fSAl Viro __func__, dentry); 1556ecf3d1f1SJeff Layton return 1; 1557ecf3d1f1SJeff Layton } 1558ecf3d1f1SJeff Layton 1559ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 15606de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 15616de1472fSAl Viro __func__, dentry); 1562ecf3d1f1SJeff Layton return 0; 1563ecf3d1f1SJeff Layton } 1564ecf3d1f1SJeff Layton 1565b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1566ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1567ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1568ecf3d1f1SJeff Layton return !error; 1569ecf3d1f1SJeff Layton } 1570ecf3d1f1SJeff Layton 1571ecf3d1f1SJeff Layton /* 15721da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 15731da177e4SLinus Torvalds */ 1574fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 15751da177e4SLinus Torvalds { 15766de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 15776de1472fSAl Viro dentry, dentry->d_flags); 15781da177e4SLinus Torvalds 157977f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 15802b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 158177f11192STrond Myklebust return 1; 158277f11192STrond Myklebust 15831da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 15841da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 15851da177e4SLinus Torvalds return 1; 15861da177e4SLinus Torvalds } 15871751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 15881da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 15891da177e4SLinus Torvalds * files will be cleaned up during umount */ 15901da177e4SLinus Torvalds return 1; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds return 0; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15961f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 15971b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 15981b83d707STrond Myklebust { 15991b83d707STrond Myklebust spin_lock(&inode->i_lock); 16001f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 160159a707b0STrond Myklebust if (inode->i_nlink > 0) 160259a707b0STrond Myklebust drop_nlink(inode); 160359a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 160416e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 160516e14375STrond Myklebust | NFS_INO_INVALID_CTIME 160659a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 160759a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 16081b83d707STrond Myklebust spin_unlock(&inode->i_lock); 16091b83d707STrond Myklebust } 16101b83d707STrond Myklebust 16111da177e4SLinus Torvalds /* 16121da177e4SLinus Torvalds * Called when the dentry loses inode. 16131da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 16141da177e4SLinus Torvalds */ 16151da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 16161da177e4SLinus Torvalds { 161783672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 161883672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 161983672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 162083672d39SNeil Brown 16211da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1622e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 16231f018458STrond Myklebust nfs_drop_nlink(inode); 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds iput(inode); 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 1628b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1629b1942c5fSAl Viro { 1630b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1631b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1632b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1633b1942c5fSAl Viro WARN_ON(1); 1634b1942c5fSAl Viro else 1635b1942c5fSAl Viro kfree(dentry->d_fsdata); 1636b1942c5fSAl Viro } 1637b1942c5fSAl Viro } 1638b1942c5fSAl Viro 1639f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 16401da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1641ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 16421da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 16431da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 164436d43a43SDavid Howells .d_automount = nfs_d_automount, 1645b1942c5fSAl Viro .d_release = nfs_d_release, 16461da177e4SLinus Torvalds }; 1647ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 16481da177e4SLinus Torvalds 1649597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 16501da177e4SLinus Torvalds { 16511da177e4SLinus Torvalds struct dentry *res; 16521da177e4SLinus Torvalds struct inode *inode = NULL; 1653e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1654e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 16551775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1656a1147b82STrond Myklebust unsigned long dir_verifier; 16571da177e4SLinus Torvalds int error; 16581da177e4SLinus Torvalds 16596de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 166091d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 16611da177e4SLinus Torvalds 1662130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1663130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 16641da177e4SLinus Torvalds 1665fd684071STrond Myklebust /* 1666fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1667fd684071STrond Myklebust * but don't hash the dentry. 1668fd684071STrond Myklebust */ 16699f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1670130f9ab7SAl Viro return NULL; 16711da177e4SLinus Torvalds 1672e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1673e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1674e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1675e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1676e1fb4d05STrond Myklebust goto out; 1677e1fb4d05STrond Myklebust 167814c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 167914c43f76SDavid Quigley if (IS_ERR(label)) 168014c43f76SDavid Quigley goto out; 168114c43f76SDavid Quigley 1682a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 16836e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1684f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 16851da177e4SLinus Torvalds if (error == -ENOENT) 16861da177e4SLinus Torvalds goto no_entry; 16871da177e4SLinus Torvalds if (error < 0) { 16881da177e4SLinus Torvalds res = ERR_PTR(error); 1689bf130914SAl Viro goto out_label; 16901da177e4SLinus Torvalds } 16911775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1692bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 169303f28e3aSTrond Myklebust if (IS_ERR(res)) 1694bf130914SAl Viro goto out_label; 169554ceac45SDavid Howells 169663519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 169763519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1698d69ee9b8STrond Myklebust 16991da177e4SLinus Torvalds no_entry: 170041d28bcaSAl Viro res = d_splice_alias(inode, dentry); 17019eaef27bSTrond Myklebust if (res != NULL) { 17029eaef27bSTrond Myklebust if (IS_ERR(res)) 1703bf130914SAl Viro goto out_label; 17041da177e4SLinus Torvalds dentry = res; 17059eaef27bSTrond Myklebust } 1706a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1707bf130914SAl Viro out_label: 17086e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 170914c43f76SDavid Quigley nfs4_label_free(label); 17101da177e4SLinus Torvalds out: 1711e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1712e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 17131da177e4SLinus Torvalds return res; 17141da177e4SLinus Torvalds } 1715ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 17161da177e4SLinus Torvalds 171789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 17180b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 17191da177e4SLinus Torvalds 1720f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 17210ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1722b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 17231da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 17241da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 172536d43a43SDavid Howells .d_automount = nfs_d_automount, 1726b1942c5fSAl Viro .d_release = nfs_d_release, 17271da177e4SLinus Torvalds }; 172889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 17291da177e4SLinus Torvalds 17308a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 17318a5e929dSAl Viro { 17328a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 17338a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 17348a5e929dSAl Viro res |= FMODE_READ; 17358a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 17368a5e929dSAl Viro res |= FMODE_WRITE; 17378a5e929dSAl Viro return res; 17388a5e929dSAl Viro } 17398a5e929dSAl Viro 1740532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1741cd9a1c0eSTrond Myklebust { 1742532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1743cd9a1c0eSTrond Myklebust } 1744cd9a1c0eSTrond Myklebust 1745cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1746cd9a1c0eSTrond Myklebust { 1747f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1748cd9a1c0eSTrond Myklebust return 0; 1749cd9a1c0eSTrond Myklebust } 1750cd9a1c0eSTrond Myklebust 1751d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 17520dd2b474SMiklos Szeredi struct dentry *dentry, 1753b452a458SAl Viro struct file *file, unsigned open_flags) 1754cd9a1c0eSTrond Myklebust { 17550dd2b474SMiklos Szeredi int err; 17560dd2b474SMiklos Szeredi 1757be12af3eSAl Viro err = finish_open(file, dentry, do_open); 175830d90494SAl Viro if (err) 1759d9585277SAl Viro goto out; 1760eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 176130d90494SAl Viro nfs_file_set_open_context(file, ctx); 1762eaa2b82cSNeilBrown else 17639821421aSTrond Myklebust err = -EOPENSTALE; 1764cd9a1c0eSTrond Myklebust out: 1765d9585277SAl Viro return err; 1766cd9a1c0eSTrond Myklebust } 1767cd9a1c0eSTrond Myklebust 176873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 176930d90494SAl Viro struct file *file, unsigned open_flags, 177044907d79SAl Viro umode_t mode) 17711da177e4SLinus Torvalds { 1772c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1773cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 17740dd2b474SMiklos Szeredi struct dentry *res; 17750dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1776f46e0bd3STrond Myklebust struct inode *inode; 17771472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1778c94c0953SAl Viro bool switched = false; 177973a09dd9SAl Viro int created = 0; 1780898f635cSTrond Myklebust int err; 17811da177e4SLinus Torvalds 17820dd2b474SMiklos Szeredi /* Expect a negative dentry */ 17832b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 17840dd2b474SMiklos Szeredi 17851e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 17866de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17871e7cb3dcSChuck Lever 17889597c13bSJeff Layton err = nfs_check_flags(open_flags); 17899597c13bSJeff Layton if (err) 17909597c13bSJeff Layton return err; 17919597c13bSJeff Layton 17920dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 17930dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 179400699ad8SAl Viro if (!d_in_lookup(dentry)) { 17950dd2b474SMiklos Szeredi /* 17960dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 17970dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 17980dd2b474SMiklos Szeredi * again 17990dd2b474SMiklos Szeredi */ 1800d9585277SAl Viro return -ENOENT; 18010dd2b474SMiklos Szeredi } 18021472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 18031da177e4SLinus Torvalds goto no_open; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 18060dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1807d9585277SAl Viro return -ENAMETOOLONG; 18081da177e4SLinus Torvalds 18090dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1810dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1811dff25ddbSAndreas Gruenbacher 1812dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1813dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1814dff25ddbSAndreas Gruenbacher 1815536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1816dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 18170dd2b474SMiklos Szeredi } 1818536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1819536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1820536e43d1STrond Myklebust attr.ia_size = 0; 1821cd9a1c0eSTrond Myklebust } 1822cd9a1c0eSTrond Myklebust 1823c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1824c94c0953SAl Viro d_drop(dentry); 1825c94c0953SAl Viro switched = true; 1826c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1827c94c0953SAl Viro &dentry->d_name, &wq); 1828c94c0953SAl Viro if (IS_ERR(dentry)) 1829c94c0953SAl Viro return PTR_ERR(dentry); 1830c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1831c94c0953SAl Viro return finish_no_open(file, dentry); 1832c94c0953SAl Viro } 1833c94c0953SAl Viro 1834532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 18350dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 18360dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1837d9585277SAl Viro goto out; 18380dd2b474SMiklos Szeredi 18396e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 184073a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 184173a09dd9SAl Viro if (created) 184273a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1843275bb307STrond Myklebust if (IS_ERR(inode)) { 18440dd2b474SMiklos Szeredi err = PTR_ERR(inode); 18456e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 18462d9db750STrond Myklebust put_nfs_open_context(ctx); 1847d20cb71dSAl Viro d_drop(dentry); 18480dd2b474SMiklos Szeredi switch (err) { 18491da177e4SLinus Torvalds case -ENOENT: 1850774d9513SPeng Tao d_splice_alias(NULL, dentry); 1851809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18520dd2b474SMiklos Szeredi break; 18531788ea6eSJeff Layton case -EISDIR: 18546f926b5bSTrond Myklebust case -ENOTDIR: 18556f926b5bSTrond Myklebust goto no_open; 18561da177e4SLinus Torvalds case -ELOOP: 18570dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 18581da177e4SLinus Torvalds goto no_open; 18590dd2b474SMiklos Szeredi break; 18601da177e4SLinus Torvalds /* case -EINVAL: */ 18611da177e4SLinus Torvalds default: 18620dd2b474SMiklos Szeredi break; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds goto out; 18651da177e4SLinus Torvalds } 18660dd2b474SMiklos Szeredi 1867b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 18686e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 18692d9db750STrond Myklebust put_nfs_open_context(ctx); 1870d9585277SAl Viro out: 1871c94c0953SAl Viro if (unlikely(switched)) { 1872c94c0953SAl Viro d_lookup_done(dentry); 1873c94c0953SAl Viro dput(dentry); 1874c94c0953SAl Viro } 1875d9585277SAl Viro return err; 18760dd2b474SMiklos Szeredi 18771da177e4SLinus Torvalds no_open: 18781472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1879c94c0953SAl Viro if (switched) { 1880c94c0953SAl Viro d_lookup_done(dentry); 1881c94c0953SAl Viro if (!res) 1882c94c0953SAl Viro res = dentry; 1883c94c0953SAl Viro else 1884c94c0953SAl Viro dput(dentry); 1885c94c0953SAl Viro } 18860dd2b474SMiklos Szeredi if (IS_ERR(res)) 1887c94c0953SAl Viro return PTR_ERR(res); 1888e45198a6SAl Viro return finish_no_open(file, res); 18891da177e4SLinus Torvalds } 189089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 18911da177e4SLinus Torvalds 1892c7944ebbSTrond Myklebust static int 1893c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1894c7944ebbSTrond Myklebust unsigned int flags) 18951da177e4SLinus Torvalds { 1896657e94b6SNick Piggin struct inode *inode; 18971da177e4SLinus Torvalds 1898fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1899c7944ebbSTrond Myklebust goto full_reval; 1900eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1901c7944ebbSTrond Myklebust goto full_reval; 19022b484297STrond Myklebust 19032b0143b5SDavid Howells inode = d_inode(dentry); 19042b484297STrond Myklebust 19051da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 19061da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 19071da177e4SLinus Torvalds */ 1908c7944ebbSTrond Myklebust if (inode == NULL) 1909c7944ebbSTrond Myklebust goto full_reval; 191049317a7fSNeilBrown 1911efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 1912c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1913216d5d06STrond Myklebust 19141da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 19151da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1916c7944ebbSTrond Myklebust goto full_reval; 1917c7944ebbSTrond Myklebust 19181da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1919c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1920c7944ebbSTrond Myklebust goto reval_dentry; 1921c7944ebbSTrond Myklebust 1922c7944ebbSTrond Myklebust /* Check if the directory changed */ 1923c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1924c7944ebbSTrond Myklebust goto reval_dentry; 19251da177e4SLinus Torvalds 19260ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1927c7944ebbSTrond Myklebust return 1; 1928c7944ebbSTrond Myklebust reval_dentry: 1929c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1930c7944ebbSTrond Myklebust return -ECHILD; 193142f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 19320ef97dcfSMiklos Szeredi 1933c7944ebbSTrond Myklebust full_reval: 1934c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1935c7944ebbSTrond Myklebust } 1936535918f1STrond Myklebust 1937c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1938c7944ebbSTrond Myklebust { 1939c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1940c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1941c0204fd2STrond Myklebust } 1942c0204fd2STrond Myklebust 19431da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 19441da177e4SLinus Torvalds 1945406cd915SBenjamin Coddington struct dentry * 1946406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 19471775fd3eSDavid Quigley struct nfs_fattr *fattr, 19481775fd3eSDavid Quigley struct nfs4_label *label) 19491da177e4SLinus Torvalds { 1950fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 19512b0143b5SDavid Howells struct inode *dir = d_inode(parent); 19521da177e4SLinus Torvalds struct inode *inode; 1953b0c6108eSAl Viro struct dentry *d; 1954406cd915SBenjamin Coddington int error; 19551da177e4SLinus Torvalds 1956fab728e1STrond Myklebust d_drop(dentry); 1957fab728e1STrond Myklebust 19581da177e4SLinus Torvalds if (fhandle->size == 0) { 1959f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 19601da177e4SLinus Torvalds if (error) 1961fab728e1STrond Myklebust goto out_error; 19621da177e4SLinus Torvalds } 19635724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19641da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 19651da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1966a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1967a841b54dSTrond Myklebust fattr, NULL, NULL); 19681da177e4SLinus Torvalds if (error < 0) 1969fab728e1STrond Myklebust goto out_error; 19701da177e4SLinus Torvalds } 19711775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1972b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1973fab728e1STrond Myklebust out: 1974fab728e1STrond Myklebust dput(parent); 1975406cd915SBenjamin Coddington return d; 1976fab728e1STrond Myklebust out_error: 1977fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1978406cd915SBenjamin Coddington d = ERR_PTR(error); 1979406cd915SBenjamin Coddington goto out; 1980406cd915SBenjamin Coddington } 1981406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1982406cd915SBenjamin Coddington 1983406cd915SBenjamin Coddington /* 1984406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1985406cd915SBenjamin Coddington */ 1986406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1987406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1988406cd915SBenjamin Coddington struct nfs4_label *label) 1989406cd915SBenjamin Coddington { 1990406cd915SBenjamin Coddington struct dentry *d; 1991406cd915SBenjamin Coddington 1992406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1993406cd915SBenjamin Coddington if (IS_ERR(d)) 1994406cd915SBenjamin Coddington return PTR_ERR(d); 1995406cd915SBenjamin Coddington 1996406cd915SBenjamin Coddington /* Callers don't care */ 1997406cd915SBenjamin Coddington dput(d); 1998406cd915SBenjamin Coddington return 0; 19991da177e4SLinus Torvalds } 2000ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds /* 20031da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 20041da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 20051da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 20061da177e4SLinus Torvalds * reply path made it appear to have failed. 20071da177e4SLinus Torvalds */ 2008597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 2009ebfc3b49SAl Viro umode_t mode, bool excl) 20101da177e4SLinus Torvalds { 20111da177e4SLinus Torvalds struct iattr attr; 2012ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 20131da177e4SLinus Torvalds int error; 20141da177e4SLinus Torvalds 20151e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 20166de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds attr.ia_mode = mode; 20191da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 20201da177e4SLinus Torvalds 20218b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 20228867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 20238b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 20241da177e4SLinus Torvalds if (error != 0) 20251da177e4SLinus Torvalds goto out_err; 20261da177e4SLinus Torvalds return 0; 20271da177e4SLinus Torvalds out_err: 20281da177e4SLinus Torvalds d_drop(dentry); 20291da177e4SLinus Torvalds return error; 20301da177e4SLinus Torvalds } 2031ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds /* 20341da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 20351da177e4SLinus Torvalds */ 2036597d9289SBryan Schumaker int 20371a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 20381da177e4SLinus Torvalds { 20391da177e4SLinus Torvalds struct iattr attr; 20401da177e4SLinus Torvalds int status; 20411da177e4SLinus Torvalds 20421e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 20436de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds attr.ia_mode = mode; 20461da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 20471da177e4SLinus Torvalds 20481ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 20491da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 20501ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 20511da177e4SLinus Torvalds if (status != 0) 20521da177e4SLinus Torvalds goto out_err; 20531da177e4SLinus Torvalds return 0; 20541da177e4SLinus Torvalds out_err: 20551da177e4SLinus Torvalds d_drop(dentry); 20561da177e4SLinus Torvalds return status; 20571da177e4SLinus Torvalds } 2058ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds /* 20611da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 20621da177e4SLinus Torvalds */ 2063597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 20641da177e4SLinus Torvalds { 20651da177e4SLinus Torvalds struct iattr attr; 20661da177e4SLinus Torvalds int error; 20671da177e4SLinus Torvalds 20681e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 20696de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 20721da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 20731da177e4SLinus Torvalds 20741ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 20751da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 20761ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 20771da177e4SLinus Torvalds if (error != 0) 20781da177e4SLinus Torvalds goto out_err; 20791da177e4SLinus Torvalds return 0; 20801da177e4SLinus Torvalds out_err: 20811da177e4SLinus Torvalds d_drop(dentry); 20821da177e4SLinus Torvalds return error; 20831da177e4SLinus Torvalds } 2084ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 20851da177e4SLinus Torvalds 2086d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2087d45b9d8bSTrond Myklebust { 2088dc3f4198SAl Viro if (simple_positive(dentry)) 2089d45b9d8bSTrond Myklebust d_delete(dentry); 2090d45b9d8bSTrond Myklebust } 2091d45b9d8bSTrond Myklebust 2092597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 20931da177e4SLinus Torvalds { 20941da177e4SLinus Torvalds int error; 20951da177e4SLinus Torvalds 20961e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 20976de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20981da177e4SLinus Torvalds 20991ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 21002b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2101884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 21021da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 21031da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2104ba6c0592STrond Myklebust switch (error) { 2105ba6c0592STrond Myklebust case 0: 21062b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2107ba6c0592STrond Myklebust break; 2108ba6c0592STrond Myklebust case -ENOENT: 2109d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2110ba6c0592STrond Myklebust } 2111884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2112ba6c0592STrond Myklebust } else 2113ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 21141ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds return error; 21171da177e4SLinus Torvalds } 2118ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds /* 21211da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 21221da177e4SLinus Torvalds * and after checking that the file has only one user. 21231da177e4SLinus Torvalds * 21241da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 21251da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 21261da177e4SLinus Torvalds */ 21271da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 21281da177e4SLinus Torvalds { 21292b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 21302b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 21311da177e4SLinus Torvalds int error = -EBUSY; 21321da177e4SLinus Torvalds 21336de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 21361da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 21371da177e4SLinus Torvalds error = 0; 21381da177e4SLinus Torvalds goto out; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 21411ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 21421da177e4SLinus Torvalds if (inode != NULL) { 2143912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 21441da177e4SLinus Torvalds if (error == 0) 21451b83d707STrond Myklebust nfs_drop_nlink(inode); 21461da177e4SLinus Torvalds } else 2147912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2148d45b9d8bSTrond Myklebust if (error == -ENOENT) 2149d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 21501ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 21511da177e4SLinus Torvalds out: 21521da177e4SLinus Torvalds return error; 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 21561da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 21571da177e4SLinus Torvalds * 21581da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 21591da177e4SLinus Torvalds */ 2160597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 21611da177e4SLinus Torvalds { 21621da177e4SLinus Torvalds int error; 21631da177e4SLinus Torvalds int need_rehash = 0; 21641da177e4SLinus Torvalds 21651e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 21666de1472fSAl Viro dir->i_ino, dentry); 21671da177e4SLinus Torvalds 21681ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 21691da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 217084d08fa8SAl Viro if (d_count(dentry) > 1) { 21711da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2172ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 21732b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 21741da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 21751ca42382STrond Myklebust goto out; 21761da177e4SLinus Torvalds } 21771da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 21781da177e4SLinus Torvalds __d_drop(dentry); 21791da177e4SLinus Torvalds need_rehash = 1; 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 21821da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2183d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 21841da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 21851da177e4SLinus Torvalds } else if (need_rehash) 21861da177e4SLinus Torvalds d_rehash(dentry); 21871ca42382STrond Myklebust out: 21881ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 21891da177e4SLinus Torvalds return error; 21901da177e4SLinus Torvalds } 2191ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 21921da177e4SLinus Torvalds 2193873101b3SChuck Lever /* 2194873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2195873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2196873101b3SChuck Lever * using prepare_write/commit_write. 2197873101b3SChuck Lever * 2198873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2199873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2200873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2201873101b3SChuck Lever * symlink request has completed on the server. 2202873101b3SChuck Lever * 2203873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2204873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2205873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2206873101b3SChuck Lever * and move the raw page into its mapping. 2207873101b3SChuck Lever */ 2208597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 22091da177e4SLinus Torvalds { 2210873101b3SChuck Lever struct page *page; 2211873101b3SChuck Lever char *kaddr; 22121da177e4SLinus Torvalds struct iattr attr; 2213873101b3SChuck Lever unsigned int pathlen = strlen(symname); 22141da177e4SLinus Torvalds int error; 22151da177e4SLinus Torvalds 22161e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 22176de1472fSAl Viro dir->i_ino, dentry, symname); 22181da177e4SLinus Torvalds 2219873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2220873101b3SChuck Lever return -ENAMETOOLONG; 22211da177e4SLinus Torvalds 2222873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2223873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 22241da177e4SLinus Torvalds 2225e8ecde25SAl Viro page = alloc_page(GFP_USER); 222676566991STrond Myklebust if (!page) 2227873101b3SChuck Lever return -ENOMEM; 2228873101b3SChuck Lever 2229e8ecde25SAl Viro kaddr = page_address(page); 2230873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2231873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2232873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2233873101b3SChuck Lever 22341ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 223594a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 22361ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2237873101b3SChuck Lever if (error != 0) { 22381e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2239873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 22406de1472fSAl Viro dentry, symname, error); 22411da177e4SLinus Torvalds d_drop(dentry); 2242873101b3SChuck Lever __free_page(page); 22431da177e4SLinus Torvalds return error; 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 2246873101b3SChuck Lever /* 2247873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2248873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2249873101b3SChuck Lever */ 22502b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2251873101b3SChuck Lever GFP_KERNEL)) { 2252873101b3SChuck Lever SetPageUptodate(page); 2253873101b3SChuck Lever unlock_page(page); 2254a0b54addSRafael Aquini /* 2255a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2256a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2257a0b54addSRafael Aquini */ 225809cbfeafSKirill A. Shutemov put_page(page); 2259873101b3SChuck Lever } else 2260873101b3SChuck Lever __free_page(page); 2261873101b3SChuck Lever 2262873101b3SChuck Lever return 0; 2263873101b3SChuck Lever } 2264ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2265873101b3SChuck Lever 2266597d9289SBryan Schumaker int 22671da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 22681da177e4SLinus Torvalds { 22692b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 22701da177e4SLinus Torvalds int error; 22711da177e4SLinus Torvalds 22726de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 22736de1472fSAl Viro old_dentry, dentry); 22741da177e4SLinus Torvalds 22751fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 22769697d234STrond Myklebust d_drop(dentry); 22771da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2278cf809556STrond Myklebust if (error == 0) { 22797de9c6eeSAl Viro ihold(inode); 22809697d234STrond Myklebust d_add(dentry, inode); 2281cf809556STrond Myklebust } 22821fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 22831da177e4SLinus Torvalds return error; 22841da177e4SLinus Torvalds } 2285ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds /* 22881da177e4SLinus Torvalds * RENAME 22891da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 22901da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 22911da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 22921da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 22931da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 22941da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 22951da177e4SLinus Torvalds * 22961da177e4SLinus Torvalds * FIXED. 22971da177e4SLinus Torvalds * 22981da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 22991da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 23001da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 23011da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 23021da177e4SLinus Torvalds * using the inode layer 23031da177e4SLinus Torvalds * 23041da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 23051da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 23061da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 23071da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 23081da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 23091da177e4SLinus Torvalds * the rename. 23101da177e4SLinus Torvalds */ 2311597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 23121cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 23131cd66c93SMiklos Szeredi unsigned int flags) 23141da177e4SLinus Torvalds { 23152b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 23162b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2317d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 231880a491fdSJeff Layton struct rpc_task *task; 23191da177e4SLinus Torvalds int error = -EBUSY; 23201da177e4SLinus Torvalds 23211cd66c93SMiklos Szeredi if (flags) 23221cd66c93SMiklos Szeredi return -EINVAL; 23231cd66c93SMiklos Szeredi 23246de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 23256de1472fSAl Viro old_dentry, new_dentry, 232684d08fa8SAl Viro d_count(new_dentry)); 23271da177e4SLinus Torvalds 232870ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 23291da177e4SLinus Torvalds /* 233028f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 233128f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 233228f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 233328f79a1aSMiklos Szeredi * the new target. 23341da177e4SLinus Torvalds */ 233527226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 233627226104SMiklos Szeredi /* 233727226104SMiklos Szeredi * To prevent any new references to the target during the 233827226104SMiklos Szeredi * rename, we unhash the dentry in advance. 233927226104SMiklos Szeredi */ 2340d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 234127226104SMiklos Szeredi d_drop(new_dentry); 2342d9f29500SBenjamin Coddington rehash = new_dentry; 2343d9f29500SBenjamin Coddington } 234427226104SMiklos Szeredi 234584d08fa8SAl Viro if (d_count(new_dentry) > 2) { 23461da177e4SLinus Torvalds int err; 234727226104SMiklos Szeredi 23481da177e4SLinus Torvalds /* copy the target dentry's name */ 23491da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 23501da177e4SLinus Torvalds &new_dentry->d_name); 23511da177e4SLinus Torvalds if (!dentry) 23521da177e4SLinus Torvalds goto out; 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds /* silly-rename the existing target ... */ 23551da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 235624e93025SMiklos Szeredi if (err) 23571da177e4SLinus Torvalds goto out; 235824e93025SMiklos Szeredi 235924e93025SMiklos Szeredi new_dentry = dentry; 2360d9f29500SBenjamin Coddington rehash = NULL; 236124e93025SMiklos Szeredi new_inode = NULL; 2362b1e4adf4STrond Myklebust } 236327226104SMiklos Szeredi } 23641da177e4SLinus Torvalds 2365d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 236680a491fdSJeff Layton if (IS_ERR(task)) { 236780a491fdSJeff Layton error = PTR_ERR(task); 236880a491fdSJeff Layton goto out; 236980a491fdSJeff Layton } 237080a491fdSJeff Layton 237180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2372818a8dbeSBenjamin Coddington if (error != 0) { 2373818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2374818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2375818a8dbeSBenjamin Coddington smp_wmb(); 2376818a8dbeSBenjamin Coddington } else 237780a491fdSJeff Layton error = task->tk_status; 237880a491fdSJeff Layton rpc_put_task(task); 237959a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 238059a707b0STrond Myklebust if (error == 0) { 238159a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 238259a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 238359a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 238459a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 238559a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 238659a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 238759a707b0STrond Myklebust } 23881da177e4SLinus Torvalds out: 2389d9f29500SBenjamin Coddington if (rehash) 2390d9f29500SBenjamin Coddington d_rehash(rehash); 239170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 239270ded201STrond Myklebust new_dir, new_dentry, error); 2393d9f29500SBenjamin Coddington if (!error) { 2394d9f29500SBenjamin Coddington if (new_inode != NULL) 2395d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2396d9f29500SBenjamin Coddington /* 2397d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2398d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2399d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2400d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2401d9f29500SBenjamin Coddington */ 2402d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2403d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2404d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2405d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2406d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2407d9f29500SBenjamin Coddington 24081da177e4SLinus Torvalds /* new dentry created? */ 24091da177e4SLinus Torvalds if (dentry) 24101da177e4SLinus Torvalds dput(dentry); 24111da177e4SLinus Torvalds return error; 24121da177e4SLinus Torvalds } 2413ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 24141da177e4SLinus Torvalds 2415cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2416cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2417cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2418cfcea3e8STrond Myklebust 2419a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 24203a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 24213a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 24223a505845STrond Myklebust 24231c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 24241c3c07e9STrond Myklebust { 2425b68572e0SNeilBrown put_cred(entry->cred); 2426f682a398SNeilBrown kfree_rcu(entry, rcu_head); 24274e857c58SPeter Zijlstra smp_mb__before_atomic(); 2428cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 24294e857c58SPeter Zijlstra smp_mb__after_atomic(); 24301c3c07e9STrond Myklebust } 24311c3c07e9STrond Myklebust 24321a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 24331a81bb8aSTrond Myklebust { 24341a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 24351a81bb8aSTrond Myklebust 24361a81bb8aSTrond Myklebust while (!list_empty(head)) { 24371a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 24381a81bb8aSTrond Myklebust list_del(&cache->lru); 24391a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 24401a81bb8aSTrond Myklebust } 24411a81bb8aSTrond Myklebust } 24421a81bb8aSTrond Myklebust 24433a505845STrond Myklebust static unsigned long 24443a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2445979df72eSTrond Myklebust { 2446979df72eSTrond Myklebust LIST_HEAD(head); 2447aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2448979df72eSTrond Myklebust struct nfs_access_entry *cache; 24491ab6c499SDave Chinner long freed = 0; 2450979df72eSTrond Myklebust 2451a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2452aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2453979df72eSTrond Myklebust struct inode *inode; 2454979df72eSTrond Myklebust 2455979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2456979df72eSTrond Myklebust break; 24579c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2458979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2459979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2460979df72eSTrond Myklebust goto remove_lru_entry; 2461979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2462979df72eSTrond Myklebust struct nfs_access_entry, lru); 2463979df72eSTrond Myklebust list_move(&cache->lru, &head); 2464979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 24651ab6c499SDave Chinner freed++; 2466979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2467979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2468979df72eSTrond Myklebust &nfs_access_lru_list); 2469979df72eSTrond Myklebust else { 2470979df72eSTrond Myklebust remove_lru_entry: 2471979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 24724e857c58SPeter Zijlstra smp_mb__before_atomic(); 2473979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 24744e857c58SPeter Zijlstra smp_mb__after_atomic(); 2475979df72eSTrond Myklebust } 247659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2477979df72eSTrond Myklebust } 2478979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24791a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24801ab6c499SDave Chinner return freed; 24811ab6c499SDave Chinner } 24821ab6c499SDave Chinner 24831ab6c499SDave Chinner unsigned long 24843a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 24853a505845STrond Myklebust { 24863a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 24873a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 24883a505845STrond Myklebust 24893a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 24903a505845STrond Myklebust return SHRINK_STOP; 24913a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 24923a505845STrond Myklebust } 24933a505845STrond Myklebust 24943a505845STrond Myklebust 24953a505845STrond Myklebust unsigned long 24961ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 24971ab6c499SDave Chinner { 249855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2499979df72eSTrond Myklebust } 2500979df72eSTrond Myklebust 25013a505845STrond Myklebust static void 25023a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 25033a505845STrond Myklebust { 25043a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 25053a505845STrond Myklebust unsigned long diff; 25063a505845STrond Myklebust unsigned int nr_to_scan; 25073a505845STrond Myklebust 25083a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 25093a505845STrond Myklebust return; 25103a505845STrond Myklebust nr_to_scan = 100; 25113a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 25123a505845STrond Myklebust if (diff < nr_to_scan) 25133a505845STrond Myklebust nr_to_scan = diff; 25143a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 25153a505845STrond Myklebust } 25163a505845STrond Myklebust 25171a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 25181c3c07e9STrond Myklebust { 25191c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 25201a81bb8aSTrond Myklebust struct rb_node *n; 25211c3c07e9STrond Myklebust struct nfs_access_entry *entry; 25221c3c07e9STrond Myklebust 25231c3c07e9STrond Myklebust /* Unhook entries from the cache */ 25241c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 25251c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 25261c3c07e9STrond Myklebust rb_erase(n, root_node); 25271a81bb8aSTrond Myklebust list_move(&entry->lru, head); 25281c3c07e9STrond Myklebust } 25291c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 25301c3c07e9STrond Myklebust } 25311c3c07e9STrond Myklebust 25321c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 25331c3c07e9STrond Myklebust { 25341a81bb8aSTrond Myklebust LIST_HEAD(head); 25351a81bb8aSTrond Myklebust 25361a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 25371a81bb8aSTrond Myklebust return; 2538cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2539cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 25401a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2541cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2542cfcea3e8STrond Myklebust 25431c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 25441a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 25451a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 25461a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 25471a81bb8aSTrond Myklebust nfs_access_free_list(&head); 25481c3c07e9STrond Myklebust } 25491c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 25501c3c07e9STrond Myklebust 2551b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 25521c3c07e9STrond Myklebust { 25531c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 25541c3c07e9STrond Myklebust 25551c3c07e9STrond Myklebust while (n != NULL) { 2556b68572e0SNeilBrown struct nfs_access_entry *entry = 2557b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2558b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 25591c3c07e9STrond Myklebust 2560b68572e0SNeilBrown if (cmp < 0) 25611c3c07e9STrond Myklebust n = n->rb_left; 2562b68572e0SNeilBrown else if (cmp > 0) 25631c3c07e9STrond Myklebust n = n->rb_right; 25641c3c07e9STrond Myklebust else 25651c3c07e9STrond Myklebust return entry; 25661c3c07e9STrond Myklebust } 25671c3c07e9STrond Myklebust return NULL; 25681c3c07e9STrond Myklebust } 25691c3c07e9STrond Myklebust 2570d2ae4f8bSFrank 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) 25711da177e4SLinus Torvalds { 257255296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 25731c3c07e9STrond Myklebust struct nfs_access_entry *cache; 257457b69181STrond Myklebust bool retry = true; 257557b69181STrond Myklebust int err; 25761da177e4SLinus Torvalds 25771c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 257857b69181STrond Myklebust for(;;) { 25791c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 25801c3c07e9STrond Myklebust goto out_zap; 25811c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 258257b69181STrond Myklebust err = -ENOENT; 25831c3c07e9STrond Myklebust if (cache == NULL) 25841c3c07e9STrond Myklebust goto out; 258557b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 258621c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 258757b69181STrond Myklebust break; 25885c965db8STrond Myklebust if (!retry) 25895c965db8STrond Myklebust break; 259057b69181STrond Myklebust err = -ECHILD; 259157b69181STrond Myklebust if (!may_block) 259257b69181STrond Myklebust goto out; 259357b69181STrond Myklebust spin_unlock(&inode->i_lock); 259457b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 259557b69181STrond Myklebust if (err) 259657b69181STrond Myklebust return err; 259757b69181STrond Myklebust spin_lock(&inode->i_lock); 259857b69181STrond Myklebust retry = false; 259957b69181STrond Myklebust } 26001c3c07e9STrond Myklebust res->cred = cache->cred; 26011c3c07e9STrond Myklebust res->mask = cache->mask; 2602cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 26031c3c07e9STrond Myklebust err = 0; 26041c3c07e9STrond Myklebust out: 26051c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 26061c3c07e9STrond Myklebust return err; 26071c3c07e9STrond Myklebust out_zap: 26081a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 26091a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 26101c3c07e9STrond Myklebust return -ENOENT; 26111c3c07e9STrond Myklebust } 26121c3c07e9STrond Myklebust 2613b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2614f682a398SNeilBrown { 2615f682a398SNeilBrown /* Only check the most recently returned cache entry, 2616f682a398SNeilBrown * but do it without locking. 2617f682a398SNeilBrown */ 2618f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2619f682a398SNeilBrown struct nfs_access_entry *cache; 2620f682a398SNeilBrown int err = -ECHILD; 2621f682a398SNeilBrown struct list_head *lh; 2622f682a398SNeilBrown 2623f682a398SNeilBrown rcu_read_lock(); 2624f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2625f682a398SNeilBrown goto out; 26269f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2627f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2628f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 26299a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2630f682a398SNeilBrown cache = NULL; 2631f682a398SNeilBrown if (cache == NULL) 2632f682a398SNeilBrown goto out; 263321c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2634f682a398SNeilBrown goto out; 2635f682a398SNeilBrown res->cred = cache->cred; 2636f682a398SNeilBrown res->mask = cache->mask; 263721c3ba7eSTrond Myklebust err = 0; 2638f682a398SNeilBrown out: 2639f682a398SNeilBrown rcu_read_unlock(); 2640f682a398SNeilBrown return err; 2641f682a398SNeilBrown } 2642f682a398SNeilBrown 2643d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct 2644d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block) 2645d2ae4f8bSFrank van der Linden { 2646d2ae4f8bSFrank van der Linden int status; 2647d2ae4f8bSFrank van der Linden 2648d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_rcu(inode, cred, res); 2649d2ae4f8bSFrank van der Linden if (status != 0) 2650d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_locked(inode, cred, res, 2651d2ae4f8bSFrank van der Linden may_block); 2652d2ae4f8bSFrank van der Linden 2653d2ae4f8bSFrank van der Linden return status; 2654d2ae4f8bSFrank van der Linden } 2655d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2656d2ae4f8bSFrank van der Linden 26571c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 26581c3c07e9STrond Myklebust { 2659cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2660cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 26611c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 26621c3c07e9STrond Myklebust struct rb_node *parent = NULL; 26631c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2664b68572e0SNeilBrown int cmp; 26651c3c07e9STrond Myklebust 26661c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 26671c3c07e9STrond Myklebust while (*p != NULL) { 26681c3c07e9STrond Myklebust parent = *p; 26691c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2670b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 26711c3c07e9STrond Myklebust 2672b68572e0SNeilBrown if (cmp < 0) 26731c3c07e9STrond Myklebust p = &parent->rb_left; 2674b68572e0SNeilBrown else if (cmp > 0) 26751c3c07e9STrond Myklebust p = &parent->rb_right; 26761c3c07e9STrond Myklebust else 26771c3c07e9STrond Myklebust goto found; 26781c3c07e9STrond Myklebust } 26791c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 26801c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2681cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 26821c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 26831c3c07e9STrond Myklebust return; 26841c3c07e9STrond Myklebust found: 26851c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2686cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2687cfcea3e8STrond Myklebust list_del(&entry->lru); 26881c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 26891c3c07e9STrond Myklebust nfs_access_free_entry(entry); 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 26926168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 26931da177e4SLinus Torvalds { 26941c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 26951c3c07e9STrond Myklebust if (cache == NULL) 26961c3c07e9STrond Myklebust return; 26971c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2698b68572e0SNeilBrown cache->cred = get_cred(set->cred); 26991da177e4SLinus Torvalds cache->mask = set->mask; 27001c3c07e9STrond Myklebust 2701f682a398SNeilBrown /* The above field assignments must be visible 2702f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2703f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2704f682a398SNeilBrown */ 2705f682a398SNeilBrown smp_wmb(); 27061c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2707cfcea3e8STrond Myklebust 2708cfcea3e8STrond Myklebust /* Update accounting */ 27094e857c58SPeter Zijlstra smp_mb__before_atomic(); 2710cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 27114e857c58SPeter Zijlstra smp_mb__after_atomic(); 2712cfcea3e8STrond Myklebust 2713cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 27141a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2715cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 27161a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 27171a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 27181a81bb8aSTrond Myklebust &nfs_access_lru_list); 2719cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2720cfcea3e8STrond Myklebust } 27213a505845STrond Myklebust nfs_access_cache_enforce_limit(); 27221da177e4SLinus Torvalds } 27236168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 27246168f62cSWeston Andros Adamson 27253c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 27263c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 27273c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 27283c181827SAnna Schumaker NFS_ACCESS_DELETE) 27293c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 27303c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2731ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 27323c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 27333c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 273415d4b73aSTrond Myklebust static int 2735ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 273615d4b73aSTrond Myklebust { 273715d4b73aSTrond Myklebust int mask = 0; 273815d4b73aSTrond Myklebust 273915d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 274015d4b73aSTrond Myklebust mask |= MAY_READ; 2741ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2742ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 274315d4b73aSTrond Myklebust mask |= MAY_WRITE; 2744ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 274515d4b73aSTrond Myklebust mask |= MAY_EXEC; 2746ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2747ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2748ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2749ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 275015d4b73aSTrond Myklebust mask |= MAY_EXEC; 2751ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2752ecbb903cSTrond Myklebust mask |= MAY_WRITE; 275315d4b73aSTrond Myklebust return mask; 275415d4b73aSTrond Myklebust } 275515d4b73aSTrond Myklebust 27566168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 27576168f62cSWeston Andros Adamson { 2758bd8b2441STrond Myklebust entry->mask = access_result; 27596168f62cSWeston Andros Adamson } 27606168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 27611da177e4SLinus Torvalds 2762b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 27631da177e4SLinus Torvalds { 27641da177e4SLinus Torvalds struct nfs_access_entry cache; 276557b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2766e8194b7dSTrond Myklebust int cache_mask = -1; 27671da177e4SLinus Torvalds int status; 27681da177e4SLinus Torvalds 2769f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2770f4ce1299STrond Myklebust 277157b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 27721da177e4SLinus Torvalds if (status == 0) 2773f4ce1299STrond Myklebust goto out_cached; 27741da177e4SLinus Torvalds 2775f3324a2aSNeilBrown status = -ECHILD; 277657b69181STrond Myklebust if (!may_block) 2777f3324a2aSNeilBrown goto out; 2778f3324a2aSNeilBrown 27791750d929SAnna Schumaker /* 27801750d929SAnna Schumaker * Determine which access bits we want to ask for... 27811750d929SAnna Schumaker */ 27821750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 278372832a24SFrank van der Linden if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 278472832a24SFrank van der Linden cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 278572832a24SFrank van der Linden NFS_ACCESS_XALIST; 278672832a24SFrank van der Linden } 27871750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 27881750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 27891750d929SAnna Schumaker else 27901750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 27911da177e4SLinus Torvalds cache.cred = cred; 27921da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2793a71ee337SSuresh Jayaraman if (status != 0) { 2794a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2795a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 279693ce4af7STrond Myklebust nfs_set_inode_stale(inode); 279793ce4af7STrond Myklebust else 279893ce4af7STrond Myklebust nfs_zap_caches(inode); 2799a71ee337SSuresh Jayaraman } 2800f4ce1299STrond Myklebust goto out; 2801a71ee337SSuresh Jayaraman } 28021da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2803f4ce1299STrond Myklebust out_cached: 2804ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2805bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2806f4ce1299STrond Myklebust status = -EACCES; 28071da177e4SLinus Torvalds out: 2808e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2809f4ce1299STrond Myklebust return status; 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 2812af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2813af22f94aSTrond Myklebust { 2814af22f94aSTrond Myklebust int mask = 0; 2815af22f94aSTrond Myklebust 2816f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2817f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2818f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2819f8d9a897SWeston Andros Adamson } else { 28208a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2821af22f94aSTrond Myklebust mask |= MAY_READ; 28228a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2823af22f94aSTrond Myklebust mask |= MAY_WRITE; 2824f8d9a897SWeston Andros Adamson } 2825f8d9a897SWeston Andros Adamson 2826af22f94aSTrond Myklebust return mask; 2827af22f94aSTrond Myklebust } 2828af22f94aSTrond Myklebust 2829b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2830af22f94aSTrond Myklebust { 2831af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2832af22f94aSTrond Myklebust } 283389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2834af22f94aSTrond Myklebust 28355c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 28365c5fc09aSTrond Myklebust { 28375c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 283821c3ba7eSTrond Myklebust int ret = 0; 28395c5fc09aSTrond Myklebust 28403825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 28413825827eSTrond Myklebust return 0; 2842cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 28435c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 284421c3ba7eSTrond Myklebust return -ECHILD; 284521c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 284621c3ba7eSTrond Myklebust } 28475c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 28485c5fc09aSTrond Myklebust ret = -EACCES; 28495c5fc09aSTrond Myklebust return ret; 28505c5fc09aSTrond Myklebust } 28515c5fc09aSTrond Myklebust 285210556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 28531da177e4SLinus Torvalds { 2854b68572e0SNeilBrown const struct cred *cred = current_cred(); 28551da177e4SLinus Torvalds int res = 0; 28561da177e4SLinus Torvalds 285791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 285891d5b470SChuck Lever 2859e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 28601da177e4SLinus Torvalds goto out; 28611da177e4SLinus Torvalds /* Is this sys_access() ? */ 28629cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 28631da177e4SLinus Torvalds goto force_lookup; 28641da177e4SLinus Torvalds 28651da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 28661da177e4SLinus Torvalds case S_IFLNK: 28671da177e4SLinus Torvalds goto out; 28681da177e4SLinus Torvalds case S_IFREG: 2869762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2870762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2871762674f8STrond Myklebust return 0; 28721da177e4SLinus Torvalds break; 28731da177e4SLinus Torvalds case S_IFDIR: 28741da177e4SLinus Torvalds /* 28751da177e4SLinus Torvalds * Optimize away all write operations, since the server 28761da177e4SLinus Torvalds * will check permissions when we perform the op. 28771da177e4SLinus Torvalds */ 28781da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 28791da177e4SLinus Torvalds goto out; 28801da177e4SLinus Torvalds } 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds force_lookup: 28831da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 28841da177e4SLinus Torvalds goto out_notsup; 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 28871da177e4SLinus Torvalds out: 28885c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 28895c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2890f696a365SMiklos Szeredi 28911e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 28921e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 28931da177e4SLinus Torvalds return res; 28941da177e4SLinus Torvalds out_notsup: 2895d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2896d51ac1a8SNeilBrown return -ECHILD; 2897d51ac1a8SNeilBrown 28981da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 28991da177e4SLinus Torvalds if (res == 0) 29002830ba7fSAl Viro res = generic_permission(inode, mask); 29011e7cb3dcSChuck Lever goto out; 29021da177e4SLinus Torvalds } 2903ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 29041da177e4SLinus Torvalds 29051da177e4SLinus Torvalds /* 29061da177e4SLinus Torvalds * Local variables: 29071da177e4SLinus Torvalds * version-control: t 29081da177e4SLinus Torvalds * kept-new-versions: 5 29091da177e4SLinus Torvalds * End: 29101da177e4SLinus Torvalds */ 2911