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, 61b044f645SBenjamin Coddington .iterate = 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 165*4b310319STrond Myklebust static 166*4b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 167*4b310319STrond Myklebust { 168*4b310319STrond Myklebust struct nfs_cache_array *array; 169*4b310319STrond Myklebust 170*4b310319STrond Myklebust array = kmap_atomic(page); 171*4b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 172*4b310319STrond Myklebust array->eof_index = -1; 173*4b310319STrond Myklebust kunmap_atomic(array); 174*4b310319STrond Myklebust } 175*4b310319STrond 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); 188*4b310319STrond 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; 201d1bacf9eSBryan Schumaker string->name = kmemdup(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); 45279f687a3STrond Myklebust invalidate_mapping_pages(dir->i_mapping, 0, -1); 453311324adSTrond Myklebust } 454311324adSTrond Myklebust } 455311324adSTrond Myklebust 456d69ee9b8STrond Myklebust static 457d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 458d39ab9deSBryan Schumaker { 45926fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4609ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4614a201d6eSTrond Myklebust struct dentry *dentry; 4624a201d6eSTrond Myklebust struct dentry *alias; 4632b0143b5SDavid Howells struct inode *dir = d_inode(parent); 464d39ab9deSBryan Schumaker struct inode *inode; 465aa9c2669SDavid Quigley int status; 466d39ab9deSBryan Schumaker 467fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 468fa923369STrond Myklebust return; 4696c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4706c441c25STrond Myklebust return; 47178d04af4STrond Myklebust if (filename.len == 0) 47278d04af4STrond Myklebust return; 47378d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 47478d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 47578d04af4STrond Myklebust return; 47678d04af4STrond Myklebust /* ...or '/' */ 47778d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 47878d04af4STrond Myklebust return; 4794a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4804a201d6eSTrond Myklebust if (filename.len == 1) 4814a201d6eSTrond Myklebust return; 4824a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4834a201d6eSTrond Myklebust return; 4844a201d6eSTrond Myklebust } 4858387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 486d39ab9deSBryan Schumaker 4874a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 4889ac3d3e8SAl Viro again: 4899ac3d3e8SAl Viro if (!dentry) { 4909ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 4919ac3d3e8SAl Viro if (IS_ERR(dentry)) 4929ac3d3e8SAl Viro return; 4939ac3d3e8SAl Viro } 4949ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 4956c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4966c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 4976c441c25STrond Myklebust &entry->fattr->fsid)) 4986c441c25STrond Myklebust goto out; 499d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 5007dc72d5fSTrond Myklebust if (!entry->fh->size) 5017dc72d5fSTrond Myklebust goto out; 502cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 5032b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 504aa9c2669SDavid Quigley if (!status) 5052b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 506d39ab9deSBryan Schumaker goto out; 507d39ab9deSBryan Schumaker } else { 5085542aa2fSEric W. Biederman d_invalidate(dentry); 509d39ab9deSBryan Schumaker dput(dentry); 5109ac3d3e8SAl Viro dentry = NULL; 5119ac3d3e8SAl Viro goto again; 512d39ab9deSBryan Schumaker } 513d39ab9deSBryan Schumaker } 5147dc72d5fSTrond Myklebust if (!entry->fh->size) { 5157dc72d5fSTrond Myklebust d_lookup_done(dentry); 5167dc72d5fSTrond Myklebust goto out; 5177dc72d5fSTrond Myklebust } 518d39ab9deSBryan Schumaker 5191775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 52041d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5219ac3d3e8SAl Viro d_lookup_done(dentry); 5229ac3d3e8SAl Viro if (alias) { 523d39ab9deSBryan Schumaker if (IS_ERR(alias)) 524d39ab9deSBryan Schumaker goto out; 5259ac3d3e8SAl Viro dput(dentry); 5269ac3d3e8SAl Viro dentry = alias; 5279ac3d3e8SAl Viro } 528d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 529d39ab9deSBryan Schumaker out: 530d39ab9deSBryan Schumaker dput(dentry); 531d39ab9deSBryan Schumaker } 532d39ab9deSBryan Schumaker 533d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 534d1bacf9eSBryan Schumaker static 5358cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5366650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 537d1bacf9eSBryan Schumaker { 538babddc72SBryan Schumaker struct xdr_stream stream; 539f7da7a12SBenny Halevy struct xdr_buf buf; 5406650239aSTrond Myklebust struct page *scratch; 54199424380SBryan Schumaker struct nfs_cache_array *array; 5425c346854STrond Myklebust unsigned int count = 0; 5435c346854STrond Myklebust int status; 544babddc72SBryan Schumaker 5456650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5466650239aSTrond Myklebust if (scratch == NULL) 5476650239aSTrond Myklebust return -ENOMEM; 548babddc72SBryan Schumaker 549ce85cfbeSBenjamin Coddington if (buflen == 0) 550ce85cfbeSBenjamin Coddington goto out_nopages; 551ce85cfbeSBenjamin Coddington 552f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5536650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 55499424380SBryan Schumaker 55599424380SBryan Schumaker do { 55699424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5578cd51a0cSTrond Myklebust if (status != 0) { 5588cd51a0cSTrond Myklebust if (status == -EAGAIN) 5598cd51a0cSTrond Myklebust status = 0; 56099424380SBryan Schumaker break; 5618cd51a0cSTrond Myklebust } 56299424380SBryan Schumaker 5635c346854STrond Myklebust count++; 5645c346854STrond Myklebust 565a7a3b1e9SBenjamin Coddington if (desc->plus) 566be62a1a8SMiklos Szeredi nfs_prime_dcache(file_dentry(desc->file), entry); 5678cd51a0cSTrond Myklebust 568db531db9SMax Kellermann status = nfs_readdir_add_to_array(entry, page); 5698cd51a0cSTrond Myklebust if (status != 0) 5708cd51a0cSTrond Myklebust break; 57199424380SBryan Schumaker } while (!entry->eof); 57299424380SBryan Schumaker 573ce85cfbeSBenjamin Coddington out_nopages: 57447c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 575db531db9SMax Kellermann array = kmap(page); 5768cd51a0cSTrond Myklebust array->eof_index = array->size; 57799424380SBryan Schumaker status = 0; 578db531db9SMax Kellermann kunmap(page); 57956e4ebf8SBryan Schumaker } 5806650239aSTrond Myklebust 5816650239aSTrond Myklebust put_page(scratch); 5828cd51a0cSTrond Myklebust return status; 5838cd51a0cSTrond Myklebust } 58456e4ebf8SBryan Schumaker 58556e4ebf8SBryan Schumaker static 586c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 58756e4ebf8SBryan Schumaker { 58856e4ebf8SBryan Schumaker unsigned int i; 58956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 59056e4ebf8SBryan Schumaker put_page(pages[i]); 59156e4ebf8SBryan Schumaker } 59256e4ebf8SBryan Schumaker 59356e4ebf8SBryan Schumaker /* 594bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 595bf211ca1Szhangliguang * to nfs_readdir_free_pages() 59656e4ebf8SBryan Schumaker */ 59756e4ebf8SBryan Schumaker static 598c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 59956e4ebf8SBryan Schumaker { 60056e4ebf8SBryan Schumaker unsigned int i; 60156e4ebf8SBryan Schumaker 60256e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 60356e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 60456e4ebf8SBryan Schumaker if (page == NULL) 60556e4ebf8SBryan Schumaker goto out_freepages; 60656e4ebf8SBryan Schumaker pages[i] = page; 60756e4ebf8SBryan Schumaker } 6086650239aSTrond Myklebust return 0; 60956e4ebf8SBryan Schumaker 61056e4ebf8SBryan Schumaker out_freepages: 611c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6126650239aSTrond Myklebust return -ENOMEM; 613d1bacf9eSBryan Schumaker } 614d1bacf9eSBryan Schumaker 615d1bacf9eSBryan Schumaker static 616d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 617d1bacf9eSBryan Schumaker { 61856e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 619d1bacf9eSBryan Schumaker struct nfs_entry entry; 620d1bacf9eSBryan Schumaker struct file *file = desc->file; 621d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6228cd51a0cSTrond Myklebust int status = -ENOMEM; 62356e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 624d1bacf9eSBryan Schumaker 625*4b310319STrond Myklebust nfs_readdir_init_array(page); 626*4b310319STrond Myklebust 627d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6280aded708STrond Myklebust entry.cookie = desc->last_cookie; 629d1bacf9eSBryan Schumaker entry.eof = 0; 630d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 631d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 632573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 633d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 634d1bacf9eSBryan Schumaker goto out; 635d1bacf9eSBryan Schumaker 63614c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 63714c43f76SDavid Quigley if (IS_ERR(entry.label)) { 63814c43f76SDavid Quigley status = PTR_ERR(entry.label); 63914c43f76SDavid Quigley goto out; 64014c43f76SDavid Quigley } 64114c43f76SDavid Quigley 6420795bf83SFabian Frederick array = kmap(page); 643d1bacf9eSBryan Schumaker 644c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6456650239aSTrond Myklebust if (status < 0) 646d1bacf9eSBryan Schumaker goto out_release_array; 647d1bacf9eSBryan Schumaker do { 648ac396128STrond Myklebust unsigned int pglen; 64956e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 650babddc72SBryan Schumaker 651d1bacf9eSBryan Schumaker if (status < 0) 652d1bacf9eSBryan Schumaker break; 653ac396128STrond Myklebust pglen = status; 6546650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6558cd51a0cSTrond Myklebust if (status < 0) { 6568cd51a0cSTrond Myklebust if (status == -ENOSPC) 6578cd51a0cSTrond Myklebust status = 0; 6588cd51a0cSTrond Myklebust break; 6598cd51a0cSTrond Myklebust } 6608cd51a0cSTrond Myklebust } while (array->eof_index < 0); 661d1bacf9eSBryan Schumaker 662c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 663d1bacf9eSBryan Schumaker out_release_array: 6640795bf83SFabian Frederick kunmap(page); 66514c43f76SDavid Quigley nfs4_label_free(entry.label); 666d1bacf9eSBryan Schumaker out: 667d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 668d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 669d1bacf9eSBryan Schumaker return status; 670d1bacf9eSBryan Schumaker } 671d1bacf9eSBryan Schumaker 672d1bacf9eSBryan Schumaker /* 673d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 674d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 675d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 676d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6771da177e4SLinus Torvalds */ 678d1bacf9eSBryan Schumaker static 679a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page) 680d1bacf9eSBryan Schumaker { 681a46126ccSChristoph Hellwig nfs_readdir_descriptor_t *desc = data; 682496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6838cd51a0cSTrond Myklebust int ret; 684d1bacf9eSBryan Schumaker 6858cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6868cd51a0cSTrond Myklebust if (ret < 0) 687d1bacf9eSBryan Schumaker goto error; 688d1bacf9eSBryan Schumaker SetPageUptodate(page); 689d1bacf9eSBryan Schumaker 6902aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 691cd9ae2b6STrond Myklebust /* Should never happen */ 692cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 693cd9ae2b6STrond Myklebust } 6941da177e4SLinus Torvalds unlock_page(page); 6951da177e4SLinus Torvalds return 0; 6961da177e4SLinus Torvalds error: 697*4b310319STrond Myklebust nfs_readdir_clear_array(page); 6981da177e4SLinus Torvalds unlock_page(page); 6998cd51a0cSTrond Myklebust return ret; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 702d1bacf9eSBryan Schumaker static 703d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7041da177e4SLinus Torvalds { 705b044f645SBenjamin Coddington if (!desc->page->mapping) 70611de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 70709cbfeafSKirill A. Shutemov put_page(desc->page); 7081da177e4SLinus Torvalds desc->page = NULL; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 711d1bacf9eSBryan Schumaker static 712d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7131da177e4SLinus Torvalds { 714a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 715a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds /* 719d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7201da177e4SLinus Torvalds */ 721d1bacf9eSBryan Schumaker static 722d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 723d1bacf9eSBryan Schumaker { 724d1bacf9eSBryan Schumaker int res; 725d1bacf9eSBryan Schumaker 726d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 727d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 728d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 729d1bacf9eSBryan Schumaker 730d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 73147c716cbSTrond Myklebust if (res != 0) 732d1bacf9eSBryan Schumaker cache_page_release(desc); 733d1bacf9eSBryan Schumaker return res; 734d1bacf9eSBryan Schumaker } 735d1bacf9eSBryan Schumaker 736d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7371da177e4SLinus Torvalds static inline 7381da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7391da177e4SLinus Torvalds { 7408cd51a0cSTrond Myklebust int res; 741d1bacf9eSBryan Schumaker 7420aded708STrond Myklebust if (desc->page_index == 0) { 7438cd51a0cSTrond Myklebust desc->current_index = 0; 7440aded708STrond Myklebust desc->last_cookie = 0; 7450aded708STrond Myklebust } 74647c716cbSTrond Myklebust do { 747d1bacf9eSBryan Schumaker res = find_cache_page(desc); 74847c716cbSTrond Myklebust } while (res == -EAGAIN); 7491da177e4SLinus Torvalds return res; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* 7531da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7541da177e4SLinus Torvalds */ 7551da177e4SLinus Torvalds static 75623db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7571da177e4SLinus Torvalds { 7581da177e4SLinus Torvalds struct file *file = desc->file; 759d1bacf9eSBryan Schumaker int i = 0; 760d1bacf9eSBryan Schumaker int res = 0; 761d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7628ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7638ef2ce3eSBryan Schumaker 7640795bf83SFabian Frederick array = kmap(desc->page); 765d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 766ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7671da177e4SLinus Torvalds 768ece0b423STrond Myklebust ent = &array->array[i]; 76923db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 77023db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 7716089dd0dSThomas Meyer desc->eof = true; 7721da177e4SLinus Torvalds break; 773ece0b423STrond Myklebust } 77423db8620SAl Viro desc->ctx->pos++; 775d1bacf9eSBryan Schumaker if (i < (array->size-1)) 776d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 777d1bacf9eSBryan Schumaker else 778d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7790c030806STrond Myklebust if (ctx->duped != 0) 7800c030806STrond Myklebust ctx->duped = 1; 7818cd51a0cSTrond Myklebust } 78247c716cbSTrond Myklebust if (array->eof_index >= 0) 7836089dd0dSThomas Meyer desc->eof = true; 784d1bacf9eSBryan Schumaker 7850795bf83SFabian Frederick kunmap(desc->page); 786d1bacf9eSBryan Schumaker cache_page_release(desc); 7871e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7881e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7891da177e4SLinus Torvalds return res; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* 7931da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 7941da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 7951da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 7961da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 7971da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 7981da177e4SLinus Torvalds * 7991da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8001da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8011da177e4SLinus Torvalds * we should already have a complete representation of the 8021da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8031da177e4SLinus Torvalds */ 8041da177e4SLinus Torvalds static inline 80523db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8061da177e4SLinus Torvalds { 8071da177e4SLinus Torvalds struct page *page = NULL; 8081da177e4SLinus Torvalds int status; 809496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8100c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8111da177e4SLinus Torvalds 8121e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8131e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8161da177e4SLinus Torvalds if (!page) { 8171da177e4SLinus Torvalds status = -ENOMEM; 8181da177e4SLinus Torvalds goto out; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8217a8e1dc3STrond Myklebust desc->page_index = 0; 8220aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8237a8e1dc3STrond Myklebust desc->page = page; 8240c030806STrond Myklebust ctx->duped = 0; 8257a8e1dc3STrond Myklebust 82685f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 82785f8607eSTrond Myklebust if (status < 0) 828d1bacf9eSBryan Schumaker goto out_release; 829d1bacf9eSBryan Schumaker 83023db8620SAl Viro status = nfs_do_filldir(desc); 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds out: 8331e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8343110ff80SHarvey Harrison __func__, status); 8351da177e4SLinus Torvalds return status; 8361da177e4SLinus Torvalds out_release: 837d1bacf9eSBryan Schumaker cache_page_release(desc); 8381da177e4SLinus Torvalds goto out; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds 84100a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 84200a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 84300a92642SOlivier Galibert whole directory. 8441da177e4SLinus Torvalds */ 84523db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8461da177e4SLinus Torvalds { 847be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8482b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8491da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8501da177e4SLinus Torvalds *desc = &my_desc; 85123db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 85207b5ce8eSScott Mayhew int res = 0; 8531da177e4SLinus Torvalds 8546de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8556de1472fSAl Viro file, (long long)ctx->pos); 85691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 85791d5b470SChuck Lever 8581da177e4SLinus Torvalds /* 85923db8620SAl Viro * ctx->pos points to the dirent entry number. 860f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 86100a92642SOlivier Galibert * to either find the entry with the appropriate number or 86200a92642SOlivier Galibert * revalidate the cookie. 8631da177e4SLinus Torvalds */ 8641da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8651da177e4SLinus Torvalds 86623db8620SAl Viro desc->file = file; 86723db8620SAl Viro desc->ctx = ctx; 868480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8691da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 870a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 8711da177e4SLinus Torvalds 87279f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 87323db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 874fccca7fcSTrond Myklebust if (res < 0) 875fccca7fcSTrond Myklebust goto out; 876fccca7fcSTrond Myklebust 87747c716cbSTrond Myklebust do { 8781da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 87900a92642SOlivier Galibert 8801da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 881ece0b423STrond Myklebust res = 0; 8821da177e4SLinus Torvalds /* This means either end of directory */ 8836089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 8841da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 88523db8620SAl Viro res = uncached_readdir(desc); 886ece0b423STrond Myklebust if (res == 0) 8871da177e4SLinus Torvalds continue; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds break; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8923a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8931da177e4SLinus Torvalds nfs_zap_caches(inode); 894baf57a09STrond Myklebust desc->page_index = 0; 895a7a3b1e9SBenjamin Coddington desc->plus = false; 896a7a3b1e9SBenjamin Coddington desc->eof = false; 8971da177e4SLinus Torvalds continue; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds if (res < 0) 9001da177e4SLinus Torvalds break; 9011da177e4SLinus Torvalds 90223db8620SAl Viro res = nfs_do_filldir(desc); 903ece0b423STrond Myklebust if (res < 0) 9041da177e4SLinus Torvalds break; 90547c716cbSTrond Myklebust } while (!desc->eof); 906fccca7fcSTrond Myklebust out: 9071e7cb3dcSChuck Lever if (res > 0) 9081e7cb3dcSChuck Lever res = 0; 9096de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9101da177e4SLinus Torvalds return res; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 913965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 914f0dd2136STrond Myklebust { 915b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 916480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 917b84e06c5SChuck Lever 9186de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9196de1472fSAl Viro filp, offset, whence); 920b84e06c5SChuck Lever 921965c8e59SAndrew Morton switch (whence) { 922f0dd2136STrond Myklebust default: 923b2b1ff3dSTrond Myklebust return -EINVAL; 924b2b1ff3dSTrond Myklebust case SEEK_SET: 925b2b1ff3dSTrond Myklebust if (offset < 0) 926b2b1ff3dSTrond Myklebust return -EINVAL; 927b2b1ff3dSTrond Myklebust inode_lock(inode); 928b2b1ff3dSTrond Myklebust break; 929b2b1ff3dSTrond Myklebust case SEEK_CUR: 930b2b1ff3dSTrond Myklebust if (offset == 0) 931b2b1ff3dSTrond Myklebust return filp->f_pos; 932b2b1ff3dSTrond Myklebust inode_lock(inode); 933b2b1ff3dSTrond Myklebust offset += filp->f_pos; 934b2b1ff3dSTrond Myklebust if (offset < 0) { 935b2b1ff3dSTrond Myklebust inode_unlock(inode); 936b2b1ff3dSTrond Myklebust return -EINVAL; 937b2b1ff3dSTrond Myklebust } 938f0dd2136STrond Myklebust } 939f0dd2136STrond Myklebust if (offset != filp->f_pos) { 940f0dd2136STrond Myklebust filp->f_pos = offset; 941480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9428ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 943f0dd2136STrond Myklebust } 944b044f645SBenjamin Coddington inode_unlock(inode); 945f0dd2136STrond Myklebust return offset; 946f0dd2136STrond Myklebust } 947f0dd2136STrond Myklebust 9481da177e4SLinus Torvalds /* 9491da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9501da177e4SLinus Torvalds * is a dummy operation. 9511da177e4SLinus Torvalds */ 95202c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 95302c24a82SJosef Bacik int datasync) 9541da177e4SLinus Torvalds { 9556de1472fSAl Viro struct inode *inode = file_inode(filp); 9567ea80859SChristoph Hellwig 9576de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9581e7cb3dcSChuck Lever 9595955102cSAl Viro inode_lock(inode); 9606de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9615955102cSAl Viro inode_unlock(inode); 9621da177e4SLinus Torvalds return 0; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 965bfc69a45STrond Myklebust /** 966bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 967302fad7bSTrond Myklebust * @dir: pointer to directory inode 968bfc69a45STrond Myklebust * 969bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 970bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 971bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 972bfc69a45STrond Myklebust * 973bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 974bfc69a45STrond Myklebust */ 975bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 976bfc69a45STrond Myklebust { 977011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 978bfc69a45STrond Myklebust } 97989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 980bfc69a45STrond Myklebust 9811da177e4SLinus Torvalds /* 9821da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9831da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9841da177e4SLinus Torvalds * and may need to be looked up again. 985912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 9861da177e4SLinus Torvalds */ 987912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 988912a108dSNeilBrown int rcu_walk) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9911da177e4SLinus Torvalds return 1; 9924eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 9934eec952eSTrond Myklebust return 0; 994f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 9956ecc5e8fSTrond Myklebust return 0; 996f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 9971cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 998912a108dSNeilBrown if (rcu_walk) 999f2c77f4eSTrond Myklebust return 0; 10001cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 10011cd9cb05STrond Myklebust return 0; 10021cd9cb05STrond Myklebust } 1003f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1004f2c77f4eSTrond Myklebust return 0; 1005f2c77f4eSTrond Myklebust return 1; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* 1009a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1010a12802caSTrond Myklebust * an O_EXCL create using this path component. 1011a12802caSTrond Myklebust */ 1012fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1013a12802caSTrond Myklebust { 1014a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1015a12802caSTrond Myklebust return 0; 1016fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1017a12802caSTrond Myklebust } 1018a12802caSTrond Myklebust 1019a12802caSTrond Myklebust /* 10201d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10211d6757fbSTrond Myklebust * 10221d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10231d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10241d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10251d6757fbSTrond Myklebust * 10261d6757fbSTrond Myklebust */ 102765a0c149STrond Myklebust static 1028fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 103165a0c149STrond Myklebust int ret; 10321da177e4SLinus Torvalds 103336d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10344e99a1ffSTrond Myklebust return 0; 103547921921STrond Myklebust 103647921921STrond Myklebust if (flags & LOOKUP_OPEN) { 103747921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 103847921921STrond Myklebust case S_IFREG: 103947921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 104047921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 104147921921STrond Myklebust goto out; 104247921921STrond Myklebust /* Fallthrough */ 104347921921STrond Myklebust case S_IFDIR: 104447921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 104547921921STrond Myklebust break; 104647921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 104747921921STrond Myklebust goto out_force; 104847921921STrond Myklebust } 104947921921STrond Myklebust } 105047921921STrond Myklebust 10511da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1052fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10531da177e4SLinus Torvalds goto out_force; 105465a0c149STrond Myklebust out: 1055a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 10561da177e4SLinus Torvalds out_force: 10571fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10581fa1e384SNeilBrown return -ECHILD; 105965a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 106065a0c149STrond Myklebust if (ret != 0) 106165a0c149STrond Myklebust return ret; 106265a0c149STrond Myklebust goto out; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds /* 10661da177e4SLinus Torvalds * We judge how long we want to trust negative 10671da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10681da177e4SLinus Torvalds * 10691da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10701da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1071912a108dSNeilBrown * 1072912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1073912a108dSNeilBrown * suggesting a reval is needed. 10749f6d44d4STrond Myklebust * 10759f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 10769f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 10771da177e4SLinus Torvalds */ 10781da177e4SLinus Torvalds static inline 10791da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1080fa3c56bbSAl Viro unsigned int flags) 10811da177e4SLinus Torvalds { 10829f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 10831da177e4SLinus Torvalds return 0; 10844eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10854eec952eSTrond Myklebust return 1; 1086912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10895ceb9d7fSTrond Myklebust static int 10905ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 10915ceb9d7fSTrond Myklebust struct inode *inode, int error) 10921da177e4SLinus Torvalds { 10935ceb9d7fSTrond Myklebust switch (error) { 10945ceb9d7fSTrond Myklebust case 1: 10956de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 10966de1472fSAl Viro __func__, dentry); 10971da177e4SLinus Torvalds return 1; 10985ceb9d7fSTrond Myklebust case 0: 1099a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11001da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11011da177e4SLinus Torvalds /* Purge readdir caches. */ 11021da177e4SLinus Torvalds nfs_zap_caches(inode); 1103a3f432bfSJ. Bruce Fields /* 1104a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1105a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1106a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1107a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1108a3f432bfSJ. Bruce Fields */ 1109a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 11105ceb9d7fSTrond Myklebust return 1; 11111da177e4SLinus Torvalds } 11126de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 11136de1472fSAl Viro __func__, dentry); 11141da177e4SLinus Torvalds return 0; 11155ceb9d7fSTrond Myklebust } 11166de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 11176de1472fSAl Viro __func__, dentry, error); 1118e1fb4d05STrond Myklebust return error; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11215ceb9d7fSTrond Myklebust static int 11225ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 11235ceb9d7fSTrond Myklebust unsigned int flags) 11245ceb9d7fSTrond Myklebust { 11255ceb9d7fSTrond Myklebust int ret = 1; 11265ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 11275ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 11285ceb9d7fSTrond Myklebust return -ECHILD; 11295ceb9d7fSTrond Myklebust ret = 0; 11305ceb9d7fSTrond Myklebust } 11315ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 11325ceb9d7fSTrond Myklebust } 11335ceb9d7fSTrond Myklebust 11345ceb9d7fSTrond Myklebust static int 11355ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 11365ceb9d7fSTrond Myklebust struct inode *inode) 11375ceb9d7fSTrond Myklebust { 11385ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11395ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 11405ceb9d7fSTrond Myklebust } 11415ceb9d7fSTrond Myklebust 11425ceb9d7fSTrond Myklebust static int 11435ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 11445ceb9d7fSTrond Myklebust struct inode *inode) 11455ceb9d7fSTrond Myklebust { 11465ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 11475ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 11485ceb9d7fSTrond Myklebust struct nfs4_label *label; 11495ceb9d7fSTrond Myklebust int ret; 11505ceb9d7fSTrond Myklebust 11515ceb9d7fSTrond Myklebust ret = -ENOMEM; 11525ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 11535ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 11545ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 11555ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 11565ceb9d7fSTrond Myklebust goto out; 11575ceb9d7fSTrond Myklebust 1158f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 11595ceb9d7fSTrond Myklebust if (ret < 0) { 1160f7b37b8bSTrond Myklebust switch (ret) { 1161f7b37b8bSTrond Myklebust case -ESTALE: 1162f7b37b8bSTrond Myklebust case -ENOENT: 11635ceb9d7fSTrond Myklebust ret = 0; 1164f7b37b8bSTrond Myklebust break; 1165f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1166f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1167f7b37b8bSTrond Myklebust ret = 1; 1168f7b37b8bSTrond Myklebust } 11695ceb9d7fSTrond Myklebust goto out; 11705ceb9d7fSTrond Myklebust } 11715ceb9d7fSTrond Myklebust ret = 0; 11725ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11735ceb9d7fSTrond Myklebust goto out; 11745ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 11755ceb9d7fSTrond Myklebust goto out; 11765ceb9d7fSTrond Myklebust 11775ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 11785ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11795ceb9d7fSTrond Myklebust 11805ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 11815ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 11825ceb9d7fSTrond Myklebust ret = 1; 11835ceb9d7fSTrond Myklebust out: 11845ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 11855ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 11865ceb9d7fSTrond Myklebust nfs4_label_free(label); 11875ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 11885ceb9d7fSTrond Myklebust } 11895ceb9d7fSTrond Myklebust 11905ceb9d7fSTrond Myklebust /* 11915ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 11925ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 11935ceb9d7fSTrond Myklebust * lookup. 11945ceb9d7fSTrond Myklebust * 11955ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 11965ceb9d7fSTrond Myklebust * we have an inode! 11975ceb9d7fSTrond Myklebust * 11985ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 11995ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 12005ceb9d7fSTrond Myklebust */ 12015ceb9d7fSTrond Myklebust static int 12025ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 12035ceb9d7fSTrond Myklebust unsigned int flags) 12045ceb9d7fSTrond Myklebust { 12055ceb9d7fSTrond Myklebust struct inode *inode; 12065ceb9d7fSTrond Myklebust int error; 12075ceb9d7fSTrond Myklebust 12085ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 12095ceb9d7fSTrond Myklebust inode = d_inode(dentry); 12105ceb9d7fSTrond Myklebust 12115ceb9d7fSTrond Myklebust if (!inode) 12125ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 12135ceb9d7fSTrond Myklebust 12145ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 12155ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12165ceb9d7fSTrond Myklebust __func__, dentry); 12175ceb9d7fSTrond Myklebust goto out_bad; 12185ceb9d7fSTrond Myklebust } 12195ceb9d7fSTrond Myklebust 12205ceb9d7fSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 12215ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 12225ceb9d7fSTrond Myklebust 12235ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 12245ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 12255ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 12265ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 12275ceb9d7fSTrond Myklebust if (error) { 12285ceb9d7fSTrond Myklebust if (error == -ESTALE) 12295ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 12305ceb9d7fSTrond Myklebust goto out_bad; 12315ceb9d7fSTrond Myklebust } 12325ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 12335ceb9d7fSTrond Myklebust goto out_valid; 12345ceb9d7fSTrond Myklebust } 12355ceb9d7fSTrond Myklebust 12365ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12375ceb9d7fSTrond Myklebust return -ECHILD; 12385ceb9d7fSTrond Myklebust 12395ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 12405ceb9d7fSTrond Myklebust goto out_bad; 12415ceb9d7fSTrond Myklebust 12425ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 12435ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 12445ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 12455ceb9d7fSTrond Myklebust return error; 12465ceb9d7fSTrond Myklebust out_valid: 12475ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12485ceb9d7fSTrond Myklebust out_bad: 12495ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12505ceb9d7fSTrond Myklebust return -ECHILD; 12515ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 12525ceb9d7fSTrond Myklebust } 12535ceb9d7fSTrond Myklebust 12545ceb9d7fSTrond Myklebust static int 1255c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1256c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 12575ceb9d7fSTrond Myklebust { 12585ceb9d7fSTrond Myklebust struct dentry *parent; 12595ceb9d7fSTrond Myklebust struct inode *dir; 12605ceb9d7fSTrond Myklebust int ret; 12615ceb9d7fSTrond Myklebust 12625ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 12635ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 12645ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 12655ceb9d7fSTrond Myklebust if (!dir) 12665ceb9d7fSTrond Myklebust return -ECHILD; 1267c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 12685ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 12695ceb9d7fSTrond Myklebust return -ECHILD; 12705ceb9d7fSTrond Myklebust } else { 12715ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1272c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 12735ceb9d7fSTrond Myklebust dput(parent); 12745ceb9d7fSTrond Myklebust } 12755ceb9d7fSTrond Myklebust return ret; 12765ceb9d7fSTrond Myklebust } 12775ceb9d7fSTrond Myklebust 1278c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1279c7944ebbSTrond Myklebust { 1280c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1281c7944ebbSTrond Myklebust } 1282c7944ebbSTrond Myklebust 12831da177e4SLinus Torvalds /* 12842b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1285ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1286ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1287ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1288ecf3d1f1SJeff Layton * 1289ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1290ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1291ecf3d1f1SJeff Layton */ 1292ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1293ecf3d1f1SJeff Layton { 12942b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 12959cdd1d3fSTrond Myklebust int error = 0; 1296ecf3d1f1SJeff Layton 1297ecf3d1f1SJeff Layton /* 1298ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1299ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1300ecf3d1f1SJeff Layton * eventually need to do something more here. 1301ecf3d1f1SJeff Layton */ 1302ecf3d1f1SJeff Layton if (!inode) { 13036de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 13046de1472fSAl Viro __func__, dentry); 1305ecf3d1f1SJeff Layton return 1; 1306ecf3d1f1SJeff Layton } 1307ecf3d1f1SJeff Layton 1308ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 13096de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13106de1472fSAl Viro __func__, dentry); 1311ecf3d1f1SJeff Layton return 0; 1312ecf3d1f1SJeff Layton } 1313ecf3d1f1SJeff Layton 1314b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1315ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1316ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1317ecf3d1f1SJeff Layton return !error; 1318ecf3d1f1SJeff Layton } 1319ecf3d1f1SJeff Layton 1320ecf3d1f1SJeff Layton /* 13211da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 13221da177e4SLinus Torvalds */ 1323fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 13241da177e4SLinus Torvalds { 13256de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13266de1472fSAl Viro dentry, dentry->d_flags); 13271da177e4SLinus Torvalds 132877f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13292b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 133077f11192STrond Myklebust return 1; 133177f11192STrond Myklebust 13321da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13331da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13341da177e4SLinus Torvalds return 1; 13351da177e4SLinus Torvalds } 13361751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 13371da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13381da177e4SLinus Torvalds * files will be cleaned up during umount */ 13391da177e4SLinus Torvalds return 1; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds return 0; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13461b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13471b83d707STrond Myklebust { 13481b83d707STrond Myklebust spin_lock(&inode->i_lock); 13491f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 135059a707b0STrond Myklebust if (inode->i_nlink > 0) 135159a707b0STrond Myklebust drop_nlink(inode); 135259a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 135316e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 135416e14375STrond Myklebust | NFS_INO_INVALID_CTIME 135559a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 135659a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 13571b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13581b83d707STrond Myklebust } 13591b83d707STrond Myklebust 13601da177e4SLinus Torvalds /* 13611da177e4SLinus Torvalds * Called when the dentry loses inode. 13621da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13631da177e4SLinus Torvalds */ 13641da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13651da177e4SLinus Torvalds { 136683672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 136783672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 136883672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 136983672d39SNeil Brown 13701da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1371e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13721f018458STrond Myklebust nfs_drop_nlink(inode); 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds iput(inode); 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 1377b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1378b1942c5fSAl Viro { 1379b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1380b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1381b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1382b1942c5fSAl Viro WARN_ON(1); 1383b1942c5fSAl Viro else 1384b1942c5fSAl Viro kfree(dentry->d_fsdata); 1385b1942c5fSAl Viro } 1386b1942c5fSAl Viro } 1387b1942c5fSAl Viro 1388f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 13891da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1390ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 13911da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13921da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 139336d43a43SDavid Howells .d_automount = nfs_d_automount, 1394b1942c5fSAl Viro .d_release = nfs_d_release, 13951da177e4SLinus Torvalds }; 1396ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 13971da177e4SLinus Torvalds 1398597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 13991da177e4SLinus Torvalds { 14001da177e4SLinus Torvalds struct dentry *res; 14011da177e4SLinus Torvalds struct inode *inode = NULL; 1402e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1403e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 14041775fd3eSDavid Quigley struct nfs4_label *label = NULL; 14051da177e4SLinus Torvalds int error; 14061da177e4SLinus Torvalds 14076de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 140891d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 14091da177e4SLinus Torvalds 1410130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1411130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 14121da177e4SLinus Torvalds 1413fd684071STrond Myklebust /* 1414fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1415fd684071STrond Myklebust * but don't hash the dentry. 1416fd684071STrond Myklebust */ 14179f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1418130f9ab7SAl Viro return NULL; 14191da177e4SLinus Torvalds 1420e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1421e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1422e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1423e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1424e1fb4d05STrond Myklebust goto out; 1425e1fb4d05STrond Myklebust 142614c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 142714c43f76SDavid Quigley if (IS_ERR(label)) 142814c43f76SDavid Quigley goto out; 142914c43f76SDavid Quigley 14306e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1431f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 14321da177e4SLinus Torvalds if (error == -ENOENT) 14331da177e4SLinus Torvalds goto no_entry; 14341da177e4SLinus Torvalds if (error < 0) { 14351da177e4SLinus Torvalds res = ERR_PTR(error); 1436bf130914SAl Viro goto out_label; 14371da177e4SLinus Torvalds } 14381775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1439bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 144003f28e3aSTrond Myklebust if (IS_ERR(res)) 1441bf130914SAl Viro goto out_label; 144254ceac45SDavid Howells 144363519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 144463519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1445d69ee9b8STrond Myklebust 14461da177e4SLinus Torvalds no_entry: 144741d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14489eaef27bSTrond Myklebust if (res != NULL) { 14499eaef27bSTrond Myklebust if (IS_ERR(res)) 1450bf130914SAl Viro goto out_label; 14511da177e4SLinus Torvalds dentry = res; 14529eaef27bSTrond Myklebust } 14531da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1454bf130914SAl Viro out_label: 14556e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 145614c43f76SDavid Quigley nfs4_label_free(label); 14571da177e4SLinus Torvalds out: 1458e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1459e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14601da177e4SLinus Torvalds return res; 14611da177e4SLinus Torvalds } 1462ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14631da177e4SLinus Torvalds 146489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14650b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14661da177e4SLinus Torvalds 1467f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14680ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1469b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 14701da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14711da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 147236d43a43SDavid Howells .d_automount = nfs_d_automount, 1473b1942c5fSAl Viro .d_release = nfs_d_release, 14741da177e4SLinus Torvalds }; 147589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14761da177e4SLinus Torvalds 14778a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14788a5e929dSAl Viro { 14798a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14808a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14818a5e929dSAl Viro res |= FMODE_READ; 14828a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14838a5e929dSAl Viro res |= FMODE_WRITE; 14848a5e929dSAl Viro return res; 14858a5e929dSAl Viro } 14868a5e929dSAl Viro 1487532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1488cd9a1c0eSTrond Myklebust { 1489532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1490cd9a1c0eSTrond Myklebust } 1491cd9a1c0eSTrond Myklebust 1492cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1493cd9a1c0eSTrond Myklebust { 1494f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1495cd9a1c0eSTrond Myklebust return 0; 1496cd9a1c0eSTrond Myklebust } 1497cd9a1c0eSTrond Myklebust 1498d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 14990dd2b474SMiklos Szeredi struct dentry *dentry, 1500b452a458SAl Viro struct file *file, unsigned open_flags) 1501cd9a1c0eSTrond Myklebust { 15020dd2b474SMiklos Szeredi int err; 15030dd2b474SMiklos Szeredi 1504be12af3eSAl Viro err = finish_open(file, dentry, do_open); 150530d90494SAl Viro if (err) 1506d9585277SAl Viro goto out; 1507eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 150830d90494SAl Viro nfs_file_set_open_context(file, ctx); 1509eaa2b82cSNeilBrown else 15109821421aSTrond Myklebust err = -EOPENSTALE; 1511cd9a1c0eSTrond Myklebust out: 1512d9585277SAl Viro return err; 1513cd9a1c0eSTrond Myklebust } 1514cd9a1c0eSTrond Myklebust 151573a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 151630d90494SAl Viro struct file *file, unsigned open_flags, 151744907d79SAl Viro umode_t mode) 15181da177e4SLinus Torvalds { 1519c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1520cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 15210dd2b474SMiklos Szeredi struct dentry *res; 15220dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1523f46e0bd3STrond Myklebust struct inode *inode; 15241472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1525c94c0953SAl Viro bool switched = false; 152673a09dd9SAl Viro int created = 0; 1527898f635cSTrond Myklebust int err; 15281da177e4SLinus Torvalds 15290dd2b474SMiklos Szeredi /* Expect a negative dentry */ 15302b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 15310dd2b474SMiklos Szeredi 15321e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 15336de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15341e7cb3dcSChuck Lever 15359597c13bSJeff Layton err = nfs_check_flags(open_flags); 15369597c13bSJeff Layton if (err) 15379597c13bSJeff Layton return err; 15389597c13bSJeff Layton 15390dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15400dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 154100699ad8SAl Viro if (!d_in_lookup(dentry)) { 15420dd2b474SMiklos Szeredi /* 15430dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15440dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15450dd2b474SMiklos Szeredi * again 15460dd2b474SMiklos Szeredi */ 1547d9585277SAl Viro return -ENOENT; 15480dd2b474SMiklos Szeredi } 15491472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15501da177e4SLinus Torvalds goto no_open; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15530dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1554d9585277SAl Viro return -ENAMETOOLONG; 15551da177e4SLinus Torvalds 15560dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1557dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1558dff25ddbSAndreas Gruenbacher 1559dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1560dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1561dff25ddbSAndreas Gruenbacher 1562536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1563dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 15640dd2b474SMiklos Szeredi } 1565536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1566536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1567536e43d1STrond Myklebust attr.ia_size = 0; 1568cd9a1c0eSTrond Myklebust } 1569cd9a1c0eSTrond Myklebust 1570c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1571c94c0953SAl Viro d_drop(dentry); 1572c94c0953SAl Viro switched = true; 1573c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1574c94c0953SAl Viro &dentry->d_name, &wq); 1575c94c0953SAl Viro if (IS_ERR(dentry)) 1576c94c0953SAl Viro return PTR_ERR(dentry); 1577c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1578c94c0953SAl Viro return finish_no_open(file, dentry); 1579c94c0953SAl Viro } 1580c94c0953SAl Viro 1581532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 15820dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15830dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1584d9585277SAl Viro goto out; 15850dd2b474SMiklos Szeredi 15866e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 158773a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 158873a09dd9SAl Viro if (created) 158973a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1590275bb307STrond Myklebust if (IS_ERR(inode)) { 15910dd2b474SMiklos Szeredi err = PTR_ERR(inode); 15926e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15932d9db750STrond Myklebust put_nfs_open_context(ctx); 1594d20cb71dSAl Viro d_drop(dentry); 15950dd2b474SMiklos Szeredi switch (err) { 15961da177e4SLinus Torvalds case -ENOENT: 1597774d9513SPeng Tao d_splice_alias(NULL, dentry); 1598809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15990dd2b474SMiklos Szeredi break; 16001788ea6eSJeff Layton case -EISDIR: 16016f926b5bSTrond Myklebust case -ENOTDIR: 16026f926b5bSTrond Myklebust goto no_open; 16031da177e4SLinus Torvalds case -ELOOP: 16040dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 16051da177e4SLinus Torvalds goto no_open; 16060dd2b474SMiklos Szeredi break; 16071da177e4SLinus Torvalds /* case -EINVAL: */ 16081da177e4SLinus Torvalds default: 16090dd2b474SMiklos Szeredi break; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds goto out; 16121da177e4SLinus Torvalds } 16130dd2b474SMiklos Szeredi 1614b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 16156e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16162d9db750STrond Myklebust put_nfs_open_context(ctx); 1617d9585277SAl Viro out: 1618c94c0953SAl Viro if (unlikely(switched)) { 1619c94c0953SAl Viro d_lookup_done(dentry); 1620c94c0953SAl Viro dput(dentry); 1621c94c0953SAl Viro } 1622d9585277SAl Viro return err; 16230dd2b474SMiklos Szeredi 16241da177e4SLinus Torvalds no_open: 16251472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1626c94c0953SAl Viro if (switched) { 1627c94c0953SAl Viro d_lookup_done(dentry); 1628c94c0953SAl Viro if (!res) 1629c94c0953SAl Viro res = dentry; 1630c94c0953SAl Viro else 1631c94c0953SAl Viro dput(dentry); 1632c94c0953SAl Viro } 16330dd2b474SMiklos Szeredi if (IS_ERR(res)) 1634c94c0953SAl Viro return PTR_ERR(res); 1635e45198a6SAl Viro return finish_no_open(file, res); 16361da177e4SLinus Torvalds } 163789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 16381da177e4SLinus Torvalds 1639c7944ebbSTrond Myklebust static int 1640c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1641c7944ebbSTrond Myklebust unsigned int flags) 16421da177e4SLinus Torvalds { 1643657e94b6SNick Piggin struct inode *inode; 16441da177e4SLinus Torvalds 1645fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1646c7944ebbSTrond Myklebust goto full_reval; 1647eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1648c7944ebbSTrond Myklebust goto full_reval; 16492b484297STrond Myklebust 16502b0143b5SDavid Howells inode = d_inode(dentry); 16512b484297STrond Myklebust 16521da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 16531da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 16541da177e4SLinus Torvalds */ 1655c7944ebbSTrond Myklebust if (inode == NULL) 1656c7944ebbSTrond Myklebust goto full_reval; 165749317a7fSNeilBrown 1658c7944ebbSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 1659c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1660216d5d06STrond Myklebust 16611da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16621da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1663c7944ebbSTrond Myklebust goto full_reval; 1664c7944ebbSTrond Myklebust 16651da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1666c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1667c7944ebbSTrond Myklebust goto reval_dentry; 1668c7944ebbSTrond Myklebust 1669c7944ebbSTrond Myklebust /* Check if the directory changed */ 1670c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1671c7944ebbSTrond Myklebust goto reval_dentry; 16721da177e4SLinus Torvalds 16730ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1674c7944ebbSTrond Myklebust return 1; 1675c7944ebbSTrond Myklebust reval_dentry: 1676c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1677c7944ebbSTrond Myklebust return -ECHILD; 167842f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 16790ef97dcfSMiklos Szeredi 1680c7944ebbSTrond Myklebust full_reval: 1681c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1682c7944ebbSTrond Myklebust } 1683535918f1STrond Myklebust 1684c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1685c7944ebbSTrond Myklebust { 1686c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1687c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1688c0204fd2STrond Myklebust } 1689c0204fd2STrond Myklebust 16901da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16911da177e4SLinus Torvalds 1692406cd915SBenjamin Coddington struct dentry * 1693406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 16941775fd3eSDavid Quigley struct nfs_fattr *fattr, 16951775fd3eSDavid Quigley struct nfs4_label *label) 16961da177e4SLinus Torvalds { 1697fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 16982b0143b5SDavid Howells struct inode *dir = d_inode(parent); 16991da177e4SLinus Torvalds struct inode *inode; 1700b0c6108eSAl Viro struct dentry *d; 1701406cd915SBenjamin Coddington int error; 17021da177e4SLinus Torvalds 1703fab728e1STrond Myklebust d_drop(dentry); 1704fab728e1STrond Myklebust 17051da177e4SLinus Torvalds if (fhandle->size == 0) { 1706f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 17071da177e4SLinus Torvalds if (error) 1708fab728e1STrond Myklebust goto out_error; 17091da177e4SLinus Torvalds } 17105724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17111da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 17121da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1713a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1714a841b54dSTrond Myklebust fattr, NULL, NULL); 17151da177e4SLinus Torvalds if (error < 0) 1716fab728e1STrond Myklebust goto out_error; 17171da177e4SLinus Torvalds } 17181775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1719b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1720fab728e1STrond Myklebust out: 1721fab728e1STrond Myklebust dput(parent); 1722406cd915SBenjamin Coddington return d; 1723fab728e1STrond Myklebust out_error: 1724fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1725406cd915SBenjamin Coddington d = ERR_PTR(error); 1726406cd915SBenjamin Coddington goto out; 1727406cd915SBenjamin Coddington } 1728406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1729406cd915SBenjamin Coddington 1730406cd915SBenjamin Coddington /* 1731406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1732406cd915SBenjamin Coddington */ 1733406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1734406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1735406cd915SBenjamin Coddington struct nfs4_label *label) 1736406cd915SBenjamin Coddington { 1737406cd915SBenjamin Coddington struct dentry *d; 1738406cd915SBenjamin Coddington 1739406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1740406cd915SBenjamin Coddington if (IS_ERR(d)) 1741406cd915SBenjamin Coddington return PTR_ERR(d); 1742406cd915SBenjamin Coddington 1743406cd915SBenjamin Coddington /* Callers don't care */ 1744406cd915SBenjamin Coddington dput(d); 1745406cd915SBenjamin Coddington return 0; 17461da177e4SLinus Torvalds } 1747ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds /* 17501da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17511da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17521da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17531da177e4SLinus Torvalds * reply path made it appear to have failed. 17541da177e4SLinus Torvalds */ 1755597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1756ebfc3b49SAl Viro umode_t mode, bool excl) 17571da177e4SLinus Torvalds { 17581da177e4SLinus Torvalds struct iattr attr; 1759ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 17601da177e4SLinus Torvalds int error; 17611da177e4SLinus Torvalds 17621e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 17636de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds attr.ia_mode = mode; 17661da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17671da177e4SLinus Torvalds 17688b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17698867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17708b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17711da177e4SLinus Torvalds if (error != 0) 17721da177e4SLinus Torvalds goto out_err; 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds out_err: 17751da177e4SLinus Torvalds d_drop(dentry); 17761da177e4SLinus Torvalds return error; 17771da177e4SLinus Torvalds } 1778ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds /* 17811da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17821da177e4SLinus Torvalds */ 1783597d9289SBryan Schumaker int 17841a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17851da177e4SLinus Torvalds { 17861da177e4SLinus Torvalds struct iattr attr; 17871da177e4SLinus Torvalds int status; 17881da177e4SLinus Torvalds 17891e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 17906de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds attr.ia_mode = mode; 17931da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17941da177e4SLinus Torvalds 17951ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 17961da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 17971ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 17981da177e4SLinus Torvalds if (status != 0) 17991da177e4SLinus Torvalds goto out_err; 18001da177e4SLinus Torvalds return 0; 18011da177e4SLinus Torvalds out_err: 18021da177e4SLinus Torvalds d_drop(dentry); 18031da177e4SLinus Torvalds return status; 18041da177e4SLinus Torvalds } 1805ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds /* 18081da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18091da177e4SLinus Torvalds */ 1810597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 18111da177e4SLinus Torvalds { 18121da177e4SLinus Torvalds struct iattr attr; 18131da177e4SLinus Torvalds int error; 18141da177e4SLinus Torvalds 18151e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 18166de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18191da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 18201da177e4SLinus Torvalds 18211ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 18221da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 18231ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 18241da177e4SLinus Torvalds if (error != 0) 18251da177e4SLinus Torvalds goto out_err; 18261da177e4SLinus Torvalds return 0; 18271da177e4SLinus Torvalds out_err: 18281da177e4SLinus Torvalds d_drop(dentry); 18291da177e4SLinus Torvalds return error; 18301da177e4SLinus Torvalds } 1831ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 18321da177e4SLinus Torvalds 1833d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1834d45b9d8bSTrond Myklebust { 1835dc3f4198SAl Viro if (simple_positive(dentry)) 1836d45b9d8bSTrond Myklebust d_delete(dentry); 1837d45b9d8bSTrond Myklebust } 1838d45b9d8bSTrond Myklebust 1839597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 18401da177e4SLinus Torvalds { 18411da177e4SLinus Torvalds int error; 18421da177e4SLinus Torvalds 18431e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 18446de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18451da177e4SLinus Torvalds 18461ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18472b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1848884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18491da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18501da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1851ba6c0592STrond Myklebust switch (error) { 1852ba6c0592STrond Myklebust case 0: 18532b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1854ba6c0592STrond Myklebust break; 1855ba6c0592STrond Myklebust case -ENOENT: 1856d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1857ba6c0592STrond Myklebust } 1858884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1859ba6c0592STrond Myklebust } else 1860ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18611ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds return error; 18641da177e4SLinus Torvalds } 1865ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* 18681da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18691da177e4SLinus Torvalds * and after checking that the file has only one user. 18701da177e4SLinus Torvalds * 18711da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18721da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18731da177e4SLinus Torvalds */ 18741da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18751da177e4SLinus Torvalds { 18762b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18772b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18781da177e4SLinus Torvalds int error = -EBUSY; 18791da177e4SLinus Torvalds 18806de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18831da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18841da177e4SLinus Torvalds error = 0; 18851da177e4SLinus Torvalds goto out; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 18881ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 18891da177e4SLinus Torvalds if (inode != NULL) { 1890912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 18911da177e4SLinus Torvalds if (error == 0) 18921b83d707STrond Myklebust nfs_drop_nlink(inode); 18931da177e4SLinus Torvalds } else 1894912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 1895d45b9d8bSTrond Myklebust if (error == -ENOENT) 1896d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18971ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 18981da177e4SLinus Torvalds out: 18991da177e4SLinus Torvalds return error; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 19031da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 19041da177e4SLinus Torvalds * 19051da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 19061da177e4SLinus Torvalds */ 1907597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 19081da177e4SLinus Torvalds { 19091da177e4SLinus Torvalds int error; 19101da177e4SLinus Torvalds int need_rehash = 0; 19111da177e4SLinus Torvalds 19121e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 19136de1472fSAl Viro dir->i_ino, dentry); 19141da177e4SLinus Torvalds 19151ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 19161da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 191784d08fa8SAl Viro if (d_count(dentry) > 1) { 19181da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1919ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 19202b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 19211da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 19221ca42382STrond Myklebust goto out; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 19251da177e4SLinus Torvalds __d_drop(dentry); 19261da177e4SLinus Torvalds need_rehash = 1; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 19291da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1930d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 19311da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19321da177e4SLinus Torvalds } else if (need_rehash) 19331da177e4SLinus Torvalds d_rehash(dentry); 19341ca42382STrond Myklebust out: 19351ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 19361da177e4SLinus Torvalds return error; 19371da177e4SLinus Torvalds } 1938ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 19391da177e4SLinus Torvalds 1940873101b3SChuck Lever /* 1941873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1942873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1943873101b3SChuck Lever * using prepare_write/commit_write. 1944873101b3SChuck Lever * 1945873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1946873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1947873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1948873101b3SChuck Lever * symlink request has completed on the server. 1949873101b3SChuck Lever * 1950873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1951873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1952873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1953873101b3SChuck Lever * and move the raw page into its mapping. 1954873101b3SChuck Lever */ 1955597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19561da177e4SLinus Torvalds { 1957873101b3SChuck Lever struct page *page; 1958873101b3SChuck Lever char *kaddr; 19591da177e4SLinus Torvalds struct iattr attr; 1960873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19611da177e4SLinus Torvalds int error; 19621da177e4SLinus Torvalds 19631e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 19646de1472fSAl Viro dir->i_ino, dentry, symname); 19651da177e4SLinus Torvalds 1966873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1967873101b3SChuck Lever return -ENAMETOOLONG; 19681da177e4SLinus Torvalds 1969873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1970873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19711da177e4SLinus Torvalds 1972e8ecde25SAl Viro page = alloc_page(GFP_USER); 197376566991STrond Myklebust if (!page) 1974873101b3SChuck Lever return -ENOMEM; 1975873101b3SChuck Lever 1976e8ecde25SAl Viro kaddr = page_address(page); 1977873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1978873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1979873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1980873101b3SChuck Lever 19811ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 198294a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19831ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1984873101b3SChuck Lever if (error != 0) { 19851e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1986873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 19876de1472fSAl Viro dentry, symname, error); 19881da177e4SLinus Torvalds d_drop(dentry); 1989873101b3SChuck Lever __free_page(page); 19901da177e4SLinus Torvalds return error; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 1993873101b3SChuck Lever /* 1994873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1995873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1996873101b3SChuck Lever */ 19972b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 1998873101b3SChuck Lever GFP_KERNEL)) { 1999873101b3SChuck Lever SetPageUptodate(page); 2000873101b3SChuck Lever unlock_page(page); 2001a0b54addSRafael Aquini /* 2002a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2003a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2004a0b54addSRafael Aquini */ 200509cbfeafSKirill A. Shutemov put_page(page); 2006873101b3SChuck Lever } else 2007873101b3SChuck Lever __free_page(page); 2008873101b3SChuck Lever 2009873101b3SChuck Lever return 0; 2010873101b3SChuck Lever } 2011ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2012873101b3SChuck Lever 2013597d9289SBryan Schumaker int 20141da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20151da177e4SLinus Torvalds { 20162b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 20171da177e4SLinus Torvalds int error; 20181da177e4SLinus Torvalds 20196de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 20206de1472fSAl Viro old_dentry, dentry); 20211da177e4SLinus Torvalds 20221fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 20239697d234STrond Myklebust d_drop(dentry); 20241da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2025cf809556STrond Myklebust if (error == 0) { 20267de9c6eeSAl Viro ihold(inode); 20279697d234STrond Myklebust d_add(dentry, inode); 2028cf809556STrond Myklebust } 20291fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 20301da177e4SLinus Torvalds return error; 20311da177e4SLinus Torvalds } 2032ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds /* 20351da177e4SLinus Torvalds * RENAME 20361da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20371da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20381da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20391da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20401da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20411da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20421da177e4SLinus Torvalds * 20431da177e4SLinus Torvalds * FIXED. 20441da177e4SLinus Torvalds * 20451da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20461da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20471da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20481da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20491da177e4SLinus Torvalds * using the inode layer 20501da177e4SLinus Torvalds * 20511da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20521da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20531da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20541da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20551da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20561da177e4SLinus Torvalds * the rename. 20571da177e4SLinus Torvalds */ 2058597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 20591cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 20601cd66c93SMiklos Szeredi unsigned int flags) 20611da177e4SLinus Torvalds { 20622b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 20632b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2064d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 206580a491fdSJeff Layton struct rpc_task *task; 20661da177e4SLinus Torvalds int error = -EBUSY; 20671da177e4SLinus Torvalds 20681cd66c93SMiklos Szeredi if (flags) 20691cd66c93SMiklos Szeredi return -EINVAL; 20701cd66c93SMiklos Szeredi 20716de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20726de1472fSAl Viro old_dentry, new_dentry, 207384d08fa8SAl Viro d_count(new_dentry)); 20741da177e4SLinus Torvalds 207570ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20761da177e4SLinus Torvalds /* 207728f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 207828f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 207928f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 208028f79a1aSMiklos Szeredi * the new target. 20811da177e4SLinus Torvalds */ 208227226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 208327226104SMiklos Szeredi /* 208427226104SMiklos Szeredi * To prevent any new references to the target during the 208527226104SMiklos Szeredi * rename, we unhash the dentry in advance. 208627226104SMiklos Szeredi */ 2087d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 208827226104SMiklos Szeredi d_drop(new_dentry); 2089d9f29500SBenjamin Coddington rehash = new_dentry; 2090d9f29500SBenjamin Coddington } 209127226104SMiklos Szeredi 209284d08fa8SAl Viro if (d_count(new_dentry) > 2) { 20931da177e4SLinus Torvalds int err; 209427226104SMiklos Szeredi 20951da177e4SLinus Torvalds /* copy the target dentry's name */ 20961da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 20971da177e4SLinus Torvalds &new_dentry->d_name); 20981da177e4SLinus Torvalds if (!dentry) 20991da177e4SLinus Torvalds goto out; 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds /* silly-rename the existing target ... */ 21021da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 210324e93025SMiklos Szeredi if (err) 21041da177e4SLinus Torvalds goto out; 210524e93025SMiklos Szeredi 210624e93025SMiklos Szeredi new_dentry = dentry; 2107d9f29500SBenjamin Coddington rehash = NULL; 210824e93025SMiklos Szeredi new_inode = NULL; 2109b1e4adf4STrond Myklebust } 211027226104SMiklos Szeredi } 21111da177e4SLinus Torvalds 2112d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 211380a491fdSJeff Layton if (IS_ERR(task)) { 211480a491fdSJeff Layton error = PTR_ERR(task); 211580a491fdSJeff Layton goto out; 211680a491fdSJeff Layton } 211780a491fdSJeff Layton 211880a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2119818a8dbeSBenjamin Coddington if (error != 0) { 2120818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2121818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2122818a8dbeSBenjamin Coddington smp_wmb(); 2123818a8dbeSBenjamin Coddington } else 212480a491fdSJeff Layton error = task->tk_status; 212580a491fdSJeff Layton rpc_put_task(task); 212659a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 212759a707b0STrond Myklebust if (error == 0) { 212859a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 212959a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 213059a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 213159a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 213259a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 213359a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 213459a707b0STrond Myklebust } 21351da177e4SLinus Torvalds out: 2136d9f29500SBenjamin Coddington if (rehash) 2137d9f29500SBenjamin Coddington d_rehash(rehash); 213870ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 213970ded201STrond Myklebust new_dir, new_dentry, error); 2140d9f29500SBenjamin Coddington if (!error) { 2141d9f29500SBenjamin Coddington if (new_inode != NULL) 2142d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2143d9f29500SBenjamin Coddington /* 2144d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2145d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2146d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2147d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2148d9f29500SBenjamin Coddington */ 2149d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2150d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2151d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2152d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2153d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2154d9f29500SBenjamin Coddington 21551da177e4SLinus Torvalds /* new dentry created? */ 21561da177e4SLinus Torvalds if (dentry) 21571da177e4SLinus Torvalds dput(dentry); 21581da177e4SLinus Torvalds return error; 21591da177e4SLinus Torvalds } 2160ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 21611da177e4SLinus Torvalds 2162cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2163cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2164cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2165cfcea3e8STrond Myklebust 21663a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 21673a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 21683a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 21693a505845STrond Myklebust 21701c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 21711c3c07e9STrond Myklebust { 2172b68572e0SNeilBrown put_cred(entry->cred); 2173f682a398SNeilBrown kfree_rcu(entry, rcu_head); 21744e857c58SPeter Zijlstra smp_mb__before_atomic(); 2175cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 21764e857c58SPeter Zijlstra smp_mb__after_atomic(); 21771c3c07e9STrond Myklebust } 21781c3c07e9STrond Myklebust 21791a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 21801a81bb8aSTrond Myklebust { 21811a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 21821a81bb8aSTrond Myklebust 21831a81bb8aSTrond Myklebust while (!list_empty(head)) { 21841a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 21851a81bb8aSTrond Myklebust list_del(&cache->lru); 21861a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 21871a81bb8aSTrond Myklebust } 21881a81bb8aSTrond Myklebust } 21891a81bb8aSTrond Myklebust 21903a505845STrond Myklebust static unsigned long 21913a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2192979df72eSTrond Myklebust { 2193979df72eSTrond Myklebust LIST_HEAD(head); 2194aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2195979df72eSTrond Myklebust struct nfs_access_entry *cache; 21961ab6c499SDave Chinner long freed = 0; 2197979df72eSTrond Myklebust 2198a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2199aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2200979df72eSTrond Myklebust struct inode *inode; 2201979df72eSTrond Myklebust 2202979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2203979df72eSTrond Myklebust break; 22049c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2205979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2206979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2207979df72eSTrond Myklebust goto remove_lru_entry; 2208979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2209979df72eSTrond Myklebust struct nfs_access_entry, lru); 2210979df72eSTrond Myklebust list_move(&cache->lru, &head); 2211979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 22121ab6c499SDave Chinner freed++; 2213979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2214979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2215979df72eSTrond Myklebust &nfs_access_lru_list); 2216979df72eSTrond Myklebust else { 2217979df72eSTrond Myklebust remove_lru_entry: 2218979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 22194e857c58SPeter Zijlstra smp_mb__before_atomic(); 2220979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 22214e857c58SPeter Zijlstra smp_mb__after_atomic(); 2222979df72eSTrond Myklebust } 222359844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2224979df72eSTrond Myklebust } 2225979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22261a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22271ab6c499SDave Chinner return freed; 22281ab6c499SDave Chinner } 22291ab6c499SDave Chinner 22301ab6c499SDave Chinner unsigned long 22313a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 22323a505845STrond Myklebust { 22333a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 22343a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 22353a505845STrond Myklebust 22363a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 22373a505845STrond Myklebust return SHRINK_STOP; 22383a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 22393a505845STrond Myklebust } 22403a505845STrond Myklebust 22413a505845STrond Myklebust 22423a505845STrond Myklebust unsigned long 22431ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 22441ab6c499SDave Chinner { 224555f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2246979df72eSTrond Myklebust } 2247979df72eSTrond Myklebust 22483a505845STrond Myklebust static void 22493a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22503a505845STrond Myklebust { 22513a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22523a505845STrond Myklebust unsigned long diff; 22533a505845STrond Myklebust unsigned int nr_to_scan; 22543a505845STrond Myklebust 22553a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22563a505845STrond Myklebust return; 22573a505845STrond Myklebust nr_to_scan = 100; 22583a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 22593a505845STrond Myklebust if (diff < nr_to_scan) 22603a505845STrond Myklebust nr_to_scan = diff; 22613a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 22623a505845STrond Myklebust } 22633a505845STrond Myklebust 22641a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 22651c3c07e9STrond Myklebust { 22661c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22671a81bb8aSTrond Myklebust struct rb_node *n; 22681c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22691c3c07e9STrond Myklebust 22701c3c07e9STrond Myklebust /* Unhook entries from the cache */ 22711c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 22721c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22731c3c07e9STrond Myklebust rb_erase(n, root_node); 22741a81bb8aSTrond Myklebust list_move(&entry->lru, head); 22751c3c07e9STrond Myklebust } 22761c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 22771c3c07e9STrond Myklebust } 22781c3c07e9STrond Myklebust 22791c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 22801c3c07e9STrond Myklebust { 22811a81bb8aSTrond Myklebust LIST_HEAD(head); 22821a81bb8aSTrond Myklebust 22831a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 22841a81bb8aSTrond Myklebust return; 2285cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2286cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22871a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2288cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2289cfcea3e8STrond Myklebust 22901c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22911a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 22921a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22931a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22941a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22951c3c07e9STrond Myklebust } 22961c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 22971c3c07e9STrond Myklebust 2298b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 22991c3c07e9STrond Myklebust { 23001c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 23011c3c07e9STrond Myklebust 23021c3c07e9STrond Myklebust while (n != NULL) { 2303b68572e0SNeilBrown struct nfs_access_entry *entry = 2304b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2305b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 23061c3c07e9STrond Myklebust 2307b68572e0SNeilBrown if (cmp < 0) 23081c3c07e9STrond Myklebust n = n->rb_left; 2309b68572e0SNeilBrown else if (cmp > 0) 23101c3c07e9STrond Myklebust n = n->rb_right; 23111c3c07e9STrond Myklebust else 23121c3c07e9STrond Myklebust return entry; 23131c3c07e9STrond Myklebust } 23141c3c07e9STrond Myklebust return NULL; 23151c3c07e9STrond Myklebust } 23161c3c07e9STrond Myklebust 2317b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 23181da177e4SLinus Torvalds { 231955296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 23201c3c07e9STrond Myklebust struct nfs_access_entry *cache; 232157b69181STrond Myklebust bool retry = true; 232257b69181STrond Myklebust int err; 23231da177e4SLinus Torvalds 23241c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 232557b69181STrond Myklebust for(;;) { 23261c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 23271c3c07e9STrond Myklebust goto out_zap; 23281c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 232957b69181STrond Myklebust err = -ENOENT; 23301c3c07e9STrond Myklebust if (cache == NULL) 23311c3c07e9STrond Myklebust goto out; 233257b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 233321c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 233457b69181STrond Myklebust break; 23355c965db8STrond Myklebust if (!retry) 23365c965db8STrond Myklebust break; 233757b69181STrond Myklebust err = -ECHILD; 233857b69181STrond Myklebust if (!may_block) 233957b69181STrond Myklebust goto out; 234057b69181STrond Myklebust spin_unlock(&inode->i_lock); 234157b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 234257b69181STrond Myklebust if (err) 234357b69181STrond Myklebust return err; 234457b69181STrond Myklebust spin_lock(&inode->i_lock); 234557b69181STrond Myklebust retry = false; 234657b69181STrond Myklebust } 23471c3c07e9STrond Myklebust res->cred = cache->cred; 23481c3c07e9STrond Myklebust res->mask = cache->mask; 2349cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23501c3c07e9STrond Myklebust err = 0; 23511c3c07e9STrond Myklebust out: 23521c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23531c3c07e9STrond Myklebust return err; 23541c3c07e9STrond Myklebust out_zap: 23551a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23561a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 23571c3c07e9STrond Myklebust return -ENOENT; 23581c3c07e9STrond Myklebust } 23591c3c07e9STrond Myklebust 2360b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2361f682a398SNeilBrown { 2362f682a398SNeilBrown /* Only check the most recently returned cache entry, 2363f682a398SNeilBrown * but do it without locking. 2364f682a398SNeilBrown */ 2365f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2366f682a398SNeilBrown struct nfs_access_entry *cache; 2367f682a398SNeilBrown int err = -ECHILD; 2368f682a398SNeilBrown struct list_head *lh; 2369f682a398SNeilBrown 2370f682a398SNeilBrown rcu_read_lock(); 2371f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2372f682a398SNeilBrown goto out; 2373f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2374f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2375f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 23769a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2377f682a398SNeilBrown cache = NULL; 2378f682a398SNeilBrown if (cache == NULL) 2379f682a398SNeilBrown goto out; 238021c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2381f682a398SNeilBrown goto out; 2382f682a398SNeilBrown res->cred = cache->cred; 2383f682a398SNeilBrown res->mask = cache->mask; 238421c3ba7eSTrond Myklebust err = 0; 2385f682a398SNeilBrown out: 2386f682a398SNeilBrown rcu_read_unlock(); 2387f682a398SNeilBrown return err; 2388f682a398SNeilBrown } 2389f682a398SNeilBrown 23901c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 23911c3c07e9STrond Myklebust { 2392cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2393cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23941c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 23951c3c07e9STrond Myklebust struct rb_node *parent = NULL; 23961c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2397b68572e0SNeilBrown int cmp; 23981c3c07e9STrond Myklebust 23991c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24001c3c07e9STrond Myklebust while (*p != NULL) { 24011c3c07e9STrond Myklebust parent = *p; 24021c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2403b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 24041c3c07e9STrond Myklebust 2405b68572e0SNeilBrown if (cmp < 0) 24061c3c07e9STrond Myklebust p = &parent->rb_left; 2407b68572e0SNeilBrown else if (cmp > 0) 24081c3c07e9STrond Myklebust p = &parent->rb_right; 24091c3c07e9STrond Myklebust else 24101c3c07e9STrond Myklebust goto found; 24111c3c07e9STrond Myklebust } 24121c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 24131c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2414cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 24151c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24161c3c07e9STrond Myklebust return; 24171c3c07e9STrond Myklebust found: 24181c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2419cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2420cfcea3e8STrond Myklebust list_del(&entry->lru); 24211c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24221c3c07e9STrond Myklebust nfs_access_free_entry(entry); 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 24256168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 24261da177e4SLinus Torvalds { 24271c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 24281c3c07e9STrond Myklebust if (cache == NULL) 24291c3c07e9STrond Myklebust return; 24301c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2431b68572e0SNeilBrown cache->cred = get_cred(set->cred); 24321da177e4SLinus Torvalds cache->mask = set->mask; 24331c3c07e9STrond Myklebust 2434f682a398SNeilBrown /* The above field assignments must be visible 2435f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2436f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2437f682a398SNeilBrown */ 2438f682a398SNeilBrown smp_wmb(); 24391c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2440cfcea3e8STrond Myklebust 2441cfcea3e8STrond Myklebust /* Update accounting */ 24424e857c58SPeter Zijlstra smp_mb__before_atomic(); 2443cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 24444e857c58SPeter Zijlstra smp_mb__after_atomic(); 2445cfcea3e8STrond Myklebust 2446cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24471a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2448cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24491a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24501a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24511a81bb8aSTrond Myklebust &nfs_access_lru_list); 2452cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2453cfcea3e8STrond Myklebust } 24543a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24551da177e4SLinus Torvalds } 24566168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 24576168f62cSWeston Andros Adamson 24583c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 24593c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24603c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 24613c181827SAnna Schumaker NFS_ACCESS_DELETE) 24623c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24633c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2464ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 24653c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 24663c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 246715d4b73aSTrond Myklebust static int 2468ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 246915d4b73aSTrond Myklebust { 247015d4b73aSTrond Myklebust int mask = 0; 247115d4b73aSTrond Myklebust 247215d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 247315d4b73aSTrond Myklebust mask |= MAY_READ; 2474ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2475ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 247615d4b73aSTrond Myklebust mask |= MAY_WRITE; 2477ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 247815d4b73aSTrond Myklebust mask |= MAY_EXEC; 2479ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2480ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2481ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2482ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 248315d4b73aSTrond Myklebust mask |= MAY_EXEC; 2484ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2485ecbb903cSTrond Myklebust mask |= MAY_WRITE; 248615d4b73aSTrond Myklebust return mask; 248715d4b73aSTrond Myklebust } 248815d4b73aSTrond Myklebust 24896168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 24906168f62cSWeston Andros Adamson { 2491bd8b2441STrond Myklebust entry->mask = access_result; 24926168f62cSWeston Andros Adamson } 24936168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 24941da177e4SLinus Torvalds 2495b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 24961da177e4SLinus Torvalds { 24971da177e4SLinus Torvalds struct nfs_access_entry cache; 249857b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2499e8194b7dSTrond Myklebust int cache_mask = -1; 25001da177e4SLinus Torvalds int status; 25011da177e4SLinus Torvalds 2502f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2503f4ce1299STrond Myklebust 2504f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2505f682a398SNeilBrown if (status != 0) 250657b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 25071da177e4SLinus Torvalds if (status == 0) 2508f4ce1299STrond Myklebust goto out_cached; 25091da177e4SLinus Torvalds 2510f3324a2aSNeilBrown status = -ECHILD; 251157b69181STrond Myklebust if (!may_block) 2512f3324a2aSNeilBrown goto out; 2513f3324a2aSNeilBrown 25141750d929SAnna Schumaker /* 25151750d929SAnna Schumaker * Determine which access bits we want to ask for... 25161750d929SAnna Schumaker */ 25171750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 25181750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 25191750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 25201750d929SAnna Schumaker else 25211750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 25221da177e4SLinus Torvalds cache.cred = cred; 25231da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2524a71ee337SSuresh Jayaraman if (status != 0) { 2525a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2526a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2527a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2528a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2529a71ee337SSuresh Jayaraman } 2530f4ce1299STrond Myklebust goto out; 2531a71ee337SSuresh Jayaraman } 25321da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2533f4ce1299STrond Myklebust out_cached: 2534ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2535bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2536f4ce1299STrond Myklebust status = -EACCES; 25371da177e4SLinus Torvalds out: 2538e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2539f4ce1299STrond Myklebust return status; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 2542af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2543af22f94aSTrond Myklebust { 2544af22f94aSTrond Myklebust int mask = 0; 2545af22f94aSTrond Myklebust 2546f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2547f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2548f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2549f8d9a897SWeston Andros Adamson } else { 25508a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2551af22f94aSTrond Myklebust mask |= MAY_READ; 25528a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2553af22f94aSTrond Myklebust mask |= MAY_WRITE; 2554f8d9a897SWeston Andros Adamson } 2555f8d9a897SWeston Andros Adamson 2556af22f94aSTrond Myklebust return mask; 2557af22f94aSTrond Myklebust } 2558af22f94aSTrond Myklebust 2559b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2560af22f94aSTrond Myklebust { 2561af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2562af22f94aSTrond Myklebust } 256389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2564af22f94aSTrond Myklebust 25655c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 25665c5fc09aSTrond Myklebust { 25675c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 256821c3ba7eSTrond Myklebust int ret = 0; 25695c5fc09aSTrond Myklebust 25703825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 25713825827eSTrond Myklebust return 0; 2572cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 25735c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 257421c3ba7eSTrond Myklebust return -ECHILD; 257521c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 257621c3ba7eSTrond Myklebust } 25775c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 25785c5fc09aSTrond Myklebust ret = -EACCES; 25795c5fc09aSTrond Myklebust return ret; 25805c5fc09aSTrond Myklebust } 25815c5fc09aSTrond Myklebust 258210556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 25831da177e4SLinus Torvalds { 2584b68572e0SNeilBrown const struct cred *cred = current_cred(); 25851da177e4SLinus Torvalds int res = 0; 25861da177e4SLinus Torvalds 258791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 258891d5b470SChuck Lever 2589e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 25901da177e4SLinus Torvalds goto out; 25911da177e4SLinus Torvalds /* Is this sys_access() ? */ 25929cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 25931da177e4SLinus Torvalds goto force_lookup; 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 25961da177e4SLinus Torvalds case S_IFLNK: 25971da177e4SLinus Torvalds goto out; 25981da177e4SLinus Torvalds case S_IFREG: 2599762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2600762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2601762674f8STrond Myklebust return 0; 26021da177e4SLinus Torvalds break; 26031da177e4SLinus Torvalds case S_IFDIR: 26041da177e4SLinus Torvalds /* 26051da177e4SLinus Torvalds * Optimize away all write operations, since the server 26061da177e4SLinus Torvalds * will check permissions when we perform the op. 26071da177e4SLinus Torvalds */ 26081da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 26091da177e4SLinus Torvalds goto out; 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds force_lookup: 26131da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 26141da177e4SLinus Torvalds goto out_notsup; 26151da177e4SLinus Torvalds 2616f3324a2aSNeilBrown /* Always try fast lookups first */ 2617f3324a2aSNeilBrown rcu_read_lock(); 2618f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2619f3324a2aSNeilBrown rcu_read_unlock(); 2620f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2621f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 26221da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2623f3324a2aSNeilBrown } 26241da177e4SLinus Torvalds out: 26255c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 26265c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2627f696a365SMiklos Szeredi 26281e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 26291e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 26301da177e4SLinus Torvalds return res; 26311da177e4SLinus Torvalds out_notsup: 2632d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2633d51ac1a8SNeilBrown return -ECHILD; 2634d51ac1a8SNeilBrown 26351da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 26361da177e4SLinus Torvalds if (res == 0) 26372830ba7fSAl Viro res = generic_permission(inode, mask); 26381e7cb3dcSChuck Lever goto out; 26391da177e4SLinus Torvalds } 2640ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds /* 26431da177e4SLinus Torvalds * Local variables: 26441da177e4SLinus Torvalds * version-control: t 26451da177e4SLinus Torvalds * kept-new-versions: 5 26461da177e4SLinus Torvalds * End: 26471da177e4SLinus Torvalds */ 2648