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 1581f4eab7eSNeil Brown unsigned long timestamp; 1594704f0e2STrond Myklebust unsigned long gencount; 160d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 161a7a3b1e9SBenjamin Coddington bool plus; 162a7a3b1e9SBenjamin Coddington bool eof; 1631da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1641da177e4SLinus Torvalds 1654b310319STrond Myklebust static 1664b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 1674b310319STrond Myklebust { 1684b310319STrond Myklebust struct nfs_cache_array *array; 1694b310319STrond Myklebust 1704b310319STrond Myklebust array = kmap_atomic(page); 1714b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1724b310319STrond Myklebust array->eof_index = -1; 1734b310319STrond Myklebust kunmap_atomic(array); 1744b310319STrond Myklebust } 1754b310319STrond Myklebust 176d1bacf9eSBryan Schumaker /* 177d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 178d1bacf9eSBryan Schumaker */ 179d1bacf9eSBryan Schumaker static 18011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 181d1bacf9eSBryan Schumaker { 18211de3b11STrond Myklebust struct nfs_cache_array *array; 183d1bacf9eSBryan Schumaker int i; 1848cd51a0cSTrond Myklebust 1852b86ce2dSCong Wang array = kmap_atomic(page); 186d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 187d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1884b310319STrond Myklebust array->size = 0; 1892b86ce2dSCong Wang kunmap_atomic(array); 190d1bacf9eSBryan Schumaker } 191d1bacf9eSBryan Schumaker 192d1bacf9eSBryan Schumaker /* 193d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 194d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 195d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 196d1bacf9eSBryan Schumaker */ 197d1bacf9eSBryan Schumaker static 1984a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 199d1bacf9eSBryan Schumaker { 200d1bacf9eSBryan Schumaker string->len = len; 2013803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2024a201d6eSTrond Myklebust if (string->name == NULL) 2034a201d6eSTrond Myklebust return -ENOMEM; 20404e4bd1cSCatalin Marinas /* 20504e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 20604e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 20704e4bd1cSCatalin Marinas */ 20804e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2098387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2104a201d6eSTrond Myklebust return 0; 211d1bacf9eSBryan Schumaker } 212d1bacf9eSBryan Schumaker 213d1bacf9eSBryan Schumaker static 214d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 215d1bacf9eSBryan Schumaker { 2160795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2174a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2184a201d6eSTrond Myklebust int ret; 2194a201d6eSTrond Myklebust 2204a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2213020093fSTrond Myklebust 2223020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2233020093fSTrond Myklebust ret = -ENOSPC; 2243020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2253020093fSTrond Myklebust goto out; 2263020093fSTrond Myklebust 2274a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2284a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2290b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2304a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2314a201d6eSTrond Myklebust if (ret) 2324a201d6eSTrond Myklebust goto out; 233d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2348cd51a0cSTrond Myklebust array->size++; 23547c716cbSTrond Myklebust if (entry->eof != 0) 236d1bacf9eSBryan Schumaker array->eof_index = array->size; 2374a201d6eSTrond Myklebust out: 2380795bf83SFabian Frederick kunmap(page); 2394a201d6eSTrond Myklebust return ret; 240d1bacf9eSBryan Schumaker } 241d1bacf9eSBryan Schumaker 242d1bacf9eSBryan Schumaker static 243d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 244d1bacf9eSBryan Schumaker { 24523db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 246d1bacf9eSBryan Schumaker unsigned int index; 247d1bacf9eSBryan Schumaker 248d1bacf9eSBryan Schumaker if (diff < 0) 249d1bacf9eSBryan Schumaker goto out_eof; 250d1bacf9eSBryan Schumaker if (diff >= array->size) { 2518cd51a0cSTrond Myklebust if (array->eof_index >= 0) 252d1bacf9eSBryan Schumaker goto out_eof; 253d1bacf9eSBryan Schumaker return -EAGAIN; 254d1bacf9eSBryan Schumaker } 255d1bacf9eSBryan Schumaker 256d1bacf9eSBryan Schumaker index = (unsigned int)diff; 257d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 258d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 259d1bacf9eSBryan Schumaker return 0; 260d1bacf9eSBryan Schumaker out_eof: 2616089dd0dSThomas Meyer desc->eof = true; 262d1bacf9eSBryan Schumaker return -EBADCOOKIE; 263d1bacf9eSBryan Schumaker } 264d1bacf9eSBryan Schumaker 2654db72b40SJeff Layton static bool 2664db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2674db72b40SJeff Layton { 2684db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2694db72b40SJeff Layton return false; 2704db72b40SJeff Layton smp_rmb(); 2714db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2724db72b40SJeff Layton } 2734db72b40SJeff Layton 274d1bacf9eSBryan Schumaker static 275d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 276d1bacf9eSBryan Schumaker { 277d1bacf9eSBryan Schumaker int i; 2788ef2ce3eSBryan Schumaker loff_t new_pos; 279d1bacf9eSBryan Schumaker int status = -EAGAIN; 280d1bacf9eSBryan Schumaker 281d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2828cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 283496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2840c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2850c030806STrond Myklebust 2868ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2874db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 2884db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 2890c030806STrond Myklebust ctx->duped = 0; 2900c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 29123db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2920c030806STrond Myklebust if (ctx->duped > 0 2930c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2940c030806STrond Myklebust if (printk_ratelimit()) { 2956de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 2960c030806STrond Myklebust "Please contact your server vendor. " 2979581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 2989581a4aeSJeff Layton desc->file, array->array[i].string.len, 2999581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3000c030806STrond Myklebust } 3010c030806STrond Myklebust status = -ELOOP; 3020c030806STrond Myklebust goto out; 3030c030806STrond Myklebust } 3048ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3050c030806STrond Myklebust ctx->duped = -1; 3068ef2ce3eSBryan Schumaker } 30723db8620SAl Viro desc->ctx->pos = new_pos; 3088cd51a0cSTrond Myklebust desc->cache_entry_index = i; 30947c716cbSTrond Myklebust return 0; 3108cd51a0cSTrond Myklebust } 3118cd51a0cSTrond Myklebust } 31247c716cbSTrond Myklebust if (array->eof_index >= 0) { 313d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 31418fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 3156089dd0dSThomas Meyer desc->eof = true; 316d1bacf9eSBryan Schumaker } 3170c030806STrond Myklebust out: 318d1bacf9eSBryan Schumaker return status; 319d1bacf9eSBryan Schumaker } 320d1bacf9eSBryan Schumaker 321d1bacf9eSBryan Schumaker static 322d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 323d1bacf9eSBryan Schumaker { 324d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 32547c716cbSTrond Myklebust int status; 326d1bacf9eSBryan Schumaker 3270795bf83SFabian Frederick array = kmap(desc->page); 328d1bacf9eSBryan Schumaker 329d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 330d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 331d1bacf9eSBryan Schumaker else 332d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 333d1bacf9eSBryan Schumaker 33447c716cbSTrond Myklebust if (status == -EAGAIN) { 3350aded708STrond Myklebust desc->last_cookie = array->last_cookie; 336e47c085aSTrond Myklebust desc->current_index += array->size; 33747c716cbSTrond Myklebust desc->page_index++; 33847c716cbSTrond Myklebust } 3390795bf83SFabian Frederick kunmap(desc->page); 340d1bacf9eSBryan Schumaker return status; 341d1bacf9eSBryan Schumaker } 342d1bacf9eSBryan Schumaker 343d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 344d1bacf9eSBryan Schumaker static 34556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 346d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 347d1bacf9eSBryan Schumaker { 348480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 349684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3504704f0e2STrond Myklebust unsigned long timestamp, gencount; 3511da177e4SLinus Torvalds int error; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds again: 3541da177e4SLinus Torvalds timestamp = jiffies; 3554704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 356be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3571da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3581da177e4SLinus Torvalds if (error < 0) { 3591da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3601da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3611da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3623a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 363a7a3b1e9SBenjamin Coddington desc->plus = false; 3641da177e4SLinus Torvalds goto again; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds goto error; 3671da177e4SLinus Torvalds } 3681f4eab7eSNeil Brown desc->timestamp = timestamp; 3694704f0e2STrond Myklebust desc->gencount = gencount; 370d1bacf9eSBryan Schumaker error: 371d1bacf9eSBryan Schumaker return error; 372d1bacf9eSBryan Schumaker } 373d1bacf9eSBryan Schumaker 374573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 375573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 376d1bacf9eSBryan Schumaker { 377573c4e1eSChuck Lever int error; 378d1bacf9eSBryan Schumaker 379573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 380573c4e1eSChuck Lever if (error) 381573c4e1eSChuck Lever return error; 382d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 383d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 384d1bacf9eSBryan Schumaker return 0; 385d1bacf9eSBryan Schumaker } 386d1bacf9eSBryan Schumaker 387fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 388fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 389fa923369STrond Myklebust */ 390d39ab9deSBryan Schumaker static 391d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 392d39ab9deSBryan Schumaker { 393d8fdb47fSTrond Myklebust struct inode *inode; 394fa923369STrond Myklebust struct nfs_inode *nfsi; 395fa923369STrond Myklebust 3962b0143b5SDavid Howells if (d_really_is_negative(dentry)) 3972b0143b5SDavid Howells return 0; 398fa923369STrond Myklebust 399d8fdb47fSTrond Myklebust inode = d_inode(dentry); 400d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 401d8fdb47fSTrond Myklebust return 0; 402d8fdb47fSTrond Myklebust 403d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4047dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 405d39ab9deSBryan Schumaker return 0; 4067dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4077dc72d5fSTrond Myklebust return 0; 4087dc72d5fSTrond Myklebust return 1; 409d39ab9deSBryan Schumaker } 410d39ab9deSBryan Schumaker 411d39ab9deSBryan Schumaker static 41223db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 413d69ee9b8STrond Myklebust { 414d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 415d69ee9b8STrond Myklebust return false; 416d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 417d69ee9b8STrond Myklebust return true; 41823db8620SAl Viro if (ctx->pos == 0) 419d69ee9b8STrond Myklebust return true; 420d69ee9b8STrond Myklebust return false; 421d69ee9b8STrond Myklebust } 422d69ee9b8STrond Myklebust 423d69ee9b8STrond Myklebust /* 42463519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 42563519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 426d69ee9b8STrond Myklebust * directory. 427d69ee9b8STrond Myklebust */ 428d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 429d69ee9b8STrond Myklebust { 43063519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 43163519fbcSTrond Myklebust 43263519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 43363519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 43463519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 435d69ee9b8STrond Myklebust } 436d69ee9b8STrond Myklebust 437311324adSTrond Myklebust /* 438311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 439311324adSTrond Myklebust * 440311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 441311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 442311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 443311324adSTrond Myklebust * cache flush. 444311324adSTrond Myklebust */ 445311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 446311324adSTrond Myklebust { 44763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 44863519fbcSTrond Myklebust 44963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 45063519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 45163519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 452*227823d2SDai Ngo invalidate_mapping_pages(dir->i_mapping, 453*227823d2SDai Ngo nfsi->page_index + 1, -1); 454311324adSTrond Myklebust } 455311324adSTrond Myklebust } 456311324adSTrond Myklebust 457d69ee9b8STrond Myklebust static 458d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 459d39ab9deSBryan Schumaker { 46026fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4619ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4624a201d6eSTrond Myklebust struct dentry *dentry; 4634a201d6eSTrond Myklebust struct dentry *alias; 4642b0143b5SDavid Howells struct inode *dir = d_inode(parent); 465d39ab9deSBryan Schumaker struct inode *inode; 466aa9c2669SDavid Quigley int status; 467d39ab9deSBryan Schumaker 468fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 469fa923369STrond Myklebust return; 4706c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4716c441c25STrond Myklebust return; 47278d04af4STrond Myklebust if (filename.len == 0) 47378d04af4STrond Myklebust return; 47478d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 47578d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 47678d04af4STrond Myklebust return; 47778d04af4STrond Myklebust /* ...or '/' */ 47878d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 47978d04af4STrond Myklebust return; 4804a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4814a201d6eSTrond Myklebust if (filename.len == 1) 4824a201d6eSTrond Myklebust return; 4834a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4844a201d6eSTrond Myklebust return; 4854a201d6eSTrond Myklebust } 4868387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 487d39ab9deSBryan Schumaker 4884a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 4899ac3d3e8SAl Viro again: 4909ac3d3e8SAl Viro if (!dentry) { 4919ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 4929ac3d3e8SAl Viro if (IS_ERR(dentry)) 4939ac3d3e8SAl Viro return; 4949ac3d3e8SAl Viro } 4959ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 4966c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4976c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 4986c441c25STrond Myklebust &entry->fattr->fsid)) 4996c441c25STrond Myklebust goto out; 500d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5017dc72d5fSTrond Myklebust if (!entry->fh->size) 5027dc72d5fSTrond Myklebust goto out; 503cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 5042b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 505aa9c2669SDavid Quigley if (!status) 5062b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 507d39ab9deSBryan Schumaker goto out; 508d39ab9deSBryan Schumaker } else { 5095542aa2fSEric W. Biederman d_invalidate(dentry); 510d39ab9deSBryan Schumaker dput(dentry); 5119ac3d3e8SAl Viro dentry = NULL; 5129ac3d3e8SAl Viro goto again; 513d39ab9deSBryan Schumaker } 514d39ab9deSBryan Schumaker } 5157dc72d5fSTrond Myklebust if (!entry->fh->size) { 5167dc72d5fSTrond Myklebust d_lookup_done(dentry); 5177dc72d5fSTrond Myklebust goto out; 5187dc72d5fSTrond Myklebust } 519d39ab9deSBryan Schumaker 5201775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 52141d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5229ac3d3e8SAl Viro d_lookup_done(dentry); 5239ac3d3e8SAl Viro if (alias) { 524d39ab9deSBryan Schumaker if (IS_ERR(alias)) 525d39ab9deSBryan Schumaker goto out; 5269ac3d3e8SAl Viro dput(dentry); 5279ac3d3e8SAl Viro dentry = alias; 5289ac3d3e8SAl Viro } 529d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 530d39ab9deSBryan Schumaker out: 531d39ab9deSBryan Schumaker dput(dentry); 532d39ab9deSBryan Schumaker } 533d39ab9deSBryan Schumaker 534d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 535d1bacf9eSBryan Schumaker static 5368cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5376650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 538d1bacf9eSBryan Schumaker { 539babddc72SBryan Schumaker struct xdr_stream stream; 540f7da7a12SBenny Halevy struct xdr_buf buf; 5416650239aSTrond Myklebust struct page *scratch; 54299424380SBryan Schumaker struct nfs_cache_array *array; 5435c346854STrond Myklebust unsigned int count = 0; 5445c346854STrond Myklebust int status; 545babddc72SBryan Schumaker 5466650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5476650239aSTrond Myklebust if (scratch == NULL) 5486650239aSTrond Myklebust return -ENOMEM; 549babddc72SBryan Schumaker 550ce85cfbeSBenjamin Coddington if (buflen == 0) 551ce85cfbeSBenjamin Coddington goto out_nopages; 552ce85cfbeSBenjamin Coddington 553f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5546650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 55599424380SBryan Schumaker 55699424380SBryan Schumaker do { 55799424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5588cd51a0cSTrond Myklebust if (status != 0) { 5598cd51a0cSTrond Myklebust if (status == -EAGAIN) 5608cd51a0cSTrond Myklebust status = 0; 56199424380SBryan Schumaker break; 5628cd51a0cSTrond Myklebust } 56399424380SBryan Schumaker 5645c346854STrond Myklebust count++; 5655c346854STrond Myklebust 566a7a3b1e9SBenjamin Coddington if (desc->plus) 567be62a1a8SMiklos Szeredi nfs_prime_dcache(file_dentry(desc->file), entry); 5688cd51a0cSTrond Myklebust 569db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 5708cd51a0cSTrond Myklebust if (status != 0) 5718cd51a0cSTrond Myklebust break; 57299424380SBryan Schumaker } while (!entry->eof); 57399424380SBryan Schumaker 574ce85cfbeSBenjamin Coddington out_nopages: 57547c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 576db531db9SMax Kellermann array = kmap(page); 5778cd51a0cSTrond Myklebust array->eof_index = array->size; 57899424380SBryan Schumaker status = 0; 579db531db9SMax Kellermann kunmap(page); 58056e4ebf8SBryan Schumaker } 5816650239aSTrond Myklebust 5826650239aSTrond Myklebust put_page(scratch); 5838cd51a0cSTrond Myklebust return status; 5848cd51a0cSTrond Myklebust } 58556e4ebf8SBryan Schumaker 58656e4ebf8SBryan Schumaker static 587c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 58856e4ebf8SBryan Schumaker { 58956e4ebf8SBryan Schumaker unsigned int i; 59056e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 59156e4ebf8SBryan Schumaker put_page(pages[i]); 59256e4ebf8SBryan Schumaker } 59356e4ebf8SBryan Schumaker 59456e4ebf8SBryan Schumaker /* 595bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 596bf211ca1Szhangliguang * to nfs_readdir_free_pages() 59756e4ebf8SBryan Schumaker */ 59856e4ebf8SBryan Schumaker static 599c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 60056e4ebf8SBryan Schumaker { 60156e4ebf8SBryan Schumaker unsigned int i; 60256e4ebf8SBryan Schumaker 60356e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 60456e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 60556e4ebf8SBryan Schumaker if (page == NULL) 60656e4ebf8SBryan Schumaker goto out_freepages; 60756e4ebf8SBryan Schumaker pages[i] = page; 60856e4ebf8SBryan Schumaker } 6096650239aSTrond Myklebust return 0; 61056e4ebf8SBryan Schumaker 61156e4ebf8SBryan Schumaker out_freepages: 612c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6136650239aSTrond Myklebust return -ENOMEM; 614d1bacf9eSBryan Schumaker } 615d1bacf9eSBryan Schumaker 616d1bacf9eSBryan Schumaker static 617d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 618d1bacf9eSBryan Schumaker { 61956e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 620d1bacf9eSBryan Schumaker struct nfs_entry entry; 621d1bacf9eSBryan Schumaker struct file *file = desc->file; 622d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6238cd51a0cSTrond Myklebust int status = -ENOMEM; 62456e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 625d1bacf9eSBryan Schumaker 6264b310319STrond Myklebust nfs_readdir_init_array(page); 6274b310319STrond Myklebust 628d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6290aded708STrond Myklebust entry.cookie = desc->last_cookie; 630d1bacf9eSBryan Schumaker entry.eof = 0; 631d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 632d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 633573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 634d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 635d1bacf9eSBryan Schumaker goto out; 636d1bacf9eSBryan Schumaker 63714c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 63814c43f76SDavid Quigley if (IS_ERR(entry.label)) { 63914c43f76SDavid Quigley status = PTR_ERR(entry.label); 64014c43f76SDavid Quigley goto out; 64114c43f76SDavid Quigley } 64214c43f76SDavid Quigley 6430795bf83SFabian Frederick array = kmap(page); 644d1bacf9eSBryan Schumaker 645c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6466650239aSTrond Myklebust if (status < 0) 647d1bacf9eSBryan Schumaker goto out_release_array; 648d1bacf9eSBryan Schumaker do { 649ac396128STrond Myklebust unsigned int pglen; 65056e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 651babddc72SBryan Schumaker 652d1bacf9eSBryan Schumaker if (status < 0) 653d1bacf9eSBryan Schumaker break; 654ac396128STrond Myklebust pglen = status; 6556650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6568cd51a0cSTrond Myklebust if (status < 0) { 6578cd51a0cSTrond Myklebust if (status == -ENOSPC) 6588cd51a0cSTrond Myklebust status = 0; 6598cd51a0cSTrond Myklebust break; 6608cd51a0cSTrond Myklebust } 6618cd51a0cSTrond Myklebust } while (array->eof_index < 0); 662d1bacf9eSBryan Schumaker 663c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 664d1bacf9eSBryan Schumaker out_release_array: 6650795bf83SFabian Frederick kunmap(page); 66614c43f76SDavid Quigley nfs4_label_free(entry.label); 667d1bacf9eSBryan Schumaker out: 668d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 669d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 670d1bacf9eSBryan Schumaker return status; 671d1bacf9eSBryan Schumaker } 672d1bacf9eSBryan Schumaker 673d1bacf9eSBryan Schumaker /* 674d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 675d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 676d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 677d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6781da177e4SLinus Torvalds */ 679d1bacf9eSBryan Schumaker static 680a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 681d1bacf9eSBryan Schumaker { 682a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 683496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6848cd51a0cSTrond Myklebust int ret; 685d1bacf9eSBryan Schumaker 6868cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6878cd51a0cSTrond Myklebust if (ret < 0) 688d1bacf9eSBryan Schumaker goto error; 689d1bacf9eSBryan Schumaker SetPageUptodate(page); 690d1bacf9eSBryan Schumaker 6912aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 692cd9ae2b6STrond Myklebust /* Should never happen */ 693cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 694cd9ae2b6STrond Myklebust } 6951da177e4SLinus Torvalds unlock_page(page); 6961da177e4SLinus Torvalds return 0; 6971da177e4SLinus Torvalds error: 6984b310319STrond Myklebust nfs_readdir_clear_array(page); 6991da177e4SLinus Torvalds unlock_page(page); 7008cd51a0cSTrond Myklebust return ret; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703d1bacf9eSBryan Schumaker static 704d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7051da177e4SLinus Torvalds { 70609cbfeafSKirill A. Shutemov put_page(desc->page); 7071da177e4SLinus Torvalds desc->page = NULL; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710d1bacf9eSBryan Schumaker static 711d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7121da177e4SLinus Torvalds { 713a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 714a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* 718d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 719114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7201da177e4SLinus Torvalds */ 721d1bacf9eSBryan Schumaker static 722114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 723d1bacf9eSBryan Schumaker { 724*227823d2SDai Ngo struct inode *inode = file_inode(desc->file); 725*227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode); 726d1bacf9eSBryan Schumaker int res; 727d1bacf9eSBryan Schumaker 728d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 729d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 730d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 731114de382STrond Myklebust res = lock_page_killable(desc->page); 73247c716cbSTrond Myklebust if (res != 0) 733114de382STrond Myklebust goto error; 734114de382STrond Myklebust res = -EAGAIN; 735114de382STrond Myklebust if (desc->page->mapping != NULL) { 736114de382STrond Myklebust res = nfs_readdir_search_array(desc); 737*227823d2SDai Ngo if (res == 0) { 738*227823d2SDai Ngo nfsi->page_index = desc->page_index; 739114de382STrond Myklebust return 0; 740114de382STrond Myklebust } 741*227823d2SDai Ngo } 742114de382STrond Myklebust unlock_page(desc->page); 743114de382STrond Myklebust error: 744d1bacf9eSBryan Schumaker cache_page_release(desc); 745d1bacf9eSBryan Schumaker return res; 746d1bacf9eSBryan Schumaker } 747d1bacf9eSBryan Schumaker 748d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7491da177e4SLinus Torvalds static inline 7501da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7511da177e4SLinus Torvalds { 7528cd51a0cSTrond Myklebust int res; 753d1bacf9eSBryan Schumaker 7540aded708STrond Myklebust if (desc->page_index == 0) { 7558cd51a0cSTrond Myklebust desc->current_index = 0; 7560aded708STrond Myklebust desc->last_cookie = 0; 7570aded708STrond Myklebust } 75847c716cbSTrond Myklebust do { 759114de382STrond Myklebust res = find_and_lock_cache_page(desc); 76047c716cbSTrond Myklebust } while (res == -EAGAIN); 7611da177e4SLinus Torvalds return res; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* 7651da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7661da177e4SLinus Torvalds */ 7671da177e4SLinus Torvalds static 76823db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7691da177e4SLinus Torvalds { 7701da177e4SLinus Torvalds struct file *file = desc->file; 771d1bacf9eSBryan Schumaker int i = 0; 772d1bacf9eSBryan Schumaker int res = 0; 773d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7748ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7758ef2ce3eSBryan Schumaker 7760795bf83SFabian Frederick array = kmap(desc->page); 777d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 778ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7791da177e4SLinus Torvalds 780ece0b423STrond Myklebust ent = &array->array[i]; 78123db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 78223db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 7836089dd0dSThomas Meyer desc->eof = true; 7841da177e4SLinus Torvalds break; 785ece0b423STrond Myklebust } 78623db8620SAl Viro desc->ctx->pos++; 787d1bacf9eSBryan Schumaker if (i < (array->size-1)) 788d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 789d1bacf9eSBryan Schumaker else 790d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7910c030806STrond Myklebust if (ctx->duped != 0) 7920c030806STrond Myklebust ctx->duped = 1; 7938cd51a0cSTrond Myklebust } 79447c716cbSTrond Myklebust if (array->eof_index >= 0) 7956089dd0dSThomas Meyer desc->eof = true; 796d1bacf9eSBryan Schumaker 7970795bf83SFabian Frederick kunmap(desc->page); 7981e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7991e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8001da177e4SLinus Torvalds return res; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8051da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8061da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8071da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8081da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8091da177e4SLinus Torvalds * 8101da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8111da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8121da177e4SLinus Torvalds * we should already have a complete representation of the 8131da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds static inline 81623db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8171da177e4SLinus Torvalds { 8181da177e4SLinus Torvalds struct page *page = NULL; 8191da177e4SLinus Torvalds int status; 820496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8210c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8221da177e4SLinus Torvalds 8231e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8241e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8271da177e4SLinus Torvalds if (!page) { 8281da177e4SLinus Torvalds status = -ENOMEM; 8291da177e4SLinus Torvalds goto out; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8327a8e1dc3STrond Myklebust desc->page_index = 0; 8330aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8347a8e1dc3STrond Myklebust desc->page = page; 8350c030806STrond Myklebust ctx->duped = 0; 8367a8e1dc3STrond Myklebust 83785f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 83885f8607eSTrond Myklebust if (status < 0) 839d1bacf9eSBryan Schumaker goto out_release; 840d1bacf9eSBryan Schumaker 84123db8620SAl Viro status = nfs_do_filldir(desc); 8421da177e4SLinus Torvalds 843114de382STrond Myklebust out_release: 844114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 845114de382STrond Myklebust cache_page_release(desc); 8461da177e4SLinus Torvalds out: 8471e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8483110ff80SHarvey Harrison __func__, status); 8491da177e4SLinus Torvalds return status; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 85200a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 85300a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 85400a92642SOlivier Galibert whole directory. 8551da177e4SLinus Torvalds */ 85623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8571da177e4SLinus Torvalds { 858be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8592b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8601da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8611da177e4SLinus Torvalds *desc = &my_desc; 86223db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 86307b5ce8eSScott Mayhew int res = 0; 8641da177e4SLinus Torvalds 8656de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8666de1472fSAl Viro file, (long long)ctx->pos); 86791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 86891d5b470SChuck Lever 8691da177e4SLinus Torvalds /* 87023db8620SAl Viro * ctx->pos points to the dirent entry number. 871f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 87200a92642SOlivier Galibert * to either find the entry with the appropriate number or 87300a92642SOlivier Galibert * revalidate the cookie. 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8761da177e4SLinus Torvalds 87723db8620SAl Viro desc->file = file; 87823db8620SAl Viro desc->ctx = ctx; 879480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8801da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 881a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 8821da177e4SLinus Torvalds 88379f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 88423db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 885fccca7fcSTrond Myklebust if (res < 0) 886fccca7fcSTrond Myklebust goto out; 887fccca7fcSTrond Myklebust 88847c716cbSTrond Myklebust do { 8891da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 89000a92642SOlivier Galibert 8911da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 892ece0b423STrond Myklebust res = 0; 8931da177e4SLinus Torvalds /* This means either end of directory */ 8946089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 8951da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 89623db8620SAl Viro res = uncached_readdir(desc); 897ece0b423STrond Myklebust if (res == 0) 8981da177e4SLinus Torvalds continue; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds break; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9033a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9041da177e4SLinus Torvalds nfs_zap_caches(inode); 905baf57a09STrond Myklebust desc->page_index = 0; 906a7a3b1e9SBenjamin Coddington desc->plus = false; 907a7a3b1e9SBenjamin Coddington desc->eof = false; 9081da177e4SLinus Torvalds continue; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds if (res < 0) 9111da177e4SLinus Torvalds break; 9121da177e4SLinus Torvalds 91323db8620SAl Viro res = nfs_do_filldir(desc); 914114de382STrond Myklebust unlock_page(desc->page); 915114de382STrond Myklebust cache_page_release(desc); 916ece0b423STrond Myklebust if (res < 0) 9171da177e4SLinus Torvalds break; 91847c716cbSTrond Myklebust } while (!desc->eof); 919fccca7fcSTrond Myklebust out: 9201e7cb3dcSChuck Lever if (res > 0) 9211e7cb3dcSChuck Lever res = 0; 9226de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9231da177e4SLinus Torvalds return res; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 926965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 927f0dd2136STrond Myklebust { 928b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 929480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 930b84e06c5SChuck Lever 9316de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9326de1472fSAl Viro filp, offset, whence); 933b84e06c5SChuck Lever 934965c8e59SAndrew Morton switch (whence) { 935f0dd2136STrond Myklebust default: 936b2b1ff3dSTrond Myklebust return -EINVAL; 937b2b1ff3dSTrond Myklebust case SEEK_SET: 938b2b1ff3dSTrond Myklebust if (offset < 0) 939b2b1ff3dSTrond Myklebust return -EINVAL; 940b2b1ff3dSTrond Myklebust inode_lock(inode); 941b2b1ff3dSTrond Myklebust break; 942b2b1ff3dSTrond Myklebust case SEEK_CUR: 943b2b1ff3dSTrond Myklebust if (offset == 0) 944b2b1ff3dSTrond Myklebust return filp->f_pos; 945b2b1ff3dSTrond Myklebust inode_lock(inode); 946b2b1ff3dSTrond Myklebust offset += filp->f_pos; 947b2b1ff3dSTrond Myklebust if (offset < 0) { 948b2b1ff3dSTrond Myklebust inode_unlock(inode); 949b2b1ff3dSTrond Myklebust return -EINVAL; 950b2b1ff3dSTrond Myklebust } 951f0dd2136STrond Myklebust } 952f0dd2136STrond Myklebust if (offset != filp->f_pos) { 953f0dd2136STrond Myklebust filp->f_pos = offset; 954480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9558ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 956f0dd2136STrond Myklebust } 957b044f645SBenjamin Coddington inode_unlock(inode); 958f0dd2136STrond Myklebust return offset; 959f0dd2136STrond Myklebust } 960f0dd2136STrond Myklebust 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9631da177e4SLinus Torvalds * is a dummy operation. 9641da177e4SLinus Torvalds */ 96502c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 96602c24a82SJosef Bacik int datasync) 9671da177e4SLinus Torvalds { 9686de1472fSAl Viro struct inode *inode = file_inode(filp); 9697ea80859SChristoph Hellwig 9706de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9711e7cb3dcSChuck Lever 9725955102cSAl Viro inode_lock(inode); 9736de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9745955102cSAl Viro inode_unlock(inode); 9751da177e4SLinus Torvalds return 0; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 978bfc69a45STrond Myklebust /** 979bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 980302fad7bSTrond Myklebust * @dir: pointer to directory inode 981bfc69a45STrond Myklebust * 982bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 983bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 984bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 985bfc69a45STrond Myklebust * 986bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 987bfc69a45STrond Myklebust */ 988bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 989bfc69a45STrond Myklebust { 990011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 991bfc69a45STrond Myklebust } 99289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 993bfc69a45STrond Myklebust 9941da177e4SLinus Torvalds /* 9951da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9961da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9971da177e4SLinus Torvalds * and may need to be looked up again. 998912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 9991da177e4SLinus Torvalds */ 1000912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1001912a108dSNeilBrown int rcu_walk) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10041da177e4SLinus Torvalds return 1; 10054eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10064eec952eSTrond Myklebust return 0; 1007f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10086ecc5e8fSTrond Myklebust return 0; 1009f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 10101cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1011912a108dSNeilBrown if (rcu_walk) 1012f2c77f4eSTrond Myklebust return 0; 10131cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 10141cd9cb05STrond Myklebust return 0; 10151cd9cb05STrond Myklebust } 1016f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1017f2c77f4eSTrond Myklebust return 0; 1018f2c77f4eSTrond Myklebust return 1; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds /* 1022a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1023a12802caSTrond Myklebust * an O_EXCL create using this path component. 1024a12802caSTrond Myklebust */ 1025fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1026a12802caSTrond Myklebust { 1027a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1028a12802caSTrond Myklebust return 0; 1029fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1030a12802caSTrond Myklebust } 1031a12802caSTrond Myklebust 1032a12802caSTrond Myklebust /* 10331d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10341d6757fbSTrond Myklebust * 10351d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10361d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10371d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10381d6757fbSTrond Myklebust * 10391d6757fbSTrond Myklebust */ 104065a0c149STrond Myklebust static 1041fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10421da177e4SLinus Torvalds { 10431da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 104465a0c149STrond Myklebust int ret; 10451da177e4SLinus Torvalds 104636d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10474e99a1ffSTrond Myklebust return 0; 104847921921STrond Myklebust 104947921921STrond Myklebust if (flags & LOOKUP_OPEN) { 105047921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 105147921921STrond Myklebust case S_IFREG: 105247921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 105347921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 105447921921STrond Myklebust goto out; 105547921921STrond Myklebust /* Fallthrough */ 105647921921STrond Myklebust case S_IFDIR: 105747921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 105847921921STrond Myklebust break; 105947921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 106047921921STrond Myklebust goto out_force; 106147921921STrond Myklebust } 106247921921STrond Myklebust } 106347921921STrond Myklebust 10641da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1065fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10661da177e4SLinus Torvalds goto out_force; 106765a0c149STrond Myklebust out: 1068a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 10691da177e4SLinus Torvalds out_force: 10701fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10711fa1e384SNeilBrown return -ECHILD; 107265a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 107365a0c149STrond Myklebust if (ret != 0) 107465a0c149STrond Myklebust return ret; 107565a0c149STrond Myklebust goto out; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds /* 10791da177e4SLinus Torvalds * We judge how long we want to trust negative 10801da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10811da177e4SLinus Torvalds * 10821da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10831da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1084912a108dSNeilBrown * 1085912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1086912a108dSNeilBrown * suggesting a reval is needed. 10879f6d44d4STrond Myklebust * 10889f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 10899f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 10901da177e4SLinus Torvalds */ 10911da177e4SLinus Torvalds static inline 10921da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1093fa3c56bbSAl Viro unsigned int flags) 10941da177e4SLinus Torvalds { 10959f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 10961da177e4SLinus Torvalds return 0; 10974eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10984eec952eSTrond Myklebust return 1; 1099912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11025ceb9d7fSTrond Myklebust static int 11035ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 11045ceb9d7fSTrond Myklebust struct inode *inode, int error) 11051da177e4SLinus Torvalds { 11065ceb9d7fSTrond Myklebust switch (error) { 11075ceb9d7fSTrond Myklebust case 1: 11086de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 11096de1472fSAl Viro __func__, dentry); 11101da177e4SLinus Torvalds return 1; 11115ceb9d7fSTrond Myklebust case 0: 1112a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11131da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11141da177e4SLinus Torvalds /* Purge readdir caches. */ 11151da177e4SLinus Torvalds nfs_zap_caches(inode); 1116a3f432bfSJ. Bruce Fields /* 1117a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1118a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1119a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1120a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1121a3f432bfSJ. Bruce Fields */ 1122a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 11235ceb9d7fSTrond Myklebust return 1; 11241da177e4SLinus Torvalds } 11256de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 11266de1472fSAl Viro __func__, dentry); 11271da177e4SLinus Torvalds return 0; 11285ceb9d7fSTrond Myklebust } 11296de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 11306de1472fSAl Viro __func__, dentry, error); 1131e1fb4d05STrond Myklebust return error; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11345ceb9d7fSTrond Myklebust static int 11355ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 11365ceb9d7fSTrond Myklebust unsigned int flags) 11375ceb9d7fSTrond Myklebust { 11385ceb9d7fSTrond Myklebust int ret = 1; 11395ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 11405ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 11415ceb9d7fSTrond Myklebust return -ECHILD; 11425ceb9d7fSTrond Myklebust ret = 0; 11435ceb9d7fSTrond Myklebust } 11445ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 11455ceb9d7fSTrond Myklebust } 11465ceb9d7fSTrond Myklebust 11475ceb9d7fSTrond Myklebust static int 11485ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 11495ceb9d7fSTrond Myklebust struct inode *inode) 11505ceb9d7fSTrond Myklebust { 11515ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11525ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 11535ceb9d7fSTrond Myklebust } 11545ceb9d7fSTrond Myklebust 11555ceb9d7fSTrond Myklebust static int 11565ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 11575ceb9d7fSTrond Myklebust struct inode *inode) 11585ceb9d7fSTrond Myklebust { 11595ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 11605ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 11615ceb9d7fSTrond Myklebust struct nfs4_label *label; 11625ceb9d7fSTrond Myklebust int ret; 11635ceb9d7fSTrond Myklebust 11645ceb9d7fSTrond Myklebust ret = -ENOMEM; 11655ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 11665ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 11675ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 11685ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 11695ceb9d7fSTrond Myklebust goto out; 11705ceb9d7fSTrond Myklebust 1171f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 11725ceb9d7fSTrond Myklebust if (ret < 0) { 1173f7b37b8bSTrond Myklebust switch (ret) { 1174f7b37b8bSTrond Myklebust case -ESTALE: 1175f7b37b8bSTrond Myklebust case -ENOENT: 11765ceb9d7fSTrond Myklebust ret = 0; 1177f7b37b8bSTrond Myklebust break; 1178f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1179f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1180f7b37b8bSTrond Myklebust ret = 1; 1181f7b37b8bSTrond Myklebust } 11825ceb9d7fSTrond Myklebust goto out; 11835ceb9d7fSTrond Myklebust } 11845ceb9d7fSTrond Myklebust ret = 0; 11855ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11865ceb9d7fSTrond Myklebust goto out; 11875ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 11885ceb9d7fSTrond Myklebust goto out; 11895ceb9d7fSTrond Myklebust 11905ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 11915ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11925ceb9d7fSTrond Myklebust 11935ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 11945ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 11955ceb9d7fSTrond Myklebust ret = 1; 11965ceb9d7fSTrond Myklebust out: 11975ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 11985ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 11995ceb9d7fSTrond Myklebust nfs4_label_free(label); 12005ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 12015ceb9d7fSTrond Myklebust } 12025ceb9d7fSTrond Myklebust 12035ceb9d7fSTrond Myklebust /* 12045ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 12055ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 12065ceb9d7fSTrond Myklebust * lookup. 12075ceb9d7fSTrond Myklebust * 12085ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 12095ceb9d7fSTrond Myklebust * we have an inode! 12105ceb9d7fSTrond Myklebust * 12115ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 12125ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 12135ceb9d7fSTrond Myklebust */ 12145ceb9d7fSTrond Myklebust static int 12155ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 12165ceb9d7fSTrond Myklebust unsigned int flags) 12175ceb9d7fSTrond Myklebust { 12185ceb9d7fSTrond Myklebust struct inode *inode; 12195ceb9d7fSTrond Myklebust int error; 12205ceb9d7fSTrond Myklebust 12215ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 12225ceb9d7fSTrond Myklebust inode = d_inode(dentry); 12235ceb9d7fSTrond Myklebust 12245ceb9d7fSTrond Myklebust if (!inode) 12255ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 12265ceb9d7fSTrond Myklebust 12275ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 12285ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12295ceb9d7fSTrond Myklebust __func__, dentry); 12305ceb9d7fSTrond Myklebust goto out_bad; 12315ceb9d7fSTrond Myklebust } 12325ceb9d7fSTrond Myklebust 12335ceb9d7fSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 12345ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 12355ceb9d7fSTrond Myklebust 12365ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 12375ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 12385ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 12395ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 12405ceb9d7fSTrond Myklebust if (error) { 12415ceb9d7fSTrond Myklebust if (error == -ESTALE) 12425ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 12435ceb9d7fSTrond Myklebust goto out_bad; 12445ceb9d7fSTrond Myklebust } 12455ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 12465ceb9d7fSTrond Myklebust goto out_valid; 12475ceb9d7fSTrond Myklebust } 12485ceb9d7fSTrond Myklebust 12495ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12505ceb9d7fSTrond Myklebust return -ECHILD; 12515ceb9d7fSTrond Myklebust 12525ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 12535ceb9d7fSTrond Myklebust goto out_bad; 12545ceb9d7fSTrond Myklebust 12555ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 12565ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 12575ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 12585ceb9d7fSTrond Myklebust return error; 12595ceb9d7fSTrond Myklebust out_valid: 12605ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12615ceb9d7fSTrond Myklebust out_bad: 12625ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12635ceb9d7fSTrond Myklebust return -ECHILD; 12645ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 12655ceb9d7fSTrond Myklebust } 12665ceb9d7fSTrond Myklebust 12675ceb9d7fSTrond Myklebust static int 1268c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1269c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 12705ceb9d7fSTrond Myklebust { 12715ceb9d7fSTrond Myklebust struct dentry *parent; 12725ceb9d7fSTrond Myklebust struct inode *dir; 12735ceb9d7fSTrond Myklebust int ret; 12745ceb9d7fSTrond Myklebust 12755ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 12765ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 12775ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 12785ceb9d7fSTrond Myklebust if (!dir) 12795ceb9d7fSTrond Myklebust return -ECHILD; 1280c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 12815ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 12825ceb9d7fSTrond Myklebust return -ECHILD; 12835ceb9d7fSTrond Myklebust } else { 12845ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1285c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 12865ceb9d7fSTrond Myklebust dput(parent); 12875ceb9d7fSTrond Myklebust } 12885ceb9d7fSTrond Myklebust return ret; 12895ceb9d7fSTrond Myklebust } 12905ceb9d7fSTrond Myklebust 1291c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1292c7944ebbSTrond Myklebust { 1293c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1294c7944ebbSTrond Myklebust } 1295c7944ebbSTrond Myklebust 12961da177e4SLinus Torvalds /* 12972b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1298ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1299ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1300ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1301ecf3d1f1SJeff Layton * 1302ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1303ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1304ecf3d1f1SJeff Layton */ 1305ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1306ecf3d1f1SJeff Layton { 13072b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 13089cdd1d3fSTrond Myklebust int error = 0; 1309ecf3d1f1SJeff Layton 1310ecf3d1f1SJeff Layton /* 1311ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1312ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1313ecf3d1f1SJeff Layton * eventually need to do something more here. 1314ecf3d1f1SJeff Layton */ 1315ecf3d1f1SJeff Layton if (!inode) { 13166de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 13176de1472fSAl Viro __func__, dentry); 1318ecf3d1f1SJeff Layton return 1; 1319ecf3d1f1SJeff Layton } 1320ecf3d1f1SJeff Layton 1321ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 13226de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13236de1472fSAl Viro __func__, dentry); 1324ecf3d1f1SJeff Layton return 0; 1325ecf3d1f1SJeff Layton } 1326ecf3d1f1SJeff Layton 1327b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1328ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1329ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1330ecf3d1f1SJeff Layton return !error; 1331ecf3d1f1SJeff Layton } 1332ecf3d1f1SJeff Layton 1333ecf3d1f1SJeff Layton /* 13341da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 13351da177e4SLinus Torvalds */ 1336fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 13371da177e4SLinus Torvalds { 13386de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13396de1472fSAl Viro dentry, dentry->d_flags); 13401da177e4SLinus Torvalds 134177f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13422b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 134377f11192STrond Myklebust return 1; 134477f11192STrond Myklebust 13451da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13461da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13471da177e4SLinus Torvalds return 1; 13481da177e4SLinus Torvalds } 13491751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 13501da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13511da177e4SLinus Torvalds * files will be cleaned up during umount */ 13521da177e4SLinus Torvalds return 1; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds return 0; 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 13581f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13591b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13601b83d707STrond Myklebust { 13611b83d707STrond Myklebust spin_lock(&inode->i_lock); 13621f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 136359a707b0STrond Myklebust if (inode->i_nlink > 0) 136459a707b0STrond Myklebust drop_nlink(inode); 136559a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 136616e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 136716e14375STrond Myklebust | NFS_INO_INVALID_CTIME 136859a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 136959a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 13701b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13711b83d707STrond Myklebust } 13721b83d707STrond Myklebust 13731da177e4SLinus Torvalds /* 13741da177e4SLinus Torvalds * Called when the dentry loses inode. 13751da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13761da177e4SLinus Torvalds */ 13771da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13781da177e4SLinus Torvalds { 137983672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 138083672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 138183672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 138283672d39SNeil Brown 13831da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1384e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13851f018458STrond Myklebust nfs_drop_nlink(inode); 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds iput(inode); 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 1390b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1391b1942c5fSAl Viro { 1392b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1393b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1394b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1395b1942c5fSAl Viro WARN_ON(1); 1396b1942c5fSAl Viro else 1397b1942c5fSAl Viro kfree(dentry->d_fsdata); 1398b1942c5fSAl Viro } 1399b1942c5fSAl Viro } 1400b1942c5fSAl Viro 1401f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 14021da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1403ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 14041da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14051da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 140636d43a43SDavid Howells .d_automount = nfs_d_automount, 1407b1942c5fSAl Viro .d_release = nfs_d_release, 14081da177e4SLinus Torvalds }; 1409ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 14101da177e4SLinus Torvalds 1411597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 14121da177e4SLinus Torvalds { 14131da177e4SLinus Torvalds struct dentry *res; 14141da177e4SLinus Torvalds struct inode *inode = NULL; 1415e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1416e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 14171775fd3eSDavid Quigley struct nfs4_label *label = NULL; 14181da177e4SLinus Torvalds int error; 14191da177e4SLinus Torvalds 14206de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 142191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 14221da177e4SLinus Torvalds 1423130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1424130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 14251da177e4SLinus Torvalds 1426fd684071STrond Myklebust /* 1427fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1428fd684071STrond Myklebust * but don't hash the dentry. 1429fd684071STrond Myklebust */ 14309f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1431130f9ab7SAl Viro return NULL; 14321da177e4SLinus Torvalds 1433e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1434e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1435e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1436e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1437e1fb4d05STrond Myklebust goto out; 1438e1fb4d05STrond Myklebust 143914c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 144014c43f76SDavid Quigley if (IS_ERR(label)) 144114c43f76SDavid Quigley goto out; 144214c43f76SDavid Quigley 14436e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1444f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 14451da177e4SLinus Torvalds if (error == -ENOENT) 14461da177e4SLinus Torvalds goto no_entry; 14471da177e4SLinus Torvalds if (error < 0) { 14481da177e4SLinus Torvalds res = ERR_PTR(error); 1449bf130914SAl Viro goto out_label; 14501da177e4SLinus Torvalds } 14511775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1452bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 145303f28e3aSTrond Myklebust if (IS_ERR(res)) 1454bf130914SAl Viro goto out_label; 145554ceac45SDavid Howells 145663519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 145763519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1458d69ee9b8STrond Myklebust 14591da177e4SLinus Torvalds no_entry: 146041d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14619eaef27bSTrond Myklebust if (res != NULL) { 14629eaef27bSTrond Myklebust if (IS_ERR(res)) 1463bf130914SAl Viro goto out_label; 14641da177e4SLinus Torvalds dentry = res; 14659eaef27bSTrond Myklebust } 14661da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1467bf130914SAl Viro out_label: 14686e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 146914c43f76SDavid Quigley nfs4_label_free(label); 14701da177e4SLinus Torvalds out: 1471e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1472e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14731da177e4SLinus Torvalds return res; 14741da177e4SLinus Torvalds } 1475ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14761da177e4SLinus Torvalds 147789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14780b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14791da177e4SLinus Torvalds 1480f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14810ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1482b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 14831da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14841da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 148536d43a43SDavid Howells .d_automount = nfs_d_automount, 1486b1942c5fSAl Viro .d_release = nfs_d_release, 14871da177e4SLinus Torvalds }; 148889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14891da177e4SLinus Torvalds 14908a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14918a5e929dSAl Viro { 14928a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14938a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14948a5e929dSAl Viro res |= FMODE_READ; 14958a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14968a5e929dSAl Viro res |= FMODE_WRITE; 14978a5e929dSAl Viro return res; 14988a5e929dSAl Viro } 14998a5e929dSAl Viro 1500532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1501cd9a1c0eSTrond Myklebust { 1502532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1503cd9a1c0eSTrond Myklebust } 1504cd9a1c0eSTrond Myklebust 1505cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1506cd9a1c0eSTrond Myklebust { 1507f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1508cd9a1c0eSTrond Myklebust return 0; 1509cd9a1c0eSTrond Myklebust } 1510cd9a1c0eSTrond Myklebust 1511d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 15120dd2b474SMiklos Szeredi struct dentry *dentry, 1513b452a458SAl Viro struct file *file, unsigned open_flags) 1514cd9a1c0eSTrond Myklebust { 15150dd2b474SMiklos Szeredi int err; 15160dd2b474SMiklos Szeredi 1517be12af3eSAl Viro err = finish_open(file, dentry, do_open); 151830d90494SAl Viro if (err) 1519d9585277SAl Viro goto out; 1520eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 152130d90494SAl Viro nfs_file_set_open_context(file, ctx); 1522eaa2b82cSNeilBrown else 15239821421aSTrond Myklebust err = -EOPENSTALE; 1524cd9a1c0eSTrond Myklebust out: 1525d9585277SAl Viro return err; 1526cd9a1c0eSTrond Myklebust } 1527cd9a1c0eSTrond Myklebust 152873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 152930d90494SAl Viro struct file *file, unsigned open_flags, 153044907d79SAl Viro umode_t mode) 15311da177e4SLinus Torvalds { 1532c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1533cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 15340dd2b474SMiklos Szeredi struct dentry *res; 15350dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1536f46e0bd3STrond Myklebust struct inode *inode; 15371472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1538c94c0953SAl Viro bool switched = false; 153973a09dd9SAl Viro int created = 0; 1540898f635cSTrond Myklebust int err; 15411da177e4SLinus Torvalds 15420dd2b474SMiklos Szeredi /* Expect a negative dentry */ 15432b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 15440dd2b474SMiklos Szeredi 15451e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 15466de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15471e7cb3dcSChuck Lever 15489597c13bSJeff Layton err = nfs_check_flags(open_flags); 15499597c13bSJeff Layton if (err) 15509597c13bSJeff Layton return err; 15519597c13bSJeff Layton 15520dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15530dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 155400699ad8SAl Viro if (!d_in_lookup(dentry)) { 15550dd2b474SMiklos Szeredi /* 15560dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15570dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15580dd2b474SMiklos Szeredi * again 15590dd2b474SMiklos Szeredi */ 1560d9585277SAl Viro return -ENOENT; 15610dd2b474SMiklos Szeredi } 15621472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15631da177e4SLinus Torvalds goto no_open; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 15660dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1567d9585277SAl Viro return -ENAMETOOLONG; 15681da177e4SLinus Torvalds 15690dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1570dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1571dff25ddbSAndreas Gruenbacher 1572dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1573dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1574dff25ddbSAndreas Gruenbacher 1575536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1576dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 15770dd2b474SMiklos Szeredi } 1578536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1579536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1580536e43d1STrond Myklebust attr.ia_size = 0; 1581cd9a1c0eSTrond Myklebust } 1582cd9a1c0eSTrond Myklebust 1583c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1584c94c0953SAl Viro d_drop(dentry); 1585c94c0953SAl Viro switched = true; 1586c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1587c94c0953SAl Viro &dentry->d_name, &wq); 1588c94c0953SAl Viro if (IS_ERR(dentry)) 1589c94c0953SAl Viro return PTR_ERR(dentry); 1590c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1591c94c0953SAl Viro return finish_no_open(file, dentry); 1592c94c0953SAl Viro } 1593c94c0953SAl Viro 1594532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 15950dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15960dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1597d9585277SAl Viro goto out; 15980dd2b474SMiklos Szeredi 15996e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 160073a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 160173a09dd9SAl Viro if (created) 160273a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1603275bb307STrond Myklebust if (IS_ERR(inode)) { 16040dd2b474SMiklos Szeredi err = PTR_ERR(inode); 16056e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16062d9db750STrond Myklebust put_nfs_open_context(ctx); 1607d20cb71dSAl Viro d_drop(dentry); 16080dd2b474SMiklos Szeredi switch (err) { 16091da177e4SLinus Torvalds case -ENOENT: 1610774d9513SPeng Tao d_splice_alias(NULL, dentry); 1611809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16120dd2b474SMiklos Szeredi break; 16131788ea6eSJeff Layton case -EISDIR: 16146f926b5bSTrond Myklebust case -ENOTDIR: 16156f926b5bSTrond Myklebust goto no_open; 16161da177e4SLinus Torvalds case -ELOOP: 16170dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 16181da177e4SLinus Torvalds goto no_open; 16190dd2b474SMiklos Szeredi break; 16201da177e4SLinus Torvalds /* case -EINVAL: */ 16211da177e4SLinus Torvalds default: 16220dd2b474SMiklos Szeredi break; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds goto out; 16251da177e4SLinus Torvalds } 16260dd2b474SMiklos Szeredi 1627b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 16286e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16292d9db750STrond Myklebust put_nfs_open_context(ctx); 1630d9585277SAl Viro out: 1631c94c0953SAl Viro if (unlikely(switched)) { 1632c94c0953SAl Viro d_lookup_done(dentry); 1633c94c0953SAl Viro dput(dentry); 1634c94c0953SAl Viro } 1635d9585277SAl Viro return err; 16360dd2b474SMiklos Szeredi 16371da177e4SLinus Torvalds no_open: 16381472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1639c94c0953SAl Viro if (switched) { 1640c94c0953SAl Viro d_lookup_done(dentry); 1641c94c0953SAl Viro if (!res) 1642c94c0953SAl Viro res = dentry; 1643c94c0953SAl Viro else 1644c94c0953SAl Viro dput(dentry); 1645c94c0953SAl Viro } 16460dd2b474SMiklos Szeredi if (IS_ERR(res)) 1647c94c0953SAl Viro return PTR_ERR(res); 1648e45198a6SAl Viro return finish_no_open(file, res); 16491da177e4SLinus Torvalds } 165089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 16511da177e4SLinus Torvalds 1652c7944ebbSTrond Myklebust static int 1653c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1654c7944ebbSTrond Myklebust unsigned int flags) 16551da177e4SLinus Torvalds { 1656657e94b6SNick Piggin struct inode *inode; 16571da177e4SLinus Torvalds 1658fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1659c7944ebbSTrond Myklebust goto full_reval; 1660eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1661c7944ebbSTrond Myklebust goto full_reval; 16622b484297STrond Myklebust 16632b0143b5SDavid Howells inode = d_inode(dentry); 16642b484297STrond Myklebust 16651da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 16661da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 16671da177e4SLinus Torvalds */ 1668c7944ebbSTrond Myklebust if (inode == NULL) 1669c7944ebbSTrond Myklebust goto full_reval; 167049317a7fSNeilBrown 1671c7944ebbSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 1672c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1673216d5d06STrond Myklebust 16741da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16751da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1676c7944ebbSTrond Myklebust goto full_reval; 1677c7944ebbSTrond Myklebust 16781da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1679c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1680c7944ebbSTrond Myklebust goto reval_dentry; 1681c7944ebbSTrond Myklebust 1682c7944ebbSTrond Myklebust /* Check if the directory changed */ 1683c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1684c7944ebbSTrond Myklebust goto reval_dentry; 16851da177e4SLinus Torvalds 16860ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1687c7944ebbSTrond Myklebust return 1; 1688c7944ebbSTrond Myklebust reval_dentry: 1689c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1690c7944ebbSTrond Myklebust return -ECHILD; 169142f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 16920ef97dcfSMiklos Szeredi 1693c7944ebbSTrond Myklebust full_reval: 1694c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1695c7944ebbSTrond Myklebust } 1696535918f1STrond Myklebust 1697c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1698c7944ebbSTrond Myklebust { 1699c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1700c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1701c0204fd2STrond Myklebust } 1702c0204fd2STrond Myklebust 17031da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 17041da177e4SLinus Torvalds 1705406cd915SBenjamin Coddington struct dentry * 1706406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 17071775fd3eSDavid Quigley struct nfs_fattr *fattr, 17081775fd3eSDavid Quigley struct nfs4_label *label) 17091da177e4SLinus Torvalds { 1710fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 17112b0143b5SDavid Howells struct inode *dir = d_inode(parent); 17121da177e4SLinus Torvalds struct inode *inode; 1713b0c6108eSAl Viro struct dentry *d; 1714406cd915SBenjamin Coddington int error; 17151da177e4SLinus Torvalds 1716fab728e1STrond Myklebust d_drop(dentry); 1717fab728e1STrond Myklebust 17181da177e4SLinus Torvalds if (fhandle->size == 0) { 1719f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 17201da177e4SLinus Torvalds if (error) 1721fab728e1STrond Myklebust goto out_error; 17221da177e4SLinus Torvalds } 17235724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17241da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 17251da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1726a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1727a841b54dSTrond Myklebust fattr, NULL, NULL); 17281da177e4SLinus Torvalds if (error < 0) 1729fab728e1STrond Myklebust goto out_error; 17301da177e4SLinus Torvalds } 17311775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1732b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1733fab728e1STrond Myklebust out: 1734fab728e1STrond Myklebust dput(parent); 1735406cd915SBenjamin Coddington return d; 1736fab728e1STrond Myklebust out_error: 1737fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1738406cd915SBenjamin Coddington d = ERR_PTR(error); 1739406cd915SBenjamin Coddington goto out; 1740406cd915SBenjamin Coddington } 1741406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1742406cd915SBenjamin Coddington 1743406cd915SBenjamin Coddington /* 1744406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1745406cd915SBenjamin Coddington */ 1746406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1747406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1748406cd915SBenjamin Coddington struct nfs4_label *label) 1749406cd915SBenjamin Coddington { 1750406cd915SBenjamin Coddington struct dentry *d; 1751406cd915SBenjamin Coddington 1752406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1753406cd915SBenjamin Coddington if (IS_ERR(d)) 1754406cd915SBenjamin Coddington return PTR_ERR(d); 1755406cd915SBenjamin Coddington 1756406cd915SBenjamin Coddington /* Callers don't care */ 1757406cd915SBenjamin Coddington dput(d); 1758406cd915SBenjamin Coddington return 0; 17591da177e4SLinus Torvalds } 1760ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds /* 17631da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17641da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17651da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17661da177e4SLinus Torvalds * reply path made it appear to have failed. 17671da177e4SLinus Torvalds */ 1768597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1769ebfc3b49SAl Viro umode_t mode, bool excl) 17701da177e4SLinus Torvalds { 17711da177e4SLinus Torvalds struct iattr attr; 1772ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 17731da177e4SLinus Torvalds int error; 17741da177e4SLinus Torvalds 17751e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 17766de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds attr.ia_mode = mode; 17791da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17801da177e4SLinus Torvalds 17818b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17828867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17838b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17841da177e4SLinus Torvalds if (error != 0) 17851da177e4SLinus Torvalds goto out_err; 17861da177e4SLinus Torvalds return 0; 17871da177e4SLinus Torvalds out_err: 17881da177e4SLinus Torvalds d_drop(dentry); 17891da177e4SLinus Torvalds return error; 17901da177e4SLinus Torvalds } 1791ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds /* 17941da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17951da177e4SLinus Torvalds */ 1796597d9289SBryan Schumaker int 17971a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds struct iattr attr; 18001da177e4SLinus Torvalds int status; 18011da177e4SLinus Torvalds 18021e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 18036de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds attr.ia_mode = mode; 18061da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18071da177e4SLinus Torvalds 18081ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 18091da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 18101ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 18111da177e4SLinus Torvalds if (status != 0) 18121da177e4SLinus Torvalds goto out_err; 18131da177e4SLinus Torvalds return 0; 18141da177e4SLinus Torvalds out_err: 18151da177e4SLinus Torvalds d_drop(dentry); 18161da177e4SLinus Torvalds return status; 18171da177e4SLinus Torvalds } 1818ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds /* 18211da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18221da177e4SLinus Torvalds */ 1823597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 18241da177e4SLinus Torvalds { 18251da177e4SLinus Torvalds struct iattr attr; 18261da177e4SLinus Torvalds int error; 18271da177e4SLinus Torvalds 18281e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 18296de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18321da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 18331da177e4SLinus Torvalds 18341ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 18351da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 18361ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 18371da177e4SLinus Torvalds if (error != 0) 18381da177e4SLinus Torvalds goto out_err; 18391da177e4SLinus Torvalds return 0; 18401da177e4SLinus Torvalds out_err: 18411da177e4SLinus Torvalds d_drop(dentry); 18421da177e4SLinus Torvalds return error; 18431da177e4SLinus Torvalds } 1844ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 18451da177e4SLinus Torvalds 1846d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1847d45b9d8bSTrond Myklebust { 1848dc3f4198SAl Viro if (simple_positive(dentry)) 1849d45b9d8bSTrond Myklebust d_delete(dentry); 1850d45b9d8bSTrond Myklebust } 1851d45b9d8bSTrond Myklebust 1852597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 18531da177e4SLinus Torvalds { 18541da177e4SLinus Torvalds int error; 18551da177e4SLinus Torvalds 18561e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 18576de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18581da177e4SLinus Torvalds 18591ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18602b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1861884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18621da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18631da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1864ba6c0592STrond Myklebust switch (error) { 1865ba6c0592STrond Myklebust case 0: 18662b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1867ba6c0592STrond Myklebust break; 1868ba6c0592STrond Myklebust case -ENOENT: 1869d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1870ba6c0592STrond Myklebust } 1871884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1872ba6c0592STrond Myklebust } else 1873ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18741ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds return error; 18771da177e4SLinus Torvalds } 1878ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds /* 18811da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18821da177e4SLinus Torvalds * and after checking that the file has only one user. 18831da177e4SLinus Torvalds * 18841da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18851da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18861da177e4SLinus Torvalds */ 18871da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18881da177e4SLinus Torvalds { 18892b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18902b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18911da177e4SLinus Torvalds int error = -EBUSY; 18921da177e4SLinus Torvalds 18936de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18961da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18971da177e4SLinus Torvalds error = 0; 18981da177e4SLinus Torvalds goto out; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19011ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 19021da177e4SLinus Torvalds if (inode != NULL) { 1903912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 19041da177e4SLinus Torvalds if (error == 0) 19051b83d707STrond Myklebust nfs_drop_nlink(inode); 19061da177e4SLinus Torvalds } else 1907912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 1908d45b9d8bSTrond Myklebust if (error == -ENOENT) 1909d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 19101ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 19111da177e4SLinus Torvalds out: 19121da177e4SLinus Torvalds return error; 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 19161da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 19191da177e4SLinus Torvalds */ 1920597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 19211da177e4SLinus Torvalds { 19221da177e4SLinus Torvalds int error; 19231da177e4SLinus Torvalds int need_rehash = 0; 19241da177e4SLinus Torvalds 19251e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 19266de1472fSAl Viro dir->i_ino, dentry); 19271da177e4SLinus Torvalds 19281ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 19291da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 193084d08fa8SAl Viro if (d_count(dentry) > 1) { 19311da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1932ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 19332b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 19341da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 19351ca42382STrond Myklebust goto out; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 19381da177e4SLinus Torvalds __d_drop(dentry); 19391da177e4SLinus Torvalds need_rehash = 1; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 19421da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1943d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 19441da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19451da177e4SLinus Torvalds } else if (need_rehash) 19461da177e4SLinus Torvalds d_rehash(dentry); 19471ca42382STrond Myklebust out: 19481ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 19491da177e4SLinus Torvalds return error; 19501da177e4SLinus Torvalds } 1951ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 19521da177e4SLinus Torvalds 1953873101b3SChuck Lever /* 1954873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1955873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1956873101b3SChuck Lever * using prepare_write/commit_write. 1957873101b3SChuck Lever * 1958873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1959873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1960873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1961873101b3SChuck Lever * symlink request has completed on the server. 1962873101b3SChuck Lever * 1963873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1964873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1965873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1966873101b3SChuck Lever * and move the raw page into its mapping. 1967873101b3SChuck Lever */ 1968597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19691da177e4SLinus Torvalds { 1970873101b3SChuck Lever struct page *page; 1971873101b3SChuck Lever char *kaddr; 19721da177e4SLinus Torvalds struct iattr attr; 1973873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19741da177e4SLinus Torvalds int error; 19751da177e4SLinus Torvalds 19761e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 19776de1472fSAl Viro dir->i_ino, dentry, symname); 19781da177e4SLinus Torvalds 1979873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1980873101b3SChuck Lever return -ENAMETOOLONG; 19811da177e4SLinus Torvalds 1982873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1983873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19841da177e4SLinus Torvalds 1985e8ecde25SAl Viro page = alloc_page(GFP_USER); 198676566991STrond Myklebust if (!page) 1987873101b3SChuck Lever return -ENOMEM; 1988873101b3SChuck Lever 1989e8ecde25SAl Viro kaddr = page_address(page); 1990873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1991873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1992873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1993873101b3SChuck Lever 19941ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 199594a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19961ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1997873101b3SChuck Lever if (error != 0) { 19981e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1999873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 20006de1472fSAl Viro dentry, symname, error); 20011da177e4SLinus Torvalds d_drop(dentry); 2002873101b3SChuck Lever __free_page(page); 20031da177e4SLinus Torvalds return error; 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds 2006873101b3SChuck Lever /* 2007873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2008873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2009873101b3SChuck Lever */ 20102b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2011873101b3SChuck Lever GFP_KERNEL)) { 2012873101b3SChuck Lever SetPageUptodate(page); 2013873101b3SChuck Lever unlock_page(page); 2014a0b54addSRafael Aquini /* 2015a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2016a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2017a0b54addSRafael Aquini */ 201809cbfeafSKirill A. Shutemov put_page(page); 2019873101b3SChuck Lever } else 2020873101b3SChuck Lever __free_page(page); 2021873101b3SChuck Lever 2022873101b3SChuck Lever return 0; 2023873101b3SChuck Lever } 2024ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2025873101b3SChuck Lever 2026597d9289SBryan Schumaker int 20271da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20281da177e4SLinus Torvalds { 20292b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 20301da177e4SLinus Torvalds int error; 20311da177e4SLinus Torvalds 20326de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 20336de1472fSAl Viro old_dentry, dentry); 20341da177e4SLinus Torvalds 20351fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 20369697d234STrond Myklebust d_drop(dentry); 20371da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2038cf809556STrond Myklebust if (error == 0) { 20397de9c6eeSAl Viro ihold(inode); 20409697d234STrond Myklebust d_add(dentry, inode); 2041cf809556STrond Myklebust } 20421fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 20431da177e4SLinus Torvalds return error; 20441da177e4SLinus Torvalds } 2045ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* 20481da177e4SLinus Torvalds * RENAME 20491da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20501da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20511da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20521da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20531da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20541da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20551da177e4SLinus Torvalds * 20561da177e4SLinus Torvalds * FIXED. 20571da177e4SLinus Torvalds * 20581da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20591da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20601da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20611da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20621da177e4SLinus Torvalds * using the inode layer 20631da177e4SLinus Torvalds * 20641da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20651da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20661da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20671da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20681da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20691da177e4SLinus Torvalds * the rename. 20701da177e4SLinus Torvalds */ 2071597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 20721cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 20731cd66c93SMiklos Szeredi unsigned int flags) 20741da177e4SLinus Torvalds { 20752b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 20762b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2077d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 207880a491fdSJeff Layton struct rpc_task *task; 20791da177e4SLinus Torvalds int error = -EBUSY; 20801da177e4SLinus Torvalds 20811cd66c93SMiklos Szeredi if (flags) 20821cd66c93SMiklos Szeredi return -EINVAL; 20831cd66c93SMiklos Szeredi 20846de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20856de1472fSAl Viro old_dentry, new_dentry, 208684d08fa8SAl Viro d_count(new_dentry)); 20871da177e4SLinus Torvalds 208870ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20891da177e4SLinus Torvalds /* 209028f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 209128f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 209228f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 209328f79a1aSMiklos Szeredi * the new target. 20941da177e4SLinus Torvalds */ 209527226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 209627226104SMiklos Szeredi /* 209727226104SMiklos Szeredi * To prevent any new references to the target during the 209827226104SMiklos Szeredi * rename, we unhash the dentry in advance. 209927226104SMiklos Szeredi */ 2100d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 210127226104SMiklos Szeredi d_drop(new_dentry); 2102d9f29500SBenjamin Coddington rehash = new_dentry; 2103d9f29500SBenjamin Coddington } 210427226104SMiklos Szeredi 210584d08fa8SAl Viro if (d_count(new_dentry) > 2) { 21061da177e4SLinus Torvalds int err; 210727226104SMiklos Szeredi 21081da177e4SLinus Torvalds /* copy the target dentry's name */ 21091da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 21101da177e4SLinus Torvalds &new_dentry->d_name); 21111da177e4SLinus Torvalds if (!dentry) 21121da177e4SLinus Torvalds goto out; 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds /* silly-rename the existing target ... */ 21151da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 211624e93025SMiklos Szeredi if (err) 21171da177e4SLinus Torvalds goto out; 211824e93025SMiklos Szeredi 211924e93025SMiklos Szeredi new_dentry = dentry; 2120d9f29500SBenjamin Coddington rehash = NULL; 212124e93025SMiklos Szeredi new_inode = NULL; 2122b1e4adf4STrond Myklebust } 212327226104SMiklos Szeredi } 21241da177e4SLinus Torvalds 2125d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 212680a491fdSJeff Layton if (IS_ERR(task)) { 212780a491fdSJeff Layton error = PTR_ERR(task); 212880a491fdSJeff Layton goto out; 212980a491fdSJeff Layton } 213080a491fdSJeff Layton 213180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2132818a8dbeSBenjamin Coddington if (error != 0) { 2133818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2134818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2135818a8dbeSBenjamin Coddington smp_wmb(); 2136818a8dbeSBenjamin Coddington } else 213780a491fdSJeff Layton error = task->tk_status; 213880a491fdSJeff Layton rpc_put_task(task); 213959a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 214059a707b0STrond Myklebust if (error == 0) { 214159a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 214259a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 214359a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 214459a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 214559a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 214659a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 214759a707b0STrond Myklebust } 21481da177e4SLinus Torvalds out: 2149d9f29500SBenjamin Coddington if (rehash) 2150d9f29500SBenjamin Coddington d_rehash(rehash); 215170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 215270ded201STrond Myklebust new_dir, new_dentry, error); 2153d9f29500SBenjamin Coddington if (!error) { 2154d9f29500SBenjamin Coddington if (new_inode != NULL) 2155d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2156d9f29500SBenjamin Coddington /* 2157d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2158d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2159d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2160d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2161d9f29500SBenjamin Coddington */ 2162d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2163d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2164d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2165d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2166d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2167d9f29500SBenjamin Coddington 21681da177e4SLinus Torvalds /* new dentry created? */ 21691da177e4SLinus Torvalds if (dentry) 21701da177e4SLinus Torvalds dput(dentry); 21711da177e4SLinus Torvalds return error; 21721da177e4SLinus Torvalds } 2173ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 21741da177e4SLinus Torvalds 2175cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2176cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2177cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2178cfcea3e8STrond Myklebust 21793a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 21803a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 21813a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 21823a505845STrond Myklebust 21831c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 21841c3c07e9STrond Myklebust { 2185b68572e0SNeilBrown put_cred(entry->cred); 2186f682a398SNeilBrown kfree_rcu(entry, rcu_head); 21874e857c58SPeter Zijlstra smp_mb__before_atomic(); 2188cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 21894e857c58SPeter Zijlstra smp_mb__after_atomic(); 21901c3c07e9STrond Myklebust } 21911c3c07e9STrond Myklebust 21921a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 21931a81bb8aSTrond Myklebust { 21941a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 21951a81bb8aSTrond Myklebust 21961a81bb8aSTrond Myklebust while (!list_empty(head)) { 21971a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 21981a81bb8aSTrond Myklebust list_del(&cache->lru); 21991a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 22001a81bb8aSTrond Myklebust } 22011a81bb8aSTrond Myklebust } 22021a81bb8aSTrond Myklebust 22033a505845STrond Myklebust static unsigned long 22043a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2205979df72eSTrond Myklebust { 2206979df72eSTrond Myklebust LIST_HEAD(head); 2207aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2208979df72eSTrond Myklebust struct nfs_access_entry *cache; 22091ab6c499SDave Chinner long freed = 0; 2210979df72eSTrond Myklebust 2211a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2212aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2213979df72eSTrond Myklebust struct inode *inode; 2214979df72eSTrond Myklebust 2215979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2216979df72eSTrond Myklebust break; 22179c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2218979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2219979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2220979df72eSTrond Myklebust goto remove_lru_entry; 2221979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2222979df72eSTrond Myklebust struct nfs_access_entry, lru); 2223979df72eSTrond Myklebust list_move(&cache->lru, &head); 2224979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 22251ab6c499SDave Chinner freed++; 2226979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2227979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2228979df72eSTrond Myklebust &nfs_access_lru_list); 2229979df72eSTrond Myklebust else { 2230979df72eSTrond Myklebust remove_lru_entry: 2231979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 22324e857c58SPeter Zijlstra smp_mb__before_atomic(); 2233979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 22344e857c58SPeter Zijlstra smp_mb__after_atomic(); 2235979df72eSTrond Myklebust } 223659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2237979df72eSTrond Myklebust } 2238979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22391a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22401ab6c499SDave Chinner return freed; 22411ab6c499SDave Chinner } 22421ab6c499SDave Chinner 22431ab6c499SDave Chinner unsigned long 22443a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 22453a505845STrond Myklebust { 22463a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 22473a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 22483a505845STrond Myklebust 22493a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 22503a505845STrond Myklebust return SHRINK_STOP; 22513a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 22523a505845STrond Myklebust } 22533a505845STrond Myklebust 22543a505845STrond Myklebust 22553a505845STrond Myklebust unsigned long 22561ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 22571ab6c499SDave Chinner { 225855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2259979df72eSTrond Myklebust } 2260979df72eSTrond Myklebust 22613a505845STrond Myklebust static void 22623a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22633a505845STrond Myklebust { 22643a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22653a505845STrond Myklebust unsigned long diff; 22663a505845STrond Myklebust unsigned int nr_to_scan; 22673a505845STrond Myklebust 22683a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22693a505845STrond Myklebust return; 22703a505845STrond Myklebust nr_to_scan = 100; 22713a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 22723a505845STrond Myklebust if (diff < nr_to_scan) 22733a505845STrond Myklebust nr_to_scan = diff; 22743a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 22753a505845STrond Myklebust } 22763a505845STrond Myklebust 22771a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 22781c3c07e9STrond Myklebust { 22791c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22801a81bb8aSTrond Myklebust struct rb_node *n; 22811c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22821c3c07e9STrond Myklebust 22831c3c07e9STrond Myklebust /* Unhook entries from the cache */ 22841c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 22851c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22861c3c07e9STrond Myklebust rb_erase(n, root_node); 22871a81bb8aSTrond Myklebust list_move(&entry->lru, head); 22881c3c07e9STrond Myklebust } 22891c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 22901c3c07e9STrond Myklebust } 22911c3c07e9STrond Myklebust 22921c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 22931c3c07e9STrond Myklebust { 22941a81bb8aSTrond Myklebust LIST_HEAD(head); 22951a81bb8aSTrond Myklebust 22961a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 22971a81bb8aSTrond Myklebust return; 2298cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2299cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 23001a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2301cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2302cfcea3e8STrond Myklebust 23031c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23041a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 23051a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23061a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23071a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23081c3c07e9STrond Myklebust } 23091c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 23101c3c07e9STrond Myklebust 2311b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 23121c3c07e9STrond Myklebust { 23131c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 23141c3c07e9STrond Myklebust 23151c3c07e9STrond Myklebust while (n != NULL) { 2316b68572e0SNeilBrown struct nfs_access_entry *entry = 2317b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2318b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 23191c3c07e9STrond Myklebust 2320b68572e0SNeilBrown if (cmp < 0) 23211c3c07e9STrond Myklebust n = n->rb_left; 2322b68572e0SNeilBrown else if (cmp > 0) 23231c3c07e9STrond Myklebust n = n->rb_right; 23241c3c07e9STrond Myklebust else 23251c3c07e9STrond Myklebust return entry; 23261c3c07e9STrond Myklebust } 23271c3c07e9STrond Myklebust return NULL; 23281c3c07e9STrond Myklebust } 23291c3c07e9STrond Myklebust 2330b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 23311da177e4SLinus Torvalds { 233255296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 23331c3c07e9STrond Myklebust struct nfs_access_entry *cache; 233457b69181STrond Myklebust bool retry = true; 233557b69181STrond Myklebust int err; 23361da177e4SLinus Torvalds 23371c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 233857b69181STrond Myklebust for(;;) { 23391c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 23401c3c07e9STrond Myklebust goto out_zap; 23411c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 234257b69181STrond Myklebust err = -ENOENT; 23431c3c07e9STrond Myklebust if (cache == NULL) 23441c3c07e9STrond Myklebust goto out; 234557b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 234621c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 234757b69181STrond Myklebust break; 23485c965db8STrond Myklebust if (!retry) 23495c965db8STrond Myklebust break; 235057b69181STrond Myklebust err = -ECHILD; 235157b69181STrond Myklebust if (!may_block) 235257b69181STrond Myklebust goto out; 235357b69181STrond Myklebust spin_unlock(&inode->i_lock); 235457b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 235557b69181STrond Myklebust if (err) 235657b69181STrond Myklebust return err; 235757b69181STrond Myklebust spin_lock(&inode->i_lock); 235857b69181STrond Myklebust retry = false; 235957b69181STrond Myklebust } 23601c3c07e9STrond Myklebust res->cred = cache->cred; 23611c3c07e9STrond Myklebust res->mask = cache->mask; 2362cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23631c3c07e9STrond Myklebust err = 0; 23641c3c07e9STrond Myklebust out: 23651c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23661c3c07e9STrond Myklebust return err; 23671c3c07e9STrond Myklebust out_zap: 23681a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23691a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 23701c3c07e9STrond Myklebust return -ENOENT; 23711c3c07e9STrond Myklebust } 23721c3c07e9STrond Myklebust 2373b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2374f682a398SNeilBrown { 2375f682a398SNeilBrown /* Only check the most recently returned cache entry, 2376f682a398SNeilBrown * but do it without locking. 2377f682a398SNeilBrown */ 2378f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2379f682a398SNeilBrown struct nfs_access_entry *cache; 2380f682a398SNeilBrown int err = -ECHILD; 2381f682a398SNeilBrown struct list_head *lh; 2382f682a398SNeilBrown 2383f682a398SNeilBrown rcu_read_lock(); 2384f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2385f682a398SNeilBrown goto out; 2386f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2387f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2388f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 23899a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2390f682a398SNeilBrown cache = NULL; 2391f682a398SNeilBrown if (cache == NULL) 2392f682a398SNeilBrown goto out; 239321c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2394f682a398SNeilBrown goto out; 2395f682a398SNeilBrown res->cred = cache->cred; 2396f682a398SNeilBrown res->mask = cache->mask; 239721c3ba7eSTrond Myklebust err = 0; 2398f682a398SNeilBrown out: 2399f682a398SNeilBrown rcu_read_unlock(); 2400f682a398SNeilBrown return err; 2401f682a398SNeilBrown } 2402f682a398SNeilBrown 24031c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 24041c3c07e9STrond Myklebust { 2405cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2406cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24071c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 24081c3c07e9STrond Myklebust struct rb_node *parent = NULL; 24091c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2410b68572e0SNeilBrown int cmp; 24111c3c07e9STrond Myklebust 24121c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24131c3c07e9STrond Myklebust while (*p != NULL) { 24141c3c07e9STrond Myklebust parent = *p; 24151c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2416b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 24171c3c07e9STrond Myklebust 2418b68572e0SNeilBrown if (cmp < 0) 24191c3c07e9STrond Myklebust p = &parent->rb_left; 2420b68572e0SNeilBrown else if (cmp > 0) 24211c3c07e9STrond Myklebust p = &parent->rb_right; 24221c3c07e9STrond Myklebust else 24231c3c07e9STrond Myklebust goto found; 24241c3c07e9STrond Myklebust } 24251c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 24261c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2427cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 24281c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24291c3c07e9STrond Myklebust return; 24301c3c07e9STrond Myklebust found: 24311c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2432cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2433cfcea3e8STrond Myklebust list_del(&entry->lru); 24341c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24351c3c07e9STrond Myklebust nfs_access_free_entry(entry); 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 24386168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 24391da177e4SLinus Torvalds { 24401c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 24411c3c07e9STrond Myklebust if (cache == NULL) 24421c3c07e9STrond Myklebust return; 24431c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2444b68572e0SNeilBrown cache->cred = get_cred(set->cred); 24451da177e4SLinus Torvalds cache->mask = set->mask; 24461c3c07e9STrond Myklebust 2447f682a398SNeilBrown /* The above field assignments must be visible 2448f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2449f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2450f682a398SNeilBrown */ 2451f682a398SNeilBrown smp_wmb(); 24521c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2453cfcea3e8STrond Myklebust 2454cfcea3e8STrond Myklebust /* Update accounting */ 24554e857c58SPeter Zijlstra smp_mb__before_atomic(); 2456cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 24574e857c58SPeter Zijlstra smp_mb__after_atomic(); 2458cfcea3e8STrond Myklebust 2459cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24601a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2461cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24621a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24631a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24641a81bb8aSTrond Myklebust &nfs_access_lru_list); 2465cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2466cfcea3e8STrond Myklebust } 24673a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24681da177e4SLinus Torvalds } 24696168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 24706168f62cSWeston Andros Adamson 24713c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 24723c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24733c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 24743c181827SAnna Schumaker NFS_ACCESS_DELETE) 24753c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24763c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2477ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 24783c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 24793c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 248015d4b73aSTrond Myklebust static int 2481ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 248215d4b73aSTrond Myklebust { 248315d4b73aSTrond Myklebust int mask = 0; 248415d4b73aSTrond Myklebust 248515d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 248615d4b73aSTrond Myklebust mask |= MAY_READ; 2487ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2488ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 248915d4b73aSTrond Myklebust mask |= MAY_WRITE; 2490ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 249115d4b73aSTrond Myklebust mask |= MAY_EXEC; 2492ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2493ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2494ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2495ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 249615d4b73aSTrond Myklebust mask |= MAY_EXEC; 2497ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2498ecbb903cSTrond Myklebust mask |= MAY_WRITE; 249915d4b73aSTrond Myklebust return mask; 250015d4b73aSTrond Myklebust } 250115d4b73aSTrond Myklebust 25026168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 25036168f62cSWeston Andros Adamson { 2504bd8b2441STrond Myklebust entry->mask = access_result; 25056168f62cSWeston Andros Adamson } 25066168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 25071da177e4SLinus Torvalds 2508b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 25091da177e4SLinus Torvalds { 25101da177e4SLinus Torvalds struct nfs_access_entry cache; 251157b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2512e8194b7dSTrond Myklebust int cache_mask = -1; 25131da177e4SLinus Torvalds int status; 25141da177e4SLinus Torvalds 2515f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2516f4ce1299STrond Myklebust 2517f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2518f682a398SNeilBrown if (status != 0) 251957b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 25201da177e4SLinus Torvalds if (status == 0) 2521f4ce1299STrond Myklebust goto out_cached; 25221da177e4SLinus Torvalds 2523f3324a2aSNeilBrown status = -ECHILD; 252457b69181STrond Myklebust if (!may_block) 2525f3324a2aSNeilBrown goto out; 2526f3324a2aSNeilBrown 25271750d929SAnna Schumaker /* 25281750d929SAnna Schumaker * Determine which access bits we want to ask for... 25291750d929SAnna Schumaker */ 25301750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 25311750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 25321750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 25331750d929SAnna Schumaker else 25341750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 25351da177e4SLinus Torvalds cache.cred = cred; 25361da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2537a71ee337SSuresh Jayaraman if (status != 0) { 2538a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2539a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2540a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2541a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2542a71ee337SSuresh Jayaraman } 2543f4ce1299STrond Myklebust goto out; 2544a71ee337SSuresh Jayaraman } 25451da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2546f4ce1299STrond Myklebust out_cached: 2547ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2548bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2549f4ce1299STrond Myklebust status = -EACCES; 25501da177e4SLinus Torvalds out: 2551e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2552f4ce1299STrond Myklebust return status; 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 2555af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2556af22f94aSTrond Myklebust { 2557af22f94aSTrond Myklebust int mask = 0; 2558af22f94aSTrond Myklebust 2559f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2560f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2561f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2562f8d9a897SWeston Andros Adamson } else { 25638a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2564af22f94aSTrond Myklebust mask |= MAY_READ; 25658a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2566af22f94aSTrond Myklebust mask |= MAY_WRITE; 2567f8d9a897SWeston Andros Adamson } 2568f8d9a897SWeston Andros Adamson 2569af22f94aSTrond Myklebust return mask; 2570af22f94aSTrond Myklebust } 2571af22f94aSTrond Myklebust 2572b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2573af22f94aSTrond Myklebust { 2574af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2575af22f94aSTrond Myklebust } 257689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2577af22f94aSTrond Myklebust 25785c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 25795c5fc09aSTrond Myklebust { 25805c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 258121c3ba7eSTrond Myklebust int ret = 0; 25825c5fc09aSTrond Myklebust 25833825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 25843825827eSTrond Myklebust return 0; 2585cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 25865c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 258721c3ba7eSTrond Myklebust return -ECHILD; 258821c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 258921c3ba7eSTrond Myklebust } 25905c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 25915c5fc09aSTrond Myklebust ret = -EACCES; 25925c5fc09aSTrond Myklebust return ret; 25935c5fc09aSTrond Myklebust } 25945c5fc09aSTrond Myklebust 259510556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 25961da177e4SLinus Torvalds { 2597b68572e0SNeilBrown const struct cred *cred = current_cred(); 25981da177e4SLinus Torvalds int res = 0; 25991da177e4SLinus Torvalds 260091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 260191d5b470SChuck Lever 2602e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 26031da177e4SLinus Torvalds goto out; 26041da177e4SLinus Torvalds /* Is this sys_access() ? */ 26059cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 26061da177e4SLinus Torvalds goto force_lookup; 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 26091da177e4SLinus Torvalds case S_IFLNK: 26101da177e4SLinus Torvalds goto out; 26111da177e4SLinus Torvalds case S_IFREG: 2612762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2613762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2614762674f8STrond Myklebust return 0; 26151da177e4SLinus Torvalds break; 26161da177e4SLinus Torvalds case S_IFDIR: 26171da177e4SLinus Torvalds /* 26181da177e4SLinus Torvalds * Optimize away all write operations, since the server 26191da177e4SLinus Torvalds * will check permissions when we perform the op. 26201da177e4SLinus Torvalds */ 26211da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 26221da177e4SLinus Torvalds goto out; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds force_lookup: 26261da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 26271da177e4SLinus Torvalds goto out_notsup; 26281da177e4SLinus Torvalds 2629f3324a2aSNeilBrown /* Always try fast lookups first */ 2630f3324a2aSNeilBrown rcu_read_lock(); 2631f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2632f3324a2aSNeilBrown rcu_read_unlock(); 2633f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2634f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 26351da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2636f3324a2aSNeilBrown } 26371da177e4SLinus Torvalds out: 26385c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 26395c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2640f696a365SMiklos Szeredi 26411e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 26421e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 26431da177e4SLinus Torvalds return res; 26441da177e4SLinus Torvalds out_notsup: 2645d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2646d51ac1a8SNeilBrown return -ECHILD; 2647d51ac1a8SNeilBrown 26481da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 26491da177e4SLinus Torvalds if (res == 0) 26502830ba7fSAl Viro res = generic_permission(inode, mask); 26511e7cb3dcSChuck Lever goto out; 26521da177e4SLinus Torvalds } 2653ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds /* 26561da177e4SLinus Torvalds * Local variables: 26571da177e4SLinus Torvalds * version-control: t 26581da177e4SLinus Torvalds * kept-new-versions: 5 26591da177e4SLinus Torvalds * End: 26601da177e4SLinus Torvalds */ 2661