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; 144d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 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 { 58299424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5838cd51a0cSTrond Myklebust if (status != 0) { 5848cd51a0cSTrond Myklebust if (status == -EAGAIN) 5858cd51a0cSTrond Myklebust status = 0; 58699424380SBryan Schumaker break; 5878cd51a0cSTrond Myklebust } 58899424380SBryan Schumaker 5895c346854STrond Myklebust count++; 5905c346854STrond Myklebust 591a7a3b1e9SBenjamin Coddington if (desc->plus) 592a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 593a1147b82STrond Myklebust desc->dir_verifier); 5948cd51a0cSTrond Myklebust 595db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 5968cd51a0cSTrond Myklebust if (status != 0) 5978cd51a0cSTrond Myklebust break; 59899424380SBryan Schumaker } while (!entry->eof); 59999424380SBryan Schumaker 600ce85cfbeSBenjamin Coddington out_nopages: 60147c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 602db531db9SMax Kellermann array = kmap(page); 6038cd51a0cSTrond Myklebust array->eof_index = array->size; 60499424380SBryan Schumaker status = 0; 605db531db9SMax Kellermann kunmap(page); 60656e4ebf8SBryan Schumaker } 6076650239aSTrond Myklebust 6086650239aSTrond Myklebust put_page(scratch); 6098cd51a0cSTrond Myklebust return status; 6108cd51a0cSTrond Myklebust } 61156e4ebf8SBryan Schumaker 61256e4ebf8SBryan Schumaker static 613c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 61456e4ebf8SBryan Schumaker { 61556e4ebf8SBryan Schumaker unsigned int i; 61656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 61756e4ebf8SBryan Schumaker put_page(pages[i]); 61856e4ebf8SBryan Schumaker } 61956e4ebf8SBryan Schumaker 62056e4ebf8SBryan Schumaker /* 621bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 622bf211ca1Szhangliguang * to nfs_readdir_free_pages() 62356e4ebf8SBryan Schumaker */ 62456e4ebf8SBryan Schumaker static 625c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 62656e4ebf8SBryan Schumaker { 62756e4ebf8SBryan Schumaker unsigned int i; 62856e4ebf8SBryan Schumaker 62956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 63056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 63156e4ebf8SBryan Schumaker if (page == NULL) 63256e4ebf8SBryan Schumaker goto out_freepages; 63356e4ebf8SBryan Schumaker pages[i] = page; 63456e4ebf8SBryan Schumaker } 6356650239aSTrond Myklebust return 0; 63656e4ebf8SBryan Schumaker 63756e4ebf8SBryan Schumaker out_freepages: 638c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6396650239aSTrond Myklebust return -ENOMEM; 640d1bacf9eSBryan Schumaker } 641d1bacf9eSBryan Schumaker 642d1bacf9eSBryan Schumaker static 643d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 644d1bacf9eSBryan Schumaker { 64556e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 646d1bacf9eSBryan Schumaker struct nfs_entry entry; 647d1bacf9eSBryan Schumaker struct file *file = desc->file; 648d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6498cd51a0cSTrond Myklebust int status = -ENOMEM; 65056e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 651d1bacf9eSBryan Schumaker 6524b310319STrond Myklebust nfs_readdir_init_array(page); 6534b310319STrond Myklebust 654d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6550aded708STrond Myklebust entry.cookie = desc->last_cookie; 656d1bacf9eSBryan Schumaker entry.eof = 0; 657d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 658d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 659573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 660d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 661d1bacf9eSBryan Schumaker goto out; 662d1bacf9eSBryan Schumaker 66314c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 66414c43f76SDavid Quigley if (IS_ERR(entry.label)) { 66514c43f76SDavid Quigley status = PTR_ERR(entry.label); 66614c43f76SDavid Quigley goto out; 66714c43f76SDavid Quigley } 66814c43f76SDavid Quigley 6690795bf83SFabian Frederick array = kmap(page); 670d1bacf9eSBryan Schumaker 671c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6726650239aSTrond Myklebust if (status < 0) 673d1bacf9eSBryan Schumaker goto out_release_array; 674d1bacf9eSBryan Schumaker do { 675ac396128STrond Myklebust unsigned int pglen; 67656e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 677babddc72SBryan Schumaker 678d1bacf9eSBryan Schumaker if (status < 0) 679d1bacf9eSBryan Schumaker break; 680ac396128STrond Myklebust pglen = status; 6816650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6828cd51a0cSTrond Myklebust if (status < 0) { 6838cd51a0cSTrond Myklebust if (status == -ENOSPC) 6848cd51a0cSTrond Myklebust status = 0; 6858cd51a0cSTrond Myklebust break; 6868cd51a0cSTrond Myklebust } 6878cd51a0cSTrond Myklebust } while (array->eof_index < 0); 688d1bacf9eSBryan Schumaker 689c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 690d1bacf9eSBryan Schumaker out_release_array: 6910795bf83SFabian Frederick kunmap(page); 69214c43f76SDavid Quigley nfs4_label_free(entry.label); 693d1bacf9eSBryan Schumaker out: 694d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 695d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 696d1bacf9eSBryan Schumaker return status; 697d1bacf9eSBryan Schumaker } 698d1bacf9eSBryan Schumaker 699d1bacf9eSBryan Schumaker /* 700d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 701d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 702d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 703d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7041da177e4SLinus Torvalds */ 705d1bacf9eSBryan Schumaker static 706a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 707d1bacf9eSBryan Schumaker { 708a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 709496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7108cd51a0cSTrond Myklebust int ret; 711d1bacf9eSBryan Schumaker 7128cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7138cd51a0cSTrond Myklebust if (ret < 0) 714d1bacf9eSBryan Schumaker goto error; 715d1bacf9eSBryan Schumaker SetPageUptodate(page); 716d1bacf9eSBryan Schumaker 7172aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 718cd9ae2b6STrond Myklebust /* Should never happen */ 719cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 720cd9ae2b6STrond Myklebust } 7211da177e4SLinus Torvalds unlock_page(page); 7221da177e4SLinus Torvalds return 0; 7231da177e4SLinus Torvalds error: 7244b310319STrond Myklebust nfs_readdir_clear_array(page); 7251da177e4SLinus Torvalds unlock_page(page); 7268cd51a0cSTrond Myklebust return ret; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 729d1bacf9eSBryan Schumaker static 730d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7311da177e4SLinus Torvalds { 73209cbfeafSKirill A. Shutemov put_page(desc->page); 7331da177e4SLinus Torvalds desc->page = NULL; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736d1bacf9eSBryan Schumaker static 737d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7381da177e4SLinus Torvalds { 739a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 740a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds /* 744d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 745114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7461da177e4SLinus Torvalds */ 747d1bacf9eSBryan Schumaker static 748114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 749d1bacf9eSBryan Schumaker { 750227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 751227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 752d1bacf9eSBryan Schumaker int res; 753d1bacf9eSBryan Schumaker 754d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 755d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 756d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 757114de382STrond Myklebust res = lock_page_killable(desc->page); 75847c716cbSTrond Myklebust if (res != 0) 759114de382STrond Myklebust goto error; 760114de382STrond Myklebust res = -EAGAIN; 761114de382STrond Myklebust if (desc->page->mapping != NULL) { 762114de382STrond Myklebust res = nfs_readdir_search_array(desc); 763227823d2SDai Ngo if (res == 0) { 764227823d2SDai Ngo nfsi->page_index = desc->page_index; 765114de382STrond Myklebust return 0; 766114de382STrond Myklebust } 767227823d2SDai Ngo } 768114de382STrond Myklebust unlock_page(desc->page); 769114de382STrond Myklebust error: 770d1bacf9eSBryan Schumaker cache_page_release(desc); 771d1bacf9eSBryan Schumaker return res; 772d1bacf9eSBryan Schumaker } 773d1bacf9eSBryan Schumaker 774d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7751da177e4SLinus Torvalds static inline 7761da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7771da177e4SLinus Torvalds { 7788cd51a0cSTrond Myklebust int res; 779d1bacf9eSBryan Schumaker 7800aded708STrond Myklebust if (desc->page_index == 0) { 7818cd51a0cSTrond Myklebust desc->current_index = 0; 78259e356a9STrond Myklebust desc->prev_index = 0; 7830aded708STrond Myklebust desc->last_cookie = 0; 7840aded708STrond Myklebust } 78547c716cbSTrond Myklebust do { 786114de382STrond Myklebust res = find_and_lock_cache_page(desc); 78747c716cbSTrond Myklebust } while (res == -EAGAIN); 7881da177e4SLinus Torvalds return res; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7931da177e4SLinus Torvalds */ 7941da177e4SLinus Torvalds static 79523db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds struct file *file = desc->file; 798d1bacf9eSBryan Schumaker int i = 0; 799d1bacf9eSBryan Schumaker int res = 0; 800d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 8018ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 8028ef2ce3eSBryan Schumaker 8030795bf83SFabian Frederick array = kmap(desc->page); 804d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 805ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8061da177e4SLinus Torvalds 807ece0b423STrond Myklebust ent = &array->array[i]; 80823db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 80923db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 8106089dd0dSThomas Meyer desc->eof = true; 8111da177e4SLinus Torvalds break; 812ece0b423STrond Myklebust } 813d1bacf9eSBryan Schumaker if (i < (array->size-1)) 814d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 815d1bacf9eSBryan Schumaker else 816d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 81759e356a9STrond Myklebust if (nfs_readdir_use_cookie(file)) 81859e356a9STrond Myklebust desc->ctx->pos = *desc->dir_cookie; 81959e356a9STrond Myklebust else 82059e356a9STrond Myklebust desc->ctx->pos++; 8210c030806STrond Myklebust if (ctx->duped != 0) 8220c030806STrond Myklebust ctx->duped = 1; 8238cd51a0cSTrond Myklebust } 82447c716cbSTrond Myklebust if (array->eof_index >= 0) 8256089dd0dSThomas Meyer desc->eof = true; 826d1bacf9eSBryan Schumaker 8270795bf83SFabian Frederick kunmap(desc->page); 8281e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8291e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8301da177e4SLinus Torvalds return res; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds /* 8341da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8351da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8361da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8371da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8381da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8391da177e4SLinus Torvalds * 8401da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8411da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8421da177e4SLinus Torvalds * we should already have a complete representation of the 8431da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8441da177e4SLinus Torvalds */ 8451da177e4SLinus Torvalds static inline 84623db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds struct page *page = NULL; 8491da177e4SLinus Torvalds int status; 850496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8510c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8521da177e4SLinus Torvalds 8531e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8541e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8571da177e4SLinus Torvalds if (!page) { 8581da177e4SLinus Torvalds status = -ENOMEM; 8591da177e4SLinus Torvalds goto out; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds 8627a8e1dc3STrond Myklebust desc->page_index = 0; 8630aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8647a8e1dc3STrond Myklebust desc->page = page; 8650c030806STrond Myklebust ctx->duped = 0; 8667a8e1dc3STrond Myklebust 86785f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 86885f8607eSTrond Myklebust if (status < 0) 869d1bacf9eSBryan Schumaker goto out_release; 870d1bacf9eSBryan Schumaker 87123db8620SAl Viro status = nfs_do_filldir(desc); 8721da177e4SLinus Torvalds 873114de382STrond Myklebust out_release: 874114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 875114de382STrond Myklebust cache_page_release(desc); 8761da177e4SLinus Torvalds out: 8771e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8783110ff80SHarvey Harrison __func__, status); 8791da177e4SLinus Torvalds return status; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 88200a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 88300a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 88400a92642SOlivier Galibert whole directory. 8851da177e4SLinus Torvalds */ 88623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8871da177e4SLinus Torvalds { 888be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8892b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 89023db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 89159e356a9STrond Myklebust nfs_readdir_descriptor_t my_desc = { 89259e356a9STrond Myklebust .file = file, 89359e356a9STrond Myklebust .ctx = ctx, 89459e356a9STrond Myklebust .dir_cookie = &dir_ctx->dir_cookie, 89559e356a9STrond Myklebust .plus = nfs_use_readdirplus(inode, ctx), 89659e356a9STrond Myklebust }, 89759e356a9STrond Myklebust *desc = &my_desc; 89807b5ce8eSScott Mayhew int res = 0; 8991da177e4SLinus Torvalds 9006de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9016de1472fSAl Viro file, (long long)ctx->pos); 90291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 90391d5b470SChuck Lever 9041da177e4SLinus Torvalds /* 90523db8620SAl Viro * ctx->pos points to the dirent entry number. 906f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 90700a92642SOlivier Galibert * to either find the entry with the appropriate number or 90800a92642SOlivier Galibert * revalidate the cookie. 9091da177e4SLinus Torvalds */ 91079f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 91123db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 912fccca7fcSTrond Myklebust if (res < 0) 913fccca7fcSTrond Myklebust goto out; 914fccca7fcSTrond Myklebust 91547c716cbSTrond Myklebust do { 9161da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 91700a92642SOlivier Galibert 9181da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 919ece0b423STrond Myklebust res = 0; 9201da177e4SLinus Torvalds /* This means either end of directory */ 9216089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 9221da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 92323db8620SAl Viro res = uncached_readdir(desc); 924ece0b423STrond Myklebust if (res == 0) 9251da177e4SLinus Torvalds continue; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds break; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9303a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9311da177e4SLinus Torvalds nfs_zap_caches(inode); 932baf57a09STrond Myklebust desc->page_index = 0; 933a7a3b1e9SBenjamin Coddington desc->plus = false; 934a7a3b1e9SBenjamin Coddington desc->eof = false; 9351da177e4SLinus Torvalds continue; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds if (res < 0) 9381da177e4SLinus Torvalds break; 9391da177e4SLinus Torvalds 94023db8620SAl Viro res = nfs_do_filldir(desc); 941114de382STrond Myklebust unlock_page(desc->page); 942114de382STrond Myklebust cache_page_release(desc); 943ece0b423STrond Myklebust if (res < 0) 9441da177e4SLinus Torvalds break; 94547c716cbSTrond Myklebust } while (!desc->eof); 946fccca7fcSTrond Myklebust out: 9471e7cb3dcSChuck Lever if (res > 0) 9481e7cb3dcSChuck Lever res = 0; 9496de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9501da177e4SLinus Torvalds return res; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 953965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 954f0dd2136STrond Myklebust { 955b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 956480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 957b84e06c5SChuck Lever 9586de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9596de1472fSAl Viro filp, offset, whence); 960b84e06c5SChuck Lever 961965c8e59SAndrew Morton switch (whence) { 962f0dd2136STrond Myklebust default: 963b2b1ff3dSTrond Myklebust return -EINVAL; 964b2b1ff3dSTrond Myklebust case SEEK_SET: 965b2b1ff3dSTrond Myklebust if (offset < 0) 966b2b1ff3dSTrond Myklebust return -EINVAL; 967b2b1ff3dSTrond Myklebust inode_lock(inode); 968b2b1ff3dSTrond Myklebust break; 969b2b1ff3dSTrond Myklebust case SEEK_CUR: 970b2b1ff3dSTrond Myklebust if (offset == 0) 971b2b1ff3dSTrond Myklebust return filp->f_pos; 972b2b1ff3dSTrond Myklebust inode_lock(inode); 973b2b1ff3dSTrond Myklebust offset += filp->f_pos; 974b2b1ff3dSTrond Myklebust if (offset < 0) { 975b2b1ff3dSTrond Myklebust inode_unlock(inode); 976b2b1ff3dSTrond Myklebust return -EINVAL; 977b2b1ff3dSTrond Myklebust } 978f0dd2136STrond Myklebust } 979f0dd2136STrond Myklebust if (offset != filp->f_pos) { 980f0dd2136STrond Myklebust filp->f_pos = offset; 98159e356a9STrond Myklebust if (nfs_readdir_use_cookie(filp)) 98259e356a9STrond Myklebust dir_ctx->dir_cookie = offset; 98359e356a9STrond Myklebust else 984480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9858ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 986f0dd2136STrond Myklebust } 987b044f645SBenjamin Coddington inode_unlock(inode); 988f0dd2136STrond Myklebust return offset; 989f0dd2136STrond Myklebust } 990f0dd2136STrond Myklebust 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9931da177e4SLinus Torvalds * is a dummy operation. 9941da177e4SLinus Torvalds */ 99502c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 99602c24a82SJosef Bacik int datasync) 9971da177e4SLinus Torvalds { 9986de1472fSAl Viro struct inode *inode = file_inode(filp); 9997ea80859SChristoph Hellwig 10006de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 10011e7cb3dcSChuck Lever 10025955102cSAl Viro inode_lock(inode); 10036de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 10045955102cSAl Viro inode_unlock(inode); 10051da177e4SLinus Torvalds return 0; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 1008bfc69a45STrond Myklebust /** 1009bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1010302fad7bSTrond Myklebust * @dir: pointer to directory inode 1011bfc69a45STrond Myklebust * 1012bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1013bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1014bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1015bfc69a45STrond Myklebust * 1016efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 1017efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 1018efeda80dSTrond Myklebust * 1019bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1020bfc69a45STrond Myklebust */ 1021bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1022bfc69a45STrond Myklebust { 1023efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 1024bfc69a45STrond Myklebust } 102589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1026bfc69a45STrond Myklebust 1027efeda80dSTrond Myklebust /** 1028efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1029efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1030efeda80dSTrond Myklebust * @verf: previously saved change attribute 1031efeda80dSTrond Myklebust * 1032efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1033efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1034efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1035efeda80dSTrond Myklebust */ 1036efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1037efeda80dSTrond Myklebust { 1038efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1039efeda80dSTrond Myklebust } 1040efeda80dSTrond Myklebust 1041efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1042efeda80dSTrond Myklebust { 1043efeda80dSTrond Myklebust *verf |= 1UL; 1044efeda80dSTrond Myklebust } 1045efeda80dSTrond Myklebust 1046efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1047efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1048efeda80dSTrond Myklebust { 1049efeda80dSTrond Myklebust *verf &= ~1UL; 1050efeda80dSTrond Myklebust } 1051efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1052efeda80dSTrond Myklebust 1053efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1054efeda80dSTrond Myklebust { 1055efeda80dSTrond Myklebust return verf & 1; 1056efeda80dSTrond Myklebust } 1057efeda80dSTrond Myklebust 1058efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1059efeda80dSTrond Myklebust { 1060efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1061efeda80dSTrond Myklebust } 1062efeda80dSTrond Myklebust 1063efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1064efeda80dSTrond Myklebust { 1065efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1066efeda80dSTrond Myklebust 1067efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1068efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1069efeda80dSTrond Myklebust goto out; 1070efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1071efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1072efeda80dSTrond Myklebust out: 1073efeda80dSTrond Myklebust dentry->d_time = verf; 1074efeda80dSTrond Myklebust } 1075efeda80dSTrond Myklebust 1076efeda80dSTrond Myklebust /** 1077efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1078efeda80dSTrond Myklebust * @dentry: pointer to dentry 1079efeda80dSTrond Myklebust * @verf: verifier to save 1080efeda80dSTrond Myklebust * 1081efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1082efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1083efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1084efeda80dSTrond Myklebust * look it up again after a directory change. 1085efeda80dSTrond Myklebust */ 1086efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1087efeda80dSTrond Myklebust { 1088efeda80dSTrond Myklebust 1089efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1090efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1091efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1092efeda80dSTrond Myklebust } 1093efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1094efeda80dSTrond Myklebust 1095efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1096efeda80dSTrond Myklebust /** 1097efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1098efeda80dSTrond Myklebust * @inode: pointer to inode 1099efeda80dSTrond Myklebust * 1100efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1101efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1102efeda80dSTrond Myklebust * while holding a delegation. 1103efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1104efeda80dSTrond Myklebust * returned or revoked. 1105efeda80dSTrond Myklebust */ 1106efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1107efeda80dSTrond Myklebust { 1108efeda80dSTrond Myklebust struct dentry *alias; 1109efeda80dSTrond Myklebust 1110efeda80dSTrond Myklebust if (!inode) 1111efeda80dSTrond Myklebust return; 1112efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1113efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1114efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1115efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1116efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1117efeda80dSTrond Myklebust } 1118efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1119efeda80dSTrond Myklebust } 1120efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1121efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1122efeda80dSTrond Myklebust 11231da177e4SLinus Torvalds /* 11241da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 11251da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 11261da177e4SLinus Torvalds * and may need to be looked up again. 1127912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 11281da177e4SLinus Torvalds */ 1129912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1130912a108dSNeilBrown int rcu_walk) 11311da177e4SLinus Torvalds { 11321da177e4SLinus Torvalds if (IS_ROOT(dentry)) 11331da177e4SLinus Torvalds return 1; 11344eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 11354eec952eSTrond Myklebust return 0; 1136f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 11376ecc5e8fSTrond Myklebust return 0; 1138f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 11391cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1140912a108dSNeilBrown if (rcu_walk) 1141f2c77f4eSTrond Myklebust return 0; 11421cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 11431cd9cb05STrond Myklebust return 0; 11441cd9cb05STrond Myklebust } 1145f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1146f2c77f4eSTrond Myklebust return 0; 1147f2c77f4eSTrond Myklebust return 1; 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds /* 1151a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1152a12802caSTrond Myklebust * an O_EXCL create using this path component. 1153a12802caSTrond Myklebust */ 1154fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1155a12802caSTrond Myklebust { 1156a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1157a12802caSTrond Myklebust return 0; 1158fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1159a12802caSTrond Myklebust } 1160a12802caSTrond Myklebust 1161a12802caSTrond Myklebust /* 11621d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 11631d6757fbSTrond Myklebust * 11641d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 11651d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 11661d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 11671d6757fbSTrond Myklebust * 11681d6757fbSTrond Myklebust */ 116965a0c149STrond Myklebust static 1170fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 117365a0c149STrond Myklebust int ret; 11741da177e4SLinus Torvalds 117536d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 11764e99a1ffSTrond Myklebust return 0; 117747921921STrond Myklebust 117847921921STrond Myklebust if (flags & LOOKUP_OPEN) { 117947921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 118047921921STrond Myklebust case S_IFREG: 118147921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 118247921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 118347921921STrond Myklebust goto out; 118447921921STrond Myklebust /* Fallthrough */ 118547921921STrond Myklebust case S_IFDIR: 118647921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 118747921921STrond Myklebust break; 118847921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 118947921921STrond Myklebust goto out_force; 119047921921STrond Myklebust } 119147921921STrond Myklebust } 119247921921STrond Myklebust 11931da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1194fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 11951da177e4SLinus Torvalds goto out_force; 119665a0c149STrond Myklebust out: 1197a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 11981da177e4SLinus Torvalds out_force: 11991fa1e384SNeilBrown if (flags & LOOKUP_RCU) 12001fa1e384SNeilBrown return -ECHILD; 120165a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 120265a0c149STrond Myklebust if (ret != 0) 120365a0c149STrond Myklebust return ret; 120465a0c149STrond Myklebust goto out; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* 12081da177e4SLinus Torvalds * We judge how long we want to trust negative 12091da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 12101da177e4SLinus Torvalds * 12111da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 12121da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1213912a108dSNeilBrown * 1214912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1215912a108dSNeilBrown * suggesting a reval is needed. 12169f6d44d4STrond Myklebust * 12179f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 12189f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 12191da177e4SLinus Torvalds */ 12201da177e4SLinus Torvalds static inline 12211da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1222fa3c56bbSAl Viro unsigned int flags) 12231da177e4SLinus Torvalds { 12249f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 12251da177e4SLinus Torvalds return 0; 12264eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 12274eec952eSTrond Myklebust return 1; 1228912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12315ceb9d7fSTrond Myklebust static int 12325ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 12335ceb9d7fSTrond Myklebust struct inode *inode, int error) 12341da177e4SLinus Torvalds { 12355ceb9d7fSTrond Myklebust switch (error) { 12365ceb9d7fSTrond Myklebust case 1: 12376de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12386de1472fSAl Viro __func__, dentry); 12391da177e4SLinus Torvalds return 1; 12405ceb9d7fSTrond Myklebust case 0: 1241a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12421da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12431da177e4SLinus Torvalds /* Purge readdir caches. */ 12441da177e4SLinus Torvalds nfs_zap_caches(inode); 1245a3f432bfSJ. Bruce Fields /* 1246a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1247a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1248a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1249a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1250a3f432bfSJ. Bruce Fields */ 1251a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 12525ceb9d7fSTrond Myklebust return 1; 12531da177e4SLinus Torvalds } 12546de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12556de1472fSAl Viro __func__, dentry); 12561da177e4SLinus Torvalds return 0; 12575ceb9d7fSTrond Myklebust } 12586de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12596de1472fSAl Viro __func__, dentry, error); 1260e1fb4d05STrond Myklebust return error; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12635ceb9d7fSTrond Myklebust static int 12645ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 12655ceb9d7fSTrond Myklebust unsigned int flags) 12665ceb9d7fSTrond Myklebust { 12675ceb9d7fSTrond Myklebust int ret = 1; 12685ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 12695ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12705ceb9d7fSTrond Myklebust return -ECHILD; 12715ceb9d7fSTrond Myklebust ret = 0; 12725ceb9d7fSTrond Myklebust } 12735ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 12745ceb9d7fSTrond Myklebust } 12755ceb9d7fSTrond Myklebust 12765ceb9d7fSTrond Myklebust static int 12775ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 12785ceb9d7fSTrond Myklebust struct inode *inode) 12795ceb9d7fSTrond Myklebust { 12805ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12815ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12825ceb9d7fSTrond Myklebust } 12835ceb9d7fSTrond Myklebust 12845ceb9d7fSTrond Myklebust static int 12855ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 12865ceb9d7fSTrond Myklebust struct inode *inode) 12875ceb9d7fSTrond Myklebust { 12885ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 12895ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 12905ceb9d7fSTrond Myklebust struct nfs4_label *label; 1291a1147b82STrond Myklebust unsigned long dir_verifier; 12925ceb9d7fSTrond Myklebust int ret; 12935ceb9d7fSTrond Myklebust 12945ceb9d7fSTrond Myklebust ret = -ENOMEM; 12955ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 12965ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 12975ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 12985ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 12995ceb9d7fSTrond Myklebust goto out; 13005ceb9d7fSTrond Myklebust 1301a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1302f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 13035ceb9d7fSTrond Myklebust if (ret < 0) { 1304f7b37b8bSTrond Myklebust switch (ret) { 1305f7b37b8bSTrond Myklebust case -ESTALE: 1306f7b37b8bSTrond Myklebust case -ENOENT: 13075ceb9d7fSTrond Myklebust ret = 0; 1308f7b37b8bSTrond Myklebust break; 1309f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1310f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1311f7b37b8bSTrond Myklebust ret = 1; 1312f7b37b8bSTrond Myklebust } 13135ceb9d7fSTrond Myklebust goto out; 13145ceb9d7fSTrond Myklebust } 13155ceb9d7fSTrond Myklebust ret = 0; 13165ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 13175ceb9d7fSTrond Myklebust goto out; 13185ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 13195ceb9d7fSTrond Myklebust goto out; 13205ceb9d7fSTrond Myklebust 13215ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1322a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 13235ceb9d7fSTrond Myklebust 13245ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 13255ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 13265ceb9d7fSTrond Myklebust ret = 1; 13275ceb9d7fSTrond Myklebust out: 13285ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 13295ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 13305ceb9d7fSTrond Myklebust nfs4_label_free(label); 13315ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 13325ceb9d7fSTrond Myklebust } 13335ceb9d7fSTrond Myklebust 13345ceb9d7fSTrond Myklebust /* 13355ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 13365ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 13375ceb9d7fSTrond Myklebust * lookup. 13385ceb9d7fSTrond Myklebust * 13395ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 13405ceb9d7fSTrond Myklebust * we have an inode! 13415ceb9d7fSTrond Myklebust * 13425ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 13435ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 13445ceb9d7fSTrond Myklebust */ 13455ceb9d7fSTrond Myklebust static int 13465ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 13475ceb9d7fSTrond Myklebust unsigned int flags) 13485ceb9d7fSTrond Myklebust { 13495ceb9d7fSTrond Myklebust struct inode *inode; 13505ceb9d7fSTrond Myklebust int error; 13515ceb9d7fSTrond Myklebust 13525ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 13535ceb9d7fSTrond Myklebust inode = d_inode(dentry); 13545ceb9d7fSTrond Myklebust 13555ceb9d7fSTrond Myklebust if (!inode) 13565ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 13575ceb9d7fSTrond Myklebust 13585ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 13595ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13605ceb9d7fSTrond Myklebust __func__, dentry); 13615ceb9d7fSTrond Myklebust goto out_bad; 13625ceb9d7fSTrond Myklebust } 13635ceb9d7fSTrond Myklebust 1364efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 13655ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 13665ceb9d7fSTrond Myklebust 13675ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 13685ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 13695ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 13705ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 13715ceb9d7fSTrond Myklebust if (error) { 13725ceb9d7fSTrond Myklebust if (error == -ESTALE) 13735ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 13745ceb9d7fSTrond Myklebust goto out_bad; 13755ceb9d7fSTrond Myklebust } 13765ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 13775ceb9d7fSTrond Myklebust goto out_valid; 13785ceb9d7fSTrond Myklebust } 13795ceb9d7fSTrond Myklebust 13805ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13815ceb9d7fSTrond Myklebust return -ECHILD; 13825ceb9d7fSTrond Myklebust 13835ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 13845ceb9d7fSTrond Myklebust goto out_bad; 13855ceb9d7fSTrond Myklebust 13865ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 13875ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 13885ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 13895ceb9d7fSTrond Myklebust return error; 13905ceb9d7fSTrond Myklebust out_valid: 13915ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 13925ceb9d7fSTrond Myklebust out_bad: 13935ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13945ceb9d7fSTrond Myklebust return -ECHILD; 13955ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 13965ceb9d7fSTrond Myklebust } 13975ceb9d7fSTrond Myklebust 13985ceb9d7fSTrond Myklebust static int 1399c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1400c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 14015ceb9d7fSTrond Myklebust { 14025ceb9d7fSTrond Myklebust struct dentry *parent; 14035ceb9d7fSTrond Myklebust struct inode *dir; 14045ceb9d7fSTrond Myklebust int ret; 14055ceb9d7fSTrond Myklebust 14065ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 14075ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 14085ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 14095ceb9d7fSTrond Myklebust if (!dir) 14105ceb9d7fSTrond Myklebust return -ECHILD; 1411c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 14125ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 14135ceb9d7fSTrond Myklebust return -ECHILD; 14145ceb9d7fSTrond Myklebust } else { 14155ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1416c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 14175ceb9d7fSTrond Myklebust dput(parent); 14185ceb9d7fSTrond Myklebust } 14195ceb9d7fSTrond Myklebust return ret; 14205ceb9d7fSTrond Myklebust } 14215ceb9d7fSTrond Myklebust 1422c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1423c7944ebbSTrond Myklebust { 1424c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1425c7944ebbSTrond Myklebust } 1426c7944ebbSTrond Myklebust 14271da177e4SLinus Torvalds /* 14282b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1429ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1430ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1431ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1432ecf3d1f1SJeff Layton * 1433ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1434ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1435ecf3d1f1SJeff Layton */ 1436ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1437ecf3d1f1SJeff Layton { 14382b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 14399cdd1d3fSTrond Myklebust int error = 0; 1440ecf3d1f1SJeff Layton 1441ecf3d1f1SJeff Layton /* 1442ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1443ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1444ecf3d1f1SJeff Layton * eventually need to do something more here. 1445ecf3d1f1SJeff Layton */ 1446ecf3d1f1SJeff Layton if (!inode) { 14476de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 14486de1472fSAl Viro __func__, dentry); 1449ecf3d1f1SJeff Layton return 1; 1450ecf3d1f1SJeff Layton } 1451ecf3d1f1SJeff Layton 1452ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 14536de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 14546de1472fSAl Viro __func__, dentry); 1455ecf3d1f1SJeff Layton return 0; 1456ecf3d1f1SJeff Layton } 1457ecf3d1f1SJeff Layton 1458b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1459ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1460ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1461ecf3d1f1SJeff Layton return !error; 1462ecf3d1f1SJeff Layton } 1463ecf3d1f1SJeff Layton 1464ecf3d1f1SJeff Layton /* 14651da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 14661da177e4SLinus Torvalds */ 1467fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 14681da177e4SLinus Torvalds { 14696de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 14706de1472fSAl Viro dentry, dentry->d_flags); 14711da177e4SLinus Torvalds 147277f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 14732b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 147477f11192STrond Myklebust return 1; 147577f11192STrond Myklebust 14761da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 14771da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 14781da177e4SLinus Torvalds return 1; 14791da177e4SLinus Torvalds } 14801751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 14811da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 14821da177e4SLinus Torvalds * files will be cleaned up during umount */ 14831da177e4SLinus Torvalds return 1; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds return 0; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 14891f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 14901b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 14911b83d707STrond Myklebust { 14921b83d707STrond Myklebust spin_lock(&inode->i_lock); 14931f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 149459a707b0STrond Myklebust if (inode->i_nlink > 0) 149559a707b0STrond Myklebust drop_nlink(inode); 149659a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 149716e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 149816e14375STrond Myklebust | NFS_INO_INVALID_CTIME 149959a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 150059a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 15011b83d707STrond Myklebust spin_unlock(&inode->i_lock); 15021b83d707STrond Myklebust } 15031b83d707STrond Myklebust 15041da177e4SLinus Torvalds /* 15051da177e4SLinus Torvalds * Called when the dentry loses inode. 15061da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 15071da177e4SLinus Torvalds */ 15081da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 15091da177e4SLinus Torvalds { 151083672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 151183672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 151283672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 151383672d39SNeil Brown 15141da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1515e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 15161f018458STrond Myklebust nfs_drop_nlink(inode); 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds iput(inode); 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 1521b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1522b1942c5fSAl Viro { 1523b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1524b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1525b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1526b1942c5fSAl Viro WARN_ON(1); 1527b1942c5fSAl Viro else 1528b1942c5fSAl Viro kfree(dentry->d_fsdata); 1529b1942c5fSAl Viro } 1530b1942c5fSAl Viro } 1531b1942c5fSAl Viro 1532f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 15331da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1534ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 15351da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15361da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 153736d43a43SDavid Howells .d_automount = nfs_d_automount, 1538b1942c5fSAl Viro .d_release = nfs_d_release, 15391da177e4SLinus Torvalds }; 1540ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 15411da177e4SLinus Torvalds 1542597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 15431da177e4SLinus Torvalds { 15441da177e4SLinus Torvalds struct dentry *res; 15451da177e4SLinus Torvalds struct inode *inode = NULL; 1546e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1547e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 15481775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1549a1147b82STrond Myklebust unsigned long dir_verifier; 15501da177e4SLinus Torvalds int error; 15511da177e4SLinus Torvalds 15526de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 155391d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 15541da177e4SLinus Torvalds 1555130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1556130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 15571da177e4SLinus Torvalds 1558fd684071STrond Myklebust /* 1559fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1560fd684071STrond Myklebust * but don't hash the dentry. 1561fd684071STrond Myklebust */ 15629f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1563130f9ab7SAl Viro return NULL; 15641da177e4SLinus Torvalds 1565e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1566e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1567e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1568e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1569e1fb4d05STrond Myklebust goto out; 1570e1fb4d05STrond Myklebust 157114c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 157214c43f76SDavid Quigley if (IS_ERR(label)) 157314c43f76SDavid Quigley goto out; 157414c43f76SDavid Quigley 1575a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 15766e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1577f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 15781da177e4SLinus Torvalds if (error == -ENOENT) 15791da177e4SLinus Torvalds goto no_entry; 15801da177e4SLinus Torvalds if (error < 0) { 15811da177e4SLinus Torvalds res = ERR_PTR(error); 1582bf130914SAl Viro goto out_label; 15831da177e4SLinus Torvalds } 15841775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1585bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 158603f28e3aSTrond Myklebust if (IS_ERR(res)) 1587bf130914SAl Viro goto out_label; 158854ceac45SDavid Howells 158963519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 159063519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1591d69ee9b8STrond Myklebust 15921da177e4SLinus Torvalds no_entry: 159341d28bcaSAl Viro res = d_splice_alias(inode, dentry); 15949eaef27bSTrond Myklebust if (res != NULL) { 15959eaef27bSTrond Myklebust if (IS_ERR(res)) 1596bf130914SAl Viro goto out_label; 15971da177e4SLinus Torvalds dentry = res; 15989eaef27bSTrond Myklebust } 1599a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1600bf130914SAl Viro out_label: 16016e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 160214c43f76SDavid Quigley nfs4_label_free(label); 16031da177e4SLinus Torvalds out: 1604e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1605e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 16061da177e4SLinus Torvalds return res; 16071da177e4SLinus Torvalds } 1608ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 16091da177e4SLinus Torvalds 161089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 16110b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 16121da177e4SLinus Torvalds 1613f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 16140ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1615b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 16161da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 16171da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 161836d43a43SDavid Howells .d_automount = nfs_d_automount, 1619b1942c5fSAl Viro .d_release = nfs_d_release, 16201da177e4SLinus Torvalds }; 162189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 16221da177e4SLinus Torvalds 16238a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 16248a5e929dSAl Viro { 16258a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 16268a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 16278a5e929dSAl Viro res |= FMODE_READ; 16288a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 16298a5e929dSAl Viro res |= FMODE_WRITE; 16308a5e929dSAl Viro return res; 16318a5e929dSAl Viro } 16328a5e929dSAl Viro 1633532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1634cd9a1c0eSTrond Myklebust { 1635532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1636cd9a1c0eSTrond Myklebust } 1637cd9a1c0eSTrond Myklebust 1638cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1639cd9a1c0eSTrond Myklebust { 1640f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1641cd9a1c0eSTrond Myklebust return 0; 1642cd9a1c0eSTrond Myklebust } 1643cd9a1c0eSTrond Myklebust 1644d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 16450dd2b474SMiklos Szeredi struct dentry *dentry, 1646b452a458SAl Viro struct file *file, unsigned open_flags) 1647cd9a1c0eSTrond Myklebust { 16480dd2b474SMiklos Szeredi int err; 16490dd2b474SMiklos Szeredi 1650be12af3eSAl Viro err = finish_open(file, dentry, do_open); 165130d90494SAl Viro if (err) 1652d9585277SAl Viro goto out; 1653eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 165430d90494SAl Viro nfs_file_set_open_context(file, ctx); 1655eaa2b82cSNeilBrown else 16569821421aSTrond Myklebust err = -EOPENSTALE; 1657cd9a1c0eSTrond Myklebust out: 1658d9585277SAl Viro return err; 1659cd9a1c0eSTrond Myklebust } 1660cd9a1c0eSTrond Myklebust 166173a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 166230d90494SAl Viro struct file *file, unsigned open_flags, 166344907d79SAl Viro umode_t mode) 16641da177e4SLinus Torvalds { 1665c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1666cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 16670dd2b474SMiklos Szeredi struct dentry *res; 16680dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1669f46e0bd3STrond Myklebust struct inode *inode; 16701472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1671c94c0953SAl Viro bool switched = false; 167273a09dd9SAl Viro int created = 0; 1673898f635cSTrond Myklebust int err; 16741da177e4SLinus Torvalds 16750dd2b474SMiklos Szeredi /* Expect a negative dentry */ 16762b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 16770dd2b474SMiklos Szeredi 16781e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 16796de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 16801e7cb3dcSChuck Lever 16819597c13bSJeff Layton err = nfs_check_flags(open_flags); 16829597c13bSJeff Layton if (err) 16839597c13bSJeff Layton return err; 16849597c13bSJeff Layton 16850dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 16860dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 168700699ad8SAl Viro if (!d_in_lookup(dentry)) { 16880dd2b474SMiklos Szeredi /* 16890dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 16900dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 16910dd2b474SMiklos Szeredi * again 16920dd2b474SMiklos Szeredi */ 1693d9585277SAl Viro return -ENOENT; 16940dd2b474SMiklos Szeredi } 16951472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 16961da177e4SLinus Torvalds goto no_open; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16990dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1700d9585277SAl Viro return -ENAMETOOLONG; 17011da177e4SLinus Torvalds 17020dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1703dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1704dff25ddbSAndreas Gruenbacher 1705dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1706dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1707dff25ddbSAndreas Gruenbacher 1708536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1709dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 17100dd2b474SMiklos Szeredi } 1711536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1712536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1713536e43d1STrond Myklebust attr.ia_size = 0; 1714cd9a1c0eSTrond Myklebust } 1715cd9a1c0eSTrond Myklebust 1716c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1717c94c0953SAl Viro d_drop(dentry); 1718c94c0953SAl Viro switched = true; 1719c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1720c94c0953SAl Viro &dentry->d_name, &wq); 1721c94c0953SAl Viro if (IS_ERR(dentry)) 1722c94c0953SAl Viro return PTR_ERR(dentry); 1723c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1724c94c0953SAl Viro return finish_no_open(file, dentry); 1725c94c0953SAl Viro } 1726c94c0953SAl Viro 1727532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 17280dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 17290dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1730d9585277SAl Viro goto out; 17310dd2b474SMiklos Szeredi 17326e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 173373a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 173473a09dd9SAl Viro if (created) 173573a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1736275bb307STrond Myklebust if (IS_ERR(inode)) { 17370dd2b474SMiklos Szeredi err = PTR_ERR(inode); 17386e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17392d9db750STrond Myklebust put_nfs_open_context(ctx); 1740d20cb71dSAl Viro d_drop(dentry); 17410dd2b474SMiklos Szeredi switch (err) { 17421da177e4SLinus Torvalds case -ENOENT: 1743774d9513SPeng Tao d_splice_alias(NULL, dentry); 1744809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17450dd2b474SMiklos Szeredi break; 17461788ea6eSJeff Layton case -EISDIR: 17476f926b5bSTrond Myklebust case -ENOTDIR: 17486f926b5bSTrond Myklebust goto no_open; 17491da177e4SLinus Torvalds case -ELOOP: 17500dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 17511da177e4SLinus Torvalds goto no_open; 17520dd2b474SMiklos Szeredi break; 17531da177e4SLinus Torvalds /* case -EINVAL: */ 17541da177e4SLinus Torvalds default: 17550dd2b474SMiklos Szeredi break; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds goto out; 17581da177e4SLinus Torvalds } 17590dd2b474SMiklos Szeredi 1760b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 17616e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17622d9db750STrond Myklebust put_nfs_open_context(ctx); 1763d9585277SAl Viro out: 1764c94c0953SAl Viro if (unlikely(switched)) { 1765c94c0953SAl Viro d_lookup_done(dentry); 1766c94c0953SAl Viro dput(dentry); 1767c94c0953SAl Viro } 1768d9585277SAl Viro return err; 17690dd2b474SMiklos Szeredi 17701da177e4SLinus Torvalds no_open: 17711472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1772c94c0953SAl Viro if (switched) { 1773c94c0953SAl Viro d_lookup_done(dentry); 1774c94c0953SAl Viro if (!res) 1775c94c0953SAl Viro res = dentry; 1776c94c0953SAl Viro else 1777c94c0953SAl Viro dput(dentry); 1778c94c0953SAl Viro } 17790dd2b474SMiklos Szeredi if (IS_ERR(res)) 1780c94c0953SAl Viro return PTR_ERR(res); 1781e45198a6SAl Viro return finish_no_open(file, res); 17821da177e4SLinus Torvalds } 178389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 17841da177e4SLinus Torvalds 1785c7944ebbSTrond Myklebust static int 1786c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1787c7944ebbSTrond Myklebust unsigned int flags) 17881da177e4SLinus Torvalds { 1789657e94b6SNick Piggin struct inode *inode; 17901da177e4SLinus Torvalds 1791fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1792c7944ebbSTrond Myklebust goto full_reval; 1793eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1794c7944ebbSTrond Myklebust goto full_reval; 17952b484297STrond Myklebust 17962b0143b5SDavid Howells inode = d_inode(dentry); 17972b484297STrond Myklebust 17981da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 17991da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 18001da177e4SLinus Torvalds */ 1801c7944ebbSTrond Myklebust if (inode == NULL) 1802c7944ebbSTrond Myklebust goto full_reval; 180349317a7fSNeilBrown 1804efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 1805c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1806216d5d06STrond Myklebust 18071da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 18081da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1809c7944ebbSTrond Myklebust goto full_reval; 1810c7944ebbSTrond Myklebust 18111da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1812c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1813c7944ebbSTrond Myklebust goto reval_dentry; 1814c7944ebbSTrond Myklebust 1815c7944ebbSTrond Myklebust /* Check if the directory changed */ 1816c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1817c7944ebbSTrond Myklebust goto reval_dentry; 18181da177e4SLinus Torvalds 18190ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1820c7944ebbSTrond Myklebust return 1; 1821c7944ebbSTrond Myklebust reval_dentry: 1822c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1823c7944ebbSTrond Myklebust return -ECHILD; 182442f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 18250ef97dcfSMiklos Szeredi 1826c7944ebbSTrond Myklebust full_reval: 1827c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1828c7944ebbSTrond Myklebust } 1829535918f1STrond Myklebust 1830c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1831c7944ebbSTrond Myklebust { 1832c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1833c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1834c0204fd2STrond Myklebust } 1835c0204fd2STrond Myklebust 18361da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 18371da177e4SLinus Torvalds 1838406cd915SBenjamin Coddington struct dentry * 1839406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 18401775fd3eSDavid Quigley struct nfs_fattr *fattr, 18411775fd3eSDavid Quigley struct nfs4_label *label) 18421da177e4SLinus Torvalds { 1843fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 18442b0143b5SDavid Howells struct inode *dir = d_inode(parent); 18451da177e4SLinus Torvalds struct inode *inode; 1846b0c6108eSAl Viro struct dentry *d; 1847406cd915SBenjamin Coddington int error; 18481da177e4SLinus Torvalds 1849fab728e1STrond Myklebust d_drop(dentry); 1850fab728e1STrond Myklebust 18511da177e4SLinus Torvalds if (fhandle->size == 0) { 1852f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 18531da177e4SLinus Torvalds if (error) 1854fab728e1STrond Myklebust goto out_error; 18551da177e4SLinus Torvalds } 18565724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18571da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 18581da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1859a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1860a841b54dSTrond Myklebust fattr, NULL, NULL); 18611da177e4SLinus Torvalds if (error < 0) 1862fab728e1STrond Myklebust goto out_error; 18631da177e4SLinus Torvalds } 18641775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1865b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1866fab728e1STrond Myklebust out: 1867fab728e1STrond Myklebust dput(parent); 1868406cd915SBenjamin Coddington return d; 1869fab728e1STrond Myklebust out_error: 1870fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1871406cd915SBenjamin Coddington d = ERR_PTR(error); 1872406cd915SBenjamin Coddington goto out; 1873406cd915SBenjamin Coddington } 1874406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1875406cd915SBenjamin Coddington 1876406cd915SBenjamin Coddington /* 1877406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1878406cd915SBenjamin Coddington */ 1879406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1880406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1881406cd915SBenjamin Coddington struct nfs4_label *label) 1882406cd915SBenjamin Coddington { 1883406cd915SBenjamin Coddington struct dentry *d; 1884406cd915SBenjamin Coddington 1885406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1886406cd915SBenjamin Coddington if (IS_ERR(d)) 1887406cd915SBenjamin Coddington return PTR_ERR(d); 1888406cd915SBenjamin Coddington 1889406cd915SBenjamin Coddington /* Callers don't care */ 1890406cd915SBenjamin Coddington dput(d); 1891406cd915SBenjamin Coddington return 0; 18921da177e4SLinus Torvalds } 1893ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds /* 18961da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 18971da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 18981da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 18991da177e4SLinus Torvalds * reply path made it appear to have failed. 19001da177e4SLinus Torvalds */ 1901597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1902ebfc3b49SAl Viro umode_t mode, bool excl) 19031da177e4SLinus Torvalds { 19041da177e4SLinus Torvalds struct iattr attr; 1905ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 19061da177e4SLinus Torvalds int error; 19071da177e4SLinus Torvalds 19081e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 19096de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds attr.ia_mode = mode; 19121da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19131da177e4SLinus Torvalds 19148b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 19158867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 19168b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 19171da177e4SLinus Torvalds if (error != 0) 19181da177e4SLinus Torvalds goto out_err; 19191da177e4SLinus Torvalds return 0; 19201da177e4SLinus Torvalds out_err: 19211da177e4SLinus Torvalds d_drop(dentry); 19221da177e4SLinus Torvalds return error; 19231da177e4SLinus Torvalds } 1924ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19281da177e4SLinus Torvalds */ 1929597d9289SBryan Schumaker int 19301a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 19311da177e4SLinus Torvalds { 19321da177e4SLinus Torvalds struct iattr attr; 19331da177e4SLinus Torvalds int status; 19341da177e4SLinus Torvalds 19351e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 19366de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds attr.ia_mode = mode; 19391da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19401da177e4SLinus Torvalds 19411ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 19421da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 19431ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 19441da177e4SLinus Torvalds if (status != 0) 19451da177e4SLinus Torvalds goto out_err; 19461da177e4SLinus Torvalds return 0; 19471da177e4SLinus Torvalds out_err: 19481da177e4SLinus Torvalds d_drop(dentry); 19491da177e4SLinus Torvalds return status; 19501da177e4SLinus Torvalds } 1951ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds /* 19541da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19551da177e4SLinus Torvalds */ 1956597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19571da177e4SLinus Torvalds { 19581da177e4SLinus Torvalds struct iattr attr; 19591da177e4SLinus Torvalds int error; 19601da177e4SLinus Torvalds 19611e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 19626de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19651da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 19661da177e4SLinus Torvalds 19671ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 19681da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 19691ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 19701da177e4SLinus Torvalds if (error != 0) 19711da177e4SLinus Torvalds goto out_err; 19721da177e4SLinus Torvalds return 0; 19731da177e4SLinus Torvalds out_err: 19741da177e4SLinus Torvalds d_drop(dentry); 19751da177e4SLinus Torvalds return error; 19761da177e4SLinus Torvalds } 1977ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 19781da177e4SLinus Torvalds 1979d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1980d45b9d8bSTrond Myklebust { 1981dc3f4198SAl Viro if (simple_positive(dentry)) 1982d45b9d8bSTrond Myklebust d_delete(dentry); 1983d45b9d8bSTrond Myklebust } 1984d45b9d8bSTrond Myklebust 1985597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 19861da177e4SLinus Torvalds { 19871da177e4SLinus Torvalds int error; 19881da177e4SLinus Torvalds 19891e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 19906de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19911da177e4SLinus Torvalds 19921ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 19932b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1994884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 19951da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 19961da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1997ba6c0592STrond Myklebust switch (error) { 1998ba6c0592STrond Myklebust case 0: 19992b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 2000ba6c0592STrond Myklebust break; 2001ba6c0592STrond Myklebust case -ENOENT: 2002d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 2003ba6c0592STrond Myklebust } 2004884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2005ba6c0592STrond Myklebust } else 2006ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 20071ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds return error; 20101da177e4SLinus Torvalds } 2011ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds /* 20141da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 20151da177e4SLinus Torvalds * and after checking that the file has only one user. 20161da177e4SLinus Torvalds * 20171da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 20181da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 20191da177e4SLinus Torvalds */ 20201da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 20211da177e4SLinus Torvalds { 20222b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 20232b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 20241da177e4SLinus Torvalds int error = -EBUSY; 20251da177e4SLinus Torvalds 20266de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 20291da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 20301da177e4SLinus Torvalds error = 0; 20311da177e4SLinus Torvalds goto out; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20341ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 20351da177e4SLinus Torvalds if (inode != NULL) { 2036912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 20371da177e4SLinus Torvalds if (error == 0) 20381b83d707STrond Myklebust nfs_drop_nlink(inode); 20391da177e4SLinus Torvalds } else 2040912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2041d45b9d8bSTrond Myklebust if (error == -ENOENT) 2042d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 20431ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 20441da177e4SLinus Torvalds out: 20451da177e4SLinus Torvalds return error; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 20491da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 20501da177e4SLinus Torvalds * 20511da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 20521da177e4SLinus Torvalds */ 2053597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 20541da177e4SLinus Torvalds { 20551da177e4SLinus Torvalds int error; 20561da177e4SLinus Torvalds int need_rehash = 0; 20571da177e4SLinus Torvalds 20581e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 20596de1472fSAl Viro dir->i_ino, dentry); 20601da177e4SLinus Torvalds 20611ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 20621da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 206384d08fa8SAl Viro if (d_count(dentry) > 1) { 20641da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2065ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 20662b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 20671da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 20681ca42382STrond Myklebust goto out; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 20711da177e4SLinus Torvalds __d_drop(dentry); 20721da177e4SLinus Torvalds need_rehash = 1; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 20751da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2076d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 20771da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 20781da177e4SLinus Torvalds } else if (need_rehash) 20791da177e4SLinus Torvalds d_rehash(dentry); 20801ca42382STrond Myklebust out: 20811ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 20821da177e4SLinus Torvalds return error; 20831da177e4SLinus Torvalds } 2084ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 20851da177e4SLinus Torvalds 2086873101b3SChuck Lever /* 2087873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2088873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2089873101b3SChuck Lever * using prepare_write/commit_write. 2090873101b3SChuck Lever * 2091873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2092873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2093873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2094873101b3SChuck Lever * symlink request has completed on the server. 2095873101b3SChuck Lever * 2096873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2097873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2098873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2099873101b3SChuck Lever * and move the raw page into its mapping. 2100873101b3SChuck Lever */ 2101597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 21021da177e4SLinus Torvalds { 2103873101b3SChuck Lever struct page *page; 2104873101b3SChuck Lever char *kaddr; 21051da177e4SLinus Torvalds struct iattr attr; 2106873101b3SChuck Lever unsigned int pathlen = strlen(symname); 21071da177e4SLinus Torvalds int error; 21081da177e4SLinus Torvalds 21091e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 21106de1472fSAl Viro dir->i_ino, dentry, symname); 21111da177e4SLinus Torvalds 2112873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2113873101b3SChuck Lever return -ENAMETOOLONG; 21141da177e4SLinus Torvalds 2115873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2116873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 21171da177e4SLinus Torvalds 2118e8ecde25SAl Viro page = alloc_page(GFP_USER); 211976566991STrond Myklebust if (!page) 2120873101b3SChuck Lever return -ENOMEM; 2121873101b3SChuck Lever 2122e8ecde25SAl Viro kaddr = page_address(page); 2123873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2124873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2125873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2126873101b3SChuck Lever 21271ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 212894a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 21291ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2130873101b3SChuck Lever if (error != 0) { 21311e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2132873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 21336de1472fSAl Viro dentry, symname, error); 21341da177e4SLinus Torvalds d_drop(dentry); 2135873101b3SChuck Lever __free_page(page); 21361da177e4SLinus Torvalds return error; 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds 2139873101b3SChuck Lever /* 2140873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2141873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2142873101b3SChuck Lever */ 21432b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2144873101b3SChuck Lever GFP_KERNEL)) { 2145873101b3SChuck Lever SetPageUptodate(page); 2146873101b3SChuck Lever unlock_page(page); 2147a0b54addSRafael Aquini /* 2148a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2149a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2150a0b54addSRafael Aquini */ 215109cbfeafSKirill A. Shutemov put_page(page); 2152873101b3SChuck Lever } else 2153873101b3SChuck Lever __free_page(page); 2154873101b3SChuck Lever 2155873101b3SChuck Lever return 0; 2156873101b3SChuck Lever } 2157ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2158873101b3SChuck Lever 2159597d9289SBryan Schumaker int 21601da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 21611da177e4SLinus Torvalds { 21622b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 21631da177e4SLinus Torvalds int error; 21641da177e4SLinus Torvalds 21656de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 21666de1472fSAl Viro old_dentry, dentry); 21671da177e4SLinus Torvalds 21681fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 21699697d234STrond Myklebust d_drop(dentry); 21701da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2171cf809556STrond Myklebust if (error == 0) { 21727de9c6eeSAl Viro ihold(inode); 21739697d234STrond Myklebust d_add(dentry, inode); 2174cf809556STrond Myklebust } 21751fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 21761da177e4SLinus Torvalds return error; 21771da177e4SLinus Torvalds } 2178ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds /* 21811da177e4SLinus Torvalds * RENAME 21821da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 21831da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 21841da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 21851da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 21861da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 21871da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 21881da177e4SLinus Torvalds * 21891da177e4SLinus Torvalds * FIXED. 21901da177e4SLinus Torvalds * 21911da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 21921da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 21931da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 21941da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 21951da177e4SLinus Torvalds * using the inode layer 21961da177e4SLinus Torvalds * 21971da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 21981da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 21991da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 22001da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 22011da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 22021da177e4SLinus Torvalds * the rename. 22031da177e4SLinus Torvalds */ 2204597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 22051cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 22061cd66c93SMiklos Szeredi unsigned int flags) 22071da177e4SLinus Torvalds { 22082b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 22092b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2210d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 221180a491fdSJeff Layton struct rpc_task *task; 22121da177e4SLinus Torvalds int error = -EBUSY; 22131da177e4SLinus Torvalds 22141cd66c93SMiklos Szeredi if (flags) 22151cd66c93SMiklos Szeredi return -EINVAL; 22161cd66c93SMiklos Szeredi 22176de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 22186de1472fSAl Viro old_dentry, new_dentry, 221984d08fa8SAl Viro d_count(new_dentry)); 22201da177e4SLinus Torvalds 222170ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 22221da177e4SLinus Torvalds /* 222328f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 222428f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 222528f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 222628f79a1aSMiklos Szeredi * the new target. 22271da177e4SLinus Torvalds */ 222827226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 222927226104SMiklos Szeredi /* 223027226104SMiklos Szeredi * To prevent any new references to the target during the 223127226104SMiklos Szeredi * rename, we unhash the dentry in advance. 223227226104SMiklos Szeredi */ 2233d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 223427226104SMiklos Szeredi d_drop(new_dentry); 2235d9f29500SBenjamin Coddington rehash = new_dentry; 2236d9f29500SBenjamin Coddington } 223727226104SMiklos Szeredi 223884d08fa8SAl Viro if (d_count(new_dentry) > 2) { 22391da177e4SLinus Torvalds int err; 224027226104SMiklos Szeredi 22411da177e4SLinus Torvalds /* copy the target dentry's name */ 22421da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 22431da177e4SLinus Torvalds &new_dentry->d_name); 22441da177e4SLinus Torvalds if (!dentry) 22451da177e4SLinus Torvalds goto out; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds /* silly-rename the existing target ... */ 22481da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 224924e93025SMiklos Szeredi if (err) 22501da177e4SLinus Torvalds goto out; 225124e93025SMiklos Szeredi 225224e93025SMiklos Szeredi new_dentry = dentry; 2253d9f29500SBenjamin Coddington rehash = NULL; 225424e93025SMiklos Szeredi new_inode = NULL; 2255b1e4adf4STrond Myklebust } 225627226104SMiklos Szeredi } 22571da177e4SLinus Torvalds 2258d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 225980a491fdSJeff Layton if (IS_ERR(task)) { 226080a491fdSJeff Layton error = PTR_ERR(task); 226180a491fdSJeff Layton goto out; 226280a491fdSJeff Layton } 226380a491fdSJeff Layton 226480a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2265818a8dbeSBenjamin Coddington if (error != 0) { 2266818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2267818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2268818a8dbeSBenjamin Coddington smp_wmb(); 2269818a8dbeSBenjamin Coddington } else 227080a491fdSJeff Layton error = task->tk_status; 227180a491fdSJeff Layton rpc_put_task(task); 227259a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 227359a707b0STrond Myklebust if (error == 0) { 227459a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 227559a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 227659a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 227759a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 227859a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 227959a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 228059a707b0STrond Myklebust } 22811da177e4SLinus Torvalds out: 2282d9f29500SBenjamin Coddington if (rehash) 2283d9f29500SBenjamin Coddington d_rehash(rehash); 228470ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 228570ded201STrond Myklebust new_dir, new_dentry, error); 2286d9f29500SBenjamin Coddington if (!error) { 2287d9f29500SBenjamin Coddington if (new_inode != NULL) 2288d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2289d9f29500SBenjamin Coddington /* 2290d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2291d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2292d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2293d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2294d9f29500SBenjamin Coddington */ 2295d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2296d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2297d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2298d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2299d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2300d9f29500SBenjamin Coddington 23011da177e4SLinus Torvalds /* new dentry created? */ 23021da177e4SLinus Torvalds if (dentry) 23031da177e4SLinus Torvalds dput(dentry); 23041da177e4SLinus Torvalds return error; 23051da177e4SLinus Torvalds } 2306ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 23071da177e4SLinus Torvalds 2308cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2309cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2310cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2311cfcea3e8STrond Myklebust 2312*a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024; 23133a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 23143a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 23153a505845STrond Myklebust 23161c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 23171c3c07e9STrond Myklebust { 2318b68572e0SNeilBrown put_cred(entry->cred); 2319f682a398SNeilBrown kfree_rcu(entry, rcu_head); 23204e857c58SPeter Zijlstra smp_mb__before_atomic(); 2321cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 23224e857c58SPeter Zijlstra smp_mb__after_atomic(); 23231c3c07e9STrond Myklebust } 23241c3c07e9STrond Myklebust 23251a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 23261a81bb8aSTrond Myklebust { 23271a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 23281a81bb8aSTrond Myklebust 23291a81bb8aSTrond Myklebust while (!list_empty(head)) { 23301a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 23311a81bb8aSTrond Myklebust list_del(&cache->lru); 23321a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 23331a81bb8aSTrond Myklebust } 23341a81bb8aSTrond Myklebust } 23351a81bb8aSTrond Myklebust 23363a505845STrond Myklebust static unsigned long 23373a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2338979df72eSTrond Myklebust { 2339979df72eSTrond Myklebust LIST_HEAD(head); 2340aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2341979df72eSTrond Myklebust struct nfs_access_entry *cache; 23421ab6c499SDave Chinner long freed = 0; 2343979df72eSTrond Myklebust 2344a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2345aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2346979df72eSTrond Myklebust struct inode *inode; 2347979df72eSTrond Myklebust 2348979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2349979df72eSTrond Myklebust break; 23509c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2351979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2352979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2353979df72eSTrond Myklebust goto remove_lru_entry; 2354979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2355979df72eSTrond Myklebust struct nfs_access_entry, lru); 2356979df72eSTrond Myklebust list_move(&cache->lru, &head); 2357979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 23581ab6c499SDave Chinner freed++; 2359979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2360979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2361979df72eSTrond Myklebust &nfs_access_lru_list); 2362979df72eSTrond Myklebust else { 2363979df72eSTrond Myklebust remove_lru_entry: 2364979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 23654e857c58SPeter Zijlstra smp_mb__before_atomic(); 2366979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 23674e857c58SPeter Zijlstra smp_mb__after_atomic(); 2368979df72eSTrond Myklebust } 236959844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2370979df72eSTrond Myklebust } 2371979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23721a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23731ab6c499SDave Chinner return freed; 23741ab6c499SDave Chinner } 23751ab6c499SDave Chinner 23761ab6c499SDave Chinner unsigned long 23773a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 23783a505845STrond Myklebust { 23793a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 23803a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 23813a505845STrond Myklebust 23823a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 23833a505845STrond Myklebust return SHRINK_STOP; 23843a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 23853a505845STrond Myklebust } 23863a505845STrond Myklebust 23873a505845STrond Myklebust 23883a505845STrond Myklebust unsigned long 23891ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 23901ab6c499SDave Chinner { 239155f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2392979df72eSTrond Myklebust } 2393979df72eSTrond Myklebust 23943a505845STrond Myklebust static void 23953a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 23963a505845STrond Myklebust { 23973a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 23983a505845STrond Myklebust unsigned long diff; 23993a505845STrond Myklebust unsigned int nr_to_scan; 24003a505845STrond Myklebust 24013a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 24023a505845STrond Myklebust return; 24033a505845STrond Myklebust nr_to_scan = 100; 24043a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 24053a505845STrond Myklebust if (diff < nr_to_scan) 24063a505845STrond Myklebust nr_to_scan = diff; 24073a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 24083a505845STrond Myklebust } 24093a505845STrond Myklebust 24101a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 24111c3c07e9STrond Myklebust { 24121c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24131a81bb8aSTrond Myklebust struct rb_node *n; 24141c3c07e9STrond Myklebust struct nfs_access_entry *entry; 24151c3c07e9STrond Myklebust 24161c3c07e9STrond Myklebust /* Unhook entries from the cache */ 24171c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 24181c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 24191c3c07e9STrond Myklebust rb_erase(n, root_node); 24201a81bb8aSTrond Myklebust list_move(&entry->lru, head); 24211c3c07e9STrond Myklebust } 24221c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 24231c3c07e9STrond Myklebust } 24241c3c07e9STrond Myklebust 24251c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 24261c3c07e9STrond Myklebust { 24271a81bb8aSTrond Myklebust LIST_HEAD(head); 24281a81bb8aSTrond Myklebust 24291a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 24301a81bb8aSTrond Myklebust return; 2431cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2432cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24331a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2434cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2435cfcea3e8STrond Myklebust 24361c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24371a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 24381a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 24391a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24401a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24411c3c07e9STrond Myklebust } 24421c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 24431c3c07e9STrond Myklebust 2444b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 24451c3c07e9STrond Myklebust { 24461c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 24471c3c07e9STrond Myklebust 24481c3c07e9STrond Myklebust while (n != NULL) { 2449b68572e0SNeilBrown struct nfs_access_entry *entry = 2450b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2451b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 24521c3c07e9STrond Myklebust 2453b68572e0SNeilBrown if (cmp < 0) 24541c3c07e9STrond Myklebust n = n->rb_left; 2455b68572e0SNeilBrown else if (cmp > 0) 24561c3c07e9STrond Myklebust n = n->rb_right; 24571c3c07e9STrond Myklebust else 24581c3c07e9STrond Myklebust return entry; 24591c3c07e9STrond Myklebust } 24601c3c07e9STrond Myklebust return NULL; 24611c3c07e9STrond Myklebust } 24621c3c07e9STrond Myklebust 2463b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 24641da177e4SLinus Torvalds { 246555296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 24661c3c07e9STrond Myklebust struct nfs_access_entry *cache; 246757b69181STrond Myklebust bool retry = true; 246857b69181STrond Myklebust int err; 24691da177e4SLinus Torvalds 24701c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 247157b69181STrond Myklebust for(;;) { 24721c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 24731c3c07e9STrond Myklebust goto out_zap; 24741c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 247557b69181STrond Myklebust err = -ENOENT; 24761c3c07e9STrond Myklebust if (cache == NULL) 24771c3c07e9STrond Myklebust goto out; 247857b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 247921c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 248057b69181STrond Myklebust break; 24815c965db8STrond Myklebust if (!retry) 24825c965db8STrond Myklebust break; 248357b69181STrond Myklebust err = -ECHILD; 248457b69181STrond Myklebust if (!may_block) 248557b69181STrond Myklebust goto out; 248657b69181STrond Myklebust spin_unlock(&inode->i_lock); 248757b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 248857b69181STrond Myklebust if (err) 248957b69181STrond Myklebust return err; 249057b69181STrond Myklebust spin_lock(&inode->i_lock); 249157b69181STrond Myklebust retry = false; 249257b69181STrond Myklebust } 24931c3c07e9STrond Myklebust res->cred = cache->cred; 24941c3c07e9STrond Myklebust res->mask = cache->mask; 2495cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 24961c3c07e9STrond Myklebust err = 0; 24971c3c07e9STrond Myklebust out: 24981c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24991c3c07e9STrond Myklebust return err; 25001c3c07e9STrond Myklebust out_zap: 25011a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 25021a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 25031c3c07e9STrond Myklebust return -ENOENT; 25041c3c07e9STrond Myklebust } 25051c3c07e9STrond Myklebust 2506b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2507f682a398SNeilBrown { 2508f682a398SNeilBrown /* Only check the most recently returned cache entry, 2509f682a398SNeilBrown * but do it without locking. 2510f682a398SNeilBrown */ 2511f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2512f682a398SNeilBrown struct nfs_access_entry *cache; 2513f682a398SNeilBrown int err = -ECHILD; 2514f682a398SNeilBrown struct list_head *lh; 2515f682a398SNeilBrown 2516f682a398SNeilBrown rcu_read_lock(); 2517f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2518f682a398SNeilBrown goto out; 2519f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2520f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2521f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 25229a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2523f682a398SNeilBrown cache = NULL; 2524f682a398SNeilBrown if (cache == NULL) 2525f682a398SNeilBrown goto out; 252621c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2527f682a398SNeilBrown goto out; 2528f682a398SNeilBrown res->cred = cache->cred; 2529f682a398SNeilBrown res->mask = cache->mask; 253021c3ba7eSTrond Myklebust err = 0; 2531f682a398SNeilBrown out: 2532f682a398SNeilBrown rcu_read_unlock(); 2533f682a398SNeilBrown return err; 2534f682a398SNeilBrown } 2535f682a398SNeilBrown 25361c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 25371c3c07e9STrond Myklebust { 2538cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2539cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 25401c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 25411c3c07e9STrond Myklebust struct rb_node *parent = NULL; 25421c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2543b68572e0SNeilBrown int cmp; 25441c3c07e9STrond Myklebust 25451c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 25461c3c07e9STrond Myklebust while (*p != NULL) { 25471c3c07e9STrond Myklebust parent = *p; 25481c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2549b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 25501c3c07e9STrond Myklebust 2551b68572e0SNeilBrown if (cmp < 0) 25521c3c07e9STrond Myklebust p = &parent->rb_left; 2553b68572e0SNeilBrown else if (cmp > 0) 25541c3c07e9STrond Myklebust p = &parent->rb_right; 25551c3c07e9STrond Myklebust else 25561c3c07e9STrond Myklebust goto found; 25571c3c07e9STrond Myklebust } 25581c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 25591c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2560cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 25611c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25621c3c07e9STrond Myklebust return; 25631c3c07e9STrond Myklebust found: 25641c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2565cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2566cfcea3e8STrond Myklebust list_del(&entry->lru); 25671c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25681c3c07e9STrond Myklebust nfs_access_free_entry(entry); 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 25716168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 25721da177e4SLinus Torvalds { 25731c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 25741c3c07e9STrond Myklebust if (cache == NULL) 25751c3c07e9STrond Myklebust return; 25761c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2577b68572e0SNeilBrown cache->cred = get_cred(set->cred); 25781da177e4SLinus Torvalds cache->mask = set->mask; 25791c3c07e9STrond Myklebust 2580f682a398SNeilBrown /* The above field assignments must be visible 2581f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2582f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2583f682a398SNeilBrown */ 2584f682a398SNeilBrown smp_wmb(); 25851c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2586cfcea3e8STrond Myklebust 2587cfcea3e8STrond Myklebust /* Update accounting */ 25884e857c58SPeter Zijlstra smp_mb__before_atomic(); 2589cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 25904e857c58SPeter Zijlstra smp_mb__after_atomic(); 2591cfcea3e8STrond Myklebust 2592cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 25931a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2594cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 25951a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 25961a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 25971a81bb8aSTrond Myklebust &nfs_access_lru_list); 2598cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2599cfcea3e8STrond Myklebust } 26003a505845STrond Myklebust nfs_access_cache_enforce_limit(); 26011da177e4SLinus Torvalds } 26026168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 26036168f62cSWeston Andros Adamson 26043c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 26053c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26063c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 26073c181827SAnna Schumaker NFS_ACCESS_DELETE) 26083c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 26093c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2610ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 26113c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 26123c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 261315d4b73aSTrond Myklebust static int 2614ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 261515d4b73aSTrond Myklebust { 261615d4b73aSTrond Myklebust int mask = 0; 261715d4b73aSTrond Myklebust 261815d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 261915d4b73aSTrond Myklebust mask |= MAY_READ; 2620ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2621ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 262215d4b73aSTrond Myklebust mask |= MAY_WRITE; 2623ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 262415d4b73aSTrond Myklebust mask |= MAY_EXEC; 2625ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2626ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2627ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2628ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 262915d4b73aSTrond Myklebust mask |= MAY_EXEC; 2630ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2631ecbb903cSTrond Myklebust mask |= MAY_WRITE; 263215d4b73aSTrond Myklebust return mask; 263315d4b73aSTrond Myklebust } 263415d4b73aSTrond Myklebust 26356168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 26366168f62cSWeston Andros Adamson { 2637bd8b2441STrond Myklebust entry->mask = access_result; 26386168f62cSWeston Andros Adamson } 26396168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 26401da177e4SLinus Torvalds 2641b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds struct nfs_access_entry cache; 264457b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2645e8194b7dSTrond Myklebust int cache_mask = -1; 26461da177e4SLinus Torvalds int status; 26471da177e4SLinus Torvalds 2648f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2649f4ce1299STrond Myklebust 2650f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2651f682a398SNeilBrown if (status != 0) 265257b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 26531da177e4SLinus Torvalds if (status == 0) 2654f4ce1299STrond Myklebust goto out_cached; 26551da177e4SLinus Torvalds 2656f3324a2aSNeilBrown status = -ECHILD; 265757b69181STrond Myklebust if (!may_block) 2658f3324a2aSNeilBrown goto out; 2659f3324a2aSNeilBrown 26601750d929SAnna Schumaker /* 26611750d929SAnna Schumaker * Determine which access bits we want to ask for... 26621750d929SAnna Schumaker */ 26631750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 26641750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 26651750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 26661750d929SAnna Schumaker else 26671750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 26681da177e4SLinus Torvalds cache.cred = cred; 26691da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2670a71ee337SSuresh Jayaraman if (status != 0) { 2671a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2672a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2673a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2674a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2675a71ee337SSuresh Jayaraman } 2676f4ce1299STrond Myklebust goto out; 2677a71ee337SSuresh Jayaraman } 26781da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2679f4ce1299STrond Myklebust out_cached: 2680ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2681bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2682f4ce1299STrond Myklebust status = -EACCES; 26831da177e4SLinus Torvalds out: 2684e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2685f4ce1299STrond Myklebust return status; 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds 2688af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2689af22f94aSTrond Myklebust { 2690af22f94aSTrond Myklebust int mask = 0; 2691af22f94aSTrond Myklebust 2692f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2693f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2694f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2695f8d9a897SWeston Andros Adamson } else { 26968a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2697af22f94aSTrond Myklebust mask |= MAY_READ; 26988a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2699af22f94aSTrond Myklebust mask |= MAY_WRITE; 2700f8d9a897SWeston Andros Adamson } 2701f8d9a897SWeston Andros Adamson 2702af22f94aSTrond Myklebust return mask; 2703af22f94aSTrond Myklebust } 2704af22f94aSTrond Myklebust 2705b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2706af22f94aSTrond Myklebust { 2707af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2708af22f94aSTrond Myklebust } 270989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2710af22f94aSTrond Myklebust 27115c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 27125c5fc09aSTrond Myklebust { 27135c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 271421c3ba7eSTrond Myklebust int ret = 0; 27155c5fc09aSTrond Myklebust 27163825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 27173825827eSTrond Myklebust return 0; 2718cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 27195c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 272021c3ba7eSTrond Myklebust return -ECHILD; 272121c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 272221c3ba7eSTrond Myklebust } 27235c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 27245c5fc09aSTrond Myklebust ret = -EACCES; 27255c5fc09aSTrond Myklebust return ret; 27265c5fc09aSTrond Myklebust } 27275c5fc09aSTrond Myklebust 272810556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 27291da177e4SLinus Torvalds { 2730b68572e0SNeilBrown const struct cred *cred = current_cred(); 27311da177e4SLinus Torvalds int res = 0; 27321da177e4SLinus Torvalds 273391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 273491d5b470SChuck Lever 2735e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 27361da177e4SLinus Torvalds goto out; 27371da177e4SLinus Torvalds /* Is this sys_access() ? */ 27389cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 27391da177e4SLinus Torvalds goto force_lookup; 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 27421da177e4SLinus Torvalds case S_IFLNK: 27431da177e4SLinus Torvalds goto out; 27441da177e4SLinus Torvalds case S_IFREG: 2745762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2746762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2747762674f8STrond Myklebust return 0; 27481da177e4SLinus Torvalds break; 27491da177e4SLinus Torvalds case S_IFDIR: 27501da177e4SLinus Torvalds /* 27511da177e4SLinus Torvalds * Optimize away all write operations, since the server 27521da177e4SLinus Torvalds * will check permissions when we perform the op. 27531da177e4SLinus Torvalds */ 27541da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 27551da177e4SLinus Torvalds goto out; 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds force_lookup: 27591da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 27601da177e4SLinus Torvalds goto out_notsup; 27611da177e4SLinus Torvalds 2762f3324a2aSNeilBrown /* Always try fast lookups first */ 2763f3324a2aSNeilBrown rcu_read_lock(); 2764f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2765f3324a2aSNeilBrown rcu_read_unlock(); 2766f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2767f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 27681da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2769f3324a2aSNeilBrown } 27701da177e4SLinus Torvalds out: 27715c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 27725c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2773f696a365SMiklos Szeredi 27741e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 27751e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 27761da177e4SLinus Torvalds return res; 27771da177e4SLinus Torvalds out_notsup: 2778d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2779d51ac1a8SNeilBrown return -ECHILD; 2780d51ac1a8SNeilBrown 27811da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 27821da177e4SLinus Torvalds if (res == 0) 27832830ba7fSAl Viro res = generic_permission(inode, mask); 27841e7cb3dcSChuck Lever goto out; 27851da177e4SLinus Torvalds } 2786ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds /* 27891da177e4SLinus Torvalds * Local variables: 27901da177e4SLinus Torvalds * version-control: t 27911da177e4SLinus Torvalds * kept-new-versions: 5 27921da177e4SLinus Torvalds * End: 27931da177e4SLinus Torvalds */ 2794