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 71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred) 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; 81684f39b4SNeilBrown ctx->cred = get_cred(cred); 82311324adSTrond Myklebust spin_lock(&dir->i_lock); 831c341b77STrond Myklebust if (list_empty(&nfsi->open_files) && 841c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 851c341b77STrond Myklebust nfsi->cache_validity |= NFS_INO_INVALID_DATA | 861c341b77STrond Myklebust NFS_INO_REVAL_FORCED; 87311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 88311324adSTrond Myklebust spin_unlock(&dir->i_lock); 89480c2006SBryan Schumaker return ctx; 90480c2006SBryan Schumaker } 910c030806STrond Myklebust return ERR_PTR(-ENOMEM); 920c030806STrond Myklebust } 93480c2006SBryan Schumaker 94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 95480c2006SBryan Schumaker { 96311324adSTrond Myklebust spin_lock(&dir->i_lock); 97311324adSTrond Myklebust list_del(&ctx->list); 98311324adSTrond Myklebust spin_unlock(&dir->i_lock); 99684f39b4SNeilBrown put_cred(ctx->cred); 100480c2006SBryan Schumaker kfree(ctx); 101480c2006SBryan Schumaker } 102480c2006SBryan Schumaker 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * Open file 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds static int 1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1081da177e4SLinus Torvalds { 109480c2006SBryan Schumaker int res = 0; 110480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1111da177e4SLinus Torvalds 1126de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 113cc0dd2d1SChuck Lever 114cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1151e7cb3dcSChuck Lever 116684f39b4SNeilBrown ctx = alloc_nfs_open_dir_context(inode, current_cred()); 117480c2006SBryan Schumaker if (IS_ERR(ctx)) { 118480c2006SBryan Schumaker res = PTR_ERR(ctx); 119480c2006SBryan Schumaker goto out; 120480c2006SBryan Schumaker } 121480c2006SBryan Schumaker filp->private_data = ctx; 122480c2006SBryan Schumaker out: 1231da177e4SLinus Torvalds return res; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126480c2006SBryan Schumaker static int 127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 128480c2006SBryan Schumaker { 129a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 130480c2006SBryan Schumaker return 0; 131480c2006SBryan Schumaker } 132480c2006SBryan Schumaker 133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 134d1bacf9eSBryan Schumaker u64 cookie; 135d1bacf9eSBryan Schumaker u64 ino; 136d1bacf9eSBryan Schumaker struct qstr string; 1370b26a0bfSTrond Myklebust unsigned char d_type; 138d1bacf9eSBryan Schumaker }; 139d1bacf9eSBryan Schumaker 140d1bacf9eSBryan Schumaker struct nfs_cache_array { 141d1bacf9eSBryan Schumaker u64 last_cookie; 142b1e21c97STrond Myklebust unsigned int size; 143b1e21c97STrond Myklebust unsigned char page_full : 1, 144b1e21c97STrond Myklebust page_is_eof : 1; 1455601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 146d1bacf9eSBryan Schumaker }; 147d1bacf9eSBryan Schumaker 1482e7a4641STrond Myklebust typedef struct nfs_readdir_descriptor { 1491da177e4SLinus Torvalds struct file *file; 1501da177e4SLinus Torvalds struct page *page; 15123db8620SAl Viro struct dir_context *ctx; 152*1f1d4aa4STrond Myklebust pgoff_t page_index; 1532e7a4641STrond Myklebust u64 dir_cookie; 1540aded708STrond Myklebust u64 last_cookie; 1552e7a4641STrond Myklebust u64 dup_cookie; 156f0dd2136STrond Myklebust loff_t current_index; 15759e356a9STrond Myklebust loff_t prev_index; 158d1bacf9eSBryan Schumaker 159a1147b82STrond Myklebust unsigned long dir_verifier; 1601f4eab7eSNeil Brown unsigned long timestamp; 1614704f0e2STrond Myklebust unsigned long gencount; 1622e7a4641STrond Myklebust unsigned long attr_gencount; 163d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 1642e7a4641STrond Myklebust signed char duped; 165a7a3b1e9SBenjamin Coddington bool plus; 166a7a3b1e9SBenjamin Coddington bool eof; 1671da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1681da177e4SLinus Torvalds 169*1f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array) 170*1f1d4aa4STrond Myklebust { 171*1f1d4aa4STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 172*1f1d4aa4STrond Myklebust } 173*1f1d4aa4STrond Myklebust 174*1f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie) 1754b310319STrond Myklebust { 1764b310319STrond Myklebust struct nfs_cache_array *array; 1774b310319STrond Myklebust 1784b310319STrond Myklebust array = kmap_atomic(page); 179*1f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 180*1f1d4aa4STrond Myklebust array->last_cookie = last_cookie; 1814b310319STrond Myklebust kunmap_atomic(array); 1824b310319STrond Myklebust } 1834b310319STrond Myklebust 184d1bacf9eSBryan Schumaker /* 185d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 186d1bacf9eSBryan Schumaker */ 187d1bacf9eSBryan Schumaker static 18811de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 189d1bacf9eSBryan Schumaker { 19011de3b11STrond Myklebust struct nfs_cache_array *array; 191d1bacf9eSBryan Schumaker int i; 1928cd51a0cSTrond Myklebust 1932b86ce2dSCong Wang array = kmap_atomic(page); 194d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 195d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 196*1f1d4aa4STrond Myklebust nfs_readdir_array_init(array); 1972b86ce2dSCong Wang kunmap_atomic(array); 198d1bacf9eSBryan Schumaker } 199d1bacf9eSBryan Schumaker 200b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 201b1e21c97STrond Myklebust { 202b1e21c97STrond Myklebust array->page_is_eof = 1; 203b1e21c97STrond Myklebust array->page_full = 1; 204b1e21c97STrond Myklebust } 205b1e21c97STrond Myklebust 206b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 207b1e21c97STrond Myklebust { 208b1e21c97STrond Myklebust return array->page_full; 209b1e21c97STrond Myklebust } 210b1e21c97STrond Myklebust 211d1bacf9eSBryan Schumaker /* 212d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 213d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 214d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 215d1bacf9eSBryan Schumaker */ 216d1bacf9eSBryan Schumaker static 2174a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 218d1bacf9eSBryan Schumaker { 219d1bacf9eSBryan Schumaker string->len = len; 2203803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2214a201d6eSTrond Myklebust if (string->name == NULL) 2224a201d6eSTrond Myklebust return -ENOMEM; 22304e4bd1cSCatalin Marinas /* 22404e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 22504e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 22604e4bd1cSCatalin Marinas */ 22704e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2288387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2294a201d6eSTrond Myklebust return 0; 230d1bacf9eSBryan Schumaker } 231d1bacf9eSBryan Schumaker 232b1e21c97STrond Myklebust /* 233b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds 234b1e21c97STrond Myklebust */ 235b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 236b1e21c97STrond Myklebust { 237b1e21c97STrond Myklebust struct nfs_cache_array_entry *cache_entry; 238b1e21c97STrond Myklebust 239b1e21c97STrond Myklebust if (array->page_full) 240b1e21c97STrond Myklebust return -ENOSPC; 241b1e21c97STrond Myklebust cache_entry = &array->array[array->size + 1]; 242b1e21c97STrond Myklebust if ((char *)cache_entry - (char *)array > PAGE_SIZE) { 243b1e21c97STrond Myklebust array->page_full = 1; 244b1e21c97STrond Myklebust return -ENOSPC; 245b1e21c97STrond Myklebust } 246b1e21c97STrond Myklebust return 0; 247b1e21c97STrond Myklebust } 248b1e21c97STrond Myklebust 249d1bacf9eSBryan Schumaker static 250d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 251d1bacf9eSBryan Schumaker { 2520795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2534a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2544a201d6eSTrond Myklebust int ret; 2554a201d6eSTrond Myklebust 256b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array); 257b1e21c97STrond Myklebust if (ret) 2583020093fSTrond Myklebust goto out; 2593020093fSTrond Myklebust 260b1e21c97STrond Myklebust cache_entry = &array->array[array->size]; 2614a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2624a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2630b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2644a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2654a201d6eSTrond Myklebust if (ret) 2664a201d6eSTrond Myklebust goto out; 267d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2688cd51a0cSTrond Myklebust array->size++; 26947c716cbSTrond Myklebust if (entry->eof != 0) 270b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 2714a201d6eSTrond Myklebust out: 2720795bf83SFabian Frederick kunmap(page); 2734a201d6eSTrond Myklebust return ret; 274d1bacf9eSBryan Schumaker } 275d1bacf9eSBryan Schumaker 276*1f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 277*1f1d4aa4STrond Myklebust pgoff_t index, u64 last_cookie) 278*1f1d4aa4STrond Myklebust { 279*1f1d4aa4STrond Myklebust struct page *page; 280*1f1d4aa4STrond Myklebust 281*1f1d4aa4STrond Myklebust page = grab_cache_page(mapping, index); 282*1f1d4aa4STrond Myklebust if (page && !PageUptodate(page)) { 283*1f1d4aa4STrond Myklebust nfs_readdir_page_init_array(page, last_cookie); 284*1f1d4aa4STrond Myklebust if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0) 285*1f1d4aa4STrond Myklebust nfs_zap_mapping(mapping->host, mapping); 286*1f1d4aa4STrond Myklebust SetPageUptodate(page); 287*1f1d4aa4STrond Myklebust } 288*1f1d4aa4STrond Myklebust 289*1f1d4aa4STrond Myklebust return page; 290*1f1d4aa4STrond Myklebust } 291*1f1d4aa4STrond Myklebust 292*1f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page) 293*1f1d4aa4STrond Myklebust { 294*1f1d4aa4STrond Myklebust struct nfs_cache_array *array; 295*1f1d4aa4STrond Myklebust u64 ret; 296*1f1d4aa4STrond Myklebust 297*1f1d4aa4STrond Myklebust array = kmap_atomic(page); 298*1f1d4aa4STrond Myklebust ret = array->last_cookie; 299*1f1d4aa4STrond Myklebust kunmap_atomic(array); 300*1f1d4aa4STrond Myklebust return ret; 301*1f1d4aa4STrond Myklebust } 302*1f1d4aa4STrond Myklebust 303*1f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page) 304*1f1d4aa4STrond Myklebust { 305*1f1d4aa4STrond Myklebust struct nfs_cache_array *array; 306*1f1d4aa4STrond Myklebust bool ret; 307*1f1d4aa4STrond Myklebust 308*1f1d4aa4STrond Myklebust array = kmap_atomic(page); 309*1f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array); 310*1f1d4aa4STrond Myklebust kunmap_atomic(array); 311*1f1d4aa4STrond Myklebust return ret; 312*1f1d4aa4STrond Myklebust } 313*1f1d4aa4STrond Myklebust 314b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page) 315b1e21c97STrond Myklebust { 316b1e21c97STrond Myklebust struct nfs_cache_array *array; 317b1e21c97STrond Myklebust 318b1e21c97STrond Myklebust array = kmap_atomic(page); 319b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array); 320b1e21c97STrond Myklebust kunmap_atomic(array); 321b1e21c97STrond Myklebust } 322b1e21c97STrond Myklebust 32359e356a9STrond Myklebust static inline 32459e356a9STrond Myklebust int is_32bit_api(void) 32559e356a9STrond Myklebust { 32659e356a9STrond Myklebust #ifdef CONFIG_COMPAT 32759e356a9STrond Myklebust return in_compat_syscall(); 32859e356a9STrond Myklebust #else 32959e356a9STrond Myklebust return (BITS_PER_LONG == 32); 33059e356a9STrond Myklebust #endif 33159e356a9STrond Myklebust } 33259e356a9STrond Myklebust 33359e356a9STrond Myklebust static 33459e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 33559e356a9STrond Myklebust { 33659e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 33759e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 33859e356a9STrond Myklebust return false; 33959e356a9STrond Myklebust return true; 34059e356a9STrond Myklebust } 34159e356a9STrond Myklebust 342d1bacf9eSBryan Schumaker static 343d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 344d1bacf9eSBryan Schumaker { 34523db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 346d1bacf9eSBryan Schumaker unsigned int index; 347d1bacf9eSBryan Schumaker 348d1bacf9eSBryan Schumaker if (diff < 0) 349d1bacf9eSBryan Schumaker goto out_eof; 350d1bacf9eSBryan Schumaker if (diff >= array->size) { 351b1e21c97STrond Myklebust if (array->page_is_eof) 352d1bacf9eSBryan Schumaker goto out_eof; 353d1bacf9eSBryan Schumaker return -EAGAIN; 354d1bacf9eSBryan Schumaker } 355d1bacf9eSBryan Schumaker 356d1bacf9eSBryan Schumaker index = (unsigned int)diff; 3572e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie; 358d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 359d1bacf9eSBryan Schumaker return 0; 360d1bacf9eSBryan Schumaker out_eof: 3616089dd0dSThomas Meyer desc->eof = true; 362d1bacf9eSBryan Schumaker return -EBADCOOKIE; 363d1bacf9eSBryan Schumaker } 364d1bacf9eSBryan Schumaker 3654db72b40SJeff Layton static bool 3664db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 3674db72b40SJeff Layton { 3684db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 3694db72b40SJeff Layton return false; 3704db72b40SJeff Layton smp_rmb(); 3714db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 3724db72b40SJeff Layton } 3734db72b40SJeff Layton 374d1bacf9eSBryan Schumaker static 375d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 376d1bacf9eSBryan Schumaker { 377d1bacf9eSBryan Schumaker int i; 3788ef2ce3eSBryan Schumaker loff_t new_pos; 379d1bacf9eSBryan Schumaker int status = -EAGAIN; 380d1bacf9eSBryan Schumaker 381d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3822e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) { 383496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 3840c030806STrond Myklebust 3858ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 3862e7a4641STrond Myklebust if (desc->attr_gencount != nfsi->attr_gencount || 3874db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 3882e7a4641STrond Myklebust desc->duped = 0; 3892e7a4641STrond Myklebust desc->attr_gencount = nfsi->attr_gencount; 39059e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 3912e7a4641STrond Myklebust if (desc->duped > 0 3922e7a4641STrond Myklebust && desc->dup_cookie == desc->dir_cookie) { 3930c030806STrond Myklebust if (printk_ratelimit()) { 3946de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 3950c030806STrond Myklebust "Please contact your server vendor. " 3969581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 3979581a4aeSJeff Layton desc->file, array->array[i].string.len, 3982e7a4641STrond Myklebust array->array[i].string.name, desc->dir_cookie); 3990c030806STrond Myklebust } 4000c030806STrond Myklebust status = -ELOOP; 4010c030806STrond Myklebust goto out; 4020c030806STrond Myklebust } 4032e7a4641STrond Myklebust desc->dup_cookie = desc->dir_cookie; 4042e7a4641STrond Myklebust desc->duped = -1; 4058ef2ce3eSBryan Schumaker } 40659e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 4072e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 40859e356a9STrond Myklebust else 40923db8620SAl Viro desc->ctx->pos = new_pos; 41059e356a9STrond Myklebust desc->prev_index = new_pos; 4118cd51a0cSTrond Myklebust desc->cache_entry_index = i; 41247c716cbSTrond Myklebust return 0; 4138cd51a0cSTrond Myklebust } 4148cd51a0cSTrond Myklebust } 415b1e21c97STrond Myklebust if (array->page_is_eof) { 416d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 4172e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie) 4186089dd0dSThomas Meyer desc->eof = true; 419d1bacf9eSBryan Schumaker } 4200c030806STrond Myklebust out: 421d1bacf9eSBryan Schumaker return status; 422d1bacf9eSBryan Schumaker } 423d1bacf9eSBryan Schumaker 424d1bacf9eSBryan Schumaker static 425d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 426d1bacf9eSBryan Schumaker { 427d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 42847c716cbSTrond Myklebust int status; 429d1bacf9eSBryan Schumaker 4300795bf83SFabian Frederick array = kmap(desc->page); 431d1bacf9eSBryan Schumaker 4322e7a4641STrond Myklebust if (desc->dir_cookie == 0) 433d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 434d1bacf9eSBryan Schumaker else 435d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 436d1bacf9eSBryan Schumaker 43747c716cbSTrond Myklebust if (status == -EAGAIN) { 4380aded708STrond Myklebust desc->last_cookie = array->last_cookie; 439e47c085aSTrond Myklebust desc->current_index += array->size; 44047c716cbSTrond Myklebust desc->page_index++; 44147c716cbSTrond Myklebust } 4420795bf83SFabian Frederick kunmap(desc->page); 443d1bacf9eSBryan Schumaker return status; 444d1bacf9eSBryan Schumaker } 445d1bacf9eSBryan Schumaker 446d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 447d1bacf9eSBryan Schumaker static 44856e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 449d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 450d1bacf9eSBryan Schumaker { 451480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 452684f39b4SNeilBrown const struct cred *cred = ctx->cred; 4534704f0e2STrond Myklebust unsigned long timestamp, gencount; 4541da177e4SLinus Torvalds int error; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds again: 4571da177e4SLinus Torvalds timestamp = jiffies; 4584704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 459a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 460be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 4611da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 4621da177e4SLinus Torvalds if (error < 0) { 4631da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 4641da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 4651da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 4663a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 467a7a3b1e9SBenjamin Coddington desc->plus = false; 4681da177e4SLinus Torvalds goto again; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds goto error; 4711da177e4SLinus Torvalds } 4721f4eab7eSNeil Brown desc->timestamp = timestamp; 4734704f0e2STrond Myklebust desc->gencount = gencount; 474d1bacf9eSBryan Schumaker error: 475d1bacf9eSBryan Schumaker return error; 476d1bacf9eSBryan Schumaker } 477d1bacf9eSBryan Schumaker 478573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 479573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 480d1bacf9eSBryan Schumaker { 48159e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 482573c4e1eSChuck Lever int error; 483d1bacf9eSBryan Schumaker 48459e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 485573c4e1eSChuck Lever if (error) 486573c4e1eSChuck Lever return error; 487d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 488d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 489d1bacf9eSBryan Schumaker return 0; 490d1bacf9eSBryan Schumaker } 491d1bacf9eSBryan Schumaker 492fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 493fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 494fa923369STrond Myklebust */ 495d39ab9deSBryan Schumaker static 496d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 497d39ab9deSBryan Schumaker { 498d8fdb47fSTrond Myklebust struct inode *inode; 499fa923369STrond Myklebust struct nfs_inode *nfsi; 500fa923369STrond Myklebust 5012b0143b5SDavid Howells if (d_really_is_negative(dentry)) 5022b0143b5SDavid Howells return 0; 503fa923369STrond Myklebust 504d8fdb47fSTrond Myklebust inode = d_inode(dentry); 505d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 506d8fdb47fSTrond Myklebust return 0; 507d8fdb47fSTrond Myklebust 508d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 5097dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 510d39ab9deSBryan Schumaker return 0; 5117dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 5127dc72d5fSTrond Myklebust return 0; 5137dc72d5fSTrond Myklebust return 1; 514d39ab9deSBryan Schumaker } 515d39ab9deSBryan Schumaker 516d39ab9deSBryan Schumaker static 51723db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 518d69ee9b8STrond Myklebust { 519d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 520d69ee9b8STrond Myklebust return false; 521d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 522d69ee9b8STrond Myklebust return true; 52323db8620SAl Viro if (ctx->pos == 0) 524d69ee9b8STrond Myklebust return true; 525d69ee9b8STrond Myklebust return false; 526d69ee9b8STrond Myklebust } 527d69ee9b8STrond Myklebust 528d69ee9b8STrond Myklebust /* 52963519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 53063519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 531d69ee9b8STrond Myklebust * directory. 532d69ee9b8STrond Myklebust */ 533d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 534d69ee9b8STrond Myklebust { 53563519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 53663519fbcSTrond Myklebust 53763519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 53863519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 53963519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 540d69ee9b8STrond Myklebust } 541d69ee9b8STrond Myklebust 542311324adSTrond Myklebust /* 543311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 544311324adSTrond Myklebust * 545311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 546311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 547311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 548311324adSTrond Myklebust * cache flush. 549311324adSTrond Myklebust */ 550311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 551311324adSTrond Myklebust { 55263519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 55363519fbcSTrond Myklebust 55463519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 55563519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 55663519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 557227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 558227823d2SDai Ngo nfsi->page_index + 1, -1); 559311324adSTrond Myklebust } 560311324adSTrond Myklebust } 561311324adSTrond Myklebust 562d69ee9b8STrond Myklebust static 563a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 564a1147b82STrond Myklebust unsigned long dir_verifier) 565d39ab9deSBryan Schumaker { 56626fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 5679ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 5684a201d6eSTrond Myklebust struct dentry *dentry; 5694a201d6eSTrond Myklebust struct dentry *alias; 570d39ab9deSBryan Schumaker struct inode *inode; 571aa9c2669SDavid Quigley int status; 572d39ab9deSBryan Schumaker 573fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 574fa923369STrond Myklebust return; 5756c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 5766c441c25STrond Myklebust return; 57778d04af4STrond Myklebust if (filename.len == 0) 57878d04af4STrond Myklebust return; 57978d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 58078d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 58178d04af4STrond Myklebust return; 58278d04af4STrond Myklebust /* ...or '/' */ 58378d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 58478d04af4STrond Myklebust return; 5854a201d6eSTrond Myklebust if (filename.name[0] == '.') { 5864a201d6eSTrond Myklebust if (filename.len == 1) 5874a201d6eSTrond Myklebust return; 5884a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 5894a201d6eSTrond Myklebust return; 5904a201d6eSTrond Myklebust } 5918387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 592d39ab9deSBryan Schumaker 5934a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 5949ac3d3e8SAl Viro again: 5959ac3d3e8SAl Viro if (!dentry) { 5969ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 5979ac3d3e8SAl Viro if (IS_ERR(dentry)) 5989ac3d3e8SAl Viro return; 5999ac3d3e8SAl Viro } 6009ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 6016c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 6026c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 6036c441c25STrond Myklebust &entry->fattr->fsid)) 6046c441c25STrond Myklebust goto out; 605d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 6067dc72d5fSTrond Myklebust if (!entry->fh->size) 6077dc72d5fSTrond Myklebust goto out; 608a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 6092b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 610aa9c2669SDavid Quigley if (!status) 6112b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 612d39ab9deSBryan Schumaker goto out; 613d39ab9deSBryan Schumaker } else { 6145542aa2fSEric W. Biederman d_invalidate(dentry); 615d39ab9deSBryan Schumaker dput(dentry); 6169ac3d3e8SAl Viro dentry = NULL; 6179ac3d3e8SAl Viro goto again; 618d39ab9deSBryan Schumaker } 619d39ab9deSBryan Schumaker } 6207dc72d5fSTrond Myklebust if (!entry->fh->size) { 6217dc72d5fSTrond Myklebust d_lookup_done(dentry); 6227dc72d5fSTrond Myklebust goto out; 6237dc72d5fSTrond Myklebust } 624d39ab9deSBryan Schumaker 6251775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 62641d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 6279ac3d3e8SAl Viro d_lookup_done(dentry); 6289ac3d3e8SAl Viro if (alias) { 629d39ab9deSBryan Schumaker if (IS_ERR(alias)) 630d39ab9deSBryan Schumaker goto out; 6319ac3d3e8SAl Viro dput(dentry); 6329ac3d3e8SAl Viro dentry = alias; 6339ac3d3e8SAl Viro } 634a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 635d39ab9deSBryan Schumaker out: 636d39ab9deSBryan Schumaker dput(dentry); 637d39ab9deSBryan Schumaker } 638d39ab9deSBryan Schumaker 639d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 640d1bacf9eSBryan Schumaker static 6418cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 6426650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 643d1bacf9eSBryan Schumaker { 644babddc72SBryan Schumaker struct xdr_stream stream; 645f7da7a12SBenny Halevy struct xdr_buf buf; 6466650239aSTrond Myklebust struct page *scratch; 6475c346854STrond Myklebust int status; 648babddc72SBryan Schumaker 6496650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 6506650239aSTrond Myklebust if (scratch == NULL) 6516650239aSTrond Myklebust return -ENOMEM; 652babddc72SBryan Schumaker 653f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 6546650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 65599424380SBryan Schumaker 65699424380SBryan Schumaker do { 657d33030e2SJeffrey Mitchell if (entry->label) 658d33030e2SJeffrey Mitchell entry->label->len = NFS4_MAXLABELLEN; 659d33030e2SJeffrey Mitchell 66099424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 661972bcdf2STrond Myklebust if (status != 0) 66299424380SBryan Schumaker break; 6635c346854STrond Myklebust 664a7a3b1e9SBenjamin Coddington if (desc->plus) 665a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 666a1147b82STrond Myklebust desc->dir_verifier); 6678cd51a0cSTrond Myklebust 668db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 669972bcdf2STrond Myklebust } while (!status && !entry->eof); 67099424380SBryan Schumaker 671972bcdf2STrond Myklebust switch (status) { 672972bcdf2STrond Myklebust case -EBADCOOKIE: 673972bcdf2STrond Myklebust if (entry->eof) { 674b1e21c97STrond Myklebust nfs_readdir_page_set_eof(page); 67599424380SBryan Schumaker status = 0; 67656e4ebf8SBryan Schumaker } 677972bcdf2STrond Myklebust break; 678972bcdf2STrond Myklebust case -ENOSPC: 679972bcdf2STrond Myklebust case -EAGAIN: 680972bcdf2STrond Myklebust status = 0; 681972bcdf2STrond Myklebust break; 682972bcdf2STrond Myklebust } 6836650239aSTrond Myklebust 6846650239aSTrond Myklebust put_page(scratch); 6858cd51a0cSTrond Myklebust return status; 6868cd51a0cSTrond Myklebust } 68756e4ebf8SBryan Schumaker 68856e4ebf8SBryan Schumaker static 689c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 69056e4ebf8SBryan Schumaker { 69156e4ebf8SBryan Schumaker unsigned int i; 69256e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 69356e4ebf8SBryan Schumaker put_page(pages[i]); 69456e4ebf8SBryan Schumaker } 69556e4ebf8SBryan Schumaker 69656e4ebf8SBryan Schumaker /* 697bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 698bf211ca1Szhangliguang * to nfs_readdir_free_pages() 69956e4ebf8SBryan Schumaker */ 70056e4ebf8SBryan Schumaker static 701c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 70256e4ebf8SBryan Schumaker { 70356e4ebf8SBryan Schumaker unsigned int i; 70456e4ebf8SBryan Schumaker 70556e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 70656e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 70756e4ebf8SBryan Schumaker if (page == NULL) 70856e4ebf8SBryan Schumaker goto out_freepages; 70956e4ebf8SBryan Schumaker pages[i] = page; 71056e4ebf8SBryan Schumaker } 7116650239aSTrond Myklebust return 0; 71256e4ebf8SBryan Schumaker 71356e4ebf8SBryan Schumaker out_freepages: 714c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 7156650239aSTrond Myklebust return -ENOMEM; 716d1bacf9eSBryan Schumaker } 717d1bacf9eSBryan Schumaker 718d1bacf9eSBryan Schumaker static 719d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 720d1bacf9eSBryan Schumaker { 72156e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 722d1bacf9eSBryan Schumaker struct nfs_entry entry; 723d1bacf9eSBryan Schumaker struct file *file = desc->file; 724d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 7258cd51a0cSTrond Myklebust int status = -ENOMEM; 72656e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 727d1bacf9eSBryan Schumaker 728d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 729*1f1d4aa4STrond Myklebust entry.cookie = nfs_readdir_page_last_cookie(page); 730d1bacf9eSBryan Schumaker entry.eof = 0; 731d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 732d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 733573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 734d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 735d1bacf9eSBryan Schumaker goto out; 736d1bacf9eSBryan Schumaker 73714c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 73814c43f76SDavid Quigley if (IS_ERR(entry.label)) { 73914c43f76SDavid Quigley status = PTR_ERR(entry.label); 74014c43f76SDavid Quigley goto out; 74114c43f76SDavid Quigley } 74214c43f76SDavid Quigley 7430795bf83SFabian Frederick array = kmap(page); 744d1bacf9eSBryan Schumaker 745c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 7466650239aSTrond Myklebust if (status < 0) 747d1bacf9eSBryan Schumaker goto out_release_array; 748d1bacf9eSBryan Schumaker do { 749ac396128STrond Myklebust unsigned int pglen; 75056e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 751babddc72SBryan Schumaker 752d1bacf9eSBryan Schumaker if (status < 0) 753d1bacf9eSBryan Schumaker break; 754972bcdf2STrond Myklebust 755ac396128STrond Myklebust pglen = status; 756972bcdf2STrond Myklebust if (pglen == 0) { 757972bcdf2STrond Myklebust nfs_readdir_page_set_eof(page); 7588cd51a0cSTrond Myklebust break; 7598cd51a0cSTrond Myklebust } 760972bcdf2STrond Myklebust 761972bcdf2STrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 762972bcdf2STrond Myklebust } while (!status && !nfs_readdir_array_is_full(array)); 763d1bacf9eSBryan Schumaker 764c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 765d1bacf9eSBryan Schumaker out_release_array: 7660795bf83SFabian Frederick kunmap(page); 76714c43f76SDavid Quigley nfs4_label_free(entry.label); 768d1bacf9eSBryan Schumaker out: 769d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 770d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 771d1bacf9eSBryan Schumaker return status; 772d1bacf9eSBryan Schumaker } 773d1bacf9eSBryan Schumaker 774*1f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 7751da177e4SLinus Torvalds { 77609cbfeafSKirill A. Shutemov put_page(desc->page); 7771da177e4SLinus Torvalds desc->page = NULL; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 780*1f1d4aa4STrond Myklebust static void 781*1f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 7821da177e4SLinus Torvalds { 783*1f1d4aa4STrond Myklebust unlock_page(desc->page); 784*1f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 785*1f1d4aa4STrond Myklebust } 786*1f1d4aa4STrond Myklebust 787*1f1d4aa4STrond Myklebust static struct page * 788*1f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 789*1f1d4aa4STrond Myklebust { 790*1f1d4aa4STrond Myklebust return nfs_readdir_page_get_locked(desc->file->f_mapping, 791*1f1d4aa4STrond Myklebust desc->page_index, 792*1f1d4aa4STrond Myklebust desc->last_cookie); 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds /* 796d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 797114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7981da177e4SLinus Torvalds */ 799d1bacf9eSBryan Schumaker static 800114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 801d1bacf9eSBryan Schumaker { 802227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 803227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 804d1bacf9eSBryan Schumaker int res; 805d1bacf9eSBryan Schumaker 806*1f1d4aa4STrond Myklebust desc->page = nfs_readdir_page_get_cached(desc); 807*1f1d4aa4STrond Myklebust if (!desc->page) 808*1f1d4aa4STrond Myklebust return -ENOMEM; 809*1f1d4aa4STrond Myklebust if (nfs_readdir_page_needs_filling(desc->page)) { 810*1f1d4aa4STrond Myklebust res = nfs_readdir_xdr_to_array(desc, desc->page, inode); 811*1f1d4aa4STrond Myklebust if (res < 0) 812114de382STrond Myklebust goto error; 813*1f1d4aa4STrond Myklebust } 814114de382STrond Myklebust res = nfs_readdir_search_array(desc); 815227823d2SDai Ngo if (res == 0) { 816227823d2SDai Ngo nfsi->page_index = desc->page_index; 817114de382STrond Myklebust return 0; 818114de382STrond Myklebust } 819114de382STrond Myklebust error: 820*1f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 821d1bacf9eSBryan Schumaker return res; 822d1bacf9eSBryan Schumaker } 823d1bacf9eSBryan Schumaker 824d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 8251da177e4SLinus Torvalds static inline 8261da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 8271da177e4SLinus Torvalds { 8288cd51a0cSTrond Myklebust int res; 829d1bacf9eSBryan Schumaker 8300aded708STrond Myklebust if (desc->page_index == 0) { 8318cd51a0cSTrond Myklebust desc->current_index = 0; 83259e356a9STrond Myklebust desc->prev_index = 0; 8330aded708STrond Myklebust desc->last_cookie = 0; 8340aded708STrond Myklebust } 83547c716cbSTrond Myklebust do { 836114de382STrond Myklebust res = find_and_lock_cache_page(desc); 83747c716cbSTrond Myklebust } while (res == -EAGAIN); 8381da177e4SLinus Torvalds return res; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds /* 8421da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 8431da177e4SLinus Torvalds */ 8441da177e4SLinus Torvalds static 84523db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 8461da177e4SLinus Torvalds { 8471da177e4SLinus Torvalds struct file *file = desc->file; 848d1bacf9eSBryan Schumaker int i = 0; 849d1bacf9eSBryan Schumaker int res = 0; 850d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 8518ef2ce3eSBryan Schumaker 8520795bf83SFabian Frederick array = kmap(desc->page); 853d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 854ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8551da177e4SLinus Torvalds 856ece0b423STrond Myklebust ent = &array->array[i]; 85723db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 85823db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 8596089dd0dSThomas Meyer desc->eof = true; 8601da177e4SLinus Torvalds break; 861ece0b423STrond Myklebust } 862d1bacf9eSBryan Schumaker if (i < (array->size-1)) 8632e7a4641STrond Myklebust desc->dir_cookie = array->array[i+1].cookie; 864d1bacf9eSBryan Schumaker else 8652e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie; 86659e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 8672e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie; 86859e356a9STrond Myklebust else 86959e356a9STrond Myklebust desc->ctx->pos++; 8702e7a4641STrond Myklebust if (desc->duped != 0) 8712e7a4641STrond Myklebust desc->duped = 1; 8728cd51a0cSTrond Myklebust } 873b1e21c97STrond Myklebust if (array->page_is_eof) 8746089dd0dSThomas Meyer desc->eof = true; 875d1bacf9eSBryan Schumaker 8760795bf83SFabian Frederick kunmap(desc->page); 8771e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8782e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie, res); 8791da177e4SLinus Torvalds return res; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8841da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8851da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8861da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8871da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8881da177e4SLinus Torvalds * 8891da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8901da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8911da177e4SLinus Torvalds * we should already have a complete representation of the 8921da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8931da177e4SLinus Torvalds */ 8941da177e4SLinus Torvalds static inline 89523db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8961da177e4SLinus Torvalds { 8971da177e4SLinus Torvalds struct page *page = NULL; 8981da177e4SLinus Torvalds int status; 899496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 9001da177e4SLinus Torvalds 9011e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 9022e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 9051da177e4SLinus Torvalds if (!page) { 9061da177e4SLinus Torvalds status = -ENOMEM; 9071da177e4SLinus Torvalds goto out; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9107a8e1dc3STrond Myklebust desc->page_index = 0; 9112e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie; 9127a8e1dc3STrond Myklebust desc->page = page; 9132e7a4641STrond Myklebust desc->duped = 0; 9147a8e1dc3STrond Myklebust 915*1f1d4aa4STrond Myklebust nfs_readdir_page_init_array(page, desc->dir_cookie); 91685f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 91785f8607eSTrond Myklebust if (status < 0) 918d1bacf9eSBryan Schumaker goto out_release; 919d1bacf9eSBryan Schumaker 92023db8620SAl Viro status = nfs_do_filldir(desc); 9211da177e4SLinus Torvalds 922114de382STrond Myklebust out_release: 923114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 924*1f1d4aa4STrond Myklebust nfs_readdir_page_put(desc); 9251da177e4SLinus Torvalds out: 9261e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 9273110ff80SHarvey Harrison __func__, status); 9281da177e4SLinus Torvalds return status; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 93100a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 93200a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 93300a92642SOlivier Galibert whole directory. 9341da177e4SLinus Torvalds */ 93523db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 9361da177e4SLinus Torvalds { 937be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 9382b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 93923db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 94059e356a9STrond Myklebust nfs_readdir_descriptor_t my_desc = { 94159e356a9STrond Myklebust .file = file, 94259e356a9STrond Myklebust .ctx = ctx, 94359e356a9STrond Myklebust .plus = nfs_use_readdirplus(inode, ctx), 94459e356a9STrond Myklebust }, 94559e356a9STrond Myklebust *desc = &my_desc; 94607b5ce8eSScott Mayhew int res = 0; 9471da177e4SLinus Torvalds 9486de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9496de1472fSAl Viro file, (long long)ctx->pos); 95091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 95191d5b470SChuck Lever 9521da177e4SLinus Torvalds /* 95323db8620SAl Viro * ctx->pos points to the dirent entry number. 954f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 95500a92642SOlivier Galibert * to either find the entry with the appropriate number or 95600a92642SOlivier Galibert * revalidate the cookie. 9571da177e4SLinus Torvalds */ 95879f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 95923db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 960fccca7fcSTrond Myklebust if (res < 0) 961fccca7fcSTrond Myklebust goto out; 962fccca7fcSTrond Myklebust 9632e7a4641STrond Myklebust spin_lock(&file->f_lock); 9642e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie; 9652e7a4641STrond Myklebust desc->dup_cookie = dir_ctx->dup_cookie; 9662e7a4641STrond Myklebust desc->duped = dir_ctx->duped; 9672e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount; 9682e7a4641STrond Myklebust spin_unlock(&file->f_lock); 9692e7a4641STrond Myklebust 97047c716cbSTrond Myklebust do { 9711da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 97200a92642SOlivier Galibert 9731da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 974ece0b423STrond Myklebust res = 0; 9751da177e4SLinus Torvalds /* This means either end of directory */ 9762e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) { 9771da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 97823db8620SAl Viro res = uncached_readdir(desc); 979ece0b423STrond Myklebust if (res == 0) 9801da177e4SLinus Torvalds continue; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds break; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9853a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9861da177e4SLinus Torvalds nfs_zap_caches(inode); 987baf57a09STrond Myklebust desc->page_index = 0; 988a7a3b1e9SBenjamin Coddington desc->plus = false; 989a7a3b1e9SBenjamin Coddington desc->eof = false; 9901da177e4SLinus Torvalds continue; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds if (res < 0) 9931da177e4SLinus Torvalds break; 9941da177e4SLinus Torvalds 99523db8620SAl Viro res = nfs_do_filldir(desc); 996*1f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(desc); 997ece0b423STrond Myklebust if (res < 0) 9981da177e4SLinus Torvalds break; 99947c716cbSTrond Myklebust } while (!desc->eof); 10002e7a4641STrond Myklebust 10012e7a4641STrond Myklebust spin_lock(&file->f_lock); 10022e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie; 10032e7a4641STrond Myklebust dir_ctx->dup_cookie = desc->dup_cookie; 10042e7a4641STrond Myklebust dir_ctx->duped = desc->duped; 10052e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount; 10062e7a4641STrond Myklebust spin_unlock(&file->f_lock); 10072e7a4641STrond Myklebust 1008fccca7fcSTrond Myklebust out: 10091e7cb3dcSChuck Lever if (res > 0) 10101e7cb3dcSChuck Lever res = 0; 10116de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 10121da177e4SLinus Torvalds return res; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 1015965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1016f0dd2136STrond Myklebust { 1017480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 1018b84e06c5SChuck Lever 10196de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 10206de1472fSAl Viro filp, offset, whence); 1021b84e06c5SChuck Lever 1022965c8e59SAndrew Morton switch (whence) { 1023f0dd2136STrond Myklebust default: 1024b2b1ff3dSTrond Myklebust return -EINVAL; 1025b2b1ff3dSTrond Myklebust case SEEK_SET: 1026b2b1ff3dSTrond Myklebust if (offset < 0) 1027b2b1ff3dSTrond Myklebust return -EINVAL; 102883f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1029b2b1ff3dSTrond Myklebust break; 1030b2b1ff3dSTrond Myklebust case SEEK_CUR: 1031b2b1ff3dSTrond Myklebust if (offset == 0) 1032b2b1ff3dSTrond Myklebust return filp->f_pos; 103383f2c45eSTrond Myklebust spin_lock(&filp->f_lock); 1034b2b1ff3dSTrond Myklebust offset += filp->f_pos; 1035b2b1ff3dSTrond Myklebust if (offset < 0) { 103683f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1037b2b1ff3dSTrond Myklebust return -EINVAL; 1038b2b1ff3dSTrond Myklebust } 1039f0dd2136STrond Myklebust } 1040f0dd2136STrond Myklebust if (offset != filp->f_pos) { 1041f0dd2136STrond Myklebust filp->f_pos = offset; 104259e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 104359e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 104459e356a9STrond Myklebust else 1045480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 10468ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1047f0dd2136STrond Myklebust } 104883f2c45eSTrond Myklebust spin_unlock(&filp->f_lock); 1049f0dd2136STrond Myklebust return offset; 1050f0dd2136STrond Myklebust } 1051f0dd2136STrond Myklebust 10521da177e4SLinus Torvalds /* 10531da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 10541da177e4SLinus Torvalds * is a dummy operation. 10551da177e4SLinus Torvalds */ 105602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 105702c24a82SJosef Bacik int datasync) 10581da177e4SLinus Torvalds { 10596de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 10601e7cb3dcSChuck Lever 106111decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 10621da177e4SLinus Torvalds return 0; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 1065bfc69a45STrond Myklebust /** 1066bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1067302fad7bSTrond Myklebust * @dir: pointer to directory inode 1068bfc69a45STrond Myklebust * 1069bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1070bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1071bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1072bfc69a45STrond Myklebust * 1073efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1074efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1075efeda80dSTrond Myklebust * 1076bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1077bfc69a45STrond Myklebust */ 1078bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1079bfc69a45STrond Myklebust { 1080efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1081bfc69a45STrond Myklebust } 108289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1083bfc69a45STrond Myklebust 1084efeda80dSTrond Myklebust /** 1085efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1086efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1087efeda80dSTrond Myklebust * @verf: previously saved change attribute 1088efeda80dSTrond Myklebust * 1089efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1090efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1091efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1092efeda80dSTrond Myklebust */ 1093efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1094efeda80dSTrond Myklebust { 1095efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1096efeda80dSTrond Myklebust } 1097efeda80dSTrond Myklebust 1098efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1099efeda80dSTrond Myklebust { 1100efeda80dSTrond Myklebust *verf |= 1UL; 1101efeda80dSTrond Myklebust } 1102efeda80dSTrond Myklebust 1103efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1104efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1105efeda80dSTrond Myklebust { 1106efeda80dSTrond Myklebust *verf &= ~1UL; 1107efeda80dSTrond Myklebust } 1108efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1109efeda80dSTrond Myklebust 1110efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1111efeda80dSTrond Myklebust { 1112efeda80dSTrond Myklebust return verf & 1; 1113efeda80dSTrond Myklebust } 1114efeda80dSTrond Myklebust 1115efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1116efeda80dSTrond Myklebust { 1117efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1118efeda80dSTrond Myklebust } 1119efeda80dSTrond Myklebust 1120efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1121efeda80dSTrond Myklebust { 1122efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1123efeda80dSTrond Myklebust 1124efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1125efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1126efeda80dSTrond Myklebust goto out; 1127efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1128efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1129efeda80dSTrond Myklebust out: 1130efeda80dSTrond Myklebust dentry->d_time = verf; 1131efeda80dSTrond Myklebust } 1132efeda80dSTrond Myklebust 1133efeda80dSTrond Myklebust /** 1134efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1135efeda80dSTrond Myklebust * @dentry: pointer to dentry 1136efeda80dSTrond Myklebust * @verf: verifier to save 1137efeda80dSTrond Myklebust * 1138efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1139efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1140efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1141efeda80dSTrond Myklebust * look it up again after a directory change. 1142efeda80dSTrond Myklebust */ 1143efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1144efeda80dSTrond Myklebust { 1145efeda80dSTrond Myklebust 1146efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1147efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1148efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1149efeda80dSTrond Myklebust } 1150efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1151efeda80dSTrond Myklebust 1152efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1153efeda80dSTrond Myklebust /** 1154efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1155efeda80dSTrond Myklebust * @inode: pointer to inode 1156efeda80dSTrond Myklebust * 1157efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1158efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1159efeda80dSTrond Myklebust * while holding a delegation. 1160efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1161efeda80dSTrond Myklebust * returned or revoked. 1162efeda80dSTrond Myklebust */ 1163efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1164efeda80dSTrond Myklebust { 1165efeda80dSTrond Myklebust struct dentry *alias; 1166efeda80dSTrond Myklebust 1167efeda80dSTrond Myklebust if (!inode) 1168efeda80dSTrond Myklebust return; 1169efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1170efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1171efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1172efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1173efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1174efeda80dSTrond Myklebust } 1175efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1176efeda80dSTrond Myklebust } 1177efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1178efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1179efeda80dSTrond Myklebust 11801da177e4SLinus Torvalds /* 11811da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 11821da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 11831da177e4SLinus Torvalds * and may need to be looked up again. 1184912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 11851da177e4SLinus Torvalds */ 1186912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1187912a108dSNeilBrown int rcu_walk) 11881da177e4SLinus Torvalds { 11891da177e4SLinus Torvalds if (IS_ROOT(dentry)) 11901da177e4SLinus Torvalds return 1; 11914eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 11924eec952eSTrond Myklebust return 0; 1193f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 11946ecc5e8fSTrond Myklebust return 0; 1195f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 11961cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1197912a108dSNeilBrown if (rcu_walk) 1198f2c77f4eSTrond Myklebust return 0; 11991cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 12001cd9cb05STrond Myklebust return 0; 12011cd9cb05STrond Myklebust } 1202f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1203f2c77f4eSTrond Myklebust return 0; 1204f2c77f4eSTrond Myklebust return 1; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* 1208a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1209a12802caSTrond Myklebust * an O_EXCL create using this path component. 1210a12802caSTrond Myklebust */ 1211fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1212a12802caSTrond Myklebust { 1213a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1214a12802caSTrond Myklebust return 0; 1215fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1216a12802caSTrond Myklebust } 1217a12802caSTrond Myklebust 1218a12802caSTrond Myklebust /* 12191d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 12201d6757fbSTrond Myklebust * 12211d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 12221d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 12231d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 12241d6757fbSTrond Myklebust * 12251d6757fbSTrond Myklebust */ 122665a0c149STrond Myklebust static 1227fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 12281da177e4SLinus Torvalds { 12291da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 123065a0c149STrond Myklebust int ret; 12311da177e4SLinus Torvalds 123236d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 12334e99a1ffSTrond Myklebust return 0; 123447921921STrond Myklebust 123547921921STrond Myklebust if (flags & LOOKUP_OPEN) { 123647921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 123747921921STrond Myklebust case S_IFREG: 123847921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 123947921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 124047921921STrond Myklebust goto out; 1241df561f66SGustavo A. R. Silva fallthrough; 124247921921STrond Myklebust case S_IFDIR: 124347921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 124447921921STrond Myklebust break; 124547921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 124647921921STrond Myklebust goto out_force; 124747921921STrond Myklebust } 124847921921STrond Myklebust } 124947921921STrond Myklebust 12501da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1251fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 12521da177e4SLinus Torvalds goto out_force; 125365a0c149STrond Myklebust out: 1254a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 12551da177e4SLinus Torvalds out_force: 12561fa1e384SNeilBrown if (flags & LOOKUP_RCU) 12571fa1e384SNeilBrown return -ECHILD; 125865a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 125965a0c149STrond Myklebust if (ret != 0) 126065a0c149STrond Myklebust return ret; 126165a0c149STrond Myklebust goto out; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds /* 12651da177e4SLinus Torvalds * We judge how long we want to trust negative 12661da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 12671da177e4SLinus Torvalds * 12681da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 12691da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1270912a108dSNeilBrown * 1271912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1272912a108dSNeilBrown * suggesting a reval is needed. 12739f6d44d4STrond Myklebust * 12749f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 12759f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 12761da177e4SLinus Torvalds */ 12771da177e4SLinus Torvalds static inline 12781da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1279fa3c56bbSAl Viro unsigned int flags) 12801da177e4SLinus Torvalds { 12819f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 12821da177e4SLinus Torvalds return 0; 12834eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 12844eec952eSTrond Myklebust return 1; 1285912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12885ceb9d7fSTrond Myklebust static int 12895ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 12905ceb9d7fSTrond Myklebust struct inode *inode, int error) 12911da177e4SLinus Torvalds { 12925ceb9d7fSTrond Myklebust switch (error) { 12935ceb9d7fSTrond Myklebust case 1: 12946de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12956de1472fSAl Viro __func__, dentry); 12961da177e4SLinus Torvalds return 1; 12975ceb9d7fSTrond Myklebust case 0: 1298a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12991da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 13001da177e4SLinus Torvalds /* Purge readdir caches. */ 13011da177e4SLinus Torvalds nfs_zap_caches(inode); 1302a3f432bfSJ. Bruce Fields /* 1303a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1304a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1305a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1306a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1307a3f432bfSJ. Bruce Fields */ 1308a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 13095ceb9d7fSTrond Myklebust return 1; 13101da177e4SLinus Torvalds } 13116de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 13126de1472fSAl Viro __func__, dentry); 13131da177e4SLinus Torvalds return 0; 13145ceb9d7fSTrond Myklebust } 13156de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 13166de1472fSAl Viro __func__, dentry, error); 1317e1fb4d05STrond Myklebust return error; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13205ceb9d7fSTrond Myklebust static int 13215ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 13225ceb9d7fSTrond Myklebust unsigned int flags) 13235ceb9d7fSTrond Myklebust { 13245ceb9d7fSTrond Myklebust int ret = 1; 13255ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 13265ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13275ceb9d7fSTrond Myklebust return -ECHILD; 13285ceb9d7fSTrond Myklebust ret = 0; 13295ceb9d7fSTrond Myklebust } 13305ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 13315ceb9d7fSTrond Myklebust } 13325ceb9d7fSTrond Myklebust 13335ceb9d7fSTrond Myklebust static int 13345ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 13355ceb9d7fSTrond Myklebust struct inode *inode) 13365ceb9d7fSTrond Myklebust { 13375ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 13385ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 13395ceb9d7fSTrond Myklebust } 13405ceb9d7fSTrond Myklebust 13415ceb9d7fSTrond Myklebust static int 13425ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 13435ceb9d7fSTrond Myklebust struct inode *inode) 13445ceb9d7fSTrond Myklebust { 13455ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 13465ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 13475ceb9d7fSTrond Myklebust struct nfs4_label *label; 1348a1147b82STrond Myklebust unsigned long dir_verifier; 13495ceb9d7fSTrond Myklebust int ret; 13505ceb9d7fSTrond Myklebust 13515ceb9d7fSTrond Myklebust ret = -ENOMEM; 13525ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 13535ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 13545ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 13555ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 13565ceb9d7fSTrond Myklebust goto out; 13575ceb9d7fSTrond Myklebust 1358a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1359f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 13605ceb9d7fSTrond Myklebust if (ret < 0) { 1361f7b37b8bSTrond Myklebust switch (ret) { 1362f7b37b8bSTrond Myklebust case -ESTALE: 1363f7b37b8bSTrond Myklebust case -ENOENT: 13645ceb9d7fSTrond Myklebust ret = 0; 1365f7b37b8bSTrond Myklebust break; 1366f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1367f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1368f7b37b8bSTrond Myklebust ret = 1; 1369f7b37b8bSTrond Myklebust } 13705ceb9d7fSTrond Myklebust goto out; 13715ceb9d7fSTrond Myklebust } 13725ceb9d7fSTrond Myklebust ret = 0; 13735ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 13745ceb9d7fSTrond Myklebust goto out; 13755ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 13765ceb9d7fSTrond Myklebust goto out; 13775ceb9d7fSTrond Myklebust 13785ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1379a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 13805ceb9d7fSTrond Myklebust 13815ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 13825ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 13835ceb9d7fSTrond Myklebust ret = 1; 13845ceb9d7fSTrond Myklebust out: 13855ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 13865ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 13875ceb9d7fSTrond Myklebust nfs4_label_free(label); 13885ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 13895ceb9d7fSTrond Myklebust } 13905ceb9d7fSTrond Myklebust 13915ceb9d7fSTrond Myklebust /* 13925ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 13935ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 13945ceb9d7fSTrond Myklebust * lookup. 13955ceb9d7fSTrond Myklebust * 13965ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 13975ceb9d7fSTrond Myklebust * we have an inode! 13985ceb9d7fSTrond Myklebust * 13995ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 14005ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 14015ceb9d7fSTrond Myklebust */ 14025ceb9d7fSTrond Myklebust static int 14035ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 14045ceb9d7fSTrond Myklebust unsigned int flags) 14055ceb9d7fSTrond Myklebust { 14065ceb9d7fSTrond Myklebust struct inode *inode; 14075ceb9d7fSTrond Myklebust int error; 14085ceb9d7fSTrond Myklebust 14095ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 14105ceb9d7fSTrond Myklebust inode = d_inode(dentry); 14115ceb9d7fSTrond Myklebust 14125ceb9d7fSTrond Myklebust if (!inode) 14135ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 14145ceb9d7fSTrond Myklebust 14155ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 14165ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 14175ceb9d7fSTrond Myklebust __func__, dentry); 14185ceb9d7fSTrond Myklebust goto out_bad; 14195ceb9d7fSTrond Myklebust } 14205ceb9d7fSTrond Myklebust 1421efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 14225ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 14235ceb9d7fSTrond Myklebust 14245ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 14255ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 14265ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 14275ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 14285ceb9d7fSTrond Myklebust if (error) { 14295ceb9d7fSTrond Myklebust if (error == -ESTALE) 14305ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 14315ceb9d7fSTrond Myklebust goto out_bad; 14325ceb9d7fSTrond Myklebust } 14335ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 14345ceb9d7fSTrond Myklebust goto out_valid; 14355ceb9d7fSTrond Myklebust } 14365ceb9d7fSTrond Myklebust 14375ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 14385ceb9d7fSTrond Myklebust return -ECHILD; 14395ceb9d7fSTrond Myklebust 14405ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 14415ceb9d7fSTrond Myklebust goto out_bad; 14425ceb9d7fSTrond Myklebust 14435ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 14445ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 14455ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 14465ceb9d7fSTrond Myklebust return error; 14475ceb9d7fSTrond Myklebust out_valid: 14485ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 14495ceb9d7fSTrond Myklebust out_bad: 14505ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 14515ceb9d7fSTrond Myklebust return -ECHILD; 14525ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 14535ceb9d7fSTrond Myklebust } 14545ceb9d7fSTrond Myklebust 14555ceb9d7fSTrond Myklebust static int 1456c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1457c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 14585ceb9d7fSTrond Myklebust { 14595ceb9d7fSTrond Myklebust struct dentry *parent; 14605ceb9d7fSTrond Myklebust struct inode *dir; 14615ceb9d7fSTrond Myklebust int ret; 14625ceb9d7fSTrond Myklebust 14635ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 14645ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 14655ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 14665ceb9d7fSTrond Myklebust if (!dir) 14675ceb9d7fSTrond Myklebust return -ECHILD; 1468c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 14695ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 14705ceb9d7fSTrond Myklebust return -ECHILD; 14715ceb9d7fSTrond Myklebust } else { 14725ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1473c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 14745ceb9d7fSTrond Myklebust dput(parent); 14755ceb9d7fSTrond Myklebust } 14765ceb9d7fSTrond Myklebust return ret; 14775ceb9d7fSTrond Myklebust } 14785ceb9d7fSTrond Myklebust 1479c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1480c7944ebbSTrond Myklebust { 1481c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1482c7944ebbSTrond Myklebust } 1483c7944ebbSTrond Myklebust 14841da177e4SLinus Torvalds /* 14852b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1486ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1487ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1488ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1489ecf3d1f1SJeff Layton * 1490ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1491ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1492ecf3d1f1SJeff Layton */ 1493ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1494ecf3d1f1SJeff Layton { 14952b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 14969cdd1d3fSTrond Myklebust int error = 0; 1497ecf3d1f1SJeff Layton 1498ecf3d1f1SJeff Layton /* 1499ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1500ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1501ecf3d1f1SJeff Layton * eventually need to do something more here. 1502ecf3d1f1SJeff Layton */ 1503ecf3d1f1SJeff Layton if (!inode) { 15046de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 15056de1472fSAl Viro __func__, dentry); 1506ecf3d1f1SJeff Layton return 1; 1507ecf3d1f1SJeff Layton } 1508ecf3d1f1SJeff Layton 1509ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 15106de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 15116de1472fSAl Viro __func__, dentry); 1512ecf3d1f1SJeff Layton return 0; 1513ecf3d1f1SJeff Layton } 1514ecf3d1f1SJeff Layton 1515b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1516ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1517ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1518ecf3d1f1SJeff Layton return !error; 1519ecf3d1f1SJeff Layton } 1520ecf3d1f1SJeff Layton 1521ecf3d1f1SJeff Layton /* 15221da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 15231da177e4SLinus Torvalds */ 1524fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 15251da177e4SLinus Torvalds { 15266de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 15276de1472fSAl Viro dentry, dentry->d_flags); 15281da177e4SLinus Torvalds 152977f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 15302b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 153177f11192STrond Myklebust return 1; 153277f11192STrond Myklebust 15331da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 15341da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 15351da177e4SLinus Torvalds return 1; 15361da177e4SLinus Torvalds } 15371751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 15381da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 15391da177e4SLinus Torvalds * files will be cleaned up during umount */ 15401da177e4SLinus Torvalds return 1; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds return 0; 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 15461f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 15471b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 15481b83d707STrond Myklebust { 15491b83d707STrond Myklebust spin_lock(&inode->i_lock); 15501f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 155159a707b0STrond Myklebust if (inode->i_nlink > 0) 155259a707b0STrond Myklebust drop_nlink(inode); 155359a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 155416e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 155516e14375STrond Myklebust | NFS_INO_INVALID_CTIME 155659a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 155759a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 15581b83d707STrond Myklebust spin_unlock(&inode->i_lock); 15591b83d707STrond Myklebust } 15601b83d707STrond Myklebust 15611da177e4SLinus Torvalds /* 15621da177e4SLinus Torvalds * Called when the dentry loses inode. 15631da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 15641da177e4SLinus Torvalds */ 15651da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 15661da177e4SLinus Torvalds { 156783672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 156883672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 156983672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 157083672d39SNeil Brown 15711da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1572e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 15731f018458STrond Myklebust nfs_drop_nlink(inode); 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds iput(inode); 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 1578b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1579b1942c5fSAl Viro { 1580b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1581b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1582b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1583b1942c5fSAl Viro WARN_ON(1); 1584b1942c5fSAl Viro else 1585b1942c5fSAl Viro kfree(dentry->d_fsdata); 1586b1942c5fSAl Viro } 1587b1942c5fSAl Viro } 1588b1942c5fSAl Viro 1589f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 15901da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1591ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 15921da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15931da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 159436d43a43SDavid Howells .d_automount = nfs_d_automount, 1595b1942c5fSAl Viro .d_release = nfs_d_release, 15961da177e4SLinus Torvalds }; 1597ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 15981da177e4SLinus Torvalds 1599597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 16001da177e4SLinus Torvalds { 16011da177e4SLinus Torvalds struct dentry *res; 16021da177e4SLinus Torvalds struct inode *inode = NULL; 1603e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1604e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 16051775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1606a1147b82STrond Myklebust unsigned long dir_verifier; 16071da177e4SLinus Torvalds int error; 16081da177e4SLinus Torvalds 16096de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 161091d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 16111da177e4SLinus Torvalds 1612130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1613130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 16141da177e4SLinus Torvalds 1615fd684071STrond Myklebust /* 1616fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1617fd684071STrond Myklebust * but don't hash the dentry. 1618fd684071STrond Myklebust */ 16199f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1620130f9ab7SAl Viro return NULL; 16211da177e4SLinus Torvalds 1622e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1623e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1624e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1625e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1626e1fb4d05STrond Myklebust goto out; 1627e1fb4d05STrond Myklebust 162814c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 162914c43f76SDavid Quigley if (IS_ERR(label)) 163014c43f76SDavid Quigley goto out; 163114c43f76SDavid Quigley 1632a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 16336e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1634f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 16351da177e4SLinus Torvalds if (error == -ENOENT) 16361da177e4SLinus Torvalds goto no_entry; 16371da177e4SLinus Torvalds if (error < 0) { 16381da177e4SLinus Torvalds res = ERR_PTR(error); 1639bf130914SAl Viro goto out_label; 16401da177e4SLinus Torvalds } 16411775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1642bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 164303f28e3aSTrond Myklebust if (IS_ERR(res)) 1644bf130914SAl Viro goto out_label; 164554ceac45SDavid Howells 164663519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 164763519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1648d69ee9b8STrond Myklebust 16491da177e4SLinus Torvalds no_entry: 165041d28bcaSAl Viro res = d_splice_alias(inode, dentry); 16519eaef27bSTrond Myklebust if (res != NULL) { 16529eaef27bSTrond Myklebust if (IS_ERR(res)) 1653bf130914SAl Viro goto out_label; 16541da177e4SLinus Torvalds dentry = res; 16559eaef27bSTrond Myklebust } 1656a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1657bf130914SAl Viro out_label: 16586e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 165914c43f76SDavid Quigley nfs4_label_free(label); 16601da177e4SLinus Torvalds out: 1661e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1662e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 16631da177e4SLinus Torvalds return res; 16641da177e4SLinus Torvalds } 1665ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 16661da177e4SLinus Torvalds 166789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 16680b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 16691da177e4SLinus Torvalds 1670f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 16710ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1672b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 16731da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 16741da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 167536d43a43SDavid Howells .d_automount = nfs_d_automount, 1676b1942c5fSAl Viro .d_release = nfs_d_release, 16771da177e4SLinus Torvalds }; 167889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 16791da177e4SLinus Torvalds 16808a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 16818a5e929dSAl Viro { 16828a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 16838a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 16848a5e929dSAl Viro res |= FMODE_READ; 16858a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 16868a5e929dSAl Viro res |= FMODE_WRITE; 16878a5e929dSAl Viro return res; 16888a5e929dSAl Viro } 16898a5e929dSAl Viro 1690532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1691cd9a1c0eSTrond Myklebust { 1692532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1693cd9a1c0eSTrond Myklebust } 1694cd9a1c0eSTrond Myklebust 1695cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1696cd9a1c0eSTrond Myklebust { 1697f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1698cd9a1c0eSTrond Myklebust return 0; 1699cd9a1c0eSTrond Myklebust } 1700cd9a1c0eSTrond Myklebust 1701d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 17020dd2b474SMiklos Szeredi struct dentry *dentry, 1703b452a458SAl Viro struct file *file, unsigned open_flags) 1704cd9a1c0eSTrond Myklebust { 17050dd2b474SMiklos Szeredi int err; 17060dd2b474SMiklos Szeredi 1707be12af3eSAl Viro err = finish_open(file, dentry, do_open); 170830d90494SAl Viro if (err) 1709d9585277SAl Viro goto out; 1710eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 171130d90494SAl Viro nfs_file_set_open_context(file, ctx); 1712eaa2b82cSNeilBrown else 17139821421aSTrond Myklebust err = -EOPENSTALE; 1714cd9a1c0eSTrond Myklebust out: 1715d9585277SAl Viro return err; 1716cd9a1c0eSTrond Myklebust } 1717cd9a1c0eSTrond Myklebust 171873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 171930d90494SAl Viro struct file *file, unsigned open_flags, 172044907d79SAl Viro umode_t mode) 17211da177e4SLinus Torvalds { 1722c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1723cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 17240dd2b474SMiklos Szeredi struct dentry *res; 17250dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1726f46e0bd3STrond Myklebust struct inode *inode; 17271472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1728c94c0953SAl Viro bool switched = false; 172973a09dd9SAl Viro int created = 0; 1730898f635cSTrond Myklebust int err; 17311da177e4SLinus Torvalds 17320dd2b474SMiklos Szeredi /* Expect a negative dentry */ 17332b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 17340dd2b474SMiklos Szeredi 17351e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 17366de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17371e7cb3dcSChuck Lever 17389597c13bSJeff Layton err = nfs_check_flags(open_flags); 17399597c13bSJeff Layton if (err) 17409597c13bSJeff Layton return err; 17419597c13bSJeff Layton 17420dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 17430dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 174400699ad8SAl Viro if (!d_in_lookup(dentry)) { 17450dd2b474SMiklos Szeredi /* 17460dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 17470dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 17480dd2b474SMiklos Szeredi * again 17490dd2b474SMiklos Szeredi */ 1750d9585277SAl Viro return -ENOENT; 17510dd2b474SMiklos Szeredi } 17521472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 17531da177e4SLinus Torvalds goto no_open; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 17560dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1757d9585277SAl Viro return -ENAMETOOLONG; 17581da177e4SLinus Torvalds 17590dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1760dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1761dff25ddbSAndreas Gruenbacher 1762dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1763dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1764dff25ddbSAndreas Gruenbacher 1765536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1766dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 17670dd2b474SMiklos Szeredi } 1768536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1769536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1770536e43d1STrond Myklebust attr.ia_size = 0; 1771cd9a1c0eSTrond Myklebust } 1772cd9a1c0eSTrond Myklebust 1773c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1774c94c0953SAl Viro d_drop(dentry); 1775c94c0953SAl Viro switched = true; 1776c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1777c94c0953SAl Viro &dentry->d_name, &wq); 1778c94c0953SAl Viro if (IS_ERR(dentry)) 1779c94c0953SAl Viro return PTR_ERR(dentry); 1780c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1781c94c0953SAl Viro return finish_no_open(file, dentry); 1782c94c0953SAl Viro } 1783c94c0953SAl Viro 1784532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 17850dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 17860dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1787d9585277SAl Viro goto out; 17880dd2b474SMiklos Szeredi 17896e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 179073a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 179173a09dd9SAl Viro if (created) 179273a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1793275bb307STrond Myklebust if (IS_ERR(inode)) { 17940dd2b474SMiklos Szeredi err = PTR_ERR(inode); 17956e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17962d9db750STrond Myklebust put_nfs_open_context(ctx); 1797d20cb71dSAl Viro d_drop(dentry); 17980dd2b474SMiklos Szeredi switch (err) { 17991da177e4SLinus Torvalds case -ENOENT: 1800774d9513SPeng Tao d_splice_alias(NULL, dentry); 1801809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18020dd2b474SMiklos Szeredi break; 18031788ea6eSJeff Layton case -EISDIR: 18046f926b5bSTrond Myklebust case -ENOTDIR: 18056f926b5bSTrond Myklebust goto no_open; 18061da177e4SLinus Torvalds case -ELOOP: 18070dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 18081da177e4SLinus Torvalds goto no_open; 18090dd2b474SMiklos Szeredi break; 18101da177e4SLinus Torvalds /* case -EINVAL: */ 18111da177e4SLinus Torvalds default: 18120dd2b474SMiklos Szeredi break; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds goto out; 18151da177e4SLinus Torvalds } 18160dd2b474SMiklos Szeredi 1817b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 18186e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 18192d9db750STrond Myklebust put_nfs_open_context(ctx); 1820d9585277SAl Viro out: 1821c94c0953SAl Viro if (unlikely(switched)) { 1822c94c0953SAl Viro d_lookup_done(dentry); 1823c94c0953SAl Viro dput(dentry); 1824c94c0953SAl Viro } 1825d9585277SAl Viro return err; 18260dd2b474SMiklos Szeredi 18271da177e4SLinus Torvalds no_open: 18281472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1829c94c0953SAl Viro if (switched) { 1830c94c0953SAl Viro d_lookup_done(dentry); 1831c94c0953SAl Viro if (!res) 1832c94c0953SAl Viro res = dentry; 1833c94c0953SAl Viro else 1834c94c0953SAl Viro dput(dentry); 1835c94c0953SAl Viro } 18360dd2b474SMiklos Szeredi if (IS_ERR(res)) 1837c94c0953SAl Viro return PTR_ERR(res); 1838e45198a6SAl Viro return finish_no_open(file, res); 18391da177e4SLinus Torvalds } 184089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 18411da177e4SLinus Torvalds 1842c7944ebbSTrond Myklebust static int 1843c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1844c7944ebbSTrond Myklebust unsigned int flags) 18451da177e4SLinus Torvalds { 1846657e94b6SNick Piggin struct inode *inode; 18471da177e4SLinus Torvalds 1848fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1849c7944ebbSTrond Myklebust goto full_reval; 1850eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1851c7944ebbSTrond Myklebust goto full_reval; 18522b484297STrond Myklebust 18532b0143b5SDavid Howells inode = d_inode(dentry); 18542b484297STrond Myklebust 18551da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 18561da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 18571da177e4SLinus Torvalds */ 1858c7944ebbSTrond Myklebust if (inode == NULL) 1859c7944ebbSTrond Myklebust goto full_reval; 186049317a7fSNeilBrown 1861efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 1862c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1863216d5d06STrond Myklebust 18641da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 18651da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1866c7944ebbSTrond Myklebust goto full_reval; 1867c7944ebbSTrond Myklebust 18681da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1869c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1870c7944ebbSTrond Myklebust goto reval_dentry; 1871c7944ebbSTrond Myklebust 1872c7944ebbSTrond Myklebust /* Check if the directory changed */ 1873c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1874c7944ebbSTrond Myklebust goto reval_dentry; 18751da177e4SLinus Torvalds 18760ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1877c7944ebbSTrond Myklebust return 1; 1878c7944ebbSTrond Myklebust reval_dentry: 1879c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1880c7944ebbSTrond Myklebust return -ECHILD; 188142f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 18820ef97dcfSMiklos Szeredi 1883c7944ebbSTrond Myklebust full_reval: 1884c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1885c7944ebbSTrond Myklebust } 1886535918f1STrond Myklebust 1887c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1888c7944ebbSTrond Myklebust { 1889c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1890c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1891c0204fd2STrond Myklebust } 1892c0204fd2STrond Myklebust 18931da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 18941da177e4SLinus Torvalds 1895406cd915SBenjamin Coddington struct dentry * 1896406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 18971775fd3eSDavid Quigley struct nfs_fattr *fattr, 18981775fd3eSDavid Quigley struct nfs4_label *label) 18991da177e4SLinus Torvalds { 1900fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 19012b0143b5SDavid Howells struct inode *dir = d_inode(parent); 19021da177e4SLinus Torvalds struct inode *inode; 1903b0c6108eSAl Viro struct dentry *d; 1904406cd915SBenjamin Coddington int error; 19051da177e4SLinus Torvalds 1906fab728e1STrond Myklebust d_drop(dentry); 1907fab728e1STrond Myklebust 19081da177e4SLinus Torvalds if (fhandle->size == 0) { 1909f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 19101da177e4SLinus Torvalds if (error) 1911fab728e1STrond Myklebust goto out_error; 19121da177e4SLinus Torvalds } 19135724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19141da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 19151da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1916a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1917a841b54dSTrond Myklebust fattr, NULL, NULL); 19181da177e4SLinus Torvalds if (error < 0) 1919fab728e1STrond Myklebust goto out_error; 19201da177e4SLinus Torvalds } 19211775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1922b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1923fab728e1STrond Myklebust out: 1924fab728e1STrond Myklebust dput(parent); 1925406cd915SBenjamin Coddington return d; 1926fab728e1STrond Myklebust out_error: 1927fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1928406cd915SBenjamin Coddington d = ERR_PTR(error); 1929406cd915SBenjamin Coddington goto out; 1930406cd915SBenjamin Coddington } 1931406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1932406cd915SBenjamin Coddington 1933406cd915SBenjamin Coddington /* 1934406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1935406cd915SBenjamin Coddington */ 1936406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1937406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1938406cd915SBenjamin Coddington struct nfs4_label *label) 1939406cd915SBenjamin Coddington { 1940406cd915SBenjamin Coddington struct dentry *d; 1941406cd915SBenjamin Coddington 1942406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1943406cd915SBenjamin Coddington if (IS_ERR(d)) 1944406cd915SBenjamin Coddington return PTR_ERR(d); 1945406cd915SBenjamin Coddington 1946406cd915SBenjamin Coddington /* Callers don't care */ 1947406cd915SBenjamin Coddington dput(d); 1948406cd915SBenjamin Coddington return 0; 19491da177e4SLinus Torvalds } 1950ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 19511da177e4SLinus Torvalds 19521da177e4SLinus Torvalds /* 19531da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 19541da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 19551da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 19561da177e4SLinus Torvalds * reply path made it appear to have failed. 19571da177e4SLinus Torvalds */ 1958597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1959ebfc3b49SAl Viro umode_t mode, bool excl) 19601da177e4SLinus Torvalds { 19611da177e4SLinus Torvalds struct iattr attr; 1962ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 19631da177e4SLinus Torvalds int error; 19641da177e4SLinus Torvalds 19651e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 19666de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds attr.ia_mode = mode; 19691da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19701da177e4SLinus Torvalds 19718b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 19728867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 19738b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 19741da177e4SLinus Torvalds if (error != 0) 19751da177e4SLinus Torvalds goto out_err; 19761da177e4SLinus Torvalds return 0; 19771da177e4SLinus Torvalds out_err: 19781da177e4SLinus Torvalds d_drop(dentry); 19791da177e4SLinus Torvalds return error; 19801da177e4SLinus Torvalds } 1981ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds /* 19841da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19851da177e4SLinus Torvalds */ 1986597d9289SBryan Schumaker int 19871a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 19881da177e4SLinus Torvalds { 19891da177e4SLinus Torvalds struct iattr attr; 19901da177e4SLinus Torvalds int status; 19911da177e4SLinus Torvalds 19921e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 19936de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds attr.ia_mode = mode; 19961da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19971da177e4SLinus Torvalds 19981ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 19991da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 20001ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 20011da177e4SLinus Torvalds if (status != 0) 20021da177e4SLinus Torvalds goto out_err; 20031da177e4SLinus Torvalds return 0; 20041da177e4SLinus Torvalds out_err: 20051da177e4SLinus Torvalds d_drop(dentry); 20061da177e4SLinus Torvalds return status; 20071da177e4SLinus Torvalds } 2008ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds /* 20111da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 20121da177e4SLinus Torvalds */ 2013597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 20141da177e4SLinus Torvalds { 20151da177e4SLinus Torvalds struct iattr attr; 20161da177e4SLinus Torvalds int error; 20171da177e4SLinus Torvalds 20181e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 20196de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 20221da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 20231da177e4SLinus Torvalds 20241ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 20251da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 20261ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 20271da177e4SLinus Torvalds if (error != 0) 20281da177e4SLinus Torvalds goto out_err; 20291da177e4SLinus Torvalds return 0; 20301da177e4SLinus Torvalds out_err: 20311da177e4SLinus Torvalds d_drop(dentry); 20321da177e4SLinus Torvalds return error; 20331da177e4SLinus Torvalds } 2034ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 20351da177e4SLinus Torvalds 2036d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 2037d45b9d8bSTrond Myklebust { 2038dc3f4198SAl Viro if (simple_positive(dentry)) 2039d45b9d8bSTrond Myklebust d_delete(dentry); 2040d45b9d8bSTrond Myklebust } 2041d45b9d8bSTrond Myklebust 2042597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 20431da177e4SLinus Torvalds { 20441da177e4SLinus Torvalds int error; 20451da177e4SLinus Torvalds 20461e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 20476de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 20481da177e4SLinus Torvalds 20491ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 20502b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 2051884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 20521da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 20531da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2054ba6c0592STrond Myklebust switch (error) { 2055ba6c0592STrond Myklebust case 0: 20562b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2057ba6c0592STrond Myklebust break; 2058ba6c0592STrond Myklebust case -ENOENT: 2059d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2060ba6c0592STrond Myklebust } 2061884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2062ba6c0592STrond Myklebust } else 2063ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 20641ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds return error; 20671da177e4SLinus Torvalds } 2068ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds /* 20711da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 20721da177e4SLinus Torvalds * and after checking that the file has only one user. 20731da177e4SLinus Torvalds * 20741da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 20751da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 20761da177e4SLinus Torvalds */ 20771da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 20781da177e4SLinus Torvalds { 20792b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 20802b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 20811da177e4SLinus Torvalds int error = -EBUSY; 20821da177e4SLinus Torvalds 20836de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 20861da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 20871da177e4SLinus Torvalds error = 0; 20881da177e4SLinus Torvalds goto out; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 20911ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 20921da177e4SLinus Torvalds if (inode != NULL) { 2093912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 20941da177e4SLinus Torvalds if (error == 0) 20951b83d707STrond Myklebust nfs_drop_nlink(inode); 20961da177e4SLinus Torvalds } else 2097912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2098d45b9d8bSTrond Myklebust if (error == -ENOENT) 2099d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 21001ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 21011da177e4SLinus Torvalds out: 21021da177e4SLinus Torvalds return error; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 21061da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 21071da177e4SLinus Torvalds * 21081da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 21091da177e4SLinus Torvalds */ 2110597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 21111da177e4SLinus Torvalds { 21121da177e4SLinus Torvalds int error; 21131da177e4SLinus Torvalds int need_rehash = 0; 21141da177e4SLinus Torvalds 21151e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 21166de1472fSAl Viro dir->i_ino, dentry); 21171da177e4SLinus Torvalds 21181ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 21191da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 212084d08fa8SAl Viro if (d_count(dentry) > 1) { 21211da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2122ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 21232b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 21241da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 21251ca42382STrond Myklebust goto out; 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 21281da177e4SLinus Torvalds __d_drop(dentry); 21291da177e4SLinus Torvalds need_rehash = 1; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 21321da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2133d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 21341da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 21351da177e4SLinus Torvalds } else if (need_rehash) 21361da177e4SLinus Torvalds d_rehash(dentry); 21371ca42382STrond Myklebust out: 21381ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 21391da177e4SLinus Torvalds return error; 21401da177e4SLinus Torvalds } 2141ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 21421da177e4SLinus Torvalds 2143873101b3SChuck Lever /* 2144873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2145873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2146873101b3SChuck Lever * using prepare_write/commit_write. 2147873101b3SChuck Lever * 2148873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2149873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2150873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2151873101b3SChuck Lever * symlink request has completed on the server. 2152873101b3SChuck Lever * 2153873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2154873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2155873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2156873101b3SChuck Lever * and move the raw page into its mapping. 2157873101b3SChuck Lever */ 2158597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 21591da177e4SLinus Torvalds { 2160873101b3SChuck Lever struct page *page; 2161873101b3SChuck Lever char *kaddr; 21621da177e4SLinus Torvalds struct iattr attr; 2163873101b3SChuck Lever unsigned int pathlen = strlen(symname); 21641da177e4SLinus Torvalds int error; 21651da177e4SLinus Torvalds 21661e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 21676de1472fSAl Viro dir->i_ino, dentry, symname); 21681da177e4SLinus Torvalds 2169873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2170873101b3SChuck Lever return -ENAMETOOLONG; 21711da177e4SLinus Torvalds 2172873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2173873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 21741da177e4SLinus Torvalds 2175e8ecde25SAl Viro page = alloc_page(GFP_USER); 217676566991STrond Myklebust if (!page) 2177873101b3SChuck Lever return -ENOMEM; 2178873101b3SChuck Lever 2179e8ecde25SAl Viro kaddr = page_address(page); 2180873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2181873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2182873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2183873101b3SChuck Lever 21841ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 218594a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 21861ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2187873101b3SChuck Lever if (error != 0) { 21881e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2189873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 21906de1472fSAl Viro dentry, symname, error); 21911da177e4SLinus Torvalds d_drop(dentry); 2192873101b3SChuck Lever __free_page(page); 21931da177e4SLinus Torvalds return error; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds 2196873101b3SChuck Lever /* 2197873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2198873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2199873101b3SChuck Lever */ 22002b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2201873101b3SChuck Lever GFP_KERNEL)) { 2202873101b3SChuck Lever SetPageUptodate(page); 2203873101b3SChuck Lever unlock_page(page); 2204a0b54addSRafael Aquini /* 2205a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2206a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2207a0b54addSRafael Aquini */ 220809cbfeafSKirill A. Shutemov put_page(page); 2209873101b3SChuck Lever } else 2210873101b3SChuck Lever __free_page(page); 2211873101b3SChuck Lever 2212873101b3SChuck Lever return 0; 2213873101b3SChuck Lever } 2214ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2215873101b3SChuck Lever 2216597d9289SBryan Schumaker int 22171da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 22181da177e4SLinus Torvalds { 22192b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 22201da177e4SLinus Torvalds int error; 22211da177e4SLinus Torvalds 22226de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 22236de1472fSAl Viro old_dentry, dentry); 22241da177e4SLinus Torvalds 22251fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 22269697d234STrond Myklebust d_drop(dentry); 22271da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2228cf809556STrond Myklebust if (error == 0) { 22297de9c6eeSAl Viro ihold(inode); 22309697d234STrond Myklebust d_add(dentry, inode); 2231cf809556STrond Myklebust } 22321fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 22331da177e4SLinus Torvalds return error; 22341da177e4SLinus Torvalds } 2235ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds /* 22381da177e4SLinus Torvalds * RENAME 22391da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 22401da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 22411da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 22421da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 22431da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 22441da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 22451da177e4SLinus Torvalds * 22461da177e4SLinus Torvalds * FIXED. 22471da177e4SLinus Torvalds * 22481da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 22491da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 22501da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 22511da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 22521da177e4SLinus Torvalds * using the inode layer 22531da177e4SLinus Torvalds * 22541da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 22551da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 22561da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 22571da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 22581da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 22591da177e4SLinus Torvalds * the rename. 22601da177e4SLinus Torvalds */ 2261597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 22621cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 22631cd66c93SMiklos Szeredi unsigned int flags) 22641da177e4SLinus Torvalds { 22652b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 22662b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2267d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 226880a491fdSJeff Layton struct rpc_task *task; 22691da177e4SLinus Torvalds int error = -EBUSY; 22701da177e4SLinus Torvalds 22711cd66c93SMiklos Szeredi if (flags) 22721cd66c93SMiklos Szeredi return -EINVAL; 22731cd66c93SMiklos Szeredi 22746de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 22756de1472fSAl Viro old_dentry, new_dentry, 227684d08fa8SAl Viro d_count(new_dentry)); 22771da177e4SLinus Torvalds 227870ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 22791da177e4SLinus Torvalds /* 228028f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 228128f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 228228f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 228328f79a1aSMiklos Szeredi * the new target. 22841da177e4SLinus Torvalds */ 228527226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 228627226104SMiklos Szeredi /* 228727226104SMiklos Szeredi * To prevent any new references to the target during the 228827226104SMiklos Szeredi * rename, we unhash the dentry in advance. 228927226104SMiklos Szeredi */ 2290d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 229127226104SMiklos Szeredi d_drop(new_dentry); 2292d9f29500SBenjamin Coddington rehash = new_dentry; 2293d9f29500SBenjamin Coddington } 229427226104SMiklos Szeredi 229584d08fa8SAl Viro if (d_count(new_dentry) > 2) { 22961da177e4SLinus Torvalds int err; 229727226104SMiklos Szeredi 22981da177e4SLinus Torvalds /* copy the target dentry's name */ 22991da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 23001da177e4SLinus Torvalds &new_dentry->d_name); 23011da177e4SLinus Torvalds if (!dentry) 23021da177e4SLinus Torvalds goto out; 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds /* silly-rename the existing target ... */ 23051da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 230624e93025SMiklos Szeredi if (err) 23071da177e4SLinus Torvalds goto out; 230824e93025SMiklos Szeredi 230924e93025SMiklos Szeredi new_dentry = dentry; 2310d9f29500SBenjamin Coddington rehash = NULL; 231124e93025SMiklos Szeredi new_inode = NULL; 2312b1e4adf4STrond Myklebust } 231327226104SMiklos Szeredi } 23141da177e4SLinus Torvalds 2315d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 231680a491fdSJeff Layton if (IS_ERR(task)) { 231780a491fdSJeff Layton error = PTR_ERR(task); 231880a491fdSJeff Layton goto out; 231980a491fdSJeff Layton } 232080a491fdSJeff Layton 232180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2322818a8dbeSBenjamin Coddington if (error != 0) { 2323818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2324818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2325818a8dbeSBenjamin Coddington smp_wmb(); 2326818a8dbeSBenjamin Coddington } else 232780a491fdSJeff Layton error = task->tk_status; 232880a491fdSJeff Layton rpc_put_task(task); 232959a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 233059a707b0STrond Myklebust if (error == 0) { 233159a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 233259a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 233359a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 233459a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 233559a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 233659a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 233759a707b0STrond Myklebust } 23381da177e4SLinus Torvalds out: 2339d9f29500SBenjamin Coddington if (rehash) 2340d9f29500SBenjamin Coddington d_rehash(rehash); 234170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 234270ded201STrond Myklebust new_dir, new_dentry, error); 2343d9f29500SBenjamin Coddington if (!error) { 2344d9f29500SBenjamin Coddington if (new_inode != NULL) 2345d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2346d9f29500SBenjamin Coddington /* 2347d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2348d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2349d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2350d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2351d9f29500SBenjamin Coddington */ 2352d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2353d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2354d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2355d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2356d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2357d9f29500SBenjamin Coddington 23581da177e4SLinus Torvalds /* new dentry created? */ 23591da177e4SLinus Torvalds if (dentry) 23601da177e4SLinus Torvalds dput(dentry); 23611da177e4SLinus Torvalds return error; 23621da177e4SLinus Torvalds } 2363ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 23641da177e4SLinus Torvalds 2365cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2366cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2367cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2368cfcea3e8STrond Myklebust 2369a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 23703a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 23713a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 23723a505845STrond Myklebust 23731c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 23741c3c07e9STrond Myklebust { 2375b68572e0SNeilBrown put_cred(entry->cred); 2376f682a398SNeilBrown kfree_rcu(entry, rcu_head); 23774e857c58SPeter Zijlstra smp_mb__before_atomic(); 2378cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 23794e857c58SPeter Zijlstra smp_mb__after_atomic(); 23801c3c07e9STrond Myklebust } 23811c3c07e9STrond Myklebust 23821a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 23831a81bb8aSTrond Myklebust { 23841a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 23851a81bb8aSTrond Myklebust 23861a81bb8aSTrond Myklebust while (!list_empty(head)) { 23871a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 23881a81bb8aSTrond Myklebust list_del(&cache->lru); 23891a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 23901a81bb8aSTrond Myklebust } 23911a81bb8aSTrond Myklebust } 23921a81bb8aSTrond Myklebust 23933a505845STrond Myklebust static unsigned long 23943a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2395979df72eSTrond Myklebust { 2396979df72eSTrond Myklebust LIST_HEAD(head); 2397aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2398979df72eSTrond Myklebust struct nfs_access_entry *cache; 23991ab6c499SDave Chinner long freed = 0; 2400979df72eSTrond Myklebust 2401a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2402aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2403979df72eSTrond Myklebust struct inode *inode; 2404979df72eSTrond Myklebust 2405979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2406979df72eSTrond Myklebust break; 24079c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2408979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2409979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2410979df72eSTrond Myklebust goto remove_lru_entry; 2411979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2412979df72eSTrond Myklebust struct nfs_access_entry, lru); 2413979df72eSTrond Myklebust list_move(&cache->lru, &head); 2414979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 24151ab6c499SDave Chinner freed++; 2416979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2417979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2418979df72eSTrond Myklebust &nfs_access_lru_list); 2419979df72eSTrond Myklebust else { 2420979df72eSTrond Myklebust remove_lru_entry: 2421979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 24224e857c58SPeter Zijlstra smp_mb__before_atomic(); 2423979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 24244e857c58SPeter Zijlstra smp_mb__after_atomic(); 2425979df72eSTrond Myklebust } 242659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2427979df72eSTrond Myklebust } 2428979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24291a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24301ab6c499SDave Chinner return freed; 24311ab6c499SDave Chinner } 24321ab6c499SDave Chinner 24331ab6c499SDave Chinner unsigned long 24343a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 24353a505845STrond Myklebust { 24363a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 24373a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 24383a505845STrond Myklebust 24393a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 24403a505845STrond Myklebust return SHRINK_STOP; 24413a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 24423a505845STrond Myklebust } 24433a505845STrond Myklebust 24443a505845STrond Myklebust 24453a505845STrond Myklebust unsigned long 24461ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 24471ab6c499SDave Chinner { 244855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2449979df72eSTrond Myklebust } 2450979df72eSTrond Myklebust 24513a505845STrond Myklebust static void 24523a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 24533a505845STrond Myklebust { 24543a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 24553a505845STrond Myklebust unsigned long diff; 24563a505845STrond Myklebust unsigned int nr_to_scan; 24573a505845STrond Myklebust 24583a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 24593a505845STrond Myklebust return; 24603a505845STrond Myklebust nr_to_scan = 100; 24613a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 24623a505845STrond Myklebust if (diff < nr_to_scan) 24633a505845STrond Myklebust nr_to_scan = diff; 24643a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 24653a505845STrond Myklebust } 24663a505845STrond Myklebust 24671a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 24681c3c07e9STrond Myklebust { 24691c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24701a81bb8aSTrond Myklebust struct rb_node *n; 24711c3c07e9STrond Myklebust struct nfs_access_entry *entry; 24721c3c07e9STrond Myklebust 24731c3c07e9STrond Myklebust /* Unhook entries from the cache */ 24741c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 24751c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 24761c3c07e9STrond Myklebust rb_erase(n, root_node); 24771a81bb8aSTrond Myklebust list_move(&entry->lru, head); 24781c3c07e9STrond Myklebust } 24791c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 24801c3c07e9STrond Myklebust } 24811c3c07e9STrond Myklebust 24821c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 24831c3c07e9STrond Myklebust { 24841a81bb8aSTrond Myklebust LIST_HEAD(head); 24851a81bb8aSTrond Myklebust 24861a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 24871a81bb8aSTrond Myklebust return; 2488cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2489cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24901a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2491cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2492cfcea3e8STrond Myklebust 24931c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24941a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 24951a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 24961a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24971a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24981c3c07e9STrond Myklebust } 24991c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 25001c3c07e9STrond Myklebust 2501b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 25021c3c07e9STrond Myklebust { 25031c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 25041c3c07e9STrond Myklebust 25051c3c07e9STrond Myklebust while (n != NULL) { 2506b68572e0SNeilBrown struct nfs_access_entry *entry = 2507b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2508b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 25091c3c07e9STrond Myklebust 2510b68572e0SNeilBrown if (cmp < 0) 25111c3c07e9STrond Myklebust n = n->rb_left; 2512b68572e0SNeilBrown else if (cmp > 0) 25131c3c07e9STrond Myklebust n = n->rb_right; 25141c3c07e9STrond Myklebust else 25151c3c07e9STrond Myklebust return entry; 25161c3c07e9STrond Myklebust } 25171c3c07e9STrond Myklebust return NULL; 25181c3c07e9STrond Myklebust } 25191c3c07e9STrond Myklebust 2520d2ae4f8bSFrank 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) 25211da177e4SLinus Torvalds { 252255296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 25231c3c07e9STrond Myklebust struct nfs_access_entry *cache; 252457b69181STrond Myklebust bool retry = true; 252557b69181STrond Myklebust int err; 25261da177e4SLinus Torvalds 25271c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 252857b69181STrond Myklebust for(;;) { 25291c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 25301c3c07e9STrond Myklebust goto out_zap; 25311c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 253257b69181STrond Myklebust err = -ENOENT; 25331c3c07e9STrond Myklebust if (cache == NULL) 25341c3c07e9STrond Myklebust goto out; 253557b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 253621c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 253757b69181STrond Myklebust break; 25385c965db8STrond Myklebust if (!retry) 25395c965db8STrond Myklebust break; 254057b69181STrond Myklebust err = -ECHILD; 254157b69181STrond Myklebust if (!may_block) 254257b69181STrond Myklebust goto out; 254357b69181STrond Myklebust spin_unlock(&inode->i_lock); 254457b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 254557b69181STrond Myklebust if (err) 254657b69181STrond Myklebust return err; 254757b69181STrond Myklebust spin_lock(&inode->i_lock); 254857b69181STrond Myklebust retry = false; 254957b69181STrond Myklebust } 25501c3c07e9STrond Myklebust res->cred = cache->cred; 25511c3c07e9STrond Myklebust res->mask = cache->mask; 2552cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 25531c3c07e9STrond Myklebust err = 0; 25541c3c07e9STrond Myklebust out: 25551c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25561c3c07e9STrond Myklebust return err; 25571c3c07e9STrond Myklebust out_zap: 25581a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 25591a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 25601c3c07e9STrond Myklebust return -ENOENT; 25611c3c07e9STrond Myklebust } 25621c3c07e9STrond Myklebust 2563b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2564f682a398SNeilBrown { 2565f682a398SNeilBrown /* Only check the most recently returned cache entry, 2566f682a398SNeilBrown * but do it without locking. 2567f682a398SNeilBrown */ 2568f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2569f682a398SNeilBrown struct nfs_access_entry *cache; 2570f682a398SNeilBrown int err = -ECHILD; 2571f682a398SNeilBrown struct list_head *lh; 2572f682a398SNeilBrown 2573f682a398SNeilBrown rcu_read_lock(); 2574f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2575f682a398SNeilBrown goto out; 25769f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2577f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2578f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 25799a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2580f682a398SNeilBrown cache = NULL; 2581f682a398SNeilBrown if (cache == NULL) 2582f682a398SNeilBrown goto out; 258321c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2584f682a398SNeilBrown goto out; 2585f682a398SNeilBrown res->cred = cache->cred; 2586f682a398SNeilBrown res->mask = cache->mask; 258721c3ba7eSTrond Myklebust err = 0; 2588f682a398SNeilBrown out: 2589f682a398SNeilBrown rcu_read_unlock(); 2590f682a398SNeilBrown return err; 2591f682a398SNeilBrown } 2592f682a398SNeilBrown 2593d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct 2594d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block) 2595d2ae4f8bSFrank van der Linden { 2596d2ae4f8bSFrank van der Linden int status; 2597d2ae4f8bSFrank van der Linden 2598d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_rcu(inode, cred, res); 2599d2ae4f8bSFrank van der Linden if (status != 0) 2600d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_locked(inode, cred, res, 2601d2ae4f8bSFrank van der Linden may_block); 2602d2ae4f8bSFrank van der Linden 2603d2ae4f8bSFrank van der Linden return status; 2604d2ae4f8bSFrank van der Linden } 2605d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2606d2ae4f8bSFrank van der Linden 26071c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 26081c3c07e9STrond Myklebust { 2609cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2610cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 26111c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 26121c3c07e9STrond Myklebust struct rb_node *parent = NULL; 26131c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2614b68572e0SNeilBrown int cmp; 26151c3c07e9STrond Myklebust 26161c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 26171c3c07e9STrond Myklebust while (*p != NULL) { 26181c3c07e9STrond Myklebust parent = *p; 26191c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2620b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 26211c3c07e9STrond Myklebust 2622b68572e0SNeilBrown if (cmp < 0) 26231c3c07e9STrond Myklebust p = &parent->rb_left; 2624b68572e0SNeilBrown else if (cmp > 0) 26251c3c07e9STrond Myklebust p = &parent->rb_right; 26261c3c07e9STrond Myklebust else 26271c3c07e9STrond Myklebust goto found; 26281c3c07e9STrond Myklebust } 26291c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 26301c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2631cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 26321c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 26331c3c07e9STrond Myklebust return; 26341c3c07e9STrond Myklebust found: 26351c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2636cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2637cfcea3e8STrond Myklebust list_del(&entry->lru); 26381c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 26391c3c07e9STrond Myklebust nfs_access_free_entry(entry); 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds 26426168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 26431da177e4SLinus Torvalds { 26441c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 26451c3c07e9STrond Myklebust if (cache == NULL) 26461c3c07e9STrond Myklebust return; 26471c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2648b68572e0SNeilBrown cache->cred = get_cred(set->cred); 26491da177e4SLinus Torvalds cache->mask = set->mask; 26501c3c07e9STrond Myklebust 2651f682a398SNeilBrown /* The above field assignments must be visible 2652f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2653f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2654f682a398SNeilBrown */ 2655f682a398SNeilBrown smp_wmb(); 26561c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2657cfcea3e8STrond Myklebust 2658cfcea3e8STrond Myklebust /* Update accounting */ 26594e857c58SPeter Zijlstra smp_mb__before_atomic(); 2660cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 26614e857c58SPeter Zijlstra smp_mb__after_atomic(); 2662cfcea3e8STrond Myklebust 2663cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 26641a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2665cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 26661a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 26671a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 26681a81bb8aSTrond Myklebust &nfs_access_lru_list); 2669cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2670cfcea3e8STrond Myklebust } 26713a505845STrond Myklebust nfs_access_cache_enforce_limit(); 26721da177e4SLinus Torvalds } 26736168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 26746168f62cSWeston Andros Adamson 26753c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 26763c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26773c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 26783c181827SAnna Schumaker NFS_ACCESS_DELETE) 26793c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26803c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2681ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 26823c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 26833c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 268415d4b73aSTrond Myklebust static int 2685ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 268615d4b73aSTrond Myklebust { 268715d4b73aSTrond Myklebust int mask = 0; 268815d4b73aSTrond Myklebust 268915d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 269015d4b73aSTrond Myklebust mask |= MAY_READ; 2691ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2692ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 269315d4b73aSTrond Myklebust mask |= MAY_WRITE; 2694ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 269515d4b73aSTrond Myklebust mask |= MAY_EXEC; 2696ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2697ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2698ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2699ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 270015d4b73aSTrond Myklebust mask |= MAY_EXEC; 2701ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2702ecbb903cSTrond Myklebust mask |= MAY_WRITE; 270315d4b73aSTrond Myklebust return mask; 270415d4b73aSTrond Myklebust } 270515d4b73aSTrond Myklebust 27066168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 27076168f62cSWeston Andros Adamson { 2708bd8b2441STrond Myklebust entry->mask = access_result; 27096168f62cSWeston Andros Adamson } 27106168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 27111da177e4SLinus Torvalds 2712b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 27131da177e4SLinus Torvalds { 27141da177e4SLinus Torvalds struct nfs_access_entry cache; 271557b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2716e8194b7dSTrond Myklebust int cache_mask = -1; 27171da177e4SLinus Torvalds int status; 27181da177e4SLinus Torvalds 2719f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2720f4ce1299STrond Myklebust 272157b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 27221da177e4SLinus Torvalds if (status == 0) 2723f4ce1299STrond Myklebust goto out_cached; 27241da177e4SLinus Torvalds 2725f3324a2aSNeilBrown status = -ECHILD; 272657b69181STrond Myklebust if (!may_block) 2727f3324a2aSNeilBrown goto out; 2728f3324a2aSNeilBrown 27291750d929SAnna Schumaker /* 27301750d929SAnna Schumaker * Determine which access bits we want to ask for... 27311750d929SAnna Schumaker */ 27321750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 273372832a24SFrank van der Linden if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 273472832a24SFrank van der Linden cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 273572832a24SFrank van der Linden NFS_ACCESS_XALIST; 273672832a24SFrank van der Linden } 27371750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 27381750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 27391750d929SAnna Schumaker else 27401750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 27411da177e4SLinus Torvalds cache.cred = cred; 27421da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2743a71ee337SSuresh Jayaraman if (status != 0) { 2744a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2745a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 274693ce4af7STrond Myklebust nfs_set_inode_stale(inode); 274793ce4af7STrond Myklebust else 274893ce4af7STrond Myklebust nfs_zap_caches(inode); 2749a71ee337SSuresh Jayaraman } 2750f4ce1299STrond Myklebust goto out; 2751a71ee337SSuresh Jayaraman } 27521da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2753f4ce1299STrond Myklebust out_cached: 2754ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2755bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2756f4ce1299STrond Myklebust status = -EACCES; 27571da177e4SLinus Torvalds out: 2758e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2759f4ce1299STrond Myklebust return status; 27601da177e4SLinus Torvalds } 27611da177e4SLinus Torvalds 2762af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2763af22f94aSTrond Myklebust { 2764af22f94aSTrond Myklebust int mask = 0; 2765af22f94aSTrond Myklebust 2766f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2767f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2768f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2769f8d9a897SWeston Andros Adamson } else { 27708a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2771af22f94aSTrond Myklebust mask |= MAY_READ; 27728a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2773af22f94aSTrond Myklebust mask |= MAY_WRITE; 2774f8d9a897SWeston Andros Adamson } 2775f8d9a897SWeston Andros Adamson 2776af22f94aSTrond Myklebust return mask; 2777af22f94aSTrond Myklebust } 2778af22f94aSTrond Myklebust 2779b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2780af22f94aSTrond Myklebust { 2781af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2782af22f94aSTrond Myklebust } 278389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2784af22f94aSTrond Myklebust 27855c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 27865c5fc09aSTrond Myklebust { 27875c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 278821c3ba7eSTrond Myklebust int ret = 0; 27895c5fc09aSTrond Myklebust 27903825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 27913825827eSTrond Myklebust return 0; 2792cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 27935c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 279421c3ba7eSTrond Myklebust return -ECHILD; 279521c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 279621c3ba7eSTrond Myklebust } 27975c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 27985c5fc09aSTrond Myklebust ret = -EACCES; 27995c5fc09aSTrond Myklebust return ret; 28005c5fc09aSTrond Myklebust } 28015c5fc09aSTrond Myklebust 280210556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 28031da177e4SLinus Torvalds { 2804b68572e0SNeilBrown const struct cred *cred = current_cred(); 28051da177e4SLinus Torvalds int res = 0; 28061da177e4SLinus Torvalds 280791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 280891d5b470SChuck Lever 2809e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 28101da177e4SLinus Torvalds goto out; 28111da177e4SLinus Torvalds /* Is this sys_access() ? */ 28129cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 28131da177e4SLinus Torvalds goto force_lookup; 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 28161da177e4SLinus Torvalds case S_IFLNK: 28171da177e4SLinus Torvalds goto out; 28181da177e4SLinus Torvalds case S_IFREG: 2819762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2820762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2821762674f8STrond Myklebust return 0; 28221da177e4SLinus Torvalds break; 28231da177e4SLinus Torvalds case S_IFDIR: 28241da177e4SLinus Torvalds /* 28251da177e4SLinus Torvalds * Optimize away all write operations, since the server 28261da177e4SLinus Torvalds * will check permissions when we perform the op. 28271da177e4SLinus Torvalds */ 28281da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 28291da177e4SLinus Torvalds goto out; 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds 28321da177e4SLinus Torvalds force_lookup: 28331da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 28341da177e4SLinus Torvalds goto out_notsup; 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 28371da177e4SLinus Torvalds out: 28385c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 28395c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2840f696a365SMiklos Szeredi 28411e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 28421e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 28431da177e4SLinus Torvalds return res; 28441da177e4SLinus Torvalds out_notsup: 2845d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2846d51ac1a8SNeilBrown return -ECHILD; 2847d51ac1a8SNeilBrown 28481da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 28491da177e4SLinus Torvalds if (res == 0) 28502830ba7fSAl Viro res = generic_permission(inode, mask); 28511e7cb3dcSChuck Lever goto out; 28521da177e4SLinus Torvalds } 2853ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds /* 28561da177e4SLinus Torvalds * Local variables: 28571da177e4SLinus Torvalds * version-control: t 28581da177e4SLinus Torvalds * kept-new-versions: 5 28591da177e4SLinus Torvalds * End: 28601da177e4SLinus Torvalds */ 2861