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 { 14188b8e133SChuck Lever int size; 142d1bacf9eSBryan Schumaker int eof_index; 143d1bacf9eSBryan Schumaker u64 last_cookie; 1445601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[]; 145d1bacf9eSBryan Schumaker }; 146d1bacf9eSBryan Schumaker 1471da177e4SLinus Torvalds typedef struct { 1481da177e4SLinus Torvalds struct file *file; 1491da177e4SLinus Torvalds struct page *page; 15023db8620SAl Viro struct dir_context *ctx; 1511da177e4SLinus Torvalds unsigned long page_index; 152f0dd2136STrond Myklebust u64 *dir_cookie; 1530aded708STrond Myklebust u64 last_cookie; 154f0dd2136STrond Myklebust loff_t current_index; 15559e356a9STrond Myklebust loff_t prev_index; 156d1bacf9eSBryan Schumaker 157a1147b82STrond Myklebust unsigned long dir_verifier; 1581f4eab7eSNeil Brown unsigned long timestamp; 1594704f0e2STrond Myklebust unsigned long gencount; 160d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 161a7a3b1e9SBenjamin Coddington bool plus; 162a7a3b1e9SBenjamin Coddington bool eof; 1631da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1641da177e4SLinus Torvalds 1654b310319STrond Myklebust static 1664b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 1674b310319STrond Myklebust { 1684b310319STrond Myklebust struct nfs_cache_array *array; 1694b310319STrond Myklebust 1704b310319STrond Myklebust array = kmap_atomic(page); 1714b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1724b310319STrond Myklebust array->eof_index = -1; 1734b310319STrond Myklebust kunmap_atomic(array); 1744b310319STrond Myklebust } 1754b310319STrond Myklebust 176d1bacf9eSBryan Schumaker /* 177d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 178d1bacf9eSBryan Schumaker */ 179d1bacf9eSBryan Schumaker static 18011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 181d1bacf9eSBryan Schumaker { 18211de3b11STrond Myklebust struct nfs_cache_array *array; 183d1bacf9eSBryan Schumaker int i; 1848cd51a0cSTrond Myklebust 1852b86ce2dSCong Wang array = kmap_atomic(page); 186d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 187d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1884b310319STrond Myklebust array->size = 0; 1892b86ce2dSCong Wang kunmap_atomic(array); 190d1bacf9eSBryan Schumaker } 191d1bacf9eSBryan Schumaker 192d1bacf9eSBryan Schumaker /* 193d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 194d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 195d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 196d1bacf9eSBryan Schumaker */ 197d1bacf9eSBryan Schumaker static 1984a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 199d1bacf9eSBryan Schumaker { 200d1bacf9eSBryan Schumaker string->len = len; 2013803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2024a201d6eSTrond Myklebust if (string->name == NULL) 2034a201d6eSTrond Myklebust return -ENOMEM; 20404e4bd1cSCatalin Marinas /* 20504e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 20604e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 20704e4bd1cSCatalin Marinas */ 20804e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2098387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2104a201d6eSTrond Myklebust return 0; 211d1bacf9eSBryan Schumaker } 212d1bacf9eSBryan Schumaker 213d1bacf9eSBryan Schumaker static 214d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 215d1bacf9eSBryan Schumaker { 2160795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2174a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2184a201d6eSTrond Myklebust int ret; 2194a201d6eSTrond Myklebust 2204a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2213020093fSTrond Myklebust 2223020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2233020093fSTrond Myklebust ret = -ENOSPC; 2243020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2253020093fSTrond Myklebust goto out; 2263020093fSTrond Myklebust 2274a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2284a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2290b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2304a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2314a201d6eSTrond Myklebust if (ret) 2324a201d6eSTrond Myklebust goto out; 233d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2348cd51a0cSTrond Myklebust array->size++; 23547c716cbSTrond Myklebust if (entry->eof != 0) 236d1bacf9eSBryan Schumaker array->eof_index = array->size; 2374a201d6eSTrond Myklebust out: 2380795bf83SFabian Frederick kunmap(page); 2394a201d6eSTrond Myklebust return ret; 240d1bacf9eSBryan Schumaker } 241d1bacf9eSBryan Schumaker 24259e356a9STrond Myklebust static inline 24359e356a9STrond Myklebust int is_32bit_api(void) 24459e356a9STrond Myklebust { 24559e356a9STrond Myklebust #ifdef CONFIG_COMPAT 24659e356a9STrond Myklebust return in_compat_syscall(); 24759e356a9STrond Myklebust #else 24859e356a9STrond Myklebust return (BITS_PER_LONG == 32); 24959e356a9STrond Myklebust #endif 25059e356a9STrond Myklebust } 25159e356a9STrond Myklebust 25259e356a9STrond Myklebust static 25359e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp) 25459e356a9STrond Myklebust { 25559e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) || 25659e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 25759e356a9STrond Myklebust return false; 25859e356a9STrond Myklebust return true; 25959e356a9STrond Myklebust } 26059e356a9STrond Myklebust 261d1bacf9eSBryan Schumaker static 262d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 263d1bacf9eSBryan Schumaker { 26423db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 265d1bacf9eSBryan Schumaker unsigned int index; 266d1bacf9eSBryan Schumaker 267d1bacf9eSBryan Schumaker if (diff < 0) 268d1bacf9eSBryan Schumaker goto out_eof; 269d1bacf9eSBryan Schumaker if (diff >= array->size) { 2708cd51a0cSTrond Myklebust if (array->eof_index >= 0) 271d1bacf9eSBryan Schumaker goto out_eof; 272d1bacf9eSBryan Schumaker return -EAGAIN; 273d1bacf9eSBryan Schumaker } 274d1bacf9eSBryan Schumaker 275d1bacf9eSBryan Schumaker index = (unsigned int)diff; 276d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 277d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 278d1bacf9eSBryan Schumaker return 0; 279d1bacf9eSBryan Schumaker out_eof: 2806089dd0dSThomas Meyer desc->eof = true; 281d1bacf9eSBryan Schumaker return -EBADCOOKIE; 282d1bacf9eSBryan Schumaker } 283d1bacf9eSBryan Schumaker 2844db72b40SJeff Layton static bool 2854db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2864db72b40SJeff Layton { 2874db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2884db72b40SJeff Layton return false; 2894db72b40SJeff Layton smp_rmb(); 2904db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2914db72b40SJeff Layton } 2924db72b40SJeff Layton 293d1bacf9eSBryan Schumaker static 294d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 295d1bacf9eSBryan Schumaker { 296d1bacf9eSBryan Schumaker int i; 2978ef2ce3eSBryan Schumaker loff_t new_pos; 298d1bacf9eSBryan Schumaker int status = -EAGAIN; 299d1bacf9eSBryan Schumaker 300d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3018cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 302496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 3030c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 3040c030806STrond Myklebust 3058ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 3064db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 3074db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 3080c030806STrond Myklebust ctx->duped = 0; 3090c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 31059e356a9STrond Myklebust } else if (new_pos < desc->prev_index) { 3110c030806STrond Myklebust if (ctx->duped > 0 3120c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 3130c030806STrond Myklebust if (printk_ratelimit()) { 3146de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 3150c030806STrond Myklebust "Please contact your server vendor. " 3169581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 3179581a4aeSJeff Layton desc->file, array->array[i].string.len, 3189581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3190c030806STrond Myklebust } 3200c030806STrond Myklebust status = -ELOOP; 3210c030806STrond Myklebust goto out; 3220c030806STrond Myklebust } 3238ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3240c030806STrond Myklebust ctx->duped = -1; 3258ef2ce3eSBryan Schumaker } 32659e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file)) 32759e356a9STrond Myklebust desc->ctx->pos = *desc->dir_cookie; 32859e356a9STrond Myklebust else 32923db8620SAl Viro desc->ctx->pos = new_pos; 33059e356a9STrond Myklebust desc->prev_index = new_pos; 3318cd51a0cSTrond Myklebust desc->cache_entry_index = i; 33247c716cbSTrond Myklebust return 0; 3338cd51a0cSTrond Myklebust } 3348cd51a0cSTrond Myklebust } 33547c716cbSTrond Myklebust if (array->eof_index >= 0) { 336d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 33718fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 3386089dd0dSThomas Meyer desc->eof = true; 339d1bacf9eSBryan Schumaker } 3400c030806STrond Myklebust out: 341d1bacf9eSBryan Schumaker return status; 342d1bacf9eSBryan Schumaker } 343d1bacf9eSBryan Schumaker 344d1bacf9eSBryan Schumaker static 345d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 346d1bacf9eSBryan Schumaker { 347d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 34847c716cbSTrond Myklebust int status; 349d1bacf9eSBryan Schumaker 3500795bf83SFabian Frederick array = kmap(desc->page); 351d1bacf9eSBryan Schumaker 352d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 353d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 354d1bacf9eSBryan Schumaker else 355d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 356d1bacf9eSBryan Schumaker 35747c716cbSTrond Myklebust if (status == -EAGAIN) { 3580aded708STrond Myklebust desc->last_cookie = array->last_cookie; 359e47c085aSTrond Myklebust desc->current_index += array->size; 36047c716cbSTrond Myklebust desc->page_index++; 36147c716cbSTrond Myklebust } 3620795bf83SFabian Frederick kunmap(desc->page); 363d1bacf9eSBryan Schumaker return status; 364d1bacf9eSBryan Schumaker } 365d1bacf9eSBryan Schumaker 366d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 367d1bacf9eSBryan Schumaker static 36856e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 369d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 370d1bacf9eSBryan Schumaker { 371480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 372684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3734704f0e2STrond Myklebust unsigned long timestamp, gencount; 3741da177e4SLinus Torvalds int error; 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds again: 3771da177e4SLinus Torvalds timestamp = jiffies; 3784704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 379a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 380be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3811da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3821da177e4SLinus Torvalds if (error < 0) { 3831da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3841da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3851da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3863a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 387a7a3b1e9SBenjamin Coddington desc->plus = false; 3881da177e4SLinus Torvalds goto again; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds goto error; 3911da177e4SLinus Torvalds } 3921f4eab7eSNeil Brown desc->timestamp = timestamp; 3934704f0e2STrond Myklebust desc->gencount = gencount; 394d1bacf9eSBryan Schumaker error: 395d1bacf9eSBryan Schumaker return error; 396d1bacf9eSBryan Schumaker } 397d1bacf9eSBryan Schumaker 398573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 399573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 400d1bacf9eSBryan Schumaker { 40159e356a9STrond Myklebust struct inode *inode = file_inode(desc->file); 402573c4e1eSChuck Lever int error; 403d1bacf9eSBryan Schumaker 40459e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 405573c4e1eSChuck Lever if (error) 406573c4e1eSChuck Lever return error; 407d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 408d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 409d1bacf9eSBryan Schumaker return 0; 410d1bacf9eSBryan Schumaker } 411d1bacf9eSBryan Schumaker 412fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 413fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 414fa923369STrond Myklebust */ 415d39ab9deSBryan Schumaker static 416d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 417d39ab9deSBryan Schumaker { 418d8fdb47fSTrond Myklebust struct inode *inode; 419fa923369STrond Myklebust struct nfs_inode *nfsi; 420fa923369STrond Myklebust 4212b0143b5SDavid Howells if (d_really_is_negative(dentry)) 4222b0143b5SDavid Howells return 0; 423fa923369STrond Myklebust 424d8fdb47fSTrond Myklebust inode = d_inode(dentry); 425d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 426d8fdb47fSTrond Myklebust return 0; 427d8fdb47fSTrond Myklebust 428d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4297dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 430d39ab9deSBryan Schumaker return 0; 4317dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4327dc72d5fSTrond Myklebust return 0; 4337dc72d5fSTrond Myklebust return 1; 434d39ab9deSBryan Schumaker } 435d39ab9deSBryan Schumaker 436d39ab9deSBryan Schumaker static 43723db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 438d69ee9b8STrond Myklebust { 439d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 440d69ee9b8STrond Myklebust return false; 441d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 442d69ee9b8STrond Myklebust return true; 44323db8620SAl Viro if (ctx->pos == 0) 444d69ee9b8STrond Myklebust return true; 445d69ee9b8STrond Myklebust return false; 446d69ee9b8STrond Myklebust } 447d69ee9b8STrond Myklebust 448d69ee9b8STrond Myklebust /* 44963519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 45063519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 451d69ee9b8STrond Myklebust * directory. 452d69ee9b8STrond Myklebust */ 453d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 454d69ee9b8STrond Myklebust { 45563519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 45663519fbcSTrond Myklebust 45763519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 45863519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 45963519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 460d69ee9b8STrond Myklebust } 461d69ee9b8STrond Myklebust 462311324adSTrond Myklebust /* 463311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 464311324adSTrond Myklebust * 465311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 466311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 467311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 468311324adSTrond Myklebust * cache flush. 469311324adSTrond Myklebust */ 470311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 471311324adSTrond Myklebust { 47263519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 47363519fbcSTrond Myklebust 47463519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 47563519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 47663519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 477227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 478227823d2SDai Ngo nfsi->page_index + 1, -1); 479311324adSTrond Myklebust } 480311324adSTrond Myklebust } 481311324adSTrond Myklebust 482d69ee9b8STrond Myklebust static 483a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 484a1147b82STrond Myklebust unsigned long dir_verifier) 485d39ab9deSBryan Schumaker { 48626fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4879ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4884a201d6eSTrond Myklebust struct dentry *dentry; 4894a201d6eSTrond Myklebust struct dentry *alias; 490d39ab9deSBryan Schumaker struct inode *inode; 491aa9c2669SDavid Quigley int status; 492d39ab9deSBryan Schumaker 493fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 494fa923369STrond Myklebust return; 4956c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4966c441c25STrond Myklebust return; 49778d04af4STrond Myklebust if (filename.len == 0) 49878d04af4STrond Myklebust return; 49978d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 50078d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 50178d04af4STrond Myklebust return; 50278d04af4STrond Myklebust /* ...or '/' */ 50378d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 50478d04af4STrond Myklebust return; 5054a201d6eSTrond Myklebust if (filename.name[0] == '.') { 5064a201d6eSTrond Myklebust if (filename.len == 1) 5074a201d6eSTrond Myklebust return; 5084a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 5094a201d6eSTrond Myklebust return; 5104a201d6eSTrond Myklebust } 5118387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 512d39ab9deSBryan Schumaker 5134a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 5149ac3d3e8SAl Viro again: 5159ac3d3e8SAl Viro if (!dentry) { 5169ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 5179ac3d3e8SAl Viro if (IS_ERR(dentry)) 5189ac3d3e8SAl Viro return; 5199ac3d3e8SAl Viro } 5209ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 5216c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 5226c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 5236c441c25STrond Myklebust &entry->fattr->fsid)) 5246c441c25STrond Myklebust goto out; 525d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5267dc72d5fSTrond Myklebust if (!entry->fh->size) 5277dc72d5fSTrond Myklebust goto out; 528a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 5292b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 530aa9c2669SDavid Quigley if (!status) 5312b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 532d39ab9deSBryan Schumaker goto out; 533d39ab9deSBryan Schumaker } else { 5345542aa2fSEric W. Biederman d_invalidate(dentry); 535d39ab9deSBryan Schumaker dput(dentry); 5369ac3d3e8SAl Viro dentry = NULL; 5379ac3d3e8SAl Viro goto again; 538d39ab9deSBryan Schumaker } 539d39ab9deSBryan Schumaker } 5407dc72d5fSTrond Myklebust if (!entry->fh->size) { 5417dc72d5fSTrond Myklebust d_lookup_done(dentry); 5427dc72d5fSTrond Myklebust goto out; 5437dc72d5fSTrond Myklebust } 544d39ab9deSBryan Schumaker 5451775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 54641d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5479ac3d3e8SAl Viro d_lookup_done(dentry); 5489ac3d3e8SAl Viro if (alias) { 549d39ab9deSBryan Schumaker if (IS_ERR(alias)) 550d39ab9deSBryan Schumaker goto out; 5519ac3d3e8SAl Viro dput(dentry); 5529ac3d3e8SAl Viro dentry = alias; 5539ac3d3e8SAl Viro } 554a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 555d39ab9deSBryan Schumaker out: 556d39ab9deSBryan Schumaker dput(dentry); 557d39ab9deSBryan Schumaker } 558d39ab9deSBryan Schumaker 559d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 560d1bacf9eSBryan Schumaker static 5618cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5626650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 563d1bacf9eSBryan Schumaker { 564babddc72SBryan Schumaker struct xdr_stream stream; 565f7da7a12SBenny Halevy struct xdr_buf buf; 5666650239aSTrond Myklebust struct page *scratch; 56799424380SBryan Schumaker struct nfs_cache_array *array; 5685c346854STrond Myklebust unsigned int count = 0; 5695c346854STrond Myklebust int status; 570babddc72SBryan Schumaker 5716650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5726650239aSTrond Myklebust if (scratch == NULL) 5736650239aSTrond Myklebust return -ENOMEM; 574babddc72SBryan Schumaker 575ce85cfbeSBenjamin Coddington if (buflen == 0) 576ce85cfbeSBenjamin Coddington goto out_nopages; 577ce85cfbeSBenjamin Coddington 578f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5796650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 58099424380SBryan Schumaker 58199424380SBryan Schumaker do { 582*d33030e2SJeffrey Mitchell if (entry->label) 583*d33030e2SJeffrey Mitchell entry->label->len = NFS4_MAXLABELLEN; 584*d33030e2SJeffrey Mitchell 58599424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5868cd51a0cSTrond Myklebust if (status != 0) { 5878cd51a0cSTrond Myklebust if (status == -EAGAIN) 5888cd51a0cSTrond Myklebust status = 0; 58999424380SBryan Schumaker break; 5908cd51a0cSTrond Myklebust } 59199424380SBryan Schumaker 5925c346854STrond Myklebust count++; 5935c346854STrond Myklebust 594a7a3b1e9SBenjamin Coddington if (desc->plus) 595a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 596a1147b82STrond Myklebust desc->dir_verifier); 5978cd51a0cSTrond Myklebust 598db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 5998cd51a0cSTrond Myklebust if (status != 0) 6008cd51a0cSTrond Myklebust break; 60199424380SBryan Schumaker } while (!entry->eof); 60299424380SBryan Schumaker 603ce85cfbeSBenjamin Coddington out_nopages: 60447c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 605db531db9SMax Kellermann array = kmap(page); 6068cd51a0cSTrond Myklebust array->eof_index = array->size; 60799424380SBryan Schumaker status = 0; 608db531db9SMax Kellermann kunmap(page); 60956e4ebf8SBryan Schumaker } 6106650239aSTrond Myklebust 6116650239aSTrond Myklebust put_page(scratch); 6128cd51a0cSTrond Myklebust return status; 6138cd51a0cSTrond Myklebust } 61456e4ebf8SBryan Schumaker 61556e4ebf8SBryan Schumaker static 616c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 61756e4ebf8SBryan Schumaker { 61856e4ebf8SBryan Schumaker unsigned int i; 61956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 62056e4ebf8SBryan Schumaker put_page(pages[i]); 62156e4ebf8SBryan Schumaker } 62256e4ebf8SBryan Schumaker 62356e4ebf8SBryan Schumaker /* 624bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 625bf211ca1Szhangliguang * to nfs_readdir_free_pages() 62656e4ebf8SBryan Schumaker */ 62756e4ebf8SBryan Schumaker static 628c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 62956e4ebf8SBryan Schumaker { 63056e4ebf8SBryan Schumaker unsigned int i; 63156e4ebf8SBryan Schumaker 63256e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 63356e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 63456e4ebf8SBryan Schumaker if (page == NULL) 63556e4ebf8SBryan Schumaker goto out_freepages; 63656e4ebf8SBryan Schumaker pages[i] = page; 63756e4ebf8SBryan Schumaker } 6386650239aSTrond Myklebust return 0; 63956e4ebf8SBryan Schumaker 64056e4ebf8SBryan Schumaker out_freepages: 641c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6426650239aSTrond Myklebust return -ENOMEM; 643d1bacf9eSBryan Schumaker } 644d1bacf9eSBryan Schumaker 645d1bacf9eSBryan Schumaker static 646d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 647d1bacf9eSBryan Schumaker { 64856e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 649d1bacf9eSBryan Schumaker struct nfs_entry entry; 650d1bacf9eSBryan Schumaker struct file *file = desc->file; 651d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6528cd51a0cSTrond Myklebust int status = -ENOMEM; 65356e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 654d1bacf9eSBryan Schumaker 6554b310319STrond Myklebust nfs_readdir_init_array(page); 6564b310319STrond Myklebust 657d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6580aded708STrond Myklebust entry.cookie = desc->last_cookie; 659d1bacf9eSBryan Schumaker entry.eof = 0; 660d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 661d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 662573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 663d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 664d1bacf9eSBryan Schumaker goto out; 665d1bacf9eSBryan Schumaker 66614c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 66714c43f76SDavid Quigley if (IS_ERR(entry.label)) { 66814c43f76SDavid Quigley status = PTR_ERR(entry.label); 66914c43f76SDavid Quigley goto out; 67014c43f76SDavid Quigley } 67114c43f76SDavid Quigley 6720795bf83SFabian Frederick array = kmap(page); 673d1bacf9eSBryan Schumaker 674c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6756650239aSTrond Myklebust if (status < 0) 676d1bacf9eSBryan Schumaker goto out_release_array; 677d1bacf9eSBryan Schumaker do { 678ac396128STrond Myklebust unsigned int pglen; 67956e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 680babddc72SBryan Schumaker 681d1bacf9eSBryan Schumaker if (status < 0) 682d1bacf9eSBryan Schumaker break; 683ac396128STrond Myklebust pglen = status; 6846650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6858cd51a0cSTrond Myklebust if (status < 0) { 6868cd51a0cSTrond Myklebust if (status == -ENOSPC) 6878cd51a0cSTrond Myklebust status = 0; 6888cd51a0cSTrond Myklebust break; 6898cd51a0cSTrond Myklebust } 6908cd51a0cSTrond Myklebust } while (array->eof_index < 0); 691d1bacf9eSBryan Schumaker 692c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 693d1bacf9eSBryan Schumaker out_release_array: 6940795bf83SFabian Frederick kunmap(page); 69514c43f76SDavid Quigley nfs4_label_free(entry.label); 696d1bacf9eSBryan Schumaker out: 697d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 698d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 699d1bacf9eSBryan Schumaker return status; 700d1bacf9eSBryan Schumaker } 701d1bacf9eSBryan Schumaker 702d1bacf9eSBryan Schumaker /* 703d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 704d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 705d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 706d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7071da177e4SLinus Torvalds */ 708d1bacf9eSBryan Schumaker static 709a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 710d1bacf9eSBryan Schumaker { 711a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 712496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7138cd51a0cSTrond Myklebust int ret; 714d1bacf9eSBryan Schumaker 7158cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7168cd51a0cSTrond Myklebust if (ret < 0) 717d1bacf9eSBryan Schumaker goto error; 718d1bacf9eSBryan Schumaker SetPageUptodate(page); 719d1bacf9eSBryan Schumaker 7202aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 721cd9ae2b6STrond Myklebust /* Should never happen */ 722cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 723cd9ae2b6STrond Myklebust } 7241da177e4SLinus Torvalds unlock_page(page); 7251da177e4SLinus Torvalds return 0; 7261da177e4SLinus Torvalds error: 7274b310319STrond Myklebust nfs_readdir_clear_array(page); 7281da177e4SLinus Torvalds unlock_page(page); 7298cd51a0cSTrond Myklebust return ret; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 732d1bacf9eSBryan Schumaker static 733d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7341da177e4SLinus Torvalds { 73509cbfeafSKirill A. Shutemov put_page(desc->page); 7361da177e4SLinus Torvalds desc->page = NULL; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 739d1bacf9eSBryan Schumaker static 740d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7411da177e4SLinus Torvalds { 742a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 743a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* 747d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 748114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7491da177e4SLinus Torvalds */ 750d1bacf9eSBryan Schumaker static 751114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 752d1bacf9eSBryan Schumaker { 753227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 754227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 755d1bacf9eSBryan Schumaker int res; 756d1bacf9eSBryan Schumaker 757d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 758d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 759d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 760114de382STrond Myklebust res = lock_page_killable(desc->page); 76147c716cbSTrond Myklebust if (res != 0) 762114de382STrond Myklebust goto error; 763114de382STrond Myklebust res = -EAGAIN; 764114de382STrond Myklebust if (desc->page->mapping != NULL) { 765114de382STrond Myklebust res = nfs_readdir_search_array(desc); 766227823d2SDai Ngo if (res == 0) { 767227823d2SDai Ngo nfsi->page_index = desc->page_index; 768114de382STrond Myklebust return 0; 769114de382STrond Myklebust } 770227823d2SDai Ngo } 771114de382STrond Myklebust unlock_page(desc->page); 772114de382STrond Myklebust error: 773d1bacf9eSBryan Schumaker cache_page_release(desc); 774d1bacf9eSBryan Schumaker return res; 775d1bacf9eSBryan Schumaker } 776d1bacf9eSBryan Schumaker 777d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7781da177e4SLinus Torvalds static inline 7791da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7801da177e4SLinus Torvalds { 7818cd51a0cSTrond Myklebust int res; 782d1bacf9eSBryan Schumaker 7830aded708STrond Myklebust if (desc->page_index == 0) { 7848cd51a0cSTrond Myklebust desc->current_index = 0; 78559e356a9STrond Myklebust desc->prev_index = 0; 7860aded708STrond Myklebust desc->last_cookie = 0; 7870aded708STrond Myklebust } 78847c716cbSTrond Myklebust do { 789114de382STrond Myklebust res = find_and_lock_cache_page(desc); 79047c716cbSTrond Myklebust } while (res == -EAGAIN); 7911da177e4SLinus Torvalds return res; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7961da177e4SLinus Torvalds */ 7971da177e4SLinus Torvalds static 79823db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds struct file *file = desc->file; 801d1bacf9eSBryan Schumaker int i = 0; 802d1bacf9eSBryan Schumaker int res = 0; 803d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 8048ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 8058ef2ce3eSBryan Schumaker 8060795bf83SFabian Frederick array = kmap(desc->page); 807d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 808ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8091da177e4SLinus Torvalds 810ece0b423STrond Myklebust ent = &array->array[i]; 81123db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 81223db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 8136089dd0dSThomas Meyer desc->eof = true; 8141da177e4SLinus Torvalds break; 815ece0b423STrond Myklebust } 816d1bacf9eSBryan Schumaker if (i < (array->size-1)) 817d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 818d1bacf9eSBryan Schumaker else 819d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 82059e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 82159e356a9STrond Myklebust desc->ctx->pos = *desc->dir_cookie; 82259e356a9STrond Myklebust else 82359e356a9STrond Myklebust desc->ctx->pos++; 8240c030806STrond Myklebust if (ctx->duped != 0) 8250c030806STrond Myklebust ctx->duped = 1; 8268cd51a0cSTrond Myklebust } 82747c716cbSTrond Myklebust if (array->eof_index >= 0) 8286089dd0dSThomas Meyer desc->eof = true; 829d1bacf9eSBryan Schumaker 8300795bf83SFabian Frederick kunmap(desc->page); 8311e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8321e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8331da177e4SLinus Torvalds return res; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* 8371da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8381da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8391da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8401da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8411da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8421da177e4SLinus Torvalds * 8431da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8441da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8451da177e4SLinus Torvalds * we should already have a complete representation of the 8461da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8471da177e4SLinus Torvalds */ 8481da177e4SLinus Torvalds static inline 84923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds struct page *page = NULL; 8521da177e4SLinus Torvalds int status; 853496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8540c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8551da177e4SLinus Torvalds 8561e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8571e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8601da177e4SLinus Torvalds if (!page) { 8611da177e4SLinus Torvalds status = -ENOMEM; 8621da177e4SLinus Torvalds goto out; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8657a8e1dc3STrond Myklebust desc->page_index = 0; 8660aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8677a8e1dc3STrond Myklebust desc->page = page; 8680c030806STrond Myklebust ctx->duped = 0; 8697a8e1dc3STrond Myklebust 87085f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 87185f8607eSTrond Myklebust if (status < 0) 872d1bacf9eSBryan Schumaker goto out_release; 873d1bacf9eSBryan Schumaker 87423db8620SAl Viro status = nfs_do_filldir(desc); 8751da177e4SLinus Torvalds 876114de382STrond Myklebust out_release: 877114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 878114de382STrond Myklebust cache_page_release(desc); 8791da177e4SLinus Torvalds out: 8801e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8813110ff80SHarvey Harrison __func__, status); 8821da177e4SLinus Torvalds return status; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 88500a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 88600a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 88700a92642SOlivier Galibert whole directory. 8881da177e4SLinus Torvalds */ 88923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8901da177e4SLinus Torvalds { 891be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8922b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 89323db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 89459e356a9STrond Myklebust nfs_readdir_descriptor_t my_desc = { 89559e356a9STrond Myklebust .file = file, 89659e356a9STrond Myklebust .ctx = ctx, 89759e356a9STrond Myklebust .dir_cookie = &dir_ctx->dir_cookie, 89859e356a9STrond Myklebust .plus = nfs_use_readdirplus(inode, ctx), 89959e356a9STrond Myklebust }, 90059e356a9STrond Myklebust *desc = &my_desc; 90107b5ce8eSScott Mayhew int res = 0; 9021da177e4SLinus Torvalds 9036de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9046de1472fSAl Viro file, (long long)ctx->pos); 90591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 90691d5b470SChuck Lever 9071da177e4SLinus Torvalds /* 90823db8620SAl Viro * ctx->pos points to the dirent entry number. 909f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 91000a92642SOlivier Galibert * to either find the entry with the appropriate number or 91100a92642SOlivier Galibert * revalidate the cookie. 9121da177e4SLinus Torvalds */ 91379f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 91423db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 915fccca7fcSTrond Myklebust if (res < 0) 916fccca7fcSTrond Myklebust goto out; 917fccca7fcSTrond Myklebust 91847c716cbSTrond Myklebust do { 9191da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 92000a92642SOlivier Galibert 9211da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 922ece0b423STrond Myklebust res = 0; 9231da177e4SLinus Torvalds /* This means either end of directory */ 9246089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 9251da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 92623db8620SAl Viro res = uncached_readdir(desc); 927ece0b423STrond Myklebust if (res == 0) 9281da177e4SLinus Torvalds continue; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds break; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9333a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9341da177e4SLinus Torvalds nfs_zap_caches(inode); 935baf57a09STrond Myklebust desc->page_index = 0; 936a7a3b1e9SBenjamin Coddington desc->plus = false; 937a7a3b1e9SBenjamin Coddington desc->eof = false; 9381da177e4SLinus Torvalds continue; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds if (res < 0) 9411da177e4SLinus Torvalds break; 9421da177e4SLinus Torvalds 94323db8620SAl Viro res = nfs_do_filldir(desc); 944114de382STrond Myklebust unlock_page(desc->page); 945114de382STrond Myklebust cache_page_release(desc); 946ece0b423STrond Myklebust if (res < 0) 9471da177e4SLinus Torvalds break; 94847c716cbSTrond Myklebust } while (!desc->eof); 949fccca7fcSTrond Myklebust out: 9501e7cb3dcSChuck Lever if (res > 0) 9511e7cb3dcSChuck Lever res = 0; 9526de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9531da177e4SLinus Torvalds return res; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 956965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 957f0dd2136STrond Myklebust { 958b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 959480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 960b84e06c5SChuck Lever 9616de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9626de1472fSAl Viro filp, offset, whence); 963b84e06c5SChuck Lever 964965c8e59SAndrew Morton switch (whence) { 965f0dd2136STrond Myklebust default: 966b2b1ff3dSTrond Myklebust return -EINVAL; 967b2b1ff3dSTrond Myklebust case SEEK_SET: 968b2b1ff3dSTrond Myklebust if (offset < 0) 969b2b1ff3dSTrond Myklebust return -EINVAL; 970b2b1ff3dSTrond Myklebust inode_lock(inode); 971b2b1ff3dSTrond Myklebust break; 972b2b1ff3dSTrond Myklebust case SEEK_CUR: 973b2b1ff3dSTrond Myklebust if (offset == 0) 974b2b1ff3dSTrond Myklebust return filp->f_pos; 975b2b1ff3dSTrond Myklebust inode_lock(inode); 976b2b1ff3dSTrond Myklebust offset += filp->f_pos; 977b2b1ff3dSTrond Myklebust if (offset < 0) { 978b2b1ff3dSTrond Myklebust inode_unlock(inode); 979b2b1ff3dSTrond Myklebust return -EINVAL; 980b2b1ff3dSTrond Myklebust } 981f0dd2136STrond Myklebust } 982f0dd2136STrond Myklebust if (offset != filp->f_pos) { 983f0dd2136STrond Myklebust filp->f_pos = offset; 98459e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 98559e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 98659e356a9STrond Myklebust else 987480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9888ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 989f0dd2136STrond Myklebust } 990b044f645SBenjamin Coddington inode_unlock(inode); 991f0dd2136STrond Myklebust return offset; 992f0dd2136STrond Myklebust } 993f0dd2136STrond Myklebust 9941da177e4SLinus Torvalds /* 9951da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9961da177e4SLinus Torvalds * is a dummy operation. 9971da177e4SLinus Torvalds */ 99802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 99902c24a82SJosef Bacik int datasync) 10001da177e4SLinus Torvalds { 10016de1472fSAl Viro struct inode *inode = file_inode(filp); 10027ea80859SChristoph Hellwig 10036de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 10041e7cb3dcSChuck Lever 10055955102cSAl Viro inode_lock(inode); 10066de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 10075955102cSAl Viro inode_unlock(inode); 10081da177e4SLinus Torvalds return 0; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 1011bfc69a45STrond Myklebust /** 1012bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1013302fad7bSTrond Myklebust * @dir: pointer to directory inode 1014bfc69a45STrond Myklebust * 1015bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1016bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1017bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1018bfc69a45STrond Myklebust * 1019efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1020efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1021efeda80dSTrond Myklebust * 1022bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1023bfc69a45STrond Myklebust */ 1024bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1025bfc69a45STrond Myklebust { 1026efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1027bfc69a45STrond Myklebust } 102889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1029bfc69a45STrond Myklebust 1030efeda80dSTrond Myklebust /** 1031efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1032efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1033efeda80dSTrond Myklebust * @verf: previously saved change attribute 1034efeda80dSTrond Myklebust * 1035efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1036efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1037efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1038efeda80dSTrond Myklebust */ 1039efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1040efeda80dSTrond Myklebust { 1041efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1042efeda80dSTrond Myklebust } 1043efeda80dSTrond Myklebust 1044efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1045efeda80dSTrond Myklebust { 1046efeda80dSTrond Myklebust *verf |= 1UL; 1047efeda80dSTrond Myklebust } 1048efeda80dSTrond Myklebust 1049efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1050efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1051efeda80dSTrond Myklebust { 1052efeda80dSTrond Myklebust *verf &= ~1UL; 1053efeda80dSTrond Myklebust } 1054efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1055efeda80dSTrond Myklebust 1056efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1057efeda80dSTrond Myklebust { 1058efeda80dSTrond Myklebust return verf & 1; 1059efeda80dSTrond Myklebust } 1060efeda80dSTrond Myklebust 1061efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1062efeda80dSTrond Myklebust { 1063efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1064efeda80dSTrond Myklebust } 1065efeda80dSTrond Myklebust 1066efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1067efeda80dSTrond Myklebust { 1068efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1069efeda80dSTrond Myklebust 1070efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1071efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1072efeda80dSTrond Myklebust goto out; 1073efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1074efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1075efeda80dSTrond Myklebust out: 1076efeda80dSTrond Myklebust dentry->d_time = verf; 1077efeda80dSTrond Myklebust } 1078efeda80dSTrond Myklebust 1079efeda80dSTrond Myklebust /** 1080efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1081efeda80dSTrond Myklebust * @dentry: pointer to dentry 1082efeda80dSTrond Myklebust * @verf: verifier to save 1083efeda80dSTrond Myklebust * 1084efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1085efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1086efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1087efeda80dSTrond Myklebust * look it up again after a directory change. 1088efeda80dSTrond Myklebust */ 1089efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1090efeda80dSTrond Myklebust { 1091efeda80dSTrond Myklebust 1092efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1093efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1094efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1095efeda80dSTrond Myklebust } 1096efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1097efeda80dSTrond Myklebust 1098efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1099efeda80dSTrond Myklebust /** 1100efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1101efeda80dSTrond Myklebust * @inode: pointer to inode 1102efeda80dSTrond Myklebust * 1103efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1104efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1105efeda80dSTrond Myklebust * while holding a delegation. 1106efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1107efeda80dSTrond Myklebust * returned or revoked. 1108efeda80dSTrond Myklebust */ 1109efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1110efeda80dSTrond Myklebust { 1111efeda80dSTrond Myklebust struct dentry *alias; 1112efeda80dSTrond Myklebust 1113efeda80dSTrond Myklebust if (!inode) 1114efeda80dSTrond Myklebust return; 1115efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1116efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1117efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1118efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1119efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1120efeda80dSTrond Myklebust } 1121efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1122efeda80dSTrond Myklebust } 1123efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1124efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1125efeda80dSTrond Myklebust 11261da177e4SLinus Torvalds /* 11271da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 11281da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 11291da177e4SLinus Torvalds * and may need to be looked up again. 1130912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 11311da177e4SLinus Torvalds */ 1132912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1133912a108dSNeilBrown int rcu_walk) 11341da177e4SLinus Torvalds { 11351da177e4SLinus Torvalds if (IS_ROOT(dentry)) 11361da177e4SLinus Torvalds return 1; 11374eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 11384eec952eSTrond Myklebust return 0; 1139f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 11406ecc5e8fSTrond Myklebust return 0; 1141f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 11421cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1143912a108dSNeilBrown if (rcu_walk) 1144f2c77f4eSTrond Myklebust return 0; 11451cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 11461cd9cb05STrond Myklebust return 0; 11471cd9cb05STrond Myklebust } 1148f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1149f2c77f4eSTrond Myklebust return 0; 1150f2c77f4eSTrond Myklebust return 1; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* 1154a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1155a12802caSTrond Myklebust * an O_EXCL create using this path component. 1156a12802caSTrond Myklebust */ 1157fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1158a12802caSTrond Myklebust { 1159a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1160a12802caSTrond Myklebust return 0; 1161fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1162a12802caSTrond Myklebust } 1163a12802caSTrond Myklebust 1164a12802caSTrond Myklebust /* 11651d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 11661d6757fbSTrond Myklebust * 11671d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 11681d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 11691d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 11701d6757fbSTrond Myklebust * 11711d6757fbSTrond Myklebust */ 117265a0c149STrond Myklebust static 1173fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 117665a0c149STrond Myklebust int ret; 11771da177e4SLinus Torvalds 117836d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 11794e99a1ffSTrond Myklebust return 0; 118047921921STrond Myklebust 118147921921STrond Myklebust if (flags & LOOKUP_OPEN) { 118247921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 118347921921STrond Myklebust case S_IFREG: 118447921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 118547921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 118647921921STrond Myklebust goto out; 1187df561f66SGustavo A. R. Silva fallthrough; 118847921921STrond Myklebust case S_IFDIR: 118947921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 119047921921STrond Myklebust break; 119147921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 119247921921STrond Myklebust goto out_force; 119347921921STrond Myklebust } 119447921921STrond Myklebust } 119547921921STrond Myklebust 11961da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1197fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 11981da177e4SLinus Torvalds goto out_force; 119965a0c149STrond Myklebust out: 1200a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 12011da177e4SLinus Torvalds out_force: 12021fa1e384SNeilBrown if (flags & LOOKUP_RCU) 12031fa1e384SNeilBrown return -ECHILD; 120465a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 120565a0c149STrond Myklebust if (ret != 0) 120665a0c149STrond Myklebust return ret; 120765a0c149STrond Myklebust goto out; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds /* 12111da177e4SLinus Torvalds * We judge how long we want to trust negative 12121da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 12131da177e4SLinus Torvalds * 12141da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 12151da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1216912a108dSNeilBrown * 1217912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1218912a108dSNeilBrown * suggesting a reval is needed. 12199f6d44d4STrond Myklebust * 12209f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 12219f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 12221da177e4SLinus Torvalds */ 12231da177e4SLinus Torvalds static inline 12241da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1225fa3c56bbSAl Viro unsigned int flags) 12261da177e4SLinus Torvalds { 12279f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 12281da177e4SLinus Torvalds return 0; 12294eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 12304eec952eSTrond Myklebust return 1; 1231912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds 12345ceb9d7fSTrond Myklebust static int 12355ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 12365ceb9d7fSTrond Myklebust struct inode *inode, int error) 12371da177e4SLinus Torvalds { 12385ceb9d7fSTrond Myklebust switch (error) { 12395ceb9d7fSTrond Myklebust case 1: 12406de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12416de1472fSAl Viro __func__, dentry); 12421da177e4SLinus Torvalds return 1; 12435ceb9d7fSTrond Myklebust case 0: 1244a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12451da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12461da177e4SLinus Torvalds /* Purge readdir caches. */ 12471da177e4SLinus Torvalds nfs_zap_caches(inode); 1248a3f432bfSJ. Bruce Fields /* 1249a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1250a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1251a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1252a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1253a3f432bfSJ. Bruce Fields */ 1254a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 12555ceb9d7fSTrond Myklebust return 1; 12561da177e4SLinus Torvalds } 12576de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12586de1472fSAl Viro __func__, dentry); 12591da177e4SLinus Torvalds return 0; 12605ceb9d7fSTrond Myklebust } 12616de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12626de1472fSAl Viro __func__, dentry, error); 1263e1fb4d05STrond Myklebust return error; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12665ceb9d7fSTrond Myklebust static int 12675ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 12685ceb9d7fSTrond Myklebust unsigned int flags) 12695ceb9d7fSTrond Myklebust { 12705ceb9d7fSTrond Myklebust int ret = 1; 12715ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 12725ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12735ceb9d7fSTrond Myklebust return -ECHILD; 12745ceb9d7fSTrond Myklebust ret = 0; 12755ceb9d7fSTrond Myklebust } 12765ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 12775ceb9d7fSTrond Myklebust } 12785ceb9d7fSTrond Myklebust 12795ceb9d7fSTrond Myklebust static int 12805ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 12815ceb9d7fSTrond Myklebust struct inode *inode) 12825ceb9d7fSTrond Myklebust { 12835ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12845ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12855ceb9d7fSTrond Myklebust } 12865ceb9d7fSTrond Myklebust 12875ceb9d7fSTrond Myklebust static int 12885ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 12895ceb9d7fSTrond Myklebust struct inode *inode) 12905ceb9d7fSTrond Myklebust { 12915ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 12925ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 12935ceb9d7fSTrond Myklebust struct nfs4_label *label; 1294a1147b82STrond Myklebust unsigned long dir_verifier; 12955ceb9d7fSTrond Myklebust int ret; 12965ceb9d7fSTrond Myklebust 12975ceb9d7fSTrond Myklebust ret = -ENOMEM; 12985ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 12995ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 13005ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 13015ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 13025ceb9d7fSTrond Myklebust goto out; 13035ceb9d7fSTrond Myklebust 1304a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1305f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 13065ceb9d7fSTrond Myklebust if (ret < 0) { 1307f7b37b8bSTrond Myklebust switch (ret) { 1308f7b37b8bSTrond Myklebust case -ESTALE: 1309f7b37b8bSTrond Myklebust case -ENOENT: 13105ceb9d7fSTrond Myklebust ret = 0; 1311f7b37b8bSTrond Myklebust break; 1312f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1313f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1314f7b37b8bSTrond Myklebust ret = 1; 1315f7b37b8bSTrond Myklebust } 13165ceb9d7fSTrond Myklebust goto out; 13175ceb9d7fSTrond Myklebust } 13185ceb9d7fSTrond Myklebust ret = 0; 13195ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 13205ceb9d7fSTrond Myklebust goto out; 13215ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 13225ceb9d7fSTrond Myklebust goto out; 13235ceb9d7fSTrond Myklebust 13245ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1325a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 13265ceb9d7fSTrond Myklebust 13275ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 13285ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 13295ceb9d7fSTrond Myklebust ret = 1; 13305ceb9d7fSTrond Myklebust out: 13315ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 13325ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 13335ceb9d7fSTrond Myklebust nfs4_label_free(label); 13345ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 13355ceb9d7fSTrond Myklebust } 13365ceb9d7fSTrond Myklebust 13375ceb9d7fSTrond Myklebust /* 13385ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 13395ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 13405ceb9d7fSTrond Myklebust * lookup. 13415ceb9d7fSTrond Myklebust * 13425ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 13435ceb9d7fSTrond Myklebust * we have an inode! 13445ceb9d7fSTrond Myklebust * 13455ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 13465ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 13475ceb9d7fSTrond Myklebust */ 13485ceb9d7fSTrond Myklebust static int 13495ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 13505ceb9d7fSTrond Myklebust unsigned int flags) 13515ceb9d7fSTrond Myklebust { 13525ceb9d7fSTrond Myklebust struct inode *inode; 13535ceb9d7fSTrond Myklebust int error; 13545ceb9d7fSTrond Myklebust 13555ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 13565ceb9d7fSTrond Myklebust inode = d_inode(dentry); 13575ceb9d7fSTrond Myklebust 13585ceb9d7fSTrond Myklebust if (!inode) 13595ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 13605ceb9d7fSTrond Myklebust 13615ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 13625ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13635ceb9d7fSTrond Myklebust __func__, dentry); 13645ceb9d7fSTrond Myklebust goto out_bad; 13655ceb9d7fSTrond Myklebust } 13665ceb9d7fSTrond Myklebust 1367efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 13685ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 13695ceb9d7fSTrond Myklebust 13705ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 13715ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 13725ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 13735ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 13745ceb9d7fSTrond Myklebust if (error) { 13755ceb9d7fSTrond Myklebust if (error == -ESTALE) 13765ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 13775ceb9d7fSTrond Myklebust goto out_bad; 13785ceb9d7fSTrond Myklebust } 13795ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 13805ceb9d7fSTrond Myklebust goto out_valid; 13815ceb9d7fSTrond Myklebust } 13825ceb9d7fSTrond Myklebust 13835ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13845ceb9d7fSTrond Myklebust return -ECHILD; 13855ceb9d7fSTrond Myklebust 13865ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 13875ceb9d7fSTrond Myklebust goto out_bad; 13885ceb9d7fSTrond Myklebust 13895ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 13905ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 13915ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 13925ceb9d7fSTrond Myklebust return error; 13935ceb9d7fSTrond Myklebust out_valid: 13945ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 13955ceb9d7fSTrond Myklebust out_bad: 13965ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13975ceb9d7fSTrond Myklebust return -ECHILD; 13985ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 13995ceb9d7fSTrond Myklebust } 14005ceb9d7fSTrond Myklebust 14015ceb9d7fSTrond Myklebust static int 1402c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1403c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 14045ceb9d7fSTrond Myklebust { 14055ceb9d7fSTrond Myklebust struct dentry *parent; 14065ceb9d7fSTrond Myklebust struct inode *dir; 14075ceb9d7fSTrond Myklebust int ret; 14085ceb9d7fSTrond Myklebust 14095ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 14105ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 14115ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 14125ceb9d7fSTrond Myklebust if (!dir) 14135ceb9d7fSTrond Myklebust return -ECHILD; 1414c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 14155ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 14165ceb9d7fSTrond Myklebust return -ECHILD; 14175ceb9d7fSTrond Myklebust } else { 14185ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1419c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 14205ceb9d7fSTrond Myklebust dput(parent); 14215ceb9d7fSTrond Myklebust } 14225ceb9d7fSTrond Myklebust return ret; 14235ceb9d7fSTrond Myklebust } 14245ceb9d7fSTrond Myklebust 1425c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1426c7944ebbSTrond Myklebust { 1427c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1428c7944ebbSTrond Myklebust } 1429c7944ebbSTrond Myklebust 14301da177e4SLinus Torvalds /* 14312b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1432ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1433ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1434ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1435ecf3d1f1SJeff Layton * 1436ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1437ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1438ecf3d1f1SJeff Layton */ 1439ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1440ecf3d1f1SJeff Layton { 14412b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 14429cdd1d3fSTrond Myklebust int error = 0; 1443ecf3d1f1SJeff Layton 1444ecf3d1f1SJeff Layton /* 1445ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1446ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1447ecf3d1f1SJeff Layton * eventually need to do something more here. 1448ecf3d1f1SJeff Layton */ 1449ecf3d1f1SJeff Layton if (!inode) { 14506de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 14516de1472fSAl Viro __func__, dentry); 1452ecf3d1f1SJeff Layton return 1; 1453ecf3d1f1SJeff Layton } 1454ecf3d1f1SJeff Layton 1455ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 14566de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 14576de1472fSAl Viro __func__, dentry); 1458ecf3d1f1SJeff Layton return 0; 1459ecf3d1f1SJeff Layton } 1460ecf3d1f1SJeff Layton 1461b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1462ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1463ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1464ecf3d1f1SJeff Layton return !error; 1465ecf3d1f1SJeff Layton } 1466ecf3d1f1SJeff Layton 1467ecf3d1f1SJeff Layton /* 14681da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 14691da177e4SLinus Torvalds */ 1470fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 14711da177e4SLinus Torvalds { 14726de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 14736de1472fSAl Viro dentry, dentry->d_flags); 14741da177e4SLinus Torvalds 147577f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 14762b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 147777f11192STrond Myklebust return 1; 147877f11192STrond Myklebust 14791da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 14801da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 14811da177e4SLinus Torvalds return 1; 14821da177e4SLinus Torvalds } 14831751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 14841da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 14851da177e4SLinus Torvalds * files will be cleaned up during umount */ 14861da177e4SLinus Torvalds return 1; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds return 0; 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 14931b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 14941b83d707STrond Myklebust { 14951b83d707STrond Myklebust spin_lock(&inode->i_lock); 14961f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 149759a707b0STrond Myklebust if (inode->i_nlink > 0) 149859a707b0STrond Myklebust drop_nlink(inode); 149959a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 150016e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 150116e14375STrond Myklebust | NFS_INO_INVALID_CTIME 150259a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 150359a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 15041b83d707STrond Myklebust spin_unlock(&inode->i_lock); 15051b83d707STrond Myklebust } 15061b83d707STrond Myklebust 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * Called when the dentry loses inode. 15091da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 15101da177e4SLinus Torvalds */ 15111da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 15121da177e4SLinus Torvalds { 151383672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 151483672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 151583672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 151683672d39SNeil Brown 15171da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1518e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 15191f018458STrond Myklebust nfs_drop_nlink(inode); 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds iput(inode); 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds 1524b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1525b1942c5fSAl Viro { 1526b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1527b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1528b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1529b1942c5fSAl Viro WARN_ON(1); 1530b1942c5fSAl Viro else 1531b1942c5fSAl Viro kfree(dentry->d_fsdata); 1532b1942c5fSAl Viro } 1533b1942c5fSAl Viro } 1534b1942c5fSAl Viro 1535f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 15361da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1537ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 15381da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15391da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 154036d43a43SDavid Howells .d_automount = nfs_d_automount, 1541b1942c5fSAl Viro .d_release = nfs_d_release, 15421da177e4SLinus Torvalds }; 1543ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 15441da177e4SLinus Torvalds 1545597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 15461da177e4SLinus Torvalds { 15471da177e4SLinus Torvalds struct dentry *res; 15481da177e4SLinus Torvalds struct inode *inode = NULL; 1549e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1550e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 15511775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1552a1147b82STrond Myklebust unsigned long dir_verifier; 15531da177e4SLinus Torvalds int error; 15541da177e4SLinus Torvalds 15556de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 155691d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 15571da177e4SLinus Torvalds 1558130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1559130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 15601da177e4SLinus Torvalds 1561fd684071STrond Myklebust /* 1562fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1563fd684071STrond Myklebust * but don't hash the dentry. 1564fd684071STrond Myklebust */ 15659f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1566130f9ab7SAl Viro return NULL; 15671da177e4SLinus Torvalds 1568e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1569e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1570e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1571e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1572e1fb4d05STrond Myklebust goto out; 1573e1fb4d05STrond Myklebust 157414c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 157514c43f76SDavid Quigley if (IS_ERR(label)) 157614c43f76SDavid Quigley goto out; 157714c43f76SDavid Quigley 1578a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 15796e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1580f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 15811da177e4SLinus Torvalds if (error == -ENOENT) 15821da177e4SLinus Torvalds goto no_entry; 15831da177e4SLinus Torvalds if (error < 0) { 15841da177e4SLinus Torvalds res = ERR_PTR(error); 1585bf130914SAl Viro goto out_label; 15861da177e4SLinus Torvalds } 15871775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1588bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 158903f28e3aSTrond Myklebust if (IS_ERR(res)) 1590bf130914SAl Viro goto out_label; 159154ceac45SDavid Howells 159263519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 159363519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1594d69ee9b8STrond Myklebust 15951da177e4SLinus Torvalds no_entry: 159641d28bcaSAl Viro res = d_splice_alias(inode, dentry); 15979eaef27bSTrond Myklebust if (res != NULL) { 15989eaef27bSTrond Myklebust if (IS_ERR(res)) 1599bf130914SAl Viro goto out_label; 16001da177e4SLinus Torvalds dentry = res; 16019eaef27bSTrond Myklebust } 1602a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1603bf130914SAl Viro out_label: 16046e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 160514c43f76SDavid Quigley nfs4_label_free(label); 16061da177e4SLinus Torvalds out: 1607e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1608e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 16091da177e4SLinus Torvalds return res; 16101da177e4SLinus Torvalds } 1611ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 16121da177e4SLinus Torvalds 161389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 16140b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 16151da177e4SLinus Torvalds 1616f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 16170ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1618b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 16191da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 16201da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 162136d43a43SDavid Howells .d_automount = nfs_d_automount, 1622b1942c5fSAl Viro .d_release = nfs_d_release, 16231da177e4SLinus Torvalds }; 162489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 16251da177e4SLinus Torvalds 16268a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 16278a5e929dSAl Viro { 16288a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 16298a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 16308a5e929dSAl Viro res |= FMODE_READ; 16318a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 16328a5e929dSAl Viro res |= FMODE_WRITE; 16338a5e929dSAl Viro return res; 16348a5e929dSAl Viro } 16358a5e929dSAl Viro 1636532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1637cd9a1c0eSTrond Myklebust { 1638532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1639cd9a1c0eSTrond Myklebust } 1640cd9a1c0eSTrond Myklebust 1641cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1642cd9a1c0eSTrond Myklebust { 1643f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1644cd9a1c0eSTrond Myklebust return 0; 1645cd9a1c0eSTrond Myklebust } 1646cd9a1c0eSTrond Myklebust 1647d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 16480dd2b474SMiklos Szeredi struct dentry *dentry, 1649b452a458SAl Viro struct file *file, unsigned open_flags) 1650cd9a1c0eSTrond Myklebust { 16510dd2b474SMiklos Szeredi int err; 16520dd2b474SMiklos Szeredi 1653be12af3eSAl Viro err = finish_open(file, dentry, do_open); 165430d90494SAl Viro if (err) 1655d9585277SAl Viro goto out; 1656eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 165730d90494SAl Viro nfs_file_set_open_context(file, ctx); 1658eaa2b82cSNeilBrown else 16599821421aSTrond Myklebust err = -EOPENSTALE; 1660cd9a1c0eSTrond Myklebust out: 1661d9585277SAl Viro return err; 1662cd9a1c0eSTrond Myklebust } 1663cd9a1c0eSTrond Myklebust 166473a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 166530d90494SAl Viro struct file *file, unsigned open_flags, 166644907d79SAl Viro umode_t mode) 16671da177e4SLinus Torvalds { 1668c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1669cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 16700dd2b474SMiklos Szeredi struct dentry *res; 16710dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1672f46e0bd3STrond Myklebust struct inode *inode; 16731472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1674c94c0953SAl Viro bool switched = false; 167573a09dd9SAl Viro int created = 0; 1676898f635cSTrond Myklebust int err; 16771da177e4SLinus Torvalds 16780dd2b474SMiklos Szeredi /* Expect a negative dentry */ 16792b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 16800dd2b474SMiklos Szeredi 16811e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 16826de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 16831e7cb3dcSChuck Lever 16849597c13bSJeff Layton err = nfs_check_flags(open_flags); 16859597c13bSJeff Layton if (err) 16869597c13bSJeff Layton return err; 16879597c13bSJeff Layton 16880dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 16890dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 169000699ad8SAl Viro if (!d_in_lookup(dentry)) { 16910dd2b474SMiklos Szeredi /* 16920dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 16930dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 16940dd2b474SMiklos Szeredi * again 16950dd2b474SMiklos Szeredi */ 1696d9585277SAl Viro return -ENOENT; 16970dd2b474SMiklos Szeredi } 16981472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 16991da177e4SLinus Torvalds goto no_open; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 17020dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1703d9585277SAl Viro return -ENAMETOOLONG; 17041da177e4SLinus Torvalds 17050dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1706dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1707dff25ddbSAndreas Gruenbacher 1708dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1709dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1710dff25ddbSAndreas Gruenbacher 1711536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1712dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 17130dd2b474SMiklos Szeredi } 1714536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1715536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1716536e43d1STrond Myklebust attr.ia_size = 0; 1717cd9a1c0eSTrond Myklebust } 1718cd9a1c0eSTrond Myklebust 1719c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1720c94c0953SAl Viro d_drop(dentry); 1721c94c0953SAl Viro switched = true; 1722c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1723c94c0953SAl Viro &dentry->d_name, &wq); 1724c94c0953SAl Viro if (IS_ERR(dentry)) 1725c94c0953SAl Viro return PTR_ERR(dentry); 1726c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1727c94c0953SAl Viro return finish_no_open(file, dentry); 1728c94c0953SAl Viro } 1729c94c0953SAl Viro 1730532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 17310dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 17320dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1733d9585277SAl Viro goto out; 17340dd2b474SMiklos Szeredi 17356e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 173673a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 173773a09dd9SAl Viro if (created) 173873a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1739275bb307STrond Myklebust if (IS_ERR(inode)) { 17400dd2b474SMiklos Szeredi err = PTR_ERR(inode); 17416e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17422d9db750STrond Myklebust put_nfs_open_context(ctx); 1743d20cb71dSAl Viro d_drop(dentry); 17440dd2b474SMiklos Szeredi switch (err) { 17451da177e4SLinus Torvalds case -ENOENT: 1746774d9513SPeng Tao d_splice_alias(NULL, dentry); 1747809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17480dd2b474SMiklos Szeredi break; 17491788ea6eSJeff Layton case -EISDIR: 17506f926b5bSTrond Myklebust case -ENOTDIR: 17516f926b5bSTrond Myklebust goto no_open; 17521da177e4SLinus Torvalds case -ELOOP: 17530dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 17541da177e4SLinus Torvalds goto no_open; 17550dd2b474SMiklos Szeredi break; 17561da177e4SLinus Torvalds /* case -EINVAL: */ 17571da177e4SLinus Torvalds default: 17580dd2b474SMiklos Szeredi break; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds goto out; 17611da177e4SLinus Torvalds } 17620dd2b474SMiklos Szeredi 1763b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 17646e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17652d9db750STrond Myklebust put_nfs_open_context(ctx); 1766d9585277SAl Viro out: 1767c94c0953SAl Viro if (unlikely(switched)) { 1768c94c0953SAl Viro d_lookup_done(dentry); 1769c94c0953SAl Viro dput(dentry); 1770c94c0953SAl Viro } 1771d9585277SAl Viro return err; 17720dd2b474SMiklos Szeredi 17731da177e4SLinus Torvalds no_open: 17741472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1775c94c0953SAl Viro if (switched) { 1776c94c0953SAl Viro d_lookup_done(dentry); 1777c94c0953SAl Viro if (!res) 1778c94c0953SAl Viro res = dentry; 1779c94c0953SAl Viro else 1780c94c0953SAl Viro dput(dentry); 1781c94c0953SAl Viro } 17820dd2b474SMiklos Szeredi if (IS_ERR(res)) 1783c94c0953SAl Viro return PTR_ERR(res); 1784e45198a6SAl Viro return finish_no_open(file, res); 17851da177e4SLinus Torvalds } 178689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 17871da177e4SLinus Torvalds 1788c7944ebbSTrond Myklebust static int 1789c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1790c7944ebbSTrond Myklebust unsigned int flags) 17911da177e4SLinus Torvalds { 1792657e94b6SNick Piggin struct inode *inode; 17931da177e4SLinus Torvalds 1794fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1795c7944ebbSTrond Myklebust goto full_reval; 1796eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1797c7944ebbSTrond Myklebust goto full_reval; 17982b484297STrond Myklebust 17992b0143b5SDavid Howells inode = d_inode(dentry); 18002b484297STrond Myklebust 18011da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 18021da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 18031da177e4SLinus Torvalds */ 1804c7944ebbSTrond Myklebust if (inode == NULL) 1805c7944ebbSTrond Myklebust goto full_reval; 180649317a7fSNeilBrown 1807efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 1808c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1809216d5d06STrond Myklebust 18101da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 18111da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1812c7944ebbSTrond Myklebust goto full_reval; 1813c7944ebbSTrond Myklebust 18141da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1815c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1816c7944ebbSTrond Myklebust goto reval_dentry; 1817c7944ebbSTrond Myklebust 1818c7944ebbSTrond Myklebust /* Check if the directory changed */ 1819c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1820c7944ebbSTrond Myklebust goto reval_dentry; 18211da177e4SLinus Torvalds 18220ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1823c7944ebbSTrond Myklebust return 1; 1824c7944ebbSTrond Myklebust reval_dentry: 1825c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1826c7944ebbSTrond Myklebust return -ECHILD; 182742f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 18280ef97dcfSMiklos Szeredi 1829c7944ebbSTrond Myklebust full_reval: 1830c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1831c7944ebbSTrond Myklebust } 1832535918f1STrond Myklebust 1833c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1834c7944ebbSTrond Myklebust { 1835c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1836c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1837c0204fd2STrond Myklebust } 1838c0204fd2STrond Myklebust 18391da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 18401da177e4SLinus Torvalds 1841406cd915SBenjamin Coddington struct dentry * 1842406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 18431775fd3eSDavid Quigley struct nfs_fattr *fattr, 18441775fd3eSDavid Quigley struct nfs4_label *label) 18451da177e4SLinus Torvalds { 1846fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 18472b0143b5SDavid Howells struct inode *dir = d_inode(parent); 18481da177e4SLinus Torvalds struct inode *inode; 1849b0c6108eSAl Viro struct dentry *d; 1850406cd915SBenjamin Coddington int error; 18511da177e4SLinus Torvalds 1852fab728e1STrond Myklebust d_drop(dentry); 1853fab728e1STrond Myklebust 18541da177e4SLinus Torvalds if (fhandle->size == 0) { 1855f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 18561da177e4SLinus Torvalds if (error) 1857fab728e1STrond Myklebust goto out_error; 18581da177e4SLinus Torvalds } 18595724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18601da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 18611da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1862a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1863a841b54dSTrond Myklebust fattr, NULL, NULL); 18641da177e4SLinus Torvalds if (error < 0) 1865fab728e1STrond Myklebust goto out_error; 18661da177e4SLinus Torvalds } 18671775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1868b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1869fab728e1STrond Myklebust out: 1870fab728e1STrond Myklebust dput(parent); 1871406cd915SBenjamin Coddington return d; 1872fab728e1STrond Myklebust out_error: 1873fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1874406cd915SBenjamin Coddington d = ERR_PTR(error); 1875406cd915SBenjamin Coddington goto out; 1876406cd915SBenjamin Coddington } 1877406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1878406cd915SBenjamin Coddington 1879406cd915SBenjamin Coddington /* 1880406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1881406cd915SBenjamin Coddington */ 1882406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1883406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1884406cd915SBenjamin Coddington struct nfs4_label *label) 1885406cd915SBenjamin Coddington { 1886406cd915SBenjamin Coddington struct dentry *d; 1887406cd915SBenjamin Coddington 1888406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1889406cd915SBenjamin Coddington if (IS_ERR(d)) 1890406cd915SBenjamin Coddington return PTR_ERR(d); 1891406cd915SBenjamin Coddington 1892406cd915SBenjamin Coddington /* Callers don't care */ 1893406cd915SBenjamin Coddington dput(d); 1894406cd915SBenjamin Coddington return 0; 18951da177e4SLinus Torvalds } 1896ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds /* 18991da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 19001da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 19011da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 19021da177e4SLinus Torvalds * reply path made it appear to have failed. 19031da177e4SLinus Torvalds */ 1904597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1905ebfc3b49SAl Viro umode_t mode, bool excl) 19061da177e4SLinus Torvalds { 19071da177e4SLinus Torvalds struct iattr attr; 1908ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 19091da177e4SLinus Torvalds int error; 19101da177e4SLinus Torvalds 19111e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 19126de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds attr.ia_mode = mode; 19151da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19161da177e4SLinus Torvalds 19178b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 19188867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 19198b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 19201da177e4SLinus Torvalds if (error != 0) 19211da177e4SLinus Torvalds goto out_err; 19221da177e4SLinus Torvalds return 0; 19231da177e4SLinus Torvalds out_err: 19241da177e4SLinus Torvalds d_drop(dentry); 19251da177e4SLinus Torvalds return error; 19261da177e4SLinus Torvalds } 1927ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds /* 19301da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19311da177e4SLinus Torvalds */ 1932597d9289SBryan Schumaker int 19331a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 19341da177e4SLinus Torvalds { 19351da177e4SLinus Torvalds struct iattr attr; 19361da177e4SLinus Torvalds int status; 19371da177e4SLinus Torvalds 19381e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 19396de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds attr.ia_mode = mode; 19421da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19431da177e4SLinus Torvalds 19441ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 19451da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 19461ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 19471da177e4SLinus Torvalds if (status != 0) 19481da177e4SLinus Torvalds goto out_err; 19491da177e4SLinus Torvalds return 0; 19501da177e4SLinus Torvalds out_err: 19511da177e4SLinus Torvalds d_drop(dentry); 19521da177e4SLinus Torvalds return status; 19531da177e4SLinus Torvalds } 1954ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds /* 19571da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19581da177e4SLinus Torvalds */ 1959597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19601da177e4SLinus Torvalds { 19611da177e4SLinus Torvalds struct iattr attr; 19621da177e4SLinus Torvalds int error; 19631da177e4SLinus Torvalds 19641e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 19656de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19681da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 19691da177e4SLinus Torvalds 19701ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 19711da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 19721ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 19731da177e4SLinus Torvalds if (error != 0) 19741da177e4SLinus Torvalds goto out_err; 19751da177e4SLinus Torvalds return 0; 19761da177e4SLinus Torvalds out_err: 19771da177e4SLinus Torvalds d_drop(dentry); 19781da177e4SLinus Torvalds return error; 19791da177e4SLinus Torvalds } 1980ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 19811da177e4SLinus Torvalds 1982d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1983d45b9d8bSTrond Myklebust { 1984dc3f4198SAl Viro if (simple_positive(dentry)) 1985d45b9d8bSTrond Myklebust d_delete(dentry); 1986d45b9d8bSTrond Myklebust } 1987d45b9d8bSTrond Myklebust 1988597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 19891da177e4SLinus Torvalds { 19901da177e4SLinus Torvalds int error; 19911da177e4SLinus Torvalds 19921e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 19936de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19941da177e4SLinus Torvalds 19951ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 19962b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1997884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 19981da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 19991da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 2000ba6c0592STrond Myklebust switch (error) { 2001ba6c0592STrond Myklebust case 0: 20022b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2003ba6c0592STrond Myklebust break; 2004ba6c0592STrond Myklebust case -ENOENT: 2005d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2006ba6c0592STrond Myklebust } 2007884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2008ba6c0592STrond Myklebust } else 2009ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 20101ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds return error; 20131da177e4SLinus Torvalds } 2014ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds /* 20171da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 20181da177e4SLinus Torvalds * and after checking that the file has only one user. 20191da177e4SLinus Torvalds * 20201da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 20211da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 20221da177e4SLinus Torvalds */ 20231da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 20241da177e4SLinus Torvalds { 20252b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 20262b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 20271da177e4SLinus Torvalds int error = -EBUSY; 20281da177e4SLinus Torvalds 20296de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 20321da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 20331da177e4SLinus Torvalds error = 0; 20341da177e4SLinus Torvalds goto out; 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds 20371ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 20381da177e4SLinus Torvalds if (inode != NULL) { 2039912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 20401da177e4SLinus Torvalds if (error == 0) 20411b83d707STrond Myklebust nfs_drop_nlink(inode); 20421da177e4SLinus Torvalds } else 2043912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2044d45b9d8bSTrond Myklebust if (error == -ENOENT) 2045d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 20461ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 20471da177e4SLinus Torvalds out: 20481da177e4SLinus Torvalds return error; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 20521da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 20531da177e4SLinus Torvalds * 20541da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 20551da177e4SLinus Torvalds */ 2056597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 20571da177e4SLinus Torvalds { 20581da177e4SLinus Torvalds int error; 20591da177e4SLinus Torvalds int need_rehash = 0; 20601da177e4SLinus Torvalds 20611e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 20626de1472fSAl Viro dir->i_ino, dentry); 20631da177e4SLinus Torvalds 20641ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 20651da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 206684d08fa8SAl Viro if (d_count(dentry) > 1) { 20671da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2068ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 20692b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 20701da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 20711ca42382STrond Myklebust goto out; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 20741da177e4SLinus Torvalds __d_drop(dentry); 20751da177e4SLinus Torvalds need_rehash = 1; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 20781da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2079d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 20801da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 20811da177e4SLinus Torvalds } else if (need_rehash) 20821da177e4SLinus Torvalds d_rehash(dentry); 20831ca42382STrond Myklebust out: 20841ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 20851da177e4SLinus Torvalds return error; 20861da177e4SLinus Torvalds } 2087ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 20881da177e4SLinus Torvalds 2089873101b3SChuck Lever /* 2090873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2091873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2092873101b3SChuck Lever * using prepare_write/commit_write. 2093873101b3SChuck Lever * 2094873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2095873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2096873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2097873101b3SChuck Lever * symlink request has completed on the server. 2098873101b3SChuck Lever * 2099873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2100873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2101873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2102873101b3SChuck Lever * and move the raw page into its mapping. 2103873101b3SChuck Lever */ 2104597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 21051da177e4SLinus Torvalds { 2106873101b3SChuck Lever struct page *page; 2107873101b3SChuck Lever char *kaddr; 21081da177e4SLinus Torvalds struct iattr attr; 2109873101b3SChuck Lever unsigned int pathlen = strlen(symname); 21101da177e4SLinus Torvalds int error; 21111da177e4SLinus Torvalds 21121e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 21136de1472fSAl Viro dir->i_ino, dentry, symname); 21141da177e4SLinus Torvalds 2115873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2116873101b3SChuck Lever return -ENAMETOOLONG; 21171da177e4SLinus Torvalds 2118873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2119873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 21201da177e4SLinus Torvalds 2121e8ecde25SAl Viro page = alloc_page(GFP_USER); 212276566991STrond Myklebust if (!page) 2123873101b3SChuck Lever return -ENOMEM; 2124873101b3SChuck Lever 2125e8ecde25SAl Viro kaddr = page_address(page); 2126873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2127873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2128873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2129873101b3SChuck Lever 21301ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 213194a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 21321ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2133873101b3SChuck Lever if (error != 0) { 21341e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2135873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 21366de1472fSAl Viro dentry, symname, error); 21371da177e4SLinus Torvalds d_drop(dentry); 2138873101b3SChuck Lever __free_page(page); 21391da177e4SLinus Torvalds return error; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 2142873101b3SChuck Lever /* 2143873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2144873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2145873101b3SChuck Lever */ 21462b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2147873101b3SChuck Lever GFP_KERNEL)) { 2148873101b3SChuck Lever SetPageUptodate(page); 2149873101b3SChuck Lever unlock_page(page); 2150a0b54addSRafael Aquini /* 2151a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2152a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2153a0b54addSRafael Aquini */ 215409cbfeafSKirill A. Shutemov put_page(page); 2155873101b3SChuck Lever } else 2156873101b3SChuck Lever __free_page(page); 2157873101b3SChuck Lever 2158873101b3SChuck Lever return 0; 2159873101b3SChuck Lever } 2160ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2161873101b3SChuck Lever 2162597d9289SBryan Schumaker int 21631da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 21641da177e4SLinus Torvalds { 21652b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 21661da177e4SLinus Torvalds int error; 21671da177e4SLinus Torvalds 21686de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 21696de1472fSAl Viro old_dentry, dentry); 21701da177e4SLinus Torvalds 21711fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 21729697d234STrond Myklebust d_drop(dentry); 21731da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2174cf809556STrond Myklebust if (error == 0) { 21757de9c6eeSAl Viro ihold(inode); 21769697d234STrond Myklebust d_add(dentry, inode); 2177cf809556STrond Myklebust } 21781fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 21791da177e4SLinus Torvalds return error; 21801da177e4SLinus Torvalds } 2181ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds /* 21841da177e4SLinus Torvalds * RENAME 21851da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 21861da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 21871da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 21881da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 21891da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 21901da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 21911da177e4SLinus Torvalds * 21921da177e4SLinus Torvalds * FIXED. 21931da177e4SLinus Torvalds * 21941da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 21951da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 21961da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 21971da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 21981da177e4SLinus Torvalds * using the inode layer 21991da177e4SLinus Torvalds * 22001da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 22011da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 22021da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 22031da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 22041da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 22051da177e4SLinus Torvalds * the rename. 22061da177e4SLinus Torvalds */ 2207597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 22081cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 22091cd66c93SMiklos Szeredi unsigned int flags) 22101da177e4SLinus Torvalds { 22112b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 22122b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2213d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 221480a491fdSJeff Layton struct rpc_task *task; 22151da177e4SLinus Torvalds int error = -EBUSY; 22161da177e4SLinus Torvalds 22171cd66c93SMiklos Szeredi if (flags) 22181cd66c93SMiklos Szeredi return -EINVAL; 22191cd66c93SMiklos Szeredi 22206de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 22216de1472fSAl Viro old_dentry, new_dentry, 222284d08fa8SAl Viro d_count(new_dentry)); 22231da177e4SLinus Torvalds 222470ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 22251da177e4SLinus Torvalds /* 222628f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 222728f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 222828f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 222928f79a1aSMiklos Szeredi * the new target. 22301da177e4SLinus Torvalds */ 223127226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 223227226104SMiklos Szeredi /* 223327226104SMiklos Szeredi * To prevent any new references to the target during the 223427226104SMiklos Szeredi * rename, we unhash the dentry in advance. 223527226104SMiklos Szeredi */ 2236d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 223727226104SMiklos Szeredi d_drop(new_dentry); 2238d9f29500SBenjamin Coddington rehash = new_dentry; 2239d9f29500SBenjamin Coddington } 224027226104SMiklos Szeredi 224184d08fa8SAl Viro if (d_count(new_dentry) > 2) { 22421da177e4SLinus Torvalds int err; 224327226104SMiklos Szeredi 22441da177e4SLinus Torvalds /* copy the target dentry's name */ 22451da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 22461da177e4SLinus Torvalds &new_dentry->d_name); 22471da177e4SLinus Torvalds if (!dentry) 22481da177e4SLinus Torvalds goto out; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds /* silly-rename the existing target ... */ 22511da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 225224e93025SMiklos Szeredi if (err) 22531da177e4SLinus Torvalds goto out; 225424e93025SMiklos Szeredi 225524e93025SMiklos Szeredi new_dentry = dentry; 2256d9f29500SBenjamin Coddington rehash = NULL; 225724e93025SMiklos Szeredi new_inode = NULL; 2258b1e4adf4STrond Myklebust } 225927226104SMiklos Szeredi } 22601da177e4SLinus Torvalds 2261d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 226280a491fdSJeff Layton if (IS_ERR(task)) { 226380a491fdSJeff Layton error = PTR_ERR(task); 226480a491fdSJeff Layton goto out; 226580a491fdSJeff Layton } 226680a491fdSJeff Layton 226780a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2268818a8dbeSBenjamin Coddington if (error != 0) { 2269818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2270818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2271818a8dbeSBenjamin Coddington smp_wmb(); 2272818a8dbeSBenjamin Coddington } else 227380a491fdSJeff Layton error = task->tk_status; 227480a491fdSJeff Layton rpc_put_task(task); 227559a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 227659a707b0STrond Myklebust if (error == 0) { 227759a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 227859a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 227959a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 228059a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 228159a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 228259a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 228359a707b0STrond Myklebust } 22841da177e4SLinus Torvalds out: 2285d9f29500SBenjamin Coddington if (rehash) 2286d9f29500SBenjamin Coddington d_rehash(rehash); 228770ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 228870ded201STrond Myklebust new_dir, new_dentry, error); 2289d9f29500SBenjamin Coddington if (!error) { 2290d9f29500SBenjamin Coddington if (new_inode != NULL) 2291d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2292d9f29500SBenjamin Coddington /* 2293d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2294d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2295d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2296d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2297d9f29500SBenjamin Coddington */ 2298d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2299d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2300d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2301d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2302d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2303d9f29500SBenjamin Coddington 23041da177e4SLinus Torvalds /* new dentry created? */ 23051da177e4SLinus Torvalds if (dentry) 23061da177e4SLinus Torvalds dput(dentry); 23071da177e4SLinus Torvalds return error; 23081da177e4SLinus Torvalds } 2309ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 23101da177e4SLinus Torvalds 2311cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2312cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2313cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2314cfcea3e8STrond Myklebust 2315a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 23163a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 23173a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 23183a505845STrond Myklebust 23191c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 23201c3c07e9STrond Myklebust { 2321b68572e0SNeilBrown put_cred(entry->cred); 2322f682a398SNeilBrown kfree_rcu(entry, rcu_head); 23234e857c58SPeter Zijlstra smp_mb__before_atomic(); 2324cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 23254e857c58SPeter Zijlstra smp_mb__after_atomic(); 23261c3c07e9STrond Myklebust } 23271c3c07e9STrond Myklebust 23281a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 23291a81bb8aSTrond Myklebust { 23301a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 23311a81bb8aSTrond Myklebust 23321a81bb8aSTrond Myklebust while (!list_empty(head)) { 23331a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 23341a81bb8aSTrond Myklebust list_del(&cache->lru); 23351a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 23361a81bb8aSTrond Myklebust } 23371a81bb8aSTrond Myklebust } 23381a81bb8aSTrond Myklebust 23393a505845STrond Myklebust static unsigned long 23403a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2341979df72eSTrond Myklebust { 2342979df72eSTrond Myklebust LIST_HEAD(head); 2343aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2344979df72eSTrond Myklebust struct nfs_access_entry *cache; 23451ab6c499SDave Chinner long freed = 0; 2346979df72eSTrond Myklebust 2347a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2348aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2349979df72eSTrond Myklebust struct inode *inode; 2350979df72eSTrond Myklebust 2351979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2352979df72eSTrond Myklebust break; 23539c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2354979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2355979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2356979df72eSTrond Myklebust goto remove_lru_entry; 2357979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2358979df72eSTrond Myklebust struct nfs_access_entry, lru); 2359979df72eSTrond Myklebust list_move(&cache->lru, &head); 2360979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 23611ab6c499SDave Chinner freed++; 2362979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2363979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2364979df72eSTrond Myklebust &nfs_access_lru_list); 2365979df72eSTrond Myklebust else { 2366979df72eSTrond Myklebust remove_lru_entry: 2367979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 23684e857c58SPeter Zijlstra smp_mb__before_atomic(); 2369979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 23704e857c58SPeter Zijlstra smp_mb__after_atomic(); 2371979df72eSTrond Myklebust } 237259844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2373979df72eSTrond Myklebust } 2374979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23751a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23761ab6c499SDave Chinner return freed; 23771ab6c499SDave Chinner } 23781ab6c499SDave Chinner 23791ab6c499SDave Chinner unsigned long 23803a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 23813a505845STrond Myklebust { 23823a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 23833a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 23843a505845STrond Myklebust 23853a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 23863a505845STrond Myklebust return SHRINK_STOP; 23873a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 23883a505845STrond Myklebust } 23893a505845STrond Myklebust 23903a505845STrond Myklebust 23913a505845STrond Myklebust unsigned long 23921ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 23931ab6c499SDave Chinner { 239455f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2395979df72eSTrond Myklebust } 2396979df72eSTrond Myklebust 23973a505845STrond Myklebust static void 23983a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 23993a505845STrond Myklebust { 24003a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 24013a505845STrond Myklebust unsigned long diff; 24023a505845STrond Myklebust unsigned int nr_to_scan; 24033a505845STrond Myklebust 24043a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 24053a505845STrond Myklebust return; 24063a505845STrond Myklebust nr_to_scan = 100; 24073a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 24083a505845STrond Myklebust if (diff < nr_to_scan) 24093a505845STrond Myklebust nr_to_scan = diff; 24103a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 24113a505845STrond Myklebust } 24123a505845STrond Myklebust 24131a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 24141c3c07e9STrond Myklebust { 24151c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24161a81bb8aSTrond Myklebust struct rb_node *n; 24171c3c07e9STrond Myklebust struct nfs_access_entry *entry; 24181c3c07e9STrond Myklebust 24191c3c07e9STrond Myklebust /* Unhook entries from the cache */ 24201c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 24211c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 24221c3c07e9STrond Myklebust rb_erase(n, root_node); 24231a81bb8aSTrond Myklebust list_move(&entry->lru, head); 24241c3c07e9STrond Myklebust } 24251c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 24261c3c07e9STrond Myklebust } 24271c3c07e9STrond Myklebust 24281c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 24291c3c07e9STrond Myklebust { 24301a81bb8aSTrond Myklebust LIST_HEAD(head); 24311a81bb8aSTrond Myklebust 24321a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 24331a81bb8aSTrond Myklebust return; 2434cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2435cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24361a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2437cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2438cfcea3e8STrond Myklebust 24391c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24401a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 24411a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 24421a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24431a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24441c3c07e9STrond Myklebust } 24451c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 24461c3c07e9STrond Myklebust 2447b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 24481c3c07e9STrond Myklebust { 24491c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 24501c3c07e9STrond Myklebust 24511c3c07e9STrond Myklebust while (n != NULL) { 2452b68572e0SNeilBrown struct nfs_access_entry *entry = 2453b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2454b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 24551c3c07e9STrond Myklebust 2456b68572e0SNeilBrown if (cmp < 0) 24571c3c07e9STrond Myklebust n = n->rb_left; 2458b68572e0SNeilBrown else if (cmp > 0) 24591c3c07e9STrond Myklebust n = n->rb_right; 24601c3c07e9STrond Myklebust else 24611c3c07e9STrond Myklebust return entry; 24621c3c07e9STrond Myklebust } 24631c3c07e9STrond Myklebust return NULL; 24641c3c07e9STrond Myklebust } 24651c3c07e9STrond Myklebust 2466d2ae4f8bSFrank 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) 24671da177e4SLinus Torvalds { 246855296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 24691c3c07e9STrond Myklebust struct nfs_access_entry *cache; 247057b69181STrond Myklebust bool retry = true; 247157b69181STrond Myklebust int err; 24721da177e4SLinus Torvalds 24731c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 247457b69181STrond Myklebust for(;;) { 24751c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 24761c3c07e9STrond Myklebust goto out_zap; 24771c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 247857b69181STrond Myklebust err = -ENOENT; 24791c3c07e9STrond Myklebust if (cache == NULL) 24801c3c07e9STrond Myklebust goto out; 248157b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 248221c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 248357b69181STrond Myklebust break; 24845c965db8STrond Myklebust if (!retry) 24855c965db8STrond Myklebust break; 248657b69181STrond Myklebust err = -ECHILD; 248757b69181STrond Myklebust if (!may_block) 248857b69181STrond Myklebust goto out; 248957b69181STrond Myklebust spin_unlock(&inode->i_lock); 249057b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 249157b69181STrond Myklebust if (err) 249257b69181STrond Myklebust return err; 249357b69181STrond Myklebust spin_lock(&inode->i_lock); 249457b69181STrond Myklebust retry = false; 249557b69181STrond Myklebust } 24961c3c07e9STrond Myklebust res->cred = cache->cred; 24971c3c07e9STrond Myklebust res->mask = cache->mask; 2498cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 24991c3c07e9STrond Myklebust err = 0; 25001c3c07e9STrond Myklebust out: 25011c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25021c3c07e9STrond Myklebust return err; 25031c3c07e9STrond Myklebust out_zap: 25041a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 25051a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 25061c3c07e9STrond Myklebust return -ENOENT; 25071c3c07e9STrond Myklebust } 25081c3c07e9STrond Myklebust 2509b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2510f682a398SNeilBrown { 2511f682a398SNeilBrown /* Only check the most recently returned cache entry, 2512f682a398SNeilBrown * but do it without locking. 2513f682a398SNeilBrown */ 2514f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2515f682a398SNeilBrown struct nfs_access_entry *cache; 2516f682a398SNeilBrown int err = -ECHILD; 2517f682a398SNeilBrown struct list_head *lh; 2518f682a398SNeilBrown 2519f682a398SNeilBrown rcu_read_lock(); 2520f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2521f682a398SNeilBrown goto out; 25229f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2523f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2524f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 25259a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2526f682a398SNeilBrown cache = NULL; 2527f682a398SNeilBrown if (cache == NULL) 2528f682a398SNeilBrown goto out; 252921c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2530f682a398SNeilBrown goto out; 2531f682a398SNeilBrown res->cred = cache->cred; 2532f682a398SNeilBrown res->mask = cache->mask; 253321c3ba7eSTrond Myklebust err = 0; 2534f682a398SNeilBrown out: 2535f682a398SNeilBrown rcu_read_unlock(); 2536f682a398SNeilBrown return err; 2537f682a398SNeilBrown } 2538f682a398SNeilBrown 2539d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct 2540d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block) 2541d2ae4f8bSFrank van der Linden { 2542d2ae4f8bSFrank van der Linden int status; 2543d2ae4f8bSFrank van der Linden 2544d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_rcu(inode, cred, res); 2545d2ae4f8bSFrank van der Linden if (status != 0) 2546d2ae4f8bSFrank van der Linden status = nfs_access_get_cached_locked(inode, cred, res, 2547d2ae4f8bSFrank van der Linden may_block); 2548d2ae4f8bSFrank van der Linden 2549d2ae4f8bSFrank van der Linden return status; 2550d2ae4f8bSFrank van der Linden } 2551d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2552d2ae4f8bSFrank van der Linden 25531c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 25541c3c07e9STrond Myklebust { 2555cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2556cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 25571c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 25581c3c07e9STrond Myklebust struct rb_node *parent = NULL; 25591c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2560b68572e0SNeilBrown int cmp; 25611c3c07e9STrond Myklebust 25621c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 25631c3c07e9STrond Myklebust while (*p != NULL) { 25641c3c07e9STrond Myklebust parent = *p; 25651c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2566b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 25671c3c07e9STrond Myklebust 2568b68572e0SNeilBrown if (cmp < 0) 25691c3c07e9STrond Myklebust p = &parent->rb_left; 2570b68572e0SNeilBrown else if (cmp > 0) 25711c3c07e9STrond Myklebust p = &parent->rb_right; 25721c3c07e9STrond Myklebust else 25731c3c07e9STrond Myklebust goto found; 25741c3c07e9STrond Myklebust } 25751c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 25761c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2577cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 25781c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25791c3c07e9STrond Myklebust return; 25801c3c07e9STrond Myklebust found: 25811c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2582cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2583cfcea3e8STrond Myklebust list_del(&entry->lru); 25841c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25851c3c07e9STrond Myklebust nfs_access_free_entry(entry); 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25886168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 25891da177e4SLinus Torvalds { 25901c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 25911c3c07e9STrond Myklebust if (cache == NULL) 25921c3c07e9STrond Myklebust return; 25931c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2594b68572e0SNeilBrown cache->cred = get_cred(set->cred); 25951da177e4SLinus Torvalds cache->mask = set->mask; 25961c3c07e9STrond Myklebust 2597f682a398SNeilBrown /* The above field assignments must be visible 2598f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2599f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2600f682a398SNeilBrown */ 2601f682a398SNeilBrown smp_wmb(); 26021c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2603cfcea3e8STrond Myklebust 2604cfcea3e8STrond Myklebust /* Update accounting */ 26054e857c58SPeter Zijlstra smp_mb__before_atomic(); 2606cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 26074e857c58SPeter Zijlstra smp_mb__after_atomic(); 2608cfcea3e8STrond Myklebust 2609cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 26101a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2611cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 26121a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 26131a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 26141a81bb8aSTrond Myklebust &nfs_access_lru_list); 2615cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2616cfcea3e8STrond Myklebust } 26173a505845STrond Myklebust nfs_access_cache_enforce_limit(); 26181da177e4SLinus Torvalds } 26196168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 26206168f62cSWeston Andros Adamson 26213c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 26223c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26233c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 26243c181827SAnna Schumaker NFS_ACCESS_DELETE) 26253c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26263c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2627ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 26283c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 26293c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 263015d4b73aSTrond Myklebust static int 2631ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 263215d4b73aSTrond Myklebust { 263315d4b73aSTrond Myklebust int mask = 0; 263415d4b73aSTrond Myklebust 263515d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 263615d4b73aSTrond Myklebust mask |= MAY_READ; 2637ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2638ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 263915d4b73aSTrond Myklebust mask |= MAY_WRITE; 2640ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 264115d4b73aSTrond Myklebust mask |= MAY_EXEC; 2642ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2643ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2644ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2645ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 264615d4b73aSTrond Myklebust mask |= MAY_EXEC; 2647ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2648ecbb903cSTrond Myklebust mask |= MAY_WRITE; 264915d4b73aSTrond Myklebust return mask; 265015d4b73aSTrond Myklebust } 265115d4b73aSTrond Myklebust 26526168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 26536168f62cSWeston Andros Adamson { 2654bd8b2441STrond Myklebust entry->mask = access_result; 26556168f62cSWeston Andros Adamson } 26566168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 26571da177e4SLinus Torvalds 2658b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 26591da177e4SLinus Torvalds { 26601da177e4SLinus Torvalds struct nfs_access_entry cache; 266157b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2662e8194b7dSTrond Myklebust int cache_mask = -1; 26631da177e4SLinus Torvalds int status; 26641da177e4SLinus Torvalds 2665f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2666f4ce1299STrond Myklebust 266757b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 26681da177e4SLinus Torvalds if (status == 0) 2669f4ce1299STrond Myklebust goto out_cached; 26701da177e4SLinus Torvalds 2671f3324a2aSNeilBrown status = -ECHILD; 267257b69181STrond Myklebust if (!may_block) 2673f3324a2aSNeilBrown goto out; 2674f3324a2aSNeilBrown 26751750d929SAnna Schumaker /* 26761750d929SAnna Schumaker * Determine which access bits we want to ask for... 26771750d929SAnna Schumaker */ 26781750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 267972832a24SFrank van der Linden if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 268072832a24SFrank van der Linden cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 268172832a24SFrank van der Linden NFS_ACCESS_XALIST; 268272832a24SFrank van der Linden } 26831750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 26841750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 26851750d929SAnna Schumaker else 26861750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 26871da177e4SLinus Torvalds cache.cred = cred; 26881da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2689a71ee337SSuresh Jayaraman if (status != 0) { 2690a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2691a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 269293ce4af7STrond Myklebust nfs_set_inode_stale(inode); 269393ce4af7STrond Myklebust else 269493ce4af7STrond Myklebust nfs_zap_caches(inode); 2695a71ee337SSuresh Jayaraman } 2696f4ce1299STrond Myklebust goto out; 2697a71ee337SSuresh Jayaraman } 26981da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2699f4ce1299STrond Myklebust out_cached: 2700ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2701bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2702f4ce1299STrond Myklebust status = -EACCES; 27031da177e4SLinus Torvalds out: 2704e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2705f4ce1299STrond Myklebust return status; 27061da177e4SLinus Torvalds } 27071da177e4SLinus Torvalds 2708af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2709af22f94aSTrond Myklebust { 2710af22f94aSTrond Myklebust int mask = 0; 2711af22f94aSTrond Myklebust 2712f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2713f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2714f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2715f8d9a897SWeston Andros Adamson } else { 27168a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2717af22f94aSTrond Myklebust mask |= MAY_READ; 27188a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2719af22f94aSTrond Myklebust mask |= MAY_WRITE; 2720f8d9a897SWeston Andros Adamson } 2721f8d9a897SWeston Andros Adamson 2722af22f94aSTrond Myklebust return mask; 2723af22f94aSTrond Myklebust } 2724af22f94aSTrond Myklebust 2725b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2726af22f94aSTrond Myklebust { 2727af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2728af22f94aSTrond Myklebust } 272989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2730af22f94aSTrond Myklebust 27315c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 27325c5fc09aSTrond Myklebust { 27335c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 273421c3ba7eSTrond Myklebust int ret = 0; 27355c5fc09aSTrond Myklebust 27363825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 27373825827eSTrond Myklebust return 0; 2738cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 27395c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 274021c3ba7eSTrond Myklebust return -ECHILD; 274121c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 274221c3ba7eSTrond Myklebust } 27435c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 27445c5fc09aSTrond Myklebust ret = -EACCES; 27455c5fc09aSTrond Myklebust return ret; 27465c5fc09aSTrond Myklebust } 27475c5fc09aSTrond Myklebust 274810556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 27491da177e4SLinus Torvalds { 2750b68572e0SNeilBrown const struct cred *cred = current_cred(); 27511da177e4SLinus Torvalds int res = 0; 27521da177e4SLinus Torvalds 275391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 275491d5b470SChuck Lever 2755e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 27561da177e4SLinus Torvalds goto out; 27571da177e4SLinus Torvalds /* Is this sys_access() ? */ 27589cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 27591da177e4SLinus Torvalds goto force_lookup; 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 27621da177e4SLinus Torvalds case S_IFLNK: 27631da177e4SLinus Torvalds goto out; 27641da177e4SLinus Torvalds case S_IFREG: 2765762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2766762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2767762674f8STrond Myklebust return 0; 27681da177e4SLinus Torvalds break; 27691da177e4SLinus Torvalds case S_IFDIR: 27701da177e4SLinus Torvalds /* 27711da177e4SLinus Torvalds * Optimize away all write operations, since the server 27721da177e4SLinus Torvalds * will check permissions when we perform the op. 27731da177e4SLinus Torvalds */ 27741da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 27751da177e4SLinus Torvalds goto out; 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds force_lookup: 27791da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 27801da177e4SLinus Torvalds goto out_notsup; 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 27831da177e4SLinus Torvalds out: 27845c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 27855c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2786f696a365SMiklos Szeredi 27871e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 27881e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 27891da177e4SLinus Torvalds return res; 27901da177e4SLinus Torvalds out_notsup: 2791d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2792d51ac1a8SNeilBrown return -ECHILD; 2793d51ac1a8SNeilBrown 27941da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 27951da177e4SLinus Torvalds if (res == 0) 27962830ba7fSAl Viro res = generic_permission(inode, mask); 27971e7cb3dcSChuck Lever goto out; 27981da177e4SLinus Torvalds } 2799ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds /* 28021da177e4SLinus Torvalds * Local variables: 28031da177e4SLinus Torvalds * version-control: t 28041da177e4SLinus Torvalds * kept-new-versions: 5 28051da177e4SLinus Torvalds * End: 28061da177e4SLinus Torvalds */ 2807