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 147a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool); 1481da177e4SLinus Torvalds typedef struct { 1491da177e4SLinus Torvalds struct file *file; 1501da177e4SLinus Torvalds struct page *page; 15123db8620SAl Viro struct dir_context *ctx; 1521da177e4SLinus Torvalds unsigned long page_index; 153f0dd2136STrond Myklebust u64 *dir_cookie; 1540aded708STrond Myklebust u64 last_cookie; 155f0dd2136STrond Myklebust loff_t current_index; 1561da177e4SLinus Torvalds decode_dirent_t decode; 157d1bacf9eSBryan Schumaker 158a1147b82STrond Myklebust unsigned long dir_verifier; 1591f4eab7eSNeil Brown unsigned long timestamp; 1604704f0e2STrond Myklebust unsigned long gencount; 161d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 162a7a3b1e9SBenjamin Coddington bool plus; 163a7a3b1e9SBenjamin Coddington bool eof; 1641da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1651da177e4SLinus Torvalds 1664b310319STrond Myklebust static 1674b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 1684b310319STrond Myklebust { 1694b310319STrond Myklebust struct nfs_cache_array *array; 1704b310319STrond Myklebust 1714b310319STrond Myklebust array = kmap_atomic(page); 1724b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1734b310319STrond Myklebust array->eof_index = -1; 1744b310319STrond Myklebust kunmap_atomic(array); 1754b310319STrond Myklebust } 1764b310319STrond Myklebust 177d1bacf9eSBryan Schumaker /* 178d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 179d1bacf9eSBryan Schumaker */ 180d1bacf9eSBryan Schumaker static 18111de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 182d1bacf9eSBryan Schumaker { 18311de3b11STrond Myklebust struct nfs_cache_array *array; 184d1bacf9eSBryan Schumaker int i; 1858cd51a0cSTrond Myklebust 1862b86ce2dSCong Wang array = kmap_atomic(page); 187d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 188d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1894b310319STrond Myklebust array->size = 0; 1902b86ce2dSCong Wang kunmap_atomic(array); 191d1bacf9eSBryan Schumaker } 192d1bacf9eSBryan Schumaker 193d1bacf9eSBryan Schumaker /* 194d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 195d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 196d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 197d1bacf9eSBryan Schumaker */ 198d1bacf9eSBryan Schumaker static 1994a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 200d1bacf9eSBryan Schumaker { 201d1bacf9eSBryan Schumaker string->len = len; 2023803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2034a201d6eSTrond Myklebust if (string->name == NULL) 2044a201d6eSTrond Myklebust return -ENOMEM; 20504e4bd1cSCatalin Marinas /* 20604e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 20704e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 20804e4bd1cSCatalin Marinas */ 20904e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2108387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2114a201d6eSTrond Myklebust return 0; 212d1bacf9eSBryan Schumaker } 213d1bacf9eSBryan Schumaker 214d1bacf9eSBryan Schumaker static 215d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 216d1bacf9eSBryan Schumaker { 2170795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2184a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2194a201d6eSTrond Myklebust int ret; 2204a201d6eSTrond Myklebust 2214a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2223020093fSTrond Myklebust 2233020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2243020093fSTrond Myklebust ret = -ENOSPC; 2253020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2263020093fSTrond Myklebust goto out; 2273020093fSTrond Myklebust 2284a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2294a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2300b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2314a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2324a201d6eSTrond Myklebust if (ret) 2334a201d6eSTrond Myklebust goto out; 234d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2358cd51a0cSTrond Myklebust array->size++; 23647c716cbSTrond Myklebust if (entry->eof != 0) 237d1bacf9eSBryan Schumaker array->eof_index = array->size; 2384a201d6eSTrond Myklebust out: 2390795bf83SFabian Frederick kunmap(page); 2404a201d6eSTrond Myklebust return ret; 241d1bacf9eSBryan Schumaker } 242d1bacf9eSBryan Schumaker 243d1bacf9eSBryan Schumaker static 244d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 245d1bacf9eSBryan Schumaker { 24623db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 247d1bacf9eSBryan Schumaker unsigned int index; 248d1bacf9eSBryan Schumaker 249d1bacf9eSBryan Schumaker if (diff < 0) 250d1bacf9eSBryan Schumaker goto out_eof; 251d1bacf9eSBryan Schumaker if (diff >= array->size) { 2528cd51a0cSTrond Myklebust if (array->eof_index >= 0) 253d1bacf9eSBryan Schumaker goto out_eof; 254d1bacf9eSBryan Schumaker return -EAGAIN; 255d1bacf9eSBryan Schumaker } 256d1bacf9eSBryan Schumaker 257d1bacf9eSBryan Schumaker index = (unsigned int)diff; 258d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 259d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 260d1bacf9eSBryan Schumaker return 0; 261d1bacf9eSBryan Schumaker out_eof: 2626089dd0dSThomas Meyer desc->eof = true; 263d1bacf9eSBryan Schumaker return -EBADCOOKIE; 264d1bacf9eSBryan Schumaker } 265d1bacf9eSBryan Schumaker 2664db72b40SJeff Layton static bool 2674db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2684db72b40SJeff Layton { 2694db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2704db72b40SJeff Layton return false; 2714db72b40SJeff Layton smp_rmb(); 2724db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2734db72b40SJeff Layton } 2744db72b40SJeff Layton 275d1bacf9eSBryan Schumaker static 276d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 277d1bacf9eSBryan Schumaker { 278d1bacf9eSBryan Schumaker int i; 2798ef2ce3eSBryan Schumaker loff_t new_pos; 280d1bacf9eSBryan Schumaker int status = -EAGAIN; 281d1bacf9eSBryan Schumaker 282d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2838cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 284496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2850c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2860c030806STrond Myklebust 2878ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2884db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 2894db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 2900c030806STrond Myklebust ctx->duped = 0; 2910c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 29223db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2930c030806STrond Myklebust if (ctx->duped > 0 2940c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2950c030806STrond Myklebust if (printk_ratelimit()) { 2966de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 2970c030806STrond Myklebust "Please contact your server vendor. " 2989581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 2999581a4aeSJeff Layton desc->file, array->array[i].string.len, 3009581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3010c030806STrond Myklebust } 3020c030806STrond Myklebust status = -ELOOP; 3030c030806STrond Myklebust goto out; 3040c030806STrond Myklebust } 3058ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3060c030806STrond Myklebust ctx->duped = -1; 3078ef2ce3eSBryan Schumaker } 30823db8620SAl Viro desc->ctx->pos = new_pos; 3098cd51a0cSTrond Myklebust desc->cache_entry_index = i; 31047c716cbSTrond Myklebust return 0; 3118cd51a0cSTrond Myklebust } 3128cd51a0cSTrond Myklebust } 31347c716cbSTrond Myklebust if (array->eof_index >= 0) { 314d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 31518fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 3166089dd0dSThomas Meyer desc->eof = true; 317d1bacf9eSBryan Schumaker } 3180c030806STrond Myklebust out: 319d1bacf9eSBryan Schumaker return status; 320d1bacf9eSBryan Schumaker } 321d1bacf9eSBryan Schumaker 322d1bacf9eSBryan Schumaker static 323d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 324d1bacf9eSBryan Schumaker { 325d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 32647c716cbSTrond Myklebust int status; 327d1bacf9eSBryan Schumaker 3280795bf83SFabian Frederick array = kmap(desc->page); 329d1bacf9eSBryan Schumaker 330d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 331d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 332d1bacf9eSBryan Schumaker else 333d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 334d1bacf9eSBryan Schumaker 33547c716cbSTrond Myklebust if (status == -EAGAIN) { 3360aded708STrond Myklebust desc->last_cookie = array->last_cookie; 337e47c085aSTrond Myklebust desc->current_index += array->size; 33847c716cbSTrond Myklebust desc->page_index++; 33947c716cbSTrond Myklebust } 3400795bf83SFabian Frederick kunmap(desc->page); 341d1bacf9eSBryan Schumaker return status; 342d1bacf9eSBryan Schumaker } 343d1bacf9eSBryan Schumaker 344d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 345d1bacf9eSBryan Schumaker static 34656e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 347d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 348d1bacf9eSBryan Schumaker { 349480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 350684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3514704f0e2STrond Myklebust unsigned long timestamp, gencount; 3521da177e4SLinus Torvalds int error; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds again: 3551da177e4SLinus Torvalds timestamp = jiffies; 3564704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 357a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode); 358be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3591da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3601da177e4SLinus Torvalds if (error < 0) { 3611da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3621da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3631da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3643a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 365a7a3b1e9SBenjamin Coddington desc->plus = false; 3661da177e4SLinus Torvalds goto again; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds goto error; 3691da177e4SLinus Torvalds } 3701f4eab7eSNeil Brown desc->timestamp = timestamp; 3714704f0e2STrond Myklebust desc->gencount = gencount; 372d1bacf9eSBryan Schumaker error: 373d1bacf9eSBryan Schumaker return error; 374d1bacf9eSBryan Schumaker } 375d1bacf9eSBryan Schumaker 376573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 377573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 378d1bacf9eSBryan Schumaker { 379573c4e1eSChuck Lever int error; 380d1bacf9eSBryan Schumaker 381573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 382573c4e1eSChuck Lever if (error) 383573c4e1eSChuck Lever return error; 384d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 385d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 386d1bacf9eSBryan Schumaker return 0; 387d1bacf9eSBryan Schumaker } 388d1bacf9eSBryan Schumaker 389fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 390fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 391fa923369STrond Myklebust */ 392d39ab9deSBryan Schumaker static 393d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 394d39ab9deSBryan Schumaker { 395d8fdb47fSTrond Myklebust struct inode *inode; 396fa923369STrond Myklebust struct nfs_inode *nfsi; 397fa923369STrond Myklebust 3982b0143b5SDavid Howells if (d_really_is_negative(dentry)) 3992b0143b5SDavid Howells return 0; 400fa923369STrond Myklebust 401d8fdb47fSTrond Myklebust inode = d_inode(dentry); 402d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 403d8fdb47fSTrond Myklebust return 0; 404d8fdb47fSTrond Myklebust 405d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4067dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 407d39ab9deSBryan Schumaker return 0; 4087dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4097dc72d5fSTrond Myklebust return 0; 4107dc72d5fSTrond Myklebust return 1; 411d39ab9deSBryan Schumaker } 412d39ab9deSBryan Schumaker 413d39ab9deSBryan Schumaker static 41423db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 415d69ee9b8STrond Myklebust { 416d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 417d69ee9b8STrond Myklebust return false; 418d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 419d69ee9b8STrond Myklebust return true; 42023db8620SAl Viro if (ctx->pos == 0) 421d69ee9b8STrond Myklebust return true; 422d69ee9b8STrond Myklebust return false; 423d69ee9b8STrond Myklebust } 424d69ee9b8STrond Myklebust 425d69ee9b8STrond Myklebust /* 42663519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 42763519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 428d69ee9b8STrond Myklebust * directory. 429d69ee9b8STrond Myklebust */ 430d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 431d69ee9b8STrond Myklebust { 43263519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 43363519fbcSTrond Myklebust 43463519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 43563519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 43663519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 437d69ee9b8STrond Myklebust } 438d69ee9b8STrond Myklebust 439311324adSTrond Myklebust /* 440311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 441311324adSTrond Myklebust * 442311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 443311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 444311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 445311324adSTrond Myklebust * cache flush. 446311324adSTrond Myklebust */ 447311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 448311324adSTrond Myklebust { 44963519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 45063519fbcSTrond Myklebust 45163519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 45263519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 45363519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 454227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 455227823d2SDai Ngo nfsi->page_index + 1, -1); 456311324adSTrond Myklebust } 457311324adSTrond Myklebust } 458311324adSTrond Myklebust 459d69ee9b8STrond Myklebust static 460a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 461a1147b82STrond Myklebust unsigned long dir_verifier) 462d39ab9deSBryan Schumaker { 46326fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4649ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4654a201d6eSTrond Myklebust struct dentry *dentry; 4664a201d6eSTrond Myklebust struct dentry *alias; 467d39ab9deSBryan Schumaker struct inode *inode; 468aa9c2669SDavid Quigley int status; 469d39ab9deSBryan Schumaker 470fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 471fa923369STrond Myklebust return; 4726c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4736c441c25STrond Myklebust return; 47478d04af4STrond Myklebust if (filename.len == 0) 47578d04af4STrond Myklebust return; 47678d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 47778d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 47878d04af4STrond Myklebust return; 47978d04af4STrond Myklebust /* ...or '/' */ 48078d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 48178d04af4STrond Myklebust return; 4824a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4834a201d6eSTrond Myklebust if (filename.len == 1) 4844a201d6eSTrond Myklebust return; 4854a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4864a201d6eSTrond Myklebust return; 4874a201d6eSTrond Myklebust } 4888387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 489d39ab9deSBryan Schumaker 4904a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 4919ac3d3e8SAl Viro again: 4929ac3d3e8SAl Viro if (!dentry) { 4939ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 4949ac3d3e8SAl Viro if (IS_ERR(dentry)) 4959ac3d3e8SAl Viro return; 4969ac3d3e8SAl Viro } 4979ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 4986c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4996c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 5006c441c25STrond Myklebust &entry->fattr->fsid)) 5016c441c25STrond Myklebust goto out; 502d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5037dc72d5fSTrond Myklebust if (!entry->fh->size) 5047dc72d5fSTrond Myklebust goto out; 505a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 5062b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 507aa9c2669SDavid Quigley if (!status) 5082b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 509d39ab9deSBryan Schumaker goto out; 510d39ab9deSBryan Schumaker } else { 5115542aa2fSEric W. Biederman d_invalidate(dentry); 512d39ab9deSBryan Schumaker dput(dentry); 5139ac3d3e8SAl Viro dentry = NULL; 5149ac3d3e8SAl Viro goto again; 515d39ab9deSBryan Schumaker } 516d39ab9deSBryan Schumaker } 5177dc72d5fSTrond Myklebust if (!entry->fh->size) { 5187dc72d5fSTrond Myklebust d_lookup_done(dentry); 5197dc72d5fSTrond Myklebust goto out; 5207dc72d5fSTrond Myklebust } 521d39ab9deSBryan Schumaker 5221775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 52341d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5249ac3d3e8SAl Viro d_lookup_done(dentry); 5259ac3d3e8SAl Viro if (alias) { 526d39ab9deSBryan Schumaker if (IS_ERR(alias)) 527d39ab9deSBryan Schumaker goto out; 5289ac3d3e8SAl Viro dput(dentry); 5299ac3d3e8SAl Viro dentry = alias; 5309ac3d3e8SAl Viro } 531a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 532d39ab9deSBryan Schumaker out: 533d39ab9deSBryan Schumaker dput(dentry); 534d39ab9deSBryan Schumaker } 535d39ab9deSBryan Schumaker 536d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 537d1bacf9eSBryan Schumaker static 5388cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5396650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 540d1bacf9eSBryan Schumaker { 541babddc72SBryan Schumaker struct xdr_stream stream; 542f7da7a12SBenny Halevy struct xdr_buf buf; 5436650239aSTrond Myklebust struct page *scratch; 54499424380SBryan Schumaker struct nfs_cache_array *array; 5455c346854STrond Myklebust unsigned int count = 0; 5465c346854STrond Myklebust int status; 547babddc72SBryan Schumaker 5486650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5496650239aSTrond Myklebust if (scratch == NULL) 5506650239aSTrond Myklebust return -ENOMEM; 551babddc72SBryan Schumaker 552ce85cfbeSBenjamin Coddington if (buflen == 0) 553ce85cfbeSBenjamin Coddington goto out_nopages; 554ce85cfbeSBenjamin Coddington 555f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5566650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 55799424380SBryan Schumaker 55899424380SBryan Schumaker do { 55999424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5608cd51a0cSTrond Myklebust if (status != 0) { 5618cd51a0cSTrond Myklebust if (status == -EAGAIN) 5628cd51a0cSTrond Myklebust status = 0; 56399424380SBryan Schumaker break; 5648cd51a0cSTrond Myklebust } 56599424380SBryan Schumaker 5665c346854STrond Myklebust count++; 5675c346854STrond Myklebust 568a7a3b1e9SBenjamin Coddington if (desc->plus) 569a1147b82STrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, 570a1147b82STrond Myklebust desc->dir_verifier); 5718cd51a0cSTrond Myklebust 572db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 5738cd51a0cSTrond Myklebust if (status != 0) 5748cd51a0cSTrond Myklebust break; 57599424380SBryan Schumaker } while (!entry->eof); 57699424380SBryan Schumaker 577ce85cfbeSBenjamin Coddington out_nopages: 57847c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 579db531db9SMax Kellermann array = kmap(page); 5808cd51a0cSTrond Myklebust array->eof_index = array->size; 58199424380SBryan Schumaker status = 0; 582db531db9SMax Kellermann kunmap(page); 58356e4ebf8SBryan Schumaker } 5846650239aSTrond Myklebust 5856650239aSTrond Myklebust put_page(scratch); 5868cd51a0cSTrond Myklebust return status; 5878cd51a0cSTrond Myklebust } 58856e4ebf8SBryan Schumaker 58956e4ebf8SBryan Schumaker static 590c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 59156e4ebf8SBryan Schumaker { 59256e4ebf8SBryan Schumaker unsigned int i; 59356e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 59456e4ebf8SBryan Schumaker put_page(pages[i]); 59556e4ebf8SBryan Schumaker } 59656e4ebf8SBryan Schumaker 59756e4ebf8SBryan Schumaker /* 598bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 599bf211ca1Szhangliguang * to nfs_readdir_free_pages() 60056e4ebf8SBryan Schumaker */ 60156e4ebf8SBryan Schumaker static 602c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 60356e4ebf8SBryan Schumaker { 60456e4ebf8SBryan Schumaker unsigned int i; 60556e4ebf8SBryan Schumaker 60656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 60756e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 60856e4ebf8SBryan Schumaker if (page == NULL) 60956e4ebf8SBryan Schumaker goto out_freepages; 61056e4ebf8SBryan Schumaker pages[i] = page; 61156e4ebf8SBryan Schumaker } 6126650239aSTrond Myklebust return 0; 61356e4ebf8SBryan Schumaker 61456e4ebf8SBryan Schumaker out_freepages: 615c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6166650239aSTrond Myklebust return -ENOMEM; 617d1bacf9eSBryan Schumaker } 618d1bacf9eSBryan Schumaker 619d1bacf9eSBryan Schumaker static 620d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 621d1bacf9eSBryan Schumaker { 62256e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 623d1bacf9eSBryan Schumaker struct nfs_entry entry; 624d1bacf9eSBryan Schumaker struct file *file = desc->file; 625d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6268cd51a0cSTrond Myklebust int status = -ENOMEM; 62756e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 628d1bacf9eSBryan Schumaker 6294b310319STrond Myklebust nfs_readdir_init_array(page); 6304b310319STrond Myklebust 631d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6320aded708STrond Myklebust entry.cookie = desc->last_cookie; 633d1bacf9eSBryan Schumaker entry.eof = 0; 634d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 635d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 636573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 637d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 638d1bacf9eSBryan Schumaker goto out; 639d1bacf9eSBryan Schumaker 64014c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 64114c43f76SDavid Quigley if (IS_ERR(entry.label)) { 64214c43f76SDavid Quigley status = PTR_ERR(entry.label); 64314c43f76SDavid Quigley goto out; 64414c43f76SDavid Quigley } 64514c43f76SDavid Quigley 6460795bf83SFabian Frederick array = kmap(page); 647d1bacf9eSBryan Schumaker 648c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6496650239aSTrond Myklebust if (status < 0) 650d1bacf9eSBryan Schumaker goto out_release_array; 651d1bacf9eSBryan Schumaker do { 652ac396128STrond Myklebust unsigned int pglen; 65356e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 654babddc72SBryan Schumaker 655d1bacf9eSBryan Schumaker if (status < 0) 656d1bacf9eSBryan Schumaker break; 657ac396128STrond Myklebust pglen = status; 6586650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6598cd51a0cSTrond Myklebust if (status < 0) { 6608cd51a0cSTrond Myklebust if (status == -ENOSPC) 6618cd51a0cSTrond Myklebust status = 0; 6628cd51a0cSTrond Myklebust break; 6638cd51a0cSTrond Myklebust } 6648cd51a0cSTrond Myklebust } while (array->eof_index < 0); 665d1bacf9eSBryan Schumaker 666c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 667d1bacf9eSBryan Schumaker out_release_array: 6680795bf83SFabian Frederick kunmap(page); 66914c43f76SDavid Quigley nfs4_label_free(entry.label); 670d1bacf9eSBryan Schumaker out: 671d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 672d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 673d1bacf9eSBryan Schumaker return status; 674d1bacf9eSBryan Schumaker } 675d1bacf9eSBryan Schumaker 676d1bacf9eSBryan Schumaker /* 677d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 678d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 679d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 680d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6811da177e4SLinus Torvalds */ 682d1bacf9eSBryan Schumaker static 683a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 684d1bacf9eSBryan Schumaker { 685a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 686496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6878cd51a0cSTrond Myklebust int ret; 688d1bacf9eSBryan Schumaker 6898cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6908cd51a0cSTrond Myklebust if (ret < 0) 691d1bacf9eSBryan Schumaker goto error; 692d1bacf9eSBryan Schumaker SetPageUptodate(page); 693d1bacf9eSBryan Schumaker 6942aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 695cd9ae2b6STrond Myklebust /* Should never happen */ 696cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 697cd9ae2b6STrond Myklebust } 6981da177e4SLinus Torvalds unlock_page(page); 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds error: 7014b310319STrond Myklebust nfs_readdir_clear_array(page); 7021da177e4SLinus Torvalds unlock_page(page); 7038cd51a0cSTrond Myklebust return ret; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 706d1bacf9eSBryan Schumaker static 707d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7081da177e4SLinus Torvalds { 70909cbfeafSKirill A. Shutemov put_page(desc->page); 7101da177e4SLinus Torvalds desc->page = NULL; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 713d1bacf9eSBryan Schumaker static 714d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7151da177e4SLinus Torvalds { 716a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 717a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* 721d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 722114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7231da177e4SLinus Torvalds */ 724d1bacf9eSBryan Schumaker static 725114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 726d1bacf9eSBryan Schumaker { 727227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 728227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 729d1bacf9eSBryan Schumaker int res; 730d1bacf9eSBryan Schumaker 731d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 732d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 733d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 734114de382STrond Myklebust res = lock_page_killable(desc->page); 73547c716cbSTrond Myklebust if (res != 0) 736114de382STrond Myklebust goto error; 737114de382STrond Myklebust res = -EAGAIN; 738114de382STrond Myklebust if (desc->page->mapping != NULL) { 739114de382STrond Myklebust res = nfs_readdir_search_array(desc); 740227823d2SDai Ngo if (res == 0) { 741227823d2SDai Ngo nfsi->page_index = desc->page_index; 742114de382STrond Myklebust return 0; 743114de382STrond Myklebust } 744227823d2SDai Ngo } 745114de382STrond Myklebust unlock_page(desc->page); 746114de382STrond Myklebust error: 747d1bacf9eSBryan Schumaker cache_page_release(desc); 748d1bacf9eSBryan Schumaker return res; 749d1bacf9eSBryan Schumaker } 750d1bacf9eSBryan Schumaker 751d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7521da177e4SLinus Torvalds static inline 7531da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7541da177e4SLinus Torvalds { 7558cd51a0cSTrond Myklebust int res; 756d1bacf9eSBryan Schumaker 7570aded708STrond Myklebust if (desc->page_index == 0) { 7588cd51a0cSTrond Myklebust desc->current_index = 0; 7590aded708STrond Myklebust desc->last_cookie = 0; 7600aded708STrond Myklebust } 76147c716cbSTrond Myklebust do { 762114de382STrond Myklebust res = find_and_lock_cache_page(desc); 76347c716cbSTrond Myklebust } while (res == -EAGAIN); 7641da177e4SLinus Torvalds return res; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* 7681da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7691da177e4SLinus Torvalds */ 7701da177e4SLinus Torvalds static 77123db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds struct file *file = desc->file; 774d1bacf9eSBryan Schumaker int i = 0; 775d1bacf9eSBryan Schumaker int res = 0; 776d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7778ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7788ef2ce3eSBryan Schumaker 7790795bf83SFabian Frederick array = kmap(desc->page); 780d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 781ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7821da177e4SLinus Torvalds 783ece0b423STrond Myklebust ent = &array->array[i]; 78423db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 78523db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 7866089dd0dSThomas Meyer desc->eof = true; 7871da177e4SLinus Torvalds break; 788ece0b423STrond Myklebust } 78923db8620SAl Viro desc->ctx->pos++; 790d1bacf9eSBryan Schumaker if (i < (array->size-1)) 791d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 792d1bacf9eSBryan Schumaker else 793d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7940c030806STrond Myklebust if (ctx->duped != 0) 7950c030806STrond Myklebust ctx->duped = 1; 7968cd51a0cSTrond Myklebust } 79747c716cbSTrond Myklebust if (array->eof_index >= 0) 7986089dd0dSThomas Meyer desc->eof = true; 799d1bacf9eSBryan Schumaker 8000795bf83SFabian Frederick kunmap(desc->page); 8011e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8021e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8031da177e4SLinus Torvalds return res; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* 8071da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8081da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8091da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8101da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8111da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8121da177e4SLinus Torvalds * 8131da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8141da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8151da177e4SLinus Torvalds * we should already have a complete representation of the 8161da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8171da177e4SLinus Torvalds */ 8181da177e4SLinus Torvalds static inline 81923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct page *page = NULL; 8221da177e4SLinus Torvalds int status; 823496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8240c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8251da177e4SLinus Torvalds 8261e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8271e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8301da177e4SLinus Torvalds if (!page) { 8311da177e4SLinus Torvalds status = -ENOMEM; 8321da177e4SLinus Torvalds goto out; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8357a8e1dc3STrond Myklebust desc->page_index = 0; 8360aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8377a8e1dc3STrond Myklebust desc->page = page; 8380c030806STrond Myklebust ctx->duped = 0; 8397a8e1dc3STrond Myklebust 84085f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 84185f8607eSTrond Myklebust if (status < 0) 842d1bacf9eSBryan Schumaker goto out_release; 843d1bacf9eSBryan Schumaker 84423db8620SAl Viro status = nfs_do_filldir(desc); 8451da177e4SLinus Torvalds 846114de382STrond Myklebust out_release: 847114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 848114de382STrond Myklebust cache_page_release(desc); 8491da177e4SLinus Torvalds out: 8501e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8513110ff80SHarvey Harrison __func__, status); 8521da177e4SLinus Torvalds return status; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 85500a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 85600a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 85700a92642SOlivier Galibert whole directory. 8581da177e4SLinus Torvalds */ 85923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8601da177e4SLinus Torvalds { 861be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8622b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8631da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8641da177e4SLinus Torvalds *desc = &my_desc; 86523db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 86607b5ce8eSScott Mayhew int res = 0; 8671da177e4SLinus Torvalds 8686de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8696de1472fSAl Viro file, (long long)ctx->pos); 87091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 87191d5b470SChuck Lever 8721da177e4SLinus Torvalds /* 87323db8620SAl Viro * ctx->pos points to the dirent entry number. 874f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 87500a92642SOlivier Galibert * to either find the entry with the appropriate number or 87600a92642SOlivier Galibert * revalidate the cookie. 8771da177e4SLinus Torvalds */ 8781da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8791da177e4SLinus Torvalds 88023db8620SAl Viro desc->file = file; 88123db8620SAl Viro desc->ctx = ctx; 882480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8831da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 884a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 8851da177e4SLinus Torvalds 88679f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 88723db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 888fccca7fcSTrond Myklebust if (res < 0) 889fccca7fcSTrond Myklebust goto out; 890fccca7fcSTrond Myklebust 89147c716cbSTrond Myklebust do { 8921da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 89300a92642SOlivier Galibert 8941da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 895ece0b423STrond Myklebust res = 0; 8961da177e4SLinus Torvalds /* This means either end of directory */ 8976089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 8981da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 89923db8620SAl Viro res = uncached_readdir(desc); 900ece0b423STrond Myklebust if (res == 0) 9011da177e4SLinus Torvalds continue; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds break; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9063a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9071da177e4SLinus Torvalds nfs_zap_caches(inode); 908baf57a09STrond Myklebust desc->page_index = 0; 909a7a3b1e9SBenjamin Coddington desc->plus = false; 910a7a3b1e9SBenjamin Coddington desc->eof = false; 9111da177e4SLinus Torvalds continue; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds if (res < 0) 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds 91623db8620SAl Viro res = nfs_do_filldir(desc); 917114de382STrond Myklebust unlock_page(desc->page); 918114de382STrond Myklebust cache_page_release(desc); 919ece0b423STrond Myklebust if (res < 0) 9201da177e4SLinus Torvalds break; 92147c716cbSTrond Myklebust } while (!desc->eof); 922fccca7fcSTrond Myklebust out: 9231e7cb3dcSChuck Lever if (res > 0) 9241e7cb3dcSChuck Lever res = 0; 9256de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9261da177e4SLinus Torvalds return res; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 929965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 930f0dd2136STrond Myklebust { 931b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 932480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 933b84e06c5SChuck Lever 9346de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9356de1472fSAl Viro filp, offset, whence); 936b84e06c5SChuck Lever 937965c8e59SAndrew Morton switch (whence) { 938f0dd2136STrond Myklebust default: 939b2b1ff3dSTrond Myklebust return -EINVAL; 940b2b1ff3dSTrond Myklebust case SEEK_SET: 941b2b1ff3dSTrond Myklebust if (offset < 0) 942b2b1ff3dSTrond Myklebust return -EINVAL; 943b2b1ff3dSTrond Myklebust inode_lock(inode); 944b2b1ff3dSTrond Myklebust break; 945b2b1ff3dSTrond Myklebust case SEEK_CUR: 946b2b1ff3dSTrond Myklebust if (offset == 0) 947b2b1ff3dSTrond Myklebust return filp->f_pos; 948b2b1ff3dSTrond Myklebust inode_lock(inode); 949b2b1ff3dSTrond Myklebust offset += filp->f_pos; 950b2b1ff3dSTrond Myklebust if (offset < 0) { 951b2b1ff3dSTrond Myklebust inode_unlock(inode); 952b2b1ff3dSTrond Myklebust return -EINVAL; 953b2b1ff3dSTrond Myklebust } 954f0dd2136STrond Myklebust } 955f0dd2136STrond Myklebust if (offset != filp->f_pos) { 956f0dd2136STrond Myklebust filp->f_pos = offset; 957480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9588ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 959f0dd2136STrond Myklebust } 960b044f645SBenjamin Coddington inode_unlock(inode); 961f0dd2136STrond Myklebust return offset; 962f0dd2136STrond Myklebust } 963f0dd2136STrond Myklebust 9641da177e4SLinus Torvalds /* 9651da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9661da177e4SLinus Torvalds * is a dummy operation. 9671da177e4SLinus Torvalds */ 96802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 96902c24a82SJosef Bacik int datasync) 9701da177e4SLinus Torvalds { 9716de1472fSAl Viro struct inode *inode = file_inode(filp); 9727ea80859SChristoph Hellwig 9736de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9741e7cb3dcSChuck Lever 9755955102cSAl Viro inode_lock(inode); 9766de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9775955102cSAl Viro inode_unlock(inode); 9781da177e4SLinus Torvalds return 0; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 981bfc69a45STrond Myklebust /** 982bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 983302fad7bSTrond Myklebust * @dir: pointer to directory inode 984bfc69a45STrond Myklebust * 985bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 986bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 987bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 988bfc69a45STrond Myklebust * 989*efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry 990*efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode. 991*efeda80dSTrond Myklebust * 992bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 993bfc69a45STrond Myklebust */ 994bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 995bfc69a45STrond Myklebust { 996*efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2; 997bfc69a45STrond Myklebust } 99889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 999bfc69a45STrond Myklebust 1000*efeda80dSTrond Myklebust /** 1001*efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes 1002*efeda80dSTrond Myklebust * @dir: pointer to parent directory inode 1003*efeda80dSTrond Myklebust * @verf: previously saved change attribute 1004*efeda80dSTrond Myklebust * 1005*efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute. 1006*efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on 1007*efeda80dSTrond Myklebust * the server, and that any dentries need revalidating. 1008*efeda80dSTrond Myklebust */ 1009*efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1010*efeda80dSTrond Myklebust { 1011*efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir); 1012*efeda80dSTrond Myklebust } 1013*efeda80dSTrond Myklebust 1014*efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf) 1015*efeda80dSTrond Myklebust { 1016*efeda80dSTrond Myklebust *verf |= 1UL; 1017*efeda80dSTrond Myklebust } 1018*efeda80dSTrond Myklebust 1019*efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1020*efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf) 1021*efeda80dSTrond Myklebust { 1022*efeda80dSTrond Myklebust *verf &= ~1UL; 1023*efeda80dSTrond Myklebust } 1024*efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1025*efeda80dSTrond Myklebust 1026*efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf) 1027*efeda80dSTrond Myklebust { 1028*efeda80dSTrond Myklebust return verf & 1; 1029*efeda80dSTrond Myklebust } 1030*efeda80dSTrond Myklebust 1031*efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry) 1032*efeda80dSTrond Myklebust { 1033*efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time); 1034*efeda80dSTrond Myklebust } 1035*efeda80dSTrond Myklebust 1036*efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1037*efeda80dSTrond Myklebust { 1038*efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry); 1039*efeda80dSTrond Myklebust 1040*efeda80dSTrond Myklebust if (!nfs_verifier_is_delegated(dentry) && 1041*efeda80dSTrond Myklebust !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1042*efeda80dSTrond Myklebust goto out; 1043*efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1044*efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf); 1045*efeda80dSTrond Myklebust out: 1046*efeda80dSTrond Myklebust dentry->d_time = verf; 1047*efeda80dSTrond Myklebust } 1048*efeda80dSTrond Myklebust 1049*efeda80dSTrond Myklebust /** 1050*efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry 1051*efeda80dSTrond Myklebust * @dentry: pointer to dentry 1052*efeda80dSTrond Myklebust * @verf: verifier to save 1053*efeda80dSTrond Myklebust * 1054*efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has 1055*efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated 1056*efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to 1057*efeda80dSTrond Myklebust * look it up again after a directory change. 1058*efeda80dSTrond Myklebust */ 1059*efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1060*efeda80dSTrond Myklebust { 1061*efeda80dSTrond Myklebust 1062*efeda80dSTrond Myklebust spin_lock(&dentry->d_lock); 1063*efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf); 1064*efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock); 1065*efeda80dSTrond Myklebust } 1066*efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier); 1067*efeda80dSTrond Myklebust 1068*efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4) 1069*efeda80dSTrond Myklebust /** 1070*efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1071*efeda80dSTrond Myklebust * @inode: pointer to inode 1072*efeda80dSTrond Myklebust * 1073*efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears 1074*efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated 1075*efeda80dSTrond Myklebust * while holding a delegation. 1076*efeda80dSTrond Myklebust * This function is intended for use when the delegation is being 1077*efeda80dSTrond Myklebust * returned or revoked. 1078*efeda80dSTrond Myklebust */ 1079*efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode) 1080*efeda80dSTrond Myklebust { 1081*efeda80dSTrond Myklebust struct dentry *alias; 1082*efeda80dSTrond Myklebust 1083*efeda80dSTrond Myklebust if (!inode) 1084*efeda80dSTrond Myklebust return; 1085*efeda80dSTrond Myklebust spin_lock(&inode->i_lock); 1086*efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1087*efeda80dSTrond Myklebust spin_lock(&alias->d_lock); 1088*efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time); 1089*efeda80dSTrond Myklebust spin_unlock(&alias->d_lock); 1090*efeda80dSTrond Myklebust } 1091*efeda80dSTrond Myklebust spin_unlock(&inode->i_lock); 1092*efeda80dSTrond Myklebust } 1093*efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1094*efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1095*efeda80dSTrond Myklebust 10961da177e4SLinus Torvalds /* 10971da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 10981da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10991da177e4SLinus Torvalds * and may need to be looked up again. 1100912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 11011da177e4SLinus Torvalds */ 1102912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1103912a108dSNeilBrown int rcu_walk) 11041da177e4SLinus Torvalds { 11051da177e4SLinus Torvalds if (IS_ROOT(dentry)) 11061da177e4SLinus Torvalds return 1; 11074eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 11084eec952eSTrond Myklebust return 0; 1109f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 11106ecc5e8fSTrond Myklebust return 0; 1111f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 11121cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1113912a108dSNeilBrown if (rcu_walk) 1114f2c77f4eSTrond Myklebust return 0; 11151cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 11161cd9cb05STrond Myklebust return 0; 11171cd9cb05STrond Myklebust } 1118f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1119f2c77f4eSTrond Myklebust return 0; 1120f2c77f4eSTrond Myklebust return 1; 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds /* 1124a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1125a12802caSTrond Myklebust * an O_EXCL create using this path component. 1126a12802caSTrond Myklebust */ 1127fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1128a12802caSTrond Myklebust { 1129a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1130a12802caSTrond Myklebust return 0; 1131fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1132a12802caSTrond Myklebust } 1133a12802caSTrond Myklebust 1134a12802caSTrond Myklebust /* 11351d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 11361d6757fbSTrond Myklebust * 11371d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 11381d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 11391d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 11401d6757fbSTrond Myklebust * 11411d6757fbSTrond Myklebust */ 114265a0c149STrond Myklebust static 1143fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 114665a0c149STrond Myklebust int ret; 11471da177e4SLinus Torvalds 114836d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 11494e99a1ffSTrond Myklebust return 0; 115047921921STrond Myklebust 115147921921STrond Myklebust if (flags & LOOKUP_OPEN) { 115247921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 115347921921STrond Myklebust case S_IFREG: 115447921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 115547921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 115647921921STrond Myklebust goto out; 115747921921STrond Myklebust /* Fallthrough */ 115847921921STrond Myklebust case S_IFDIR: 115947921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 116047921921STrond Myklebust break; 116147921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 116247921921STrond Myklebust goto out_force; 116347921921STrond Myklebust } 116447921921STrond Myklebust } 116547921921STrond Myklebust 11661da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1167fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 11681da177e4SLinus Torvalds goto out_force; 116965a0c149STrond Myklebust out: 1170a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 11711da177e4SLinus Torvalds out_force: 11721fa1e384SNeilBrown if (flags & LOOKUP_RCU) 11731fa1e384SNeilBrown return -ECHILD; 117465a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 117565a0c149STrond Myklebust if (ret != 0) 117665a0c149STrond Myklebust return ret; 117765a0c149STrond Myklebust goto out; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /* 11811da177e4SLinus Torvalds * We judge how long we want to trust negative 11821da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 11831da177e4SLinus Torvalds * 11841da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 11851da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1186912a108dSNeilBrown * 1187912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1188912a108dSNeilBrown * suggesting a reval is needed. 11899f6d44d4STrond Myklebust * 11909f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 11919f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 11921da177e4SLinus Torvalds */ 11931da177e4SLinus Torvalds static inline 11941da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1195fa3c56bbSAl Viro unsigned int flags) 11961da177e4SLinus Torvalds { 11979f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 11981da177e4SLinus Torvalds return 0; 11994eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 12004eec952eSTrond Myklebust return 1; 1201912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12045ceb9d7fSTrond Myklebust static int 12055ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 12065ceb9d7fSTrond Myklebust struct inode *inode, int error) 12071da177e4SLinus Torvalds { 12085ceb9d7fSTrond Myklebust switch (error) { 12095ceb9d7fSTrond Myklebust case 1: 12106de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 12116de1472fSAl Viro __func__, dentry); 12121da177e4SLinus Torvalds return 1; 12135ceb9d7fSTrond Myklebust case 0: 1214a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 12151da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 12161da177e4SLinus Torvalds /* Purge readdir caches. */ 12171da177e4SLinus Torvalds nfs_zap_caches(inode); 1218a3f432bfSJ. Bruce Fields /* 1219a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1220a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1221a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1222a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1223a3f432bfSJ. Bruce Fields */ 1224a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 12255ceb9d7fSTrond Myklebust return 1; 12261da177e4SLinus Torvalds } 12276de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 12286de1472fSAl Viro __func__, dentry); 12291da177e4SLinus Torvalds return 0; 12305ceb9d7fSTrond Myklebust } 12316de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 12326de1472fSAl Viro __func__, dentry, error); 1233e1fb4d05STrond Myklebust return error; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12365ceb9d7fSTrond Myklebust static int 12375ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 12385ceb9d7fSTrond Myklebust unsigned int flags) 12395ceb9d7fSTrond Myklebust { 12405ceb9d7fSTrond Myklebust int ret = 1; 12415ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 12425ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12435ceb9d7fSTrond Myklebust return -ECHILD; 12445ceb9d7fSTrond Myklebust ret = 0; 12455ceb9d7fSTrond Myklebust } 12465ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 12475ceb9d7fSTrond Myklebust } 12485ceb9d7fSTrond Myklebust 12495ceb9d7fSTrond Myklebust static int 12505ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 12515ceb9d7fSTrond Myklebust struct inode *inode) 12525ceb9d7fSTrond Myklebust { 12535ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12545ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12555ceb9d7fSTrond Myklebust } 12565ceb9d7fSTrond Myklebust 12575ceb9d7fSTrond Myklebust static int 12585ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 12595ceb9d7fSTrond Myklebust struct inode *inode) 12605ceb9d7fSTrond Myklebust { 12615ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 12625ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 12635ceb9d7fSTrond Myklebust struct nfs4_label *label; 1264a1147b82STrond Myklebust unsigned long dir_verifier; 12655ceb9d7fSTrond Myklebust int ret; 12665ceb9d7fSTrond Myklebust 12675ceb9d7fSTrond Myklebust ret = -ENOMEM; 12685ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 12695ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 12705ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 12715ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 12725ceb9d7fSTrond Myklebust goto out; 12735ceb9d7fSTrond Myklebust 1274a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 1275f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 12765ceb9d7fSTrond Myklebust if (ret < 0) { 1277f7b37b8bSTrond Myklebust switch (ret) { 1278f7b37b8bSTrond Myklebust case -ESTALE: 1279f7b37b8bSTrond Myklebust case -ENOENT: 12805ceb9d7fSTrond Myklebust ret = 0; 1281f7b37b8bSTrond Myklebust break; 1282f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1283f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1284f7b37b8bSTrond Myklebust ret = 1; 1285f7b37b8bSTrond Myklebust } 12865ceb9d7fSTrond Myklebust goto out; 12875ceb9d7fSTrond Myklebust } 12885ceb9d7fSTrond Myklebust ret = 0; 12895ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 12905ceb9d7fSTrond Myklebust goto out; 12915ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 12925ceb9d7fSTrond Myklebust goto out; 12935ceb9d7fSTrond Myklebust 12945ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 1295a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 12965ceb9d7fSTrond Myklebust 12975ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 12985ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 12995ceb9d7fSTrond Myklebust ret = 1; 13005ceb9d7fSTrond Myklebust out: 13015ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 13025ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 13035ceb9d7fSTrond Myklebust nfs4_label_free(label); 13045ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 13055ceb9d7fSTrond Myklebust } 13065ceb9d7fSTrond Myklebust 13075ceb9d7fSTrond Myklebust /* 13085ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 13095ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 13105ceb9d7fSTrond Myklebust * lookup. 13115ceb9d7fSTrond Myklebust * 13125ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 13135ceb9d7fSTrond Myklebust * we have an inode! 13145ceb9d7fSTrond Myklebust * 13155ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 13165ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 13175ceb9d7fSTrond Myklebust */ 13185ceb9d7fSTrond Myklebust static int 13195ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 13205ceb9d7fSTrond Myklebust unsigned int flags) 13215ceb9d7fSTrond Myklebust { 13225ceb9d7fSTrond Myklebust struct inode *inode; 13235ceb9d7fSTrond Myklebust int error; 13245ceb9d7fSTrond Myklebust 13255ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 13265ceb9d7fSTrond Myklebust inode = d_inode(dentry); 13275ceb9d7fSTrond Myklebust 13285ceb9d7fSTrond Myklebust if (!inode) 13295ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 13305ceb9d7fSTrond Myklebust 13315ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 13325ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13335ceb9d7fSTrond Myklebust __func__, dentry); 13345ceb9d7fSTrond Myklebust goto out_bad; 13355ceb9d7fSTrond Myklebust } 13365ceb9d7fSTrond Myklebust 1337*efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 13385ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 13395ceb9d7fSTrond Myklebust 13405ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 13415ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 13425ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 13435ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 13445ceb9d7fSTrond Myklebust if (error) { 13455ceb9d7fSTrond Myklebust if (error == -ESTALE) 13465ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 13475ceb9d7fSTrond Myklebust goto out_bad; 13485ceb9d7fSTrond Myklebust } 13495ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 13505ceb9d7fSTrond Myklebust goto out_valid; 13515ceb9d7fSTrond Myklebust } 13525ceb9d7fSTrond Myklebust 13535ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13545ceb9d7fSTrond Myklebust return -ECHILD; 13555ceb9d7fSTrond Myklebust 13565ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 13575ceb9d7fSTrond Myklebust goto out_bad; 13585ceb9d7fSTrond Myklebust 13595ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 13605ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 13615ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 13625ceb9d7fSTrond Myklebust return error; 13635ceb9d7fSTrond Myklebust out_valid: 13645ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 13655ceb9d7fSTrond Myklebust out_bad: 13665ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13675ceb9d7fSTrond Myklebust return -ECHILD; 13685ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 13695ceb9d7fSTrond Myklebust } 13705ceb9d7fSTrond Myklebust 13715ceb9d7fSTrond Myklebust static int 1372c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1373c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 13745ceb9d7fSTrond Myklebust { 13755ceb9d7fSTrond Myklebust struct dentry *parent; 13765ceb9d7fSTrond Myklebust struct inode *dir; 13775ceb9d7fSTrond Myklebust int ret; 13785ceb9d7fSTrond Myklebust 13795ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 13805ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 13815ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 13825ceb9d7fSTrond Myklebust if (!dir) 13835ceb9d7fSTrond Myklebust return -ECHILD; 1384c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 13855ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 13865ceb9d7fSTrond Myklebust return -ECHILD; 13875ceb9d7fSTrond Myklebust } else { 13885ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1389c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 13905ceb9d7fSTrond Myklebust dput(parent); 13915ceb9d7fSTrond Myklebust } 13925ceb9d7fSTrond Myklebust return ret; 13935ceb9d7fSTrond Myklebust } 13945ceb9d7fSTrond Myklebust 1395c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1396c7944ebbSTrond Myklebust { 1397c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1398c7944ebbSTrond Myklebust } 1399c7944ebbSTrond Myklebust 14001da177e4SLinus Torvalds /* 14012b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1402ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1403ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1404ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1405ecf3d1f1SJeff Layton * 1406ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1407ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1408ecf3d1f1SJeff Layton */ 1409ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1410ecf3d1f1SJeff Layton { 14112b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 14129cdd1d3fSTrond Myklebust int error = 0; 1413ecf3d1f1SJeff Layton 1414ecf3d1f1SJeff Layton /* 1415ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1416ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1417ecf3d1f1SJeff Layton * eventually need to do something more here. 1418ecf3d1f1SJeff Layton */ 1419ecf3d1f1SJeff Layton if (!inode) { 14206de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 14216de1472fSAl Viro __func__, dentry); 1422ecf3d1f1SJeff Layton return 1; 1423ecf3d1f1SJeff Layton } 1424ecf3d1f1SJeff Layton 1425ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 14266de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 14276de1472fSAl Viro __func__, dentry); 1428ecf3d1f1SJeff Layton return 0; 1429ecf3d1f1SJeff Layton } 1430ecf3d1f1SJeff Layton 1431b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1432ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1433ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1434ecf3d1f1SJeff Layton return !error; 1435ecf3d1f1SJeff Layton } 1436ecf3d1f1SJeff Layton 1437ecf3d1f1SJeff Layton /* 14381da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 14391da177e4SLinus Torvalds */ 1440fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 14411da177e4SLinus Torvalds { 14426de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 14436de1472fSAl Viro dentry, dentry->d_flags); 14441da177e4SLinus Torvalds 144577f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 14462b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 144777f11192STrond Myklebust return 1; 144877f11192STrond Myklebust 14491da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 14501da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 14511da177e4SLinus Torvalds return 1; 14521da177e4SLinus Torvalds } 14531751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 14541da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 14551da177e4SLinus Torvalds * files will be cleaned up during umount */ 14561da177e4SLinus Torvalds return 1; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds return 0; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 14631b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 14641b83d707STrond Myklebust { 14651b83d707STrond Myklebust spin_lock(&inode->i_lock); 14661f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 146759a707b0STrond Myklebust if (inode->i_nlink > 0) 146859a707b0STrond Myklebust drop_nlink(inode); 146959a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 147016e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 147116e14375STrond Myklebust | NFS_INO_INVALID_CTIME 147259a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 147359a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 14741b83d707STrond Myklebust spin_unlock(&inode->i_lock); 14751b83d707STrond Myklebust } 14761b83d707STrond Myklebust 14771da177e4SLinus Torvalds /* 14781da177e4SLinus Torvalds * Called when the dentry loses inode. 14791da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 14801da177e4SLinus Torvalds */ 14811da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 14821da177e4SLinus Torvalds { 148383672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 148483672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 148583672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 148683672d39SNeil Brown 14871da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1488e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 14891f018458STrond Myklebust nfs_drop_nlink(inode); 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds iput(inode); 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 1494b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1495b1942c5fSAl Viro { 1496b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1497b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1498b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1499b1942c5fSAl Viro WARN_ON(1); 1500b1942c5fSAl Viro else 1501b1942c5fSAl Viro kfree(dentry->d_fsdata); 1502b1942c5fSAl Viro } 1503b1942c5fSAl Viro } 1504b1942c5fSAl Viro 1505f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 15061da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1507ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 15081da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15091da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 151036d43a43SDavid Howells .d_automount = nfs_d_automount, 1511b1942c5fSAl Viro .d_release = nfs_d_release, 15121da177e4SLinus Torvalds }; 1513ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 15141da177e4SLinus Torvalds 1515597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 15161da177e4SLinus Torvalds { 15171da177e4SLinus Torvalds struct dentry *res; 15181da177e4SLinus Torvalds struct inode *inode = NULL; 1519e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1520e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 15211775fd3eSDavid Quigley struct nfs4_label *label = NULL; 1522a1147b82STrond Myklebust unsigned long dir_verifier; 15231da177e4SLinus Torvalds int error; 15241da177e4SLinus Torvalds 15256de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 152691d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 15271da177e4SLinus Torvalds 1528130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1529130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 15301da177e4SLinus Torvalds 1531fd684071STrond Myklebust /* 1532fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1533fd684071STrond Myklebust * but don't hash the dentry. 1534fd684071STrond Myklebust */ 15359f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1536130f9ab7SAl Viro return NULL; 15371da177e4SLinus Torvalds 1538e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1539e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1540e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1541e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1542e1fb4d05STrond Myklebust goto out; 1543e1fb4d05STrond Myklebust 154414c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 154514c43f76SDavid Quigley if (IS_ERR(label)) 154614c43f76SDavid Quigley goto out; 154714c43f76SDavid Quigley 1548a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir); 15496e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1550f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 15511da177e4SLinus Torvalds if (error == -ENOENT) 15521da177e4SLinus Torvalds goto no_entry; 15531da177e4SLinus Torvalds if (error < 0) { 15541da177e4SLinus Torvalds res = ERR_PTR(error); 1555bf130914SAl Viro goto out_label; 15561da177e4SLinus Torvalds } 15571775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1558bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 155903f28e3aSTrond Myklebust if (IS_ERR(res)) 1560bf130914SAl Viro goto out_label; 156154ceac45SDavid Howells 156263519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 156363519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1564d69ee9b8STrond Myklebust 15651da177e4SLinus Torvalds no_entry: 156641d28bcaSAl Viro res = d_splice_alias(inode, dentry); 15679eaef27bSTrond Myklebust if (res != NULL) { 15689eaef27bSTrond Myklebust if (IS_ERR(res)) 1569bf130914SAl Viro goto out_label; 15701da177e4SLinus Torvalds dentry = res; 15719eaef27bSTrond Myklebust } 1572a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier); 1573bf130914SAl Viro out_label: 15746e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 157514c43f76SDavid Quigley nfs4_label_free(label); 15761da177e4SLinus Torvalds out: 1577e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1578e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 15791da177e4SLinus Torvalds return res; 15801da177e4SLinus Torvalds } 1581ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 15821da177e4SLinus Torvalds 158389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 15840b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 15851da177e4SLinus Torvalds 1586f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 15870ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1588b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 15891da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15901da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 159136d43a43SDavid Howells .d_automount = nfs_d_automount, 1592b1942c5fSAl Viro .d_release = nfs_d_release, 15931da177e4SLinus Torvalds }; 159489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 15951da177e4SLinus Torvalds 15968a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 15978a5e929dSAl Viro { 15988a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 15998a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 16008a5e929dSAl Viro res |= FMODE_READ; 16018a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 16028a5e929dSAl Viro res |= FMODE_WRITE; 16038a5e929dSAl Viro return res; 16048a5e929dSAl Viro } 16058a5e929dSAl Viro 1606532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1607cd9a1c0eSTrond Myklebust { 1608532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1609cd9a1c0eSTrond Myklebust } 1610cd9a1c0eSTrond Myklebust 1611cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1612cd9a1c0eSTrond Myklebust { 1613f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1614cd9a1c0eSTrond Myklebust return 0; 1615cd9a1c0eSTrond Myklebust } 1616cd9a1c0eSTrond Myklebust 1617d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 16180dd2b474SMiklos Szeredi struct dentry *dentry, 1619b452a458SAl Viro struct file *file, unsigned open_flags) 1620cd9a1c0eSTrond Myklebust { 16210dd2b474SMiklos Szeredi int err; 16220dd2b474SMiklos Szeredi 1623be12af3eSAl Viro err = finish_open(file, dentry, do_open); 162430d90494SAl Viro if (err) 1625d9585277SAl Viro goto out; 1626eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 162730d90494SAl Viro nfs_file_set_open_context(file, ctx); 1628eaa2b82cSNeilBrown else 16299821421aSTrond Myklebust err = -EOPENSTALE; 1630cd9a1c0eSTrond Myklebust out: 1631d9585277SAl Viro return err; 1632cd9a1c0eSTrond Myklebust } 1633cd9a1c0eSTrond Myklebust 163473a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 163530d90494SAl Viro struct file *file, unsigned open_flags, 163644907d79SAl Viro umode_t mode) 16371da177e4SLinus Torvalds { 1638c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1639cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 16400dd2b474SMiklos Szeredi struct dentry *res; 16410dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1642f46e0bd3STrond Myklebust struct inode *inode; 16431472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1644c94c0953SAl Viro bool switched = false; 164573a09dd9SAl Viro int created = 0; 1646898f635cSTrond Myklebust int err; 16471da177e4SLinus Torvalds 16480dd2b474SMiklos Szeredi /* Expect a negative dentry */ 16492b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 16500dd2b474SMiklos Szeredi 16511e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 16526de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 16531e7cb3dcSChuck Lever 16549597c13bSJeff Layton err = nfs_check_flags(open_flags); 16559597c13bSJeff Layton if (err) 16569597c13bSJeff Layton return err; 16579597c13bSJeff Layton 16580dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 16590dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 166000699ad8SAl Viro if (!d_in_lookup(dentry)) { 16610dd2b474SMiklos Szeredi /* 16620dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 16630dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 16640dd2b474SMiklos Szeredi * again 16650dd2b474SMiklos Szeredi */ 1666d9585277SAl Viro return -ENOENT; 16670dd2b474SMiklos Szeredi } 16681472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 16691da177e4SLinus Torvalds goto no_open; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16720dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1673d9585277SAl Viro return -ENAMETOOLONG; 16741da177e4SLinus Torvalds 16750dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1676dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1677dff25ddbSAndreas Gruenbacher 1678dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1679dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1680dff25ddbSAndreas Gruenbacher 1681536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1682dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 16830dd2b474SMiklos Szeredi } 1684536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1685536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1686536e43d1STrond Myklebust attr.ia_size = 0; 1687cd9a1c0eSTrond Myklebust } 1688cd9a1c0eSTrond Myklebust 1689c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1690c94c0953SAl Viro d_drop(dentry); 1691c94c0953SAl Viro switched = true; 1692c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1693c94c0953SAl Viro &dentry->d_name, &wq); 1694c94c0953SAl Viro if (IS_ERR(dentry)) 1695c94c0953SAl Viro return PTR_ERR(dentry); 1696c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1697c94c0953SAl Viro return finish_no_open(file, dentry); 1698c94c0953SAl Viro } 1699c94c0953SAl Viro 1700532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 17010dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 17020dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1703d9585277SAl Viro goto out; 17040dd2b474SMiklos Szeredi 17056e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 170673a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 170773a09dd9SAl Viro if (created) 170873a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1709275bb307STrond Myklebust if (IS_ERR(inode)) { 17100dd2b474SMiklos Szeredi err = PTR_ERR(inode); 17116e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17122d9db750STrond Myklebust put_nfs_open_context(ctx); 1713d20cb71dSAl Viro d_drop(dentry); 17140dd2b474SMiklos Szeredi switch (err) { 17151da177e4SLinus Torvalds case -ENOENT: 1716774d9513SPeng Tao d_splice_alias(NULL, dentry); 1717809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17180dd2b474SMiklos Szeredi break; 17191788ea6eSJeff Layton case -EISDIR: 17206f926b5bSTrond Myklebust case -ENOTDIR: 17216f926b5bSTrond Myklebust goto no_open; 17221da177e4SLinus Torvalds case -ELOOP: 17230dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 17241da177e4SLinus Torvalds goto no_open; 17250dd2b474SMiklos Szeredi break; 17261da177e4SLinus Torvalds /* case -EINVAL: */ 17271da177e4SLinus Torvalds default: 17280dd2b474SMiklos Szeredi break; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds goto out; 17311da177e4SLinus Torvalds } 17320dd2b474SMiklos Szeredi 1733b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 17346e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 17352d9db750STrond Myklebust put_nfs_open_context(ctx); 1736d9585277SAl Viro out: 1737c94c0953SAl Viro if (unlikely(switched)) { 1738c94c0953SAl Viro d_lookup_done(dentry); 1739c94c0953SAl Viro dput(dentry); 1740c94c0953SAl Viro } 1741d9585277SAl Viro return err; 17420dd2b474SMiklos Szeredi 17431da177e4SLinus Torvalds no_open: 17441472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1745c94c0953SAl Viro if (switched) { 1746c94c0953SAl Viro d_lookup_done(dentry); 1747c94c0953SAl Viro if (!res) 1748c94c0953SAl Viro res = dentry; 1749c94c0953SAl Viro else 1750c94c0953SAl Viro dput(dentry); 1751c94c0953SAl Viro } 17520dd2b474SMiklos Szeredi if (IS_ERR(res)) 1753c94c0953SAl Viro return PTR_ERR(res); 1754e45198a6SAl Viro return finish_no_open(file, res); 17551da177e4SLinus Torvalds } 175689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 17571da177e4SLinus Torvalds 1758c7944ebbSTrond Myklebust static int 1759c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1760c7944ebbSTrond Myklebust unsigned int flags) 17611da177e4SLinus Torvalds { 1762657e94b6SNick Piggin struct inode *inode; 17631da177e4SLinus Torvalds 1764fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1765c7944ebbSTrond Myklebust goto full_reval; 1766eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1767c7944ebbSTrond Myklebust goto full_reval; 17682b484297STrond Myklebust 17692b0143b5SDavid Howells inode = d_inode(dentry); 17702b484297STrond Myklebust 17711da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 17721da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 17731da177e4SLinus Torvalds */ 1774c7944ebbSTrond Myklebust if (inode == NULL) 1775c7944ebbSTrond Myklebust goto full_reval; 177649317a7fSNeilBrown 1777*efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry)) 1778c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1779216d5d06STrond Myklebust 17801da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 17811da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1782c7944ebbSTrond Myklebust goto full_reval; 1783c7944ebbSTrond Myklebust 17841da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1785c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1786c7944ebbSTrond Myklebust goto reval_dentry; 1787c7944ebbSTrond Myklebust 1788c7944ebbSTrond Myklebust /* Check if the directory changed */ 1789c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1790c7944ebbSTrond Myklebust goto reval_dentry; 17911da177e4SLinus Torvalds 17920ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1793c7944ebbSTrond Myklebust return 1; 1794c7944ebbSTrond Myklebust reval_dentry: 1795c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1796c7944ebbSTrond Myklebust return -ECHILD; 179742f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 17980ef97dcfSMiklos Szeredi 1799c7944ebbSTrond Myklebust full_reval: 1800c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1801c7944ebbSTrond Myklebust } 1802535918f1STrond Myklebust 1803c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1804c7944ebbSTrond Myklebust { 1805c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1806c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1807c0204fd2STrond Myklebust } 1808c0204fd2STrond Myklebust 18091da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 18101da177e4SLinus Torvalds 1811406cd915SBenjamin Coddington struct dentry * 1812406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 18131775fd3eSDavid Quigley struct nfs_fattr *fattr, 18141775fd3eSDavid Quigley struct nfs4_label *label) 18151da177e4SLinus Torvalds { 1816fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 18172b0143b5SDavid Howells struct inode *dir = d_inode(parent); 18181da177e4SLinus Torvalds struct inode *inode; 1819b0c6108eSAl Viro struct dentry *d; 1820406cd915SBenjamin Coddington int error; 18211da177e4SLinus Torvalds 1822fab728e1STrond Myklebust d_drop(dentry); 1823fab728e1STrond Myklebust 18241da177e4SLinus Torvalds if (fhandle->size == 0) { 1825f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 18261da177e4SLinus Torvalds if (error) 1827fab728e1STrond Myklebust goto out_error; 18281da177e4SLinus Torvalds } 18295724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18301da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 18311da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1832a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1833a841b54dSTrond Myklebust fattr, NULL, NULL); 18341da177e4SLinus Torvalds if (error < 0) 1835fab728e1STrond Myklebust goto out_error; 18361da177e4SLinus Torvalds } 18371775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1838b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1839fab728e1STrond Myklebust out: 1840fab728e1STrond Myklebust dput(parent); 1841406cd915SBenjamin Coddington return d; 1842fab728e1STrond Myklebust out_error: 1843fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1844406cd915SBenjamin Coddington d = ERR_PTR(error); 1845406cd915SBenjamin Coddington goto out; 1846406cd915SBenjamin Coddington } 1847406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1848406cd915SBenjamin Coddington 1849406cd915SBenjamin Coddington /* 1850406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1851406cd915SBenjamin Coddington */ 1852406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1853406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1854406cd915SBenjamin Coddington struct nfs4_label *label) 1855406cd915SBenjamin Coddington { 1856406cd915SBenjamin Coddington struct dentry *d; 1857406cd915SBenjamin Coddington 1858406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1859406cd915SBenjamin Coddington if (IS_ERR(d)) 1860406cd915SBenjamin Coddington return PTR_ERR(d); 1861406cd915SBenjamin Coddington 1862406cd915SBenjamin Coddington /* Callers don't care */ 1863406cd915SBenjamin Coddington dput(d); 1864406cd915SBenjamin Coddington return 0; 18651da177e4SLinus Torvalds } 1866ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds /* 18691da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 18701da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 18711da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 18721da177e4SLinus Torvalds * reply path made it appear to have failed. 18731da177e4SLinus Torvalds */ 1874597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1875ebfc3b49SAl Viro umode_t mode, bool excl) 18761da177e4SLinus Torvalds { 18771da177e4SLinus Torvalds struct iattr attr; 1878ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 18791da177e4SLinus Torvalds int error; 18801da177e4SLinus Torvalds 18811e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 18826de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds attr.ia_mode = mode; 18851da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18861da177e4SLinus Torvalds 18878b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 18888867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 18898b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 18901da177e4SLinus Torvalds if (error != 0) 18911da177e4SLinus Torvalds goto out_err; 18921da177e4SLinus Torvalds return 0; 18931da177e4SLinus Torvalds out_err: 18941da177e4SLinus Torvalds d_drop(dentry); 18951da177e4SLinus Torvalds return error; 18961da177e4SLinus Torvalds } 1897ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds /* 19001da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19011da177e4SLinus Torvalds */ 1902597d9289SBryan Schumaker int 19031a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 19041da177e4SLinus Torvalds { 19051da177e4SLinus Torvalds struct iattr attr; 19061da177e4SLinus Torvalds int status; 19071da177e4SLinus Torvalds 19081e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%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 19141ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 19151da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 19161ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 19171da177e4SLinus Torvalds if (status != 0) 19181da177e4SLinus Torvalds goto out_err; 19191da177e4SLinus Torvalds return 0; 19201da177e4SLinus Torvalds out_err: 19211da177e4SLinus Torvalds d_drop(dentry); 19221da177e4SLinus Torvalds return status; 19231da177e4SLinus Torvalds } 1924ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 19281da177e4SLinus Torvalds */ 1929597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19301da177e4SLinus Torvalds { 19311da177e4SLinus Torvalds struct iattr attr; 19321da177e4SLinus Torvalds int error; 19331da177e4SLinus Torvalds 19341e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 19356de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 19381da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 19391da177e4SLinus Torvalds 19401ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 19411da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 19421ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 19431da177e4SLinus Torvalds if (error != 0) 19441da177e4SLinus Torvalds goto out_err; 19451da177e4SLinus Torvalds return 0; 19461da177e4SLinus Torvalds out_err: 19471da177e4SLinus Torvalds d_drop(dentry); 19481da177e4SLinus Torvalds return error; 19491da177e4SLinus Torvalds } 1950ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 19511da177e4SLinus Torvalds 1952d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1953d45b9d8bSTrond Myklebust { 1954dc3f4198SAl Viro if (simple_positive(dentry)) 1955d45b9d8bSTrond Myklebust d_delete(dentry); 1956d45b9d8bSTrond Myklebust } 1957d45b9d8bSTrond Myklebust 1958597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 19591da177e4SLinus Torvalds { 19601da177e4SLinus Torvalds int error; 19611da177e4SLinus Torvalds 19621e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 19636de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 19641da177e4SLinus Torvalds 19651ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 19662b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1967884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 19681da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 19691da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1970ba6c0592STrond Myklebust switch (error) { 1971ba6c0592STrond Myklebust case 0: 19722b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1973ba6c0592STrond Myklebust break; 1974ba6c0592STrond Myklebust case -ENOENT: 1975d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1976ba6c0592STrond Myklebust } 1977884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1978ba6c0592STrond Myklebust } else 1979ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 19801ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds return error; 19831da177e4SLinus Torvalds } 1984ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds /* 19871da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 19881da177e4SLinus Torvalds * and after checking that the file has only one user. 19891da177e4SLinus Torvalds * 19901da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 19911da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 19921da177e4SLinus Torvalds */ 19931da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 19941da177e4SLinus Torvalds { 19952b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 19962b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 19971da177e4SLinus Torvalds int error = -EBUSY; 19981da177e4SLinus Torvalds 19996de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 20021da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 20031da177e4SLinus Torvalds error = 0; 20041da177e4SLinus Torvalds goto out; 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds 20071ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 20081da177e4SLinus Torvalds if (inode != NULL) { 2009912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 20101da177e4SLinus Torvalds if (error == 0) 20111b83d707STrond Myklebust nfs_drop_nlink(inode); 20121da177e4SLinus Torvalds } else 2013912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 2014d45b9d8bSTrond Myklebust if (error == -ENOENT) 2015d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 20161ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 20171da177e4SLinus Torvalds out: 20181da177e4SLinus Torvalds return error; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 20221da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 20231da177e4SLinus Torvalds * 20241da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 20251da177e4SLinus Torvalds */ 2026597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 20271da177e4SLinus Torvalds { 20281da177e4SLinus Torvalds int error; 20291da177e4SLinus Torvalds int need_rehash = 0; 20301da177e4SLinus Torvalds 20311e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 20326de1472fSAl Viro dir->i_ino, dentry); 20331da177e4SLinus Torvalds 20341ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 20351da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 203684d08fa8SAl Viro if (d_count(dentry) > 1) { 20371da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 2038ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 20392b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 20401da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 20411ca42382STrond Myklebust goto out; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 20441da177e4SLinus Torvalds __d_drop(dentry); 20451da177e4SLinus Torvalds need_rehash = 1; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 20481da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 2049d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 20501da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 20511da177e4SLinus Torvalds } else if (need_rehash) 20521da177e4SLinus Torvalds d_rehash(dentry); 20531ca42382STrond Myklebust out: 20541ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 20551da177e4SLinus Torvalds return error; 20561da177e4SLinus Torvalds } 2057ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 20581da177e4SLinus Torvalds 2059873101b3SChuck Lever /* 2060873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 2061873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 2062873101b3SChuck Lever * using prepare_write/commit_write. 2063873101b3SChuck Lever * 2064873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 2065873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 2066873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2067873101b3SChuck Lever * symlink request has completed on the server. 2068873101b3SChuck Lever * 2069873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 2070873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 2071873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 2072873101b3SChuck Lever * and move the raw page into its mapping. 2073873101b3SChuck Lever */ 2074597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 20751da177e4SLinus Torvalds { 2076873101b3SChuck Lever struct page *page; 2077873101b3SChuck Lever char *kaddr; 20781da177e4SLinus Torvalds struct iattr attr; 2079873101b3SChuck Lever unsigned int pathlen = strlen(symname); 20801da177e4SLinus Torvalds int error; 20811da177e4SLinus Torvalds 20821e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 20836de1472fSAl Viro dir->i_ino, dentry, symname); 20841da177e4SLinus Torvalds 2085873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2086873101b3SChuck Lever return -ENAMETOOLONG; 20871da177e4SLinus Torvalds 2088873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2089873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 20901da177e4SLinus Torvalds 2091e8ecde25SAl Viro page = alloc_page(GFP_USER); 209276566991STrond Myklebust if (!page) 2093873101b3SChuck Lever return -ENOMEM; 2094873101b3SChuck Lever 2095e8ecde25SAl Viro kaddr = page_address(page); 2096873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2097873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2098873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2099873101b3SChuck Lever 21001ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 210194a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 21021ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2103873101b3SChuck Lever if (error != 0) { 21041e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2105873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 21066de1472fSAl Viro dentry, symname, error); 21071da177e4SLinus Torvalds d_drop(dentry); 2108873101b3SChuck Lever __free_page(page); 21091da177e4SLinus Torvalds return error; 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 2112873101b3SChuck Lever /* 2113873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2114873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2115873101b3SChuck Lever */ 21162b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2117873101b3SChuck Lever GFP_KERNEL)) { 2118873101b3SChuck Lever SetPageUptodate(page); 2119873101b3SChuck Lever unlock_page(page); 2120a0b54addSRafael Aquini /* 2121a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2122a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2123a0b54addSRafael Aquini */ 212409cbfeafSKirill A. Shutemov put_page(page); 2125873101b3SChuck Lever } else 2126873101b3SChuck Lever __free_page(page); 2127873101b3SChuck Lever 2128873101b3SChuck Lever return 0; 2129873101b3SChuck Lever } 2130ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2131873101b3SChuck Lever 2132597d9289SBryan Schumaker int 21331da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 21341da177e4SLinus Torvalds { 21352b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 21361da177e4SLinus Torvalds int error; 21371da177e4SLinus Torvalds 21386de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 21396de1472fSAl Viro old_dentry, dentry); 21401da177e4SLinus Torvalds 21411fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 21429697d234STrond Myklebust d_drop(dentry); 21431da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2144cf809556STrond Myklebust if (error == 0) { 21457de9c6eeSAl Viro ihold(inode); 21469697d234STrond Myklebust d_add(dentry, inode); 2147cf809556STrond Myklebust } 21481fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 21491da177e4SLinus Torvalds return error; 21501da177e4SLinus Torvalds } 2151ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds /* 21541da177e4SLinus Torvalds * RENAME 21551da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 21561da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 21571da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 21581da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 21591da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 21601da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 21611da177e4SLinus Torvalds * 21621da177e4SLinus Torvalds * FIXED. 21631da177e4SLinus Torvalds * 21641da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 21651da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 21661da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 21671da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 21681da177e4SLinus Torvalds * using the inode layer 21691da177e4SLinus Torvalds * 21701da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 21711da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 21721da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 21731da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 21741da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 21751da177e4SLinus Torvalds * the rename. 21761da177e4SLinus Torvalds */ 2177597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 21781cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 21791cd66c93SMiklos Szeredi unsigned int flags) 21801da177e4SLinus Torvalds { 21812b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 21822b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2183d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 218480a491fdSJeff Layton struct rpc_task *task; 21851da177e4SLinus Torvalds int error = -EBUSY; 21861da177e4SLinus Torvalds 21871cd66c93SMiklos Szeredi if (flags) 21881cd66c93SMiklos Szeredi return -EINVAL; 21891cd66c93SMiklos Szeredi 21906de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 21916de1472fSAl Viro old_dentry, new_dentry, 219284d08fa8SAl Viro d_count(new_dentry)); 21931da177e4SLinus Torvalds 219470ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 21951da177e4SLinus Torvalds /* 219628f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 219728f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 219828f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 219928f79a1aSMiklos Szeredi * the new target. 22001da177e4SLinus Torvalds */ 220127226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 220227226104SMiklos Szeredi /* 220327226104SMiklos Szeredi * To prevent any new references to the target during the 220427226104SMiklos Szeredi * rename, we unhash the dentry in advance. 220527226104SMiklos Szeredi */ 2206d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 220727226104SMiklos Szeredi d_drop(new_dentry); 2208d9f29500SBenjamin Coddington rehash = new_dentry; 2209d9f29500SBenjamin Coddington } 221027226104SMiklos Szeredi 221184d08fa8SAl Viro if (d_count(new_dentry) > 2) { 22121da177e4SLinus Torvalds int err; 221327226104SMiklos Szeredi 22141da177e4SLinus Torvalds /* copy the target dentry's name */ 22151da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 22161da177e4SLinus Torvalds &new_dentry->d_name); 22171da177e4SLinus Torvalds if (!dentry) 22181da177e4SLinus Torvalds goto out; 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds /* silly-rename the existing target ... */ 22211da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 222224e93025SMiklos Szeredi if (err) 22231da177e4SLinus Torvalds goto out; 222424e93025SMiklos Szeredi 222524e93025SMiklos Szeredi new_dentry = dentry; 2226d9f29500SBenjamin Coddington rehash = NULL; 222724e93025SMiklos Szeredi new_inode = NULL; 2228b1e4adf4STrond Myklebust } 222927226104SMiklos Szeredi } 22301da177e4SLinus Torvalds 2231d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 223280a491fdSJeff Layton if (IS_ERR(task)) { 223380a491fdSJeff Layton error = PTR_ERR(task); 223480a491fdSJeff Layton goto out; 223580a491fdSJeff Layton } 223680a491fdSJeff Layton 223780a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2238818a8dbeSBenjamin Coddington if (error != 0) { 2239818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2240818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2241818a8dbeSBenjamin Coddington smp_wmb(); 2242818a8dbeSBenjamin Coddington } else 224380a491fdSJeff Layton error = task->tk_status; 224480a491fdSJeff Layton rpc_put_task(task); 224559a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 224659a707b0STrond Myklebust if (error == 0) { 224759a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 224859a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 224959a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 225059a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 225159a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 225259a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 225359a707b0STrond Myklebust } 22541da177e4SLinus Torvalds out: 2255d9f29500SBenjamin Coddington if (rehash) 2256d9f29500SBenjamin Coddington d_rehash(rehash); 225770ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 225870ded201STrond Myklebust new_dir, new_dentry, error); 2259d9f29500SBenjamin Coddington if (!error) { 2260d9f29500SBenjamin Coddington if (new_inode != NULL) 2261d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2262d9f29500SBenjamin Coddington /* 2263d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2264d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2265d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2266d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2267d9f29500SBenjamin Coddington */ 2268d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2269d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2270d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2271d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2272d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2273d9f29500SBenjamin Coddington 22741da177e4SLinus Torvalds /* new dentry created? */ 22751da177e4SLinus Torvalds if (dentry) 22761da177e4SLinus Torvalds dput(dentry); 22771da177e4SLinus Torvalds return error; 22781da177e4SLinus Torvalds } 2279ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 22801da177e4SLinus Torvalds 2281cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2282cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2283cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2284cfcea3e8STrond Myklebust 22853a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 22863a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 22873a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 22883a505845STrond Myklebust 22891c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 22901c3c07e9STrond Myklebust { 2291b68572e0SNeilBrown put_cred(entry->cred); 2292f682a398SNeilBrown kfree_rcu(entry, rcu_head); 22934e857c58SPeter Zijlstra smp_mb__before_atomic(); 2294cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 22954e857c58SPeter Zijlstra smp_mb__after_atomic(); 22961c3c07e9STrond Myklebust } 22971c3c07e9STrond Myklebust 22981a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 22991a81bb8aSTrond Myklebust { 23001a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 23011a81bb8aSTrond Myklebust 23021a81bb8aSTrond Myklebust while (!list_empty(head)) { 23031a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 23041a81bb8aSTrond Myklebust list_del(&cache->lru); 23051a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 23061a81bb8aSTrond Myklebust } 23071a81bb8aSTrond Myklebust } 23081a81bb8aSTrond Myklebust 23093a505845STrond Myklebust static unsigned long 23103a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2311979df72eSTrond Myklebust { 2312979df72eSTrond Myklebust LIST_HEAD(head); 2313aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2314979df72eSTrond Myklebust struct nfs_access_entry *cache; 23151ab6c499SDave Chinner long freed = 0; 2316979df72eSTrond Myklebust 2317a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2318aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2319979df72eSTrond Myklebust struct inode *inode; 2320979df72eSTrond Myklebust 2321979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2322979df72eSTrond Myklebust break; 23239c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2324979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2325979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2326979df72eSTrond Myklebust goto remove_lru_entry; 2327979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2328979df72eSTrond Myklebust struct nfs_access_entry, lru); 2329979df72eSTrond Myklebust list_move(&cache->lru, &head); 2330979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 23311ab6c499SDave Chinner freed++; 2332979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2333979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2334979df72eSTrond Myklebust &nfs_access_lru_list); 2335979df72eSTrond Myklebust else { 2336979df72eSTrond Myklebust remove_lru_entry: 2337979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 23384e857c58SPeter Zijlstra smp_mb__before_atomic(); 2339979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 23404e857c58SPeter Zijlstra smp_mb__after_atomic(); 2341979df72eSTrond Myklebust } 234259844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2343979df72eSTrond Myklebust } 2344979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23451a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23461ab6c499SDave Chinner return freed; 23471ab6c499SDave Chinner } 23481ab6c499SDave Chinner 23491ab6c499SDave Chinner unsigned long 23503a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 23513a505845STrond Myklebust { 23523a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 23533a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 23543a505845STrond Myklebust 23553a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 23563a505845STrond Myklebust return SHRINK_STOP; 23573a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 23583a505845STrond Myklebust } 23593a505845STrond Myklebust 23603a505845STrond Myklebust 23613a505845STrond Myklebust unsigned long 23621ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 23631ab6c499SDave Chinner { 236455f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2365979df72eSTrond Myklebust } 2366979df72eSTrond Myklebust 23673a505845STrond Myklebust static void 23683a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 23693a505845STrond Myklebust { 23703a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 23713a505845STrond Myklebust unsigned long diff; 23723a505845STrond Myklebust unsigned int nr_to_scan; 23733a505845STrond Myklebust 23743a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 23753a505845STrond Myklebust return; 23763a505845STrond Myklebust nr_to_scan = 100; 23773a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 23783a505845STrond Myklebust if (diff < nr_to_scan) 23793a505845STrond Myklebust nr_to_scan = diff; 23803a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 23813a505845STrond Myklebust } 23823a505845STrond Myklebust 23831a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 23841c3c07e9STrond Myklebust { 23851c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23861a81bb8aSTrond Myklebust struct rb_node *n; 23871c3c07e9STrond Myklebust struct nfs_access_entry *entry; 23881c3c07e9STrond Myklebust 23891c3c07e9STrond Myklebust /* Unhook entries from the cache */ 23901c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 23911c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 23921c3c07e9STrond Myklebust rb_erase(n, root_node); 23931a81bb8aSTrond Myklebust list_move(&entry->lru, head); 23941c3c07e9STrond Myklebust } 23951c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 23961c3c07e9STrond Myklebust } 23971c3c07e9STrond Myklebust 23981c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 23991c3c07e9STrond Myklebust { 24001a81bb8aSTrond Myklebust LIST_HEAD(head); 24011a81bb8aSTrond Myklebust 24021a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 24031a81bb8aSTrond Myklebust return; 2404cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2405cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24061a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2407cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2408cfcea3e8STrond Myklebust 24091c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24101a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 24111a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 24121a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 24131a81bb8aSTrond Myklebust nfs_access_free_list(&head); 24141c3c07e9STrond Myklebust } 24151c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 24161c3c07e9STrond Myklebust 2417b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 24181c3c07e9STrond Myklebust { 24191c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 24201c3c07e9STrond Myklebust 24211c3c07e9STrond Myklebust while (n != NULL) { 2422b68572e0SNeilBrown struct nfs_access_entry *entry = 2423b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2424b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 24251c3c07e9STrond Myklebust 2426b68572e0SNeilBrown if (cmp < 0) 24271c3c07e9STrond Myklebust n = n->rb_left; 2428b68572e0SNeilBrown else if (cmp > 0) 24291c3c07e9STrond Myklebust n = n->rb_right; 24301c3c07e9STrond Myklebust else 24311c3c07e9STrond Myklebust return entry; 24321c3c07e9STrond Myklebust } 24331c3c07e9STrond Myklebust return NULL; 24341c3c07e9STrond Myklebust } 24351c3c07e9STrond Myklebust 2436b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 24371da177e4SLinus Torvalds { 243855296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 24391c3c07e9STrond Myklebust struct nfs_access_entry *cache; 244057b69181STrond Myklebust bool retry = true; 244157b69181STrond Myklebust int err; 24421da177e4SLinus Torvalds 24431c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 244457b69181STrond Myklebust for(;;) { 24451c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 24461c3c07e9STrond Myklebust goto out_zap; 24471c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 244857b69181STrond Myklebust err = -ENOENT; 24491c3c07e9STrond Myklebust if (cache == NULL) 24501c3c07e9STrond Myklebust goto out; 245157b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 245221c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 245357b69181STrond Myklebust break; 24545c965db8STrond Myklebust if (!retry) 24555c965db8STrond Myklebust break; 245657b69181STrond Myklebust err = -ECHILD; 245757b69181STrond Myklebust if (!may_block) 245857b69181STrond Myklebust goto out; 245957b69181STrond Myklebust spin_unlock(&inode->i_lock); 246057b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 246157b69181STrond Myklebust if (err) 246257b69181STrond Myklebust return err; 246357b69181STrond Myklebust spin_lock(&inode->i_lock); 246457b69181STrond Myklebust retry = false; 246557b69181STrond Myklebust } 24661c3c07e9STrond Myklebust res->cred = cache->cred; 24671c3c07e9STrond Myklebust res->mask = cache->mask; 2468cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 24691c3c07e9STrond Myklebust err = 0; 24701c3c07e9STrond Myklebust out: 24711c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24721c3c07e9STrond Myklebust return err; 24731c3c07e9STrond Myklebust out_zap: 24741a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 24751a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 24761c3c07e9STrond Myklebust return -ENOENT; 24771c3c07e9STrond Myklebust } 24781c3c07e9STrond Myklebust 2479b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2480f682a398SNeilBrown { 2481f682a398SNeilBrown /* Only check the most recently returned cache entry, 2482f682a398SNeilBrown * but do it without locking. 2483f682a398SNeilBrown */ 2484f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2485f682a398SNeilBrown struct nfs_access_entry *cache; 2486f682a398SNeilBrown int err = -ECHILD; 2487f682a398SNeilBrown struct list_head *lh; 2488f682a398SNeilBrown 2489f682a398SNeilBrown rcu_read_lock(); 2490f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2491f682a398SNeilBrown goto out; 2492f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2493f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2494f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 24959a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2496f682a398SNeilBrown cache = NULL; 2497f682a398SNeilBrown if (cache == NULL) 2498f682a398SNeilBrown goto out; 249921c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2500f682a398SNeilBrown goto out; 2501f682a398SNeilBrown res->cred = cache->cred; 2502f682a398SNeilBrown res->mask = cache->mask; 250321c3ba7eSTrond Myklebust err = 0; 2504f682a398SNeilBrown out: 2505f682a398SNeilBrown rcu_read_unlock(); 2506f682a398SNeilBrown return err; 2507f682a398SNeilBrown } 2508f682a398SNeilBrown 25091c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 25101c3c07e9STrond Myklebust { 2511cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2512cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 25131c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 25141c3c07e9STrond Myklebust struct rb_node *parent = NULL; 25151c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2516b68572e0SNeilBrown int cmp; 25171c3c07e9STrond Myklebust 25181c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 25191c3c07e9STrond Myklebust while (*p != NULL) { 25201c3c07e9STrond Myklebust parent = *p; 25211c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2522b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 25231c3c07e9STrond Myklebust 2524b68572e0SNeilBrown if (cmp < 0) 25251c3c07e9STrond Myklebust p = &parent->rb_left; 2526b68572e0SNeilBrown else if (cmp > 0) 25271c3c07e9STrond Myklebust p = &parent->rb_right; 25281c3c07e9STrond Myklebust else 25291c3c07e9STrond Myklebust goto found; 25301c3c07e9STrond Myklebust } 25311c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 25321c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2533cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 25341c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25351c3c07e9STrond Myklebust return; 25361c3c07e9STrond Myklebust found: 25371c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2538cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2539cfcea3e8STrond Myklebust list_del(&entry->lru); 25401c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 25411c3c07e9STrond Myklebust nfs_access_free_entry(entry); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25446168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 25451da177e4SLinus Torvalds { 25461c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 25471c3c07e9STrond Myklebust if (cache == NULL) 25481c3c07e9STrond Myklebust return; 25491c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2550b68572e0SNeilBrown cache->cred = get_cred(set->cred); 25511da177e4SLinus Torvalds cache->mask = set->mask; 25521c3c07e9STrond Myklebust 2553f682a398SNeilBrown /* The above field assignments must be visible 2554f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2555f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2556f682a398SNeilBrown */ 2557f682a398SNeilBrown smp_wmb(); 25581c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2559cfcea3e8STrond Myklebust 2560cfcea3e8STrond Myklebust /* Update accounting */ 25614e857c58SPeter Zijlstra smp_mb__before_atomic(); 2562cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 25634e857c58SPeter Zijlstra smp_mb__after_atomic(); 2564cfcea3e8STrond Myklebust 2565cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 25661a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2567cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 25681a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 25691a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 25701a81bb8aSTrond Myklebust &nfs_access_lru_list); 2571cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2572cfcea3e8STrond Myklebust } 25733a505845STrond Myklebust nfs_access_cache_enforce_limit(); 25741da177e4SLinus Torvalds } 25756168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 25766168f62cSWeston Andros Adamson 25773c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 25783c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 25793c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 25803c181827SAnna Schumaker NFS_ACCESS_DELETE) 25813c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 25823c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2583ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 25843c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 25853c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 258615d4b73aSTrond Myklebust static int 2587ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 258815d4b73aSTrond Myklebust { 258915d4b73aSTrond Myklebust int mask = 0; 259015d4b73aSTrond Myklebust 259115d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 259215d4b73aSTrond Myklebust mask |= MAY_READ; 2593ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2594ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 259515d4b73aSTrond Myklebust mask |= MAY_WRITE; 2596ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 259715d4b73aSTrond Myklebust mask |= MAY_EXEC; 2598ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2599ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2600ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2601ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 260215d4b73aSTrond Myklebust mask |= MAY_EXEC; 2603ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2604ecbb903cSTrond Myklebust mask |= MAY_WRITE; 260515d4b73aSTrond Myklebust return mask; 260615d4b73aSTrond Myklebust } 260715d4b73aSTrond Myklebust 26086168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 26096168f62cSWeston Andros Adamson { 2610bd8b2441STrond Myklebust entry->mask = access_result; 26116168f62cSWeston Andros Adamson } 26126168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 26131da177e4SLinus Torvalds 2614b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 26151da177e4SLinus Torvalds { 26161da177e4SLinus Torvalds struct nfs_access_entry cache; 261757b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2618e8194b7dSTrond Myklebust int cache_mask = -1; 26191da177e4SLinus Torvalds int status; 26201da177e4SLinus Torvalds 2621f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2622f4ce1299STrond Myklebust 2623f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2624f682a398SNeilBrown if (status != 0) 262557b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 26261da177e4SLinus Torvalds if (status == 0) 2627f4ce1299STrond Myklebust goto out_cached; 26281da177e4SLinus Torvalds 2629f3324a2aSNeilBrown status = -ECHILD; 263057b69181STrond Myklebust if (!may_block) 2631f3324a2aSNeilBrown goto out; 2632f3324a2aSNeilBrown 26331750d929SAnna Schumaker /* 26341750d929SAnna Schumaker * Determine which access bits we want to ask for... 26351750d929SAnna Schumaker */ 26361750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 26371750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 26381750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 26391750d929SAnna Schumaker else 26401750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 26411da177e4SLinus Torvalds cache.cred = cred; 26421da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2643a71ee337SSuresh Jayaraman if (status != 0) { 2644a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2645a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2646a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2647a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2648a71ee337SSuresh Jayaraman } 2649f4ce1299STrond Myklebust goto out; 2650a71ee337SSuresh Jayaraman } 26511da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2652f4ce1299STrond Myklebust out_cached: 2653ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2654bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2655f4ce1299STrond Myklebust status = -EACCES; 26561da177e4SLinus Torvalds out: 2657e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2658f4ce1299STrond Myklebust return status; 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds 2661af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2662af22f94aSTrond Myklebust { 2663af22f94aSTrond Myklebust int mask = 0; 2664af22f94aSTrond Myklebust 2665f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2666f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2667f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2668f8d9a897SWeston Andros Adamson } else { 26698a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2670af22f94aSTrond Myklebust mask |= MAY_READ; 26718a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2672af22f94aSTrond Myklebust mask |= MAY_WRITE; 2673f8d9a897SWeston Andros Adamson } 2674f8d9a897SWeston Andros Adamson 2675af22f94aSTrond Myklebust return mask; 2676af22f94aSTrond Myklebust } 2677af22f94aSTrond Myklebust 2678b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2679af22f94aSTrond Myklebust { 2680af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2681af22f94aSTrond Myklebust } 268289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2683af22f94aSTrond Myklebust 26845c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 26855c5fc09aSTrond Myklebust { 26865c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 268721c3ba7eSTrond Myklebust int ret = 0; 26885c5fc09aSTrond Myklebust 26893825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 26903825827eSTrond Myklebust return 0; 2691cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 26925c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 269321c3ba7eSTrond Myklebust return -ECHILD; 269421c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 269521c3ba7eSTrond Myklebust } 26965c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 26975c5fc09aSTrond Myklebust ret = -EACCES; 26985c5fc09aSTrond Myklebust return ret; 26995c5fc09aSTrond Myklebust } 27005c5fc09aSTrond Myklebust 270110556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 27021da177e4SLinus Torvalds { 2703b68572e0SNeilBrown const struct cred *cred = current_cred(); 27041da177e4SLinus Torvalds int res = 0; 27051da177e4SLinus Torvalds 270691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 270791d5b470SChuck Lever 2708e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 27091da177e4SLinus Torvalds goto out; 27101da177e4SLinus Torvalds /* Is this sys_access() ? */ 27119cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 27121da177e4SLinus Torvalds goto force_lookup; 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 27151da177e4SLinus Torvalds case S_IFLNK: 27161da177e4SLinus Torvalds goto out; 27171da177e4SLinus Torvalds case S_IFREG: 2718762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2719762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2720762674f8STrond Myklebust return 0; 27211da177e4SLinus Torvalds break; 27221da177e4SLinus Torvalds case S_IFDIR: 27231da177e4SLinus Torvalds /* 27241da177e4SLinus Torvalds * Optimize away all write operations, since the server 27251da177e4SLinus Torvalds * will check permissions when we perform the op. 27261da177e4SLinus Torvalds */ 27271da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 27281da177e4SLinus Torvalds goto out; 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds force_lookup: 27321da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 27331da177e4SLinus Torvalds goto out_notsup; 27341da177e4SLinus Torvalds 2735f3324a2aSNeilBrown /* Always try fast lookups first */ 2736f3324a2aSNeilBrown rcu_read_lock(); 2737f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2738f3324a2aSNeilBrown rcu_read_unlock(); 2739f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2740f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 27411da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2742f3324a2aSNeilBrown } 27431da177e4SLinus Torvalds out: 27445c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 27455c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2746f696a365SMiklos Szeredi 27471e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 27481e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 27491da177e4SLinus Torvalds return res; 27501da177e4SLinus Torvalds out_notsup: 2751d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2752d51ac1a8SNeilBrown return -ECHILD; 2753d51ac1a8SNeilBrown 27541da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 27551da177e4SLinus Torvalds if (res == 0) 27562830ba7fSAl Viro res = generic_permission(inode, mask); 27571e7cb3dcSChuck Lever goto out; 27581da177e4SLinus Torvalds } 2759ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds /* 27621da177e4SLinus Torvalds * Local variables: 27631da177e4SLinus Torvalds * version-control: t 27641da177e4SLinus Torvalds * kept-new-versions: 5 27651da177e4SLinus Torvalds * End: 27661da177e4SLinus Torvalds */ 2767