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 1654b310319STrond Myklebust static 1664b310319STrond Myklebust void nfs_readdir_init_array(struct page *page) 1674b310319STrond Myklebust { 1684b310319STrond Myklebust struct nfs_cache_array *array; 1694b310319STrond Myklebust 1704b310319STrond Myklebust array = kmap_atomic(page); 1714b310319STrond Myklebust memset(array, 0, sizeof(struct nfs_cache_array)); 1724b310319STrond Myklebust array->eof_index = -1; 1734b310319STrond Myklebust kunmap_atomic(array); 1744b310319STrond Myklebust } 1754b310319STrond Myklebust 176d1bacf9eSBryan Schumaker /* 177d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 178d1bacf9eSBryan Schumaker */ 179d1bacf9eSBryan Schumaker static 18011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 181d1bacf9eSBryan Schumaker { 18211de3b11STrond Myklebust struct nfs_cache_array *array; 183d1bacf9eSBryan Schumaker int i; 1848cd51a0cSTrond Myklebust 1852b86ce2dSCong Wang array = kmap_atomic(page); 186d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 187d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1884b310319STrond Myklebust array->size = 0; 1892b86ce2dSCong Wang kunmap_atomic(array); 190d1bacf9eSBryan Schumaker } 191d1bacf9eSBryan Schumaker 192d1bacf9eSBryan Schumaker /* 193d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 194d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 195d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 196d1bacf9eSBryan Schumaker */ 197d1bacf9eSBryan Schumaker static 1984a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 199d1bacf9eSBryan Schumaker { 200d1bacf9eSBryan Schumaker string->len = len; 201*3803d672STrond Myklebust string->name = kmemdup_nul(name, len, GFP_KERNEL); 2024a201d6eSTrond Myklebust if (string->name == NULL) 2034a201d6eSTrond Myklebust return -ENOMEM; 20404e4bd1cSCatalin Marinas /* 20504e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 20604e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 20704e4bd1cSCatalin Marinas */ 20804e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2098387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2104a201d6eSTrond Myklebust return 0; 211d1bacf9eSBryan Schumaker } 212d1bacf9eSBryan Schumaker 213d1bacf9eSBryan Schumaker static 214d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 215d1bacf9eSBryan Schumaker { 2160795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2174a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2184a201d6eSTrond Myklebust int ret; 2194a201d6eSTrond Myklebust 2204a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2213020093fSTrond Myklebust 2223020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2233020093fSTrond Myklebust ret = -ENOSPC; 2243020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2253020093fSTrond Myklebust goto out; 2263020093fSTrond Myklebust 2274a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2284a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2290b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2304a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2314a201d6eSTrond Myklebust if (ret) 2324a201d6eSTrond Myklebust goto out; 233d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2348cd51a0cSTrond Myklebust array->size++; 23547c716cbSTrond Myklebust if (entry->eof != 0) 236d1bacf9eSBryan Schumaker array->eof_index = array->size; 2374a201d6eSTrond Myklebust out: 2380795bf83SFabian Frederick kunmap(page); 2394a201d6eSTrond Myklebust return ret; 240d1bacf9eSBryan Schumaker } 241d1bacf9eSBryan Schumaker 242d1bacf9eSBryan Schumaker static 243d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 244d1bacf9eSBryan Schumaker { 24523db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 246d1bacf9eSBryan Schumaker unsigned int index; 247d1bacf9eSBryan Schumaker 248d1bacf9eSBryan Schumaker if (diff < 0) 249d1bacf9eSBryan Schumaker goto out_eof; 250d1bacf9eSBryan Schumaker if (diff >= array->size) { 2518cd51a0cSTrond Myklebust if (array->eof_index >= 0) 252d1bacf9eSBryan Schumaker goto out_eof; 253d1bacf9eSBryan Schumaker return -EAGAIN; 254d1bacf9eSBryan Schumaker } 255d1bacf9eSBryan Schumaker 256d1bacf9eSBryan Schumaker index = (unsigned int)diff; 257d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 258d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 259d1bacf9eSBryan Schumaker return 0; 260d1bacf9eSBryan Schumaker out_eof: 2616089dd0dSThomas Meyer desc->eof = true; 262d1bacf9eSBryan Schumaker return -EBADCOOKIE; 263d1bacf9eSBryan Schumaker } 264d1bacf9eSBryan Schumaker 2654db72b40SJeff Layton static bool 2664db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2674db72b40SJeff Layton { 2684db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2694db72b40SJeff Layton return false; 2704db72b40SJeff Layton smp_rmb(); 2714db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2724db72b40SJeff Layton } 2734db72b40SJeff Layton 274d1bacf9eSBryan Schumaker static 275d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 276d1bacf9eSBryan Schumaker { 277d1bacf9eSBryan Schumaker int i; 2788ef2ce3eSBryan Schumaker loff_t new_pos; 279d1bacf9eSBryan Schumaker int status = -EAGAIN; 280d1bacf9eSBryan Schumaker 281d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2828cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 283496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2840c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2850c030806STrond Myklebust 2868ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2874db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 2884db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 2890c030806STrond Myklebust ctx->duped = 0; 2900c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 29123db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2920c030806STrond Myklebust if (ctx->duped > 0 2930c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2940c030806STrond Myklebust if (printk_ratelimit()) { 2956de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 2960c030806STrond Myklebust "Please contact your server vendor. " 2979581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 2989581a4aeSJeff Layton desc->file, array->array[i].string.len, 2999581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 3000c030806STrond Myklebust } 3010c030806STrond Myklebust status = -ELOOP; 3020c030806STrond Myklebust goto out; 3030c030806STrond Myklebust } 3048ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3050c030806STrond Myklebust ctx->duped = -1; 3068ef2ce3eSBryan Schumaker } 30723db8620SAl Viro desc->ctx->pos = new_pos; 3088cd51a0cSTrond Myklebust desc->cache_entry_index = i; 30947c716cbSTrond Myklebust return 0; 3108cd51a0cSTrond Myklebust } 3118cd51a0cSTrond Myklebust } 31247c716cbSTrond Myklebust if (array->eof_index >= 0) { 313d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 31418fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 3156089dd0dSThomas Meyer desc->eof = true; 316d1bacf9eSBryan Schumaker } 3170c030806STrond Myklebust out: 318d1bacf9eSBryan Schumaker return status; 319d1bacf9eSBryan Schumaker } 320d1bacf9eSBryan Schumaker 321d1bacf9eSBryan Schumaker static 322d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 323d1bacf9eSBryan Schumaker { 324d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 32547c716cbSTrond Myklebust int status; 326d1bacf9eSBryan Schumaker 3270795bf83SFabian Frederick array = kmap(desc->page); 328d1bacf9eSBryan Schumaker 329d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 330d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 331d1bacf9eSBryan Schumaker else 332d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 333d1bacf9eSBryan Schumaker 33447c716cbSTrond Myklebust if (status == -EAGAIN) { 3350aded708STrond Myklebust desc->last_cookie = array->last_cookie; 336e47c085aSTrond Myklebust desc->current_index += array->size; 33747c716cbSTrond Myklebust desc->page_index++; 33847c716cbSTrond Myklebust } 3390795bf83SFabian Frederick kunmap(desc->page); 340d1bacf9eSBryan Schumaker return status; 341d1bacf9eSBryan Schumaker } 342d1bacf9eSBryan Schumaker 343d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 344d1bacf9eSBryan Schumaker static 34556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 346d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 347d1bacf9eSBryan Schumaker { 348480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 349684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3504704f0e2STrond Myklebust unsigned long timestamp, gencount; 3511da177e4SLinus Torvalds int error; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds again: 3541da177e4SLinus Torvalds timestamp = jiffies; 3554704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 356be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3571da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3581da177e4SLinus Torvalds if (error < 0) { 3591da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3601da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3611da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3623a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 363a7a3b1e9SBenjamin Coddington desc->plus = false; 3641da177e4SLinus Torvalds goto again; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds goto error; 3671da177e4SLinus Torvalds } 3681f4eab7eSNeil Brown desc->timestamp = timestamp; 3694704f0e2STrond Myklebust desc->gencount = gencount; 370d1bacf9eSBryan Schumaker error: 371d1bacf9eSBryan Schumaker return error; 372d1bacf9eSBryan Schumaker } 373d1bacf9eSBryan Schumaker 374573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 375573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 376d1bacf9eSBryan Schumaker { 377573c4e1eSChuck Lever int error; 378d1bacf9eSBryan Schumaker 379573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 380573c4e1eSChuck Lever if (error) 381573c4e1eSChuck Lever return error; 382d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 383d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 384d1bacf9eSBryan Schumaker return 0; 385d1bacf9eSBryan Schumaker } 386d1bacf9eSBryan Schumaker 387fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 388fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 389fa923369STrond Myklebust */ 390d39ab9deSBryan Schumaker static 391d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 392d39ab9deSBryan Schumaker { 393d8fdb47fSTrond Myklebust struct inode *inode; 394fa923369STrond Myklebust struct nfs_inode *nfsi; 395fa923369STrond Myklebust 3962b0143b5SDavid Howells if (d_really_is_negative(dentry)) 3972b0143b5SDavid Howells return 0; 398fa923369STrond Myklebust 399d8fdb47fSTrond Myklebust inode = d_inode(dentry); 400d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 401d8fdb47fSTrond Myklebust return 0; 402d8fdb47fSTrond Myklebust 403d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 4047dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 405d39ab9deSBryan Schumaker return 0; 4067dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 4077dc72d5fSTrond Myklebust return 0; 4087dc72d5fSTrond Myklebust return 1; 409d39ab9deSBryan Schumaker } 410d39ab9deSBryan Schumaker 411d39ab9deSBryan Schumaker static 41223db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 413d69ee9b8STrond Myklebust { 414d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 415d69ee9b8STrond Myklebust return false; 416d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 417d69ee9b8STrond Myklebust return true; 41823db8620SAl Viro if (ctx->pos == 0) 419d69ee9b8STrond Myklebust return true; 420d69ee9b8STrond Myklebust return false; 421d69ee9b8STrond Myklebust } 422d69ee9b8STrond Myklebust 423d69ee9b8STrond Myklebust /* 42463519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 42563519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 426d69ee9b8STrond Myklebust * directory. 427d69ee9b8STrond Myklebust */ 428d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 429d69ee9b8STrond Myklebust { 43063519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 43163519fbcSTrond Myklebust 43263519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 43363519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 43463519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 435d69ee9b8STrond Myklebust } 436d69ee9b8STrond Myklebust 437311324adSTrond Myklebust /* 438311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 439311324adSTrond Myklebust * 440311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 441311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 442311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 443311324adSTrond Myklebust * cache flush. 444311324adSTrond Myklebust */ 445311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 446311324adSTrond Myklebust { 44763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 44863519fbcSTrond Myklebust 44963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 45063519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 45163519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 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 6254b310319STrond Myklebust nfs_readdir_init_array(page); 6264b310319STrond 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: 6974b310319STrond 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 { 70509cbfeafSKirill A. Shutemov put_page(desc->page); 7061da177e4SLinus Torvalds desc->page = NULL; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 709d1bacf9eSBryan Schumaker static 710d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7111da177e4SLinus Torvalds { 712a46126ccSChristoph Hellwig return read_cache_page(desc->file->f_mapping, desc->page_index, 713a46126ccSChristoph Hellwig nfs_readdir_filler, desc); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds /* 717d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 718114de382STrond Myklebust * and locks the page to prevent removal from the page cache. 7191da177e4SLinus Torvalds */ 720d1bacf9eSBryan Schumaker static 721114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) 722d1bacf9eSBryan Schumaker { 723d1bacf9eSBryan Schumaker int res; 724d1bacf9eSBryan Schumaker 725d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 726d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 727d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 728114de382STrond Myklebust res = lock_page_killable(desc->page); 72947c716cbSTrond Myklebust if (res != 0) 730114de382STrond Myklebust goto error; 731114de382STrond Myklebust res = -EAGAIN; 732114de382STrond Myklebust if (desc->page->mapping != NULL) { 733114de382STrond Myklebust res = nfs_readdir_search_array(desc); 734114de382STrond Myklebust if (res == 0) 735114de382STrond Myklebust return 0; 736114de382STrond Myklebust } 737114de382STrond Myklebust unlock_page(desc->page); 738114de382STrond Myklebust error: 739d1bacf9eSBryan Schumaker cache_page_release(desc); 740d1bacf9eSBryan Schumaker return res; 741d1bacf9eSBryan Schumaker } 742d1bacf9eSBryan Schumaker 743d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7441da177e4SLinus Torvalds static inline 7451da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7461da177e4SLinus Torvalds { 7478cd51a0cSTrond Myklebust int res; 748d1bacf9eSBryan Schumaker 7490aded708STrond Myklebust if (desc->page_index == 0) { 7508cd51a0cSTrond Myklebust desc->current_index = 0; 7510aded708STrond Myklebust desc->last_cookie = 0; 7520aded708STrond Myklebust } 75347c716cbSTrond Myklebust do { 754114de382STrond Myklebust res = find_and_lock_cache_page(desc); 75547c716cbSTrond Myklebust } while (res == -EAGAIN); 7561da177e4SLinus Torvalds return res; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7611da177e4SLinus Torvalds */ 7621da177e4SLinus Torvalds static 76323db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds struct file *file = desc->file; 766d1bacf9eSBryan Schumaker int i = 0; 767d1bacf9eSBryan Schumaker int res = 0; 768d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7698ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7708ef2ce3eSBryan Schumaker 7710795bf83SFabian Frederick array = kmap(desc->page); 772d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 773ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7741da177e4SLinus Torvalds 775ece0b423STrond Myklebust ent = &array->array[i]; 77623db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 77723db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 7786089dd0dSThomas Meyer desc->eof = true; 7791da177e4SLinus Torvalds break; 780ece0b423STrond Myklebust } 78123db8620SAl Viro desc->ctx->pos++; 782d1bacf9eSBryan Schumaker if (i < (array->size-1)) 783d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 784d1bacf9eSBryan Schumaker else 785d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7860c030806STrond Myklebust if (ctx->duped != 0) 7870c030806STrond Myklebust ctx->duped = 1; 7888cd51a0cSTrond Myklebust } 78947c716cbSTrond Myklebust if (array->eof_index >= 0) 7906089dd0dSThomas Meyer desc->eof = true; 791d1bacf9eSBryan Schumaker 7920795bf83SFabian Frederick kunmap(desc->page); 7931e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7941e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7951da177e4SLinus Torvalds return res; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds /* 7991da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8001da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8011da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8021da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8031da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8041da177e4SLinus Torvalds * 8051da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8061da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8071da177e4SLinus Torvalds * we should already have a complete representation of the 8081da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8091da177e4SLinus Torvalds */ 8101da177e4SLinus Torvalds static inline 81123db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8121da177e4SLinus Torvalds { 8131da177e4SLinus Torvalds struct page *page = NULL; 8141da177e4SLinus Torvalds int status; 815496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8160c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8171da177e4SLinus Torvalds 8181e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8191e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8221da177e4SLinus Torvalds if (!page) { 8231da177e4SLinus Torvalds status = -ENOMEM; 8241da177e4SLinus Torvalds goto out; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8277a8e1dc3STrond Myklebust desc->page_index = 0; 8280aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8297a8e1dc3STrond Myklebust desc->page = page; 8300c030806STrond Myklebust ctx->duped = 0; 8317a8e1dc3STrond Myklebust 83285f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 83385f8607eSTrond Myklebust if (status < 0) 834d1bacf9eSBryan Schumaker goto out_release; 835d1bacf9eSBryan Schumaker 83623db8620SAl Viro status = nfs_do_filldir(desc); 8371da177e4SLinus Torvalds 838114de382STrond Myklebust out_release: 839114de382STrond Myklebust nfs_readdir_clear_array(desc->page); 840114de382STrond Myklebust cache_page_release(desc); 8411da177e4SLinus Torvalds out: 8421e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8433110ff80SHarvey Harrison __func__, status); 8441da177e4SLinus Torvalds return status; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 84700a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 84800a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 84900a92642SOlivier Galibert whole directory. 8501da177e4SLinus Torvalds */ 85123db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8521da177e4SLinus Torvalds { 853be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8542b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8551da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8561da177e4SLinus Torvalds *desc = &my_desc; 85723db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 85807b5ce8eSScott Mayhew int res = 0; 8591da177e4SLinus Torvalds 8606de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8616de1472fSAl Viro file, (long long)ctx->pos); 86291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 86391d5b470SChuck Lever 8641da177e4SLinus Torvalds /* 86523db8620SAl Viro * ctx->pos points to the dirent entry number. 866f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 86700a92642SOlivier Galibert * to either find the entry with the appropriate number or 86800a92642SOlivier Galibert * revalidate the cookie. 8691da177e4SLinus Torvalds */ 8701da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8711da177e4SLinus Torvalds 87223db8620SAl Viro desc->file = file; 87323db8620SAl Viro desc->ctx = ctx; 874480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8751da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 876a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 8771da177e4SLinus Torvalds 87879f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 87923db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 880fccca7fcSTrond Myklebust if (res < 0) 881fccca7fcSTrond Myklebust goto out; 882fccca7fcSTrond Myklebust 88347c716cbSTrond Myklebust do { 8841da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 88500a92642SOlivier Galibert 8861da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 887ece0b423STrond Myklebust res = 0; 8881da177e4SLinus Torvalds /* This means either end of directory */ 8896089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 8901da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 89123db8620SAl Viro res = uncached_readdir(desc); 892ece0b423STrond Myklebust if (res == 0) 8931da177e4SLinus Torvalds continue; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds break; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8983a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8991da177e4SLinus Torvalds nfs_zap_caches(inode); 900baf57a09STrond Myklebust desc->page_index = 0; 901a7a3b1e9SBenjamin Coddington desc->plus = false; 902a7a3b1e9SBenjamin Coddington desc->eof = false; 9031da177e4SLinus Torvalds continue; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds if (res < 0) 9061da177e4SLinus Torvalds break; 9071da177e4SLinus Torvalds 90823db8620SAl Viro res = nfs_do_filldir(desc); 909114de382STrond Myklebust unlock_page(desc->page); 910114de382STrond Myklebust cache_page_release(desc); 911ece0b423STrond Myklebust if (res < 0) 9121da177e4SLinus Torvalds break; 91347c716cbSTrond Myklebust } while (!desc->eof); 914fccca7fcSTrond Myklebust out: 9151e7cb3dcSChuck Lever if (res > 0) 9161e7cb3dcSChuck Lever res = 0; 9176de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9181da177e4SLinus Torvalds return res; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 921965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 922f0dd2136STrond Myklebust { 923b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 924480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 925b84e06c5SChuck Lever 9266de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9276de1472fSAl Viro filp, offset, whence); 928b84e06c5SChuck Lever 929965c8e59SAndrew Morton switch (whence) { 930f0dd2136STrond Myklebust default: 931b2b1ff3dSTrond Myklebust return -EINVAL; 932b2b1ff3dSTrond Myklebust case SEEK_SET: 933b2b1ff3dSTrond Myklebust if (offset < 0) 934b2b1ff3dSTrond Myklebust return -EINVAL; 935b2b1ff3dSTrond Myklebust inode_lock(inode); 936b2b1ff3dSTrond Myklebust break; 937b2b1ff3dSTrond Myklebust case SEEK_CUR: 938b2b1ff3dSTrond Myklebust if (offset == 0) 939b2b1ff3dSTrond Myklebust return filp->f_pos; 940b2b1ff3dSTrond Myklebust inode_lock(inode); 941b2b1ff3dSTrond Myklebust offset += filp->f_pos; 942b2b1ff3dSTrond Myklebust if (offset < 0) { 943b2b1ff3dSTrond Myklebust inode_unlock(inode); 944b2b1ff3dSTrond Myklebust return -EINVAL; 945b2b1ff3dSTrond Myklebust } 946f0dd2136STrond Myklebust } 947f0dd2136STrond Myklebust if (offset != filp->f_pos) { 948f0dd2136STrond Myklebust filp->f_pos = offset; 949480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9508ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 951f0dd2136STrond Myklebust } 952b044f645SBenjamin Coddington inode_unlock(inode); 953f0dd2136STrond Myklebust return offset; 954f0dd2136STrond Myklebust } 955f0dd2136STrond Myklebust 9561da177e4SLinus Torvalds /* 9571da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9581da177e4SLinus Torvalds * is a dummy operation. 9591da177e4SLinus Torvalds */ 96002c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 96102c24a82SJosef Bacik int datasync) 9621da177e4SLinus Torvalds { 9636de1472fSAl Viro struct inode *inode = file_inode(filp); 9647ea80859SChristoph Hellwig 9656de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9661e7cb3dcSChuck Lever 9675955102cSAl Viro inode_lock(inode); 9686de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9695955102cSAl Viro inode_unlock(inode); 9701da177e4SLinus Torvalds return 0; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 973bfc69a45STrond Myklebust /** 974bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 975302fad7bSTrond Myklebust * @dir: pointer to directory inode 976bfc69a45STrond Myklebust * 977bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 978bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 979bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 980bfc69a45STrond Myklebust * 981bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 982bfc69a45STrond Myklebust */ 983bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 984bfc69a45STrond Myklebust { 985011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 986bfc69a45STrond Myklebust } 98789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 988bfc69a45STrond Myklebust 9891da177e4SLinus Torvalds /* 9901da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9911da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9921da177e4SLinus Torvalds * and may need to be looked up again. 993912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 9941da177e4SLinus Torvalds */ 995912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 996912a108dSNeilBrown int rcu_walk) 9971da177e4SLinus Torvalds { 9981da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9991da177e4SLinus Torvalds return 1; 10004eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10014eec952eSTrond Myklebust return 0; 1002f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10036ecc5e8fSTrond Myklebust return 0; 1004f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 10051cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1006912a108dSNeilBrown if (rcu_walk) 1007f2c77f4eSTrond Myklebust return 0; 10081cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 10091cd9cb05STrond Myklebust return 0; 10101cd9cb05STrond Myklebust } 1011f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1012f2c77f4eSTrond Myklebust return 0; 1013f2c77f4eSTrond Myklebust return 1; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* 1017a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1018a12802caSTrond Myklebust * an O_EXCL create using this path component. 1019a12802caSTrond Myklebust */ 1020fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1021a12802caSTrond Myklebust { 1022a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1023a12802caSTrond Myklebust return 0; 1024fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1025a12802caSTrond Myklebust } 1026a12802caSTrond Myklebust 1027a12802caSTrond Myklebust /* 10281d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10291d6757fbSTrond Myklebust * 10301d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10311d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10321d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10331d6757fbSTrond Myklebust * 10341d6757fbSTrond Myklebust */ 103565a0c149STrond Myklebust static 1036fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 103965a0c149STrond Myklebust int ret; 10401da177e4SLinus Torvalds 104136d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10424e99a1ffSTrond Myklebust return 0; 104347921921STrond Myklebust 104447921921STrond Myklebust if (flags & LOOKUP_OPEN) { 104547921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 104647921921STrond Myklebust case S_IFREG: 104747921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 104847921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 104947921921STrond Myklebust goto out; 105047921921STrond Myklebust /* Fallthrough */ 105147921921STrond Myklebust case S_IFDIR: 105247921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 105347921921STrond Myklebust break; 105447921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 105547921921STrond Myklebust goto out_force; 105647921921STrond Myklebust } 105747921921STrond Myklebust } 105847921921STrond Myklebust 10591da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1060fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10611da177e4SLinus Torvalds goto out_force; 106265a0c149STrond Myklebust out: 1063a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 10641da177e4SLinus Torvalds out_force: 10651fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10661fa1e384SNeilBrown return -ECHILD; 106765a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 106865a0c149STrond Myklebust if (ret != 0) 106965a0c149STrond Myklebust return ret; 107065a0c149STrond Myklebust goto out; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /* 10741da177e4SLinus Torvalds * We judge how long we want to trust negative 10751da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10761da177e4SLinus Torvalds * 10771da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10781da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1079912a108dSNeilBrown * 1080912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1081912a108dSNeilBrown * suggesting a reval is needed. 10829f6d44d4STrond Myklebust * 10839f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 10849f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 10851da177e4SLinus Torvalds */ 10861da177e4SLinus Torvalds static inline 10871da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1088fa3c56bbSAl Viro unsigned int flags) 10891da177e4SLinus Torvalds { 10909f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 10911da177e4SLinus Torvalds return 0; 10924eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10934eec952eSTrond Myklebust return 1; 1094912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10975ceb9d7fSTrond Myklebust static int 10985ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 10995ceb9d7fSTrond Myklebust struct inode *inode, int error) 11001da177e4SLinus Torvalds { 11015ceb9d7fSTrond Myklebust switch (error) { 11025ceb9d7fSTrond Myklebust case 1: 11036de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 11046de1472fSAl Viro __func__, dentry); 11051da177e4SLinus Torvalds return 1; 11065ceb9d7fSTrond Myklebust case 0: 1107a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11081da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11091da177e4SLinus Torvalds /* Purge readdir caches. */ 11101da177e4SLinus Torvalds nfs_zap_caches(inode); 1111a3f432bfSJ. Bruce Fields /* 1112a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1113a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1114a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1115a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1116a3f432bfSJ. Bruce Fields */ 1117a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 11185ceb9d7fSTrond Myklebust return 1; 11191da177e4SLinus Torvalds } 11206de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 11216de1472fSAl Viro __func__, dentry); 11221da177e4SLinus Torvalds return 0; 11235ceb9d7fSTrond Myklebust } 11246de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 11256de1472fSAl Viro __func__, dentry, error); 1126e1fb4d05STrond Myklebust return error; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11295ceb9d7fSTrond Myklebust static int 11305ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 11315ceb9d7fSTrond Myklebust unsigned int flags) 11325ceb9d7fSTrond Myklebust { 11335ceb9d7fSTrond Myklebust int ret = 1; 11345ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 11355ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 11365ceb9d7fSTrond Myklebust return -ECHILD; 11375ceb9d7fSTrond Myklebust ret = 0; 11385ceb9d7fSTrond Myklebust } 11395ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 11405ceb9d7fSTrond Myklebust } 11415ceb9d7fSTrond Myklebust 11425ceb9d7fSTrond Myklebust static int 11435ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 11445ceb9d7fSTrond Myklebust struct inode *inode) 11455ceb9d7fSTrond Myklebust { 11465ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11475ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 11485ceb9d7fSTrond Myklebust } 11495ceb9d7fSTrond Myklebust 11505ceb9d7fSTrond Myklebust static int 11515ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 11525ceb9d7fSTrond Myklebust struct inode *inode) 11535ceb9d7fSTrond Myklebust { 11545ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 11555ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 11565ceb9d7fSTrond Myklebust struct nfs4_label *label; 11575ceb9d7fSTrond Myklebust int ret; 11585ceb9d7fSTrond Myklebust 11595ceb9d7fSTrond Myklebust ret = -ENOMEM; 11605ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 11615ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 11625ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 11635ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 11645ceb9d7fSTrond Myklebust goto out; 11655ceb9d7fSTrond Myklebust 1166f7b37b8bSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 11675ceb9d7fSTrond Myklebust if (ret < 0) { 1168f7b37b8bSTrond Myklebust switch (ret) { 1169f7b37b8bSTrond Myklebust case -ESTALE: 1170f7b37b8bSTrond Myklebust case -ENOENT: 11715ceb9d7fSTrond Myklebust ret = 0; 1172f7b37b8bSTrond Myklebust break; 1173f7b37b8bSTrond Myklebust case -ETIMEDOUT: 1174f7b37b8bSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1175f7b37b8bSTrond Myklebust ret = 1; 1176f7b37b8bSTrond Myklebust } 11775ceb9d7fSTrond Myklebust goto out; 11785ceb9d7fSTrond Myklebust } 11795ceb9d7fSTrond Myklebust ret = 0; 11805ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11815ceb9d7fSTrond Myklebust goto out; 11825ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 11835ceb9d7fSTrond Myklebust goto out; 11845ceb9d7fSTrond Myklebust 11855ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 11865ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11875ceb9d7fSTrond Myklebust 11885ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 11895ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 11905ceb9d7fSTrond Myklebust ret = 1; 11915ceb9d7fSTrond Myklebust out: 11925ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 11935ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 11945ceb9d7fSTrond Myklebust nfs4_label_free(label); 11955ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 11965ceb9d7fSTrond Myklebust } 11975ceb9d7fSTrond Myklebust 11985ceb9d7fSTrond Myklebust /* 11995ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 12005ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 12015ceb9d7fSTrond Myklebust * lookup. 12025ceb9d7fSTrond Myklebust * 12035ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 12045ceb9d7fSTrond Myklebust * we have an inode! 12055ceb9d7fSTrond Myklebust * 12065ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 12075ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 12085ceb9d7fSTrond Myklebust */ 12095ceb9d7fSTrond Myklebust static int 12105ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 12115ceb9d7fSTrond Myklebust unsigned int flags) 12125ceb9d7fSTrond Myklebust { 12135ceb9d7fSTrond Myklebust struct inode *inode; 12145ceb9d7fSTrond Myklebust int error; 12155ceb9d7fSTrond Myklebust 12165ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 12175ceb9d7fSTrond Myklebust inode = d_inode(dentry); 12185ceb9d7fSTrond Myklebust 12195ceb9d7fSTrond Myklebust if (!inode) 12205ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 12215ceb9d7fSTrond Myklebust 12225ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 12235ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12245ceb9d7fSTrond Myklebust __func__, dentry); 12255ceb9d7fSTrond Myklebust goto out_bad; 12265ceb9d7fSTrond Myklebust } 12275ceb9d7fSTrond Myklebust 12285ceb9d7fSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 12295ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 12305ceb9d7fSTrond Myklebust 12315ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 12325ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 12335ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 12345ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 12355ceb9d7fSTrond Myklebust if (error) { 12365ceb9d7fSTrond Myklebust if (error == -ESTALE) 12375ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 12385ceb9d7fSTrond Myklebust goto out_bad; 12395ceb9d7fSTrond Myklebust } 12405ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 12415ceb9d7fSTrond Myklebust goto out_valid; 12425ceb9d7fSTrond Myklebust } 12435ceb9d7fSTrond Myklebust 12445ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12455ceb9d7fSTrond Myklebust return -ECHILD; 12465ceb9d7fSTrond Myklebust 12475ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 12485ceb9d7fSTrond Myklebust goto out_bad; 12495ceb9d7fSTrond Myklebust 12505ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 12515ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 12525ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 12535ceb9d7fSTrond Myklebust return error; 12545ceb9d7fSTrond Myklebust out_valid: 12555ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12565ceb9d7fSTrond Myklebust out_bad: 12575ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12585ceb9d7fSTrond Myklebust return -ECHILD; 12595ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 12605ceb9d7fSTrond Myklebust } 12615ceb9d7fSTrond Myklebust 12625ceb9d7fSTrond Myklebust static int 1263c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1264c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 12655ceb9d7fSTrond Myklebust { 12665ceb9d7fSTrond Myklebust struct dentry *parent; 12675ceb9d7fSTrond Myklebust struct inode *dir; 12685ceb9d7fSTrond Myklebust int ret; 12695ceb9d7fSTrond Myklebust 12705ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 12715ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 12725ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 12735ceb9d7fSTrond Myklebust if (!dir) 12745ceb9d7fSTrond Myklebust return -ECHILD; 1275c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 12765ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 12775ceb9d7fSTrond Myklebust return -ECHILD; 12785ceb9d7fSTrond Myklebust } else { 12795ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1280c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 12815ceb9d7fSTrond Myklebust dput(parent); 12825ceb9d7fSTrond Myklebust } 12835ceb9d7fSTrond Myklebust return ret; 12845ceb9d7fSTrond Myklebust } 12855ceb9d7fSTrond Myklebust 1286c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1287c7944ebbSTrond Myklebust { 1288c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1289c7944ebbSTrond Myklebust } 1290c7944ebbSTrond Myklebust 12911da177e4SLinus Torvalds /* 12922b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1293ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1294ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1295ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1296ecf3d1f1SJeff Layton * 1297ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1298ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1299ecf3d1f1SJeff Layton */ 1300ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1301ecf3d1f1SJeff Layton { 13022b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 13039cdd1d3fSTrond Myklebust int error = 0; 1304ecf3d1f1SJeff Layton 1305ecf3d1f1SJeff Layton /* 1306ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1307ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1308ecf3d1f1SJeff Layton * eventually need to do something more here. 1309ecf3d1f1SJeff Layton */ 1310ecf3d1f1SJeff Layton if (!inode) { 13116de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 13126de1472fSAl Viro __func__, dentry); 1313ecf3d1f1SJeff Layton return 1; 1314ecf3d1f1SJeff Layton } 1315ecf3d1f1SJeff Layton 1316ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 13176de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13186de1472fSAl Viro __func__, dentry); 1319ecf3d1f1SJeff Layton return 0; 1320ecf3d1f1SJeff Layton } 1321ecf3d1f1SJeff Layton 1322b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1323ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1324ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1325ecf3d1f1SJeff Layton return !error; 1326ecf3d1f1SJeff Layton } 1327ecf3d1f1SJeff Layton 1328ecf3d1f1SJeff Layton /* 13291da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 13301da177e4SLinus Torvalds */ 1331fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 13321da177e4SLinus Torvalds { 13336de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13346de1472fSAl Viro dentry, dentry->d_flags); 13351da177e4SLinus Torvalds 133677f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13372b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 133877f11192STrond Myklebust return 1; 133977f11192STrond Myklebust 13401da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13411da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13421da177e4SLinus Torvalds return 1; 13431da177e4SLinus Torvalds } 13441751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 13451da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13461da177e4SLinus Torvalds * files will be cleaned up during umount */ 13471da177e4SLinus Torvalds return 1; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds return 0; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13541b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13551b83d707STrond Myklebust { 13561b83d707STrond Myklebust spin_lock(&inode->i_lock); 13571f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 135859a707b0STrond Myklebust if (inode->i_nlink > 0) 135959a707b0STrond Myklebust drop_nlink(inode); 136059a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 136116e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 136216e14375STrond Myklebust | NFS_INO_INVALID_CTIME 136359a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 136459a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 13651b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13661b83d707STrond Myklebust } 13671b83d707STrond Myklebust 13681da177e4SLinus Torvalds /* 13691da177e4SLinus Torvalds * Called when the dentry loses inode. 13701da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13711da177e4SLinus Torvalds */ 13721da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13731da177e4SLinus Torvalds { 137483672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 137583672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 137683672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 137783672d39SNeil Brown 13781da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1379e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13801f018458STrond Myklebust nfs_drop_nlink(inode); 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds iput(inode); 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 1385b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1386b1942c5fSAl Viro { 1387b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1388b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1389b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1390b1942c5fSAl Viro WARN_ON(1); 1391b1942c5fSAl Viro else 1392b1942c5fSAl Viro kfree(dentry->d_fsdata); 1393b1942c5fSAl Viro } 1394b1942c5fSAl Viro } 1395b1942c5fSAl Viro 1396f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 13971da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1398ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 13991da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14001da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 140136d43a43SDavid Howells .d_automount = nfs_d_automount, 1402b1942c5fSAl Viro .d_release = nfs_d_release, 14031da177e4SLinus Torvalds }; 1404ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 14051da177e4SLinus Torvalds 1406597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 14071da177e4SLinus Torvalds { 14081da177e4SLinus Torvalds struct dentry *res; 14091da177e4SLinus Torvalds struct inode *inode = NULL; 1410e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1411e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 14121775fd3eSDavid Quigley struct nfs4_label *label = NULL; 14131da177e4SLinus Torvalds int error; 14141da177e4SLinus Torvalds 14156de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 141691d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 14171da177e4SLinus Torvalds 1418130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1419130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 14201da177e4SLinus Torvalds 1421fd684071STrond Myklebust /* 1422fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1423fd684071STrond Myklebust * but don't hash the dentry. 1424fd684071STrond Myklebust */ 14259f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1426130f9ab7SAl Viro return NULL; 14271da177e4SLinus Torvalds 1428e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1429e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1430e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1431e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1432e1fb4d05STrond Myklebust goto out; 1433e1fb4d05STrond Myklebust 143414c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 143514c43f76SDavid Quigley if (IS_ERR(label)) 143614c43f76SDavid Quigley goto out; 143714c43f76SDavid Quigley 14386e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 1439f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 14401da177e4SLinus Torvalds if (error == -ENOENT) 14411da177e4SLinus Torvalds goto no_entry; 14421da177e4SLinus Torvalds if (error < 0) { 14431da177e4SLinus Torvalds res = ERR_PTR(error); 1444bf130914SAl Viro goto out_label; 14451da177e4SLinus Torvalds } 14461775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1447bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 144803f28e3aSTrond Myklebust if (IS_ERR(res)) 1449bf130914SAl Viro goto out_label; 145054ceac45SDavid Howells 145163519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 145263519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1453d69ee9b8STrond Myklebust 14541da177e4SLinus Torvalds no_entry: 145541d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14569eaef27bSTrond Myklebust if (res != NULL) { 14579eaef27bSTrond Myklebust if (IS_ERR(res)) 1458bf130914SAl Viro goto out_label; 14591da177e4SLinus Torvalds dentry = res; 14609eaef27bSTrond Myklebust } 14611da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1462bf130914SAl Viro out_label: 14636e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 146414c43f76SDavid Quigley nfs4_label_free(label); 14651da177e4SLinus Torvalds out: 1466e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1467e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14681da177e4SLinus Torvalds return res; 14691da177e4SLinus Torvalds } 1470ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14711da177e4SLinus Torvalds 147289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14730b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14741da177e4SLinus Torvalds 1475f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14760ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1477b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 14781da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14791da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 148036d43a43SDavid Howells .d_automount = nfs_d_automount, 1481b1942c5fSAl Viro .d_release = nfs_d_release, 14821da177e4SLinus Torvalds }; 148389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14841da177e4SLinus Torvalds 14858a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14868a5e929dSAl Viro { 14878a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14888a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14898a5e929dSAl Viro res |= FMODE_READ; 14908a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14918a5e929dSAl Viro res |= FMODE_WRITE; 14928a5e929dSAl Viro return res; 14938a5e929dSAl Viro } 14948a5e929dSAl Viro 1495532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1496cd9a1c0eSTrond Myklebust { 1497532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1498cd9a1c0eSTrond Myklebust } 1499cd9a1c0eSTrond Myklebust 1500cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1501cd9a1c0eSTrond Myklebust { 1502f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1503cd9a1c0eSTrond Myklebust return 0; 1504cd9a1c0eSTrond Myklebust } 1505cd9a1c0eSTrond Myklebust 1506d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 15070dd2b474SMiklos Szeredi struct dentry *dentry, 1508b452a458SAl Viro struct file *file, unsigned open_flags) 1509cd9a1c0eSTrond Myklebust { 15100dd2b474SMiklos Szeredi int err; 15110dd2b474SMiklos Szeredi 1512be12af3eSAl Viro err = finish_open(file, dentry, do_open); 151330d90494SAl Viro if (err) 1514d9585277SAl Viro goto out; 1515eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 151630d90494SAl Viro nfs_file_set_open_context(file, ctx); 1517eaa2b82cSNeilBrown else 15189821421aSTrond Myklebust err = -EOPENSTALE; 1519cd9a1c0eSTrond Myklebust out: 1520d9585277SAl Viro return err; 1521cd9a1c0eSTrond Myklebust } 1522cd9a1c0eSTrond Myklebust 152373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 152430d90494SAl Viro struct file *file, unsigned open_flags, 152544907d79SAl Viro umode_t mode) 15261da177e4SLinus Torvalds { 1527c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1528cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 15290dd2b474SMiklos Szeredi struct dentry *res; 15300dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1531f46e0bd3STrond Myklebust struct inode *inode; 15321472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1533c94c0953SAl Viro bool switched = false; 153473a09dd9SAl Viro int created = 0; 1535898f635cSTrond Myklebust int err; 15361da177e4SLinus Torvalds 15370dd2b474SMiklos Szeredi /* Expect a negative dentry */ 15382b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 15390dd2b474SMiklos Szeredi 15401e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 15416de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15421e7cb3dcSChuck Lever 15439597c13bSJeff Layton err = nfs_check_flags(open_flags); 15449597c13bSJeff Layton if (err) 15459597c13bSJeff Layton return err; 15469597c13bSJeff Layton 15470dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15480dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 154900699ad8SAl Viro if (!d_in_lookup(dentry)) { 15500dd2b474SMiklos Szeredi /* 15510dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15520dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15530dd2b474SMiklos Szeredi * again 15540dd2b474SMiklos Szeredi */ 1555d9585277SAl Viro return -ENOENT; 15560dd2b474SMiklos Szeredi } 15571472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15581da177e4SLinus Torvalds goto no_open; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15610dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1562d9585277SAl Viro return -ENAMETOOLONG; 15631da177e4SLinus Torvalds 15640dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1565dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1566dff25ddbSAndreas Gruenbacher 1567dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1568dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1569dff25ddbSAndreas Gruenbacher 1570536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1571dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 15720dd2b474SMiklos Szeredi } 1573536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1574536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1575536e43d1STrond Myklebust attr.ia_size = 0; 1576cd9a1c0eSTrond Myklebust } 1577cd9a1c0eSTrond Myklebust 1578c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1579c94c0953SAl Viro d_drop(dentry); 1580c94c0953SAl Viro switched = true; 1581c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1582c94c0953SAl Viro &dentry->d_name, &wq); 1583c94c0953SAl Viro if (IS_ERR(dentry)) 1584c94c0953SAl Viro return PTR_ERR(dentry); 1585c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1586c94c0953SAl Viro return finish_no_open(file, dentry); 1587c94c0953SAl Viro } 1588c94c0953SAl Viro 1589532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 15900dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15910dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1592d9585277SAl Viro goto out; 15930dd2b474SMiklos Szeredi 15946e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 159573a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 159673a09dd9SAl Viro if (created) 159773a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1598275bb307STrond Myklebust if (IS_ERR(inode)) { 15990dd2b474SMiklos Szeredi err = PTR_ERR(inode); 16006e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16012d9db750STrond Myklebust put_nfs_open_context(ctx); 1602d20cb71dSAl Viro d_drop(dentry); 16030dd2b474SMiklos Szeredi switch (err) { 16041da177e4SLinus Torvalds case -ENOENT: 1605774d9513SPeng Tao d_splice_alias(NULL, dentry); 1606809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16070dd2b474SMiklos Szeredi break; 16081788ea6eSJeff Layton case -EISDIR: 16096f926b5bSTrond Myklebust case -ENOTDIR: 16106f926b5bSTrond Myklebust goto no_open; 16111da177e4SLinus Torvalds case -ELOOP: 16120dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 16131da177e4SLinus Torvalds goto no_open; 16140dd2b474SMiklos Szeredi break; 16151da177e4SLinus Torvalds /* case -EINVAL: */ 16161da177e4SLinus Torvalds default: 16170dd2b474SMiklos Szeredi break; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds goto out; 16201da177e4SLinus Torvalds } 16210dd2b474SMiklos Szeredi 1622b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 16236e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16242d9db750STrond Myklebust put_nfs_open_context(ctx); 1625d9585277SAl Viro out: 1626c94c0953SAl Viro if (unlikely(switched)) { 1627c94c0953SAl Viro d_lookup_done(dentry); 1628c94c0953SAl Viro dput(dentry); 1629c94c0953SAl Viro } 1630d9585277SAl Viro return err; 16310dd2b474SMiklos Szeredi 16321da177e4SLinus Torvalds no_open: 16331472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1634c94c0953SAl Viro if (switched) { 1635c94c0953SAl Viro d_lookup_done(dentry); 1636c94c0953SAl Viro if (!res) 1637c94c0953SAl Viro res = dentry; 1638c94c0953SAl Viro else 1639c94c0953SAl Viro dput(dentry); 1640c94c0953SAl Viro } 16410dd2b474SMiklos Szeredi if (IS_ERR(res)) 1642c94c0953SAl Viro return PTR_ERR(res); 1643e45198a6SAl Viro return finish_no_open(file, res); 16441da177e4SLinus Torvalds } 164589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 16461da177e4SLinus Torvalds 1647c7944ebbSTrond Myklebust static int 1648c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1649c7944ebbSTrond Myklebust unsigned int flags) 16501da177e4SLinus Torvalds { 1651657e94b6SNick Piggin struct inode *inode; 16521da177e4SLinus Torvalds 1653fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1654c7944ebbSTrond Myklebust goto full_reval; 1655eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1656c7944ebbSTrond Myklebust goto full_reval; 16572b484297STrond Myklebust 16582b0143b5SDavid Howells inode = d_inode(dentry); 16592b484297STrond Myklebust 16601da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 16611da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 16621da177e4SLinus Torvalds */ 1663c7944ebbSTrond Myklebust if (inode == NULL) 1664c7944ebbSTrond Myklebust goto full_reval; 166549317a7fSNeilBrown 1666c7944ebbSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 1667c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1668216d5d06STrond Myklebust 16691da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16701da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1671c7944ebbSTrond Myklebust goto full_reval; 1672c7944ebbSTrond Myklebust 16731da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1674c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1675c7944ebbSTrond Myklebust goto reval_dentry; 1676c7944ebbSTrond Myklebust 1677c7944ebbSTrond Myklebust /* Check if the directory changed */ 1678c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1679c7944ebbSTrond Myklebust goto reval_dentry; 16801da177e4SLinus Torvalds 16810ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1682c7944ebbSTrond Myklebust return 1; 1683c7944ebbSTrond Myklebust reval_dentry: 1684c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1685c7944ebbSTrond Myklebust return -ECHILD; 168642f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 16870ef97dcfSMiklos Szeredi 1688c7944ebbSTrond Myklebust full_reval: 1689c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1690c7944ebbSTrond Myklebust } 1691535918f1STrond Myklebust 1692c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1693c7944ebbSTrond Myklebust { 1694c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1695c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1696c0204fd2STrond Myklebust } 1697c0204fd2STrond Myklebust 16981da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16991da177e4SLinus Torvalds 1700406cd915SBenjamin Coddington struct dentry * 1701406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 17021775fd3eSDavid Quigley struct nfs_fattr *fattr, 17031775fd3eSDavid Quigley struct nfs4_label *label) 17041da177e4SLinus Torvalds { 1705fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 17062b0143b5SDavid Howells struct inode *dir = d_inode(parent); 17071da177e4SLinus Torvalds struct inode *inode; 1708b0c6108eSAl Viro struct dentry *d; 1709406cd915SBenjamin Coddington int error; 17101da177e4SLinus Torvalds 1711fab728e1STrond Myklebust d_drop(dentry); 1712fab728e1STrond Myklebust 17131da177e4SLinus Torvalds if (fhandle->size == 0) { 1714f7b37b8bSTrond Myklebust error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 17151da177e4SLinus Torvalds if (error) 1716fab728e1STrond Myklebust goto out_error; 17171da177e4SLinus Torvalds } 17185724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17191da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 17201da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1721a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1722a841b54dSTrond Myklebust fattr, NULL, NULL); 17231da177e4SLinus Torvalds if (error < 0) 1724fab728e1STrond Myklebust goto out_error; 17251da177e4SLinus Torvalds } 17261775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1727b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1728fab728e1STrond Myklebust out: 1729fab728e1STrond Myklebust dput(parent); 1730406cd915SBenjamin Coddington return d; 1731fab728e1STrond Myklebust out_error: 1732fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1733406cd915SBenjamin Coddington d = ERR_PTR(error); 1734406cd915SBenjamin Coddington goto out; 1735406cd915SBenjamin Coddington } 1736406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 1737406cd915SBenjamin Coddington 1738406cd915SBenjamin Coddington /* 1739406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod. 1740406cd915SBenjamin Coddington */ 1741406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 1742406cd915SBenjamin Coddington struct nfs_fattr *fattr, 1743406cd915SBenjamin Coddington struct nfs4_label *label) 1744406cd915SBenjamin Coddington { 1745406cd915SBenjamin Coddington struct dentry *d; 1746406cd915SBenjamin Coddington 1747406cd915SBenjamin Coddington d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 1748406cd915SBenjamin Coddington if (IS_ERR(d)) 1749406cd915SBenjamin Coddington return PTR_ERR(d); 1750406cd915SBenjamin Coddington 1751406cd915SBenjamin Coddington /* Callers don't care */ 1752406cd915SBenjamin Coddington dput(d); 1753406cd915SBenjamin Coddington return 0; 17541da177e4SLinus Torvalds } 1755ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds /* 17581da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17591da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17601da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17611da177e4SLinus Torvalds * reply path made it appear to have failed. 17621da177e4SLinus Torvalds */ 1763597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1764ebfc3b49SAl Viro umode_t mode, bool excl) 17651da177e4SLinus Torvalds { 17661da177e4SLinus Torvalds struct iattr attr; 1767ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 17681da177e4SLinus Torvalds int error; 17691da177e4SLinus Torvalds 17701e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 17716de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds attr.ia_mode = mode; 17741da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17751da177e4SLinus Torvalds 17768b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17778867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17788b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17791da177e4SLinus Torvalds if (error != 0) 17801da177e4SLinus Torvalds goto out_err; 17811da177e4SLinus Torvalds return 0; 17821da177e4SLinus Torvalds out_err: 17831da177e4SLinus Torvalds d_drop(dentry); 17841da177e4SLinus Torvalds return error; 17851da177e4SLinus Torvalds } 1786ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds /* 17891da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17901da177e4SLinus Torvalds */ 1791597d9289SBryan Schumaker int 17921a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds struct iattr attr; 17951da177e4SLinus Torvalds int status; 17961da177e4SLinus Torvalds 17971e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 17986de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds attr.ia_mode = mode; 18011da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18021da177e4SLinus Torvalds 18031ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 18041da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 18051ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 18061da177e4SLinus Torvalds if (status != 0) 18071da177e4SLinus Torvalds goto out_err; 18081da177e4SLinus Torvalds return 0; 18091da177e4SLinus Torvalds out_err: 18101da177e4SLinus Torvalds d_drop(dentry); 18111da177e4SLinus Torvalds return status; 18121da177e4SLinus Torvalds } 1813ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds /* 18161da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18171da177e4SLinus Torvalds */ 1818597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 18191da177e4SLinus Torvalds { 18201da177e4SLinus Torvalds struct iattr attr; 18211da177e4SLinus Torvalds int error; 18221da177e4SLinus Torvalds 18231e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 18246de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18271da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 18281da177e4SLinus Torvalds 18291ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 18301da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 18311ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 18321da177e4SLinus Torvalds if (error != 0) 18331da177e4SLinus Torvalds goto out_err; 18341da177e4SLinus Torvalds return 0; 18351da177e4SLinus Torvalds out_err: 18361da177e4SLinus Torvalds d_drop(dentry); 18371da177e4SLinus Torvalds return error; 18381da177e4SLinus Torvalds } 1839ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 18401da177e4SLinus Torvalds 1841d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1842d45b9d8bSTrond Myklebust { 1843dc3f4198SAl Viro if (simple_positive(dentry)) 1844d45b9d8bSTrond Myklebust d_delete(dentry); 1845d45b9d8bSTrond Myklebust } 1846d45b9d8bSTrond Myklebust 1847597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 18481da177e4SLinus Torvalds { 18491da177e4SLinus Torvalds int error; 18501da177e4SLinus Torvalds 18511e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 18526de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18531da177e4SLinus Torvalds 18541ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18552b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1856884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18571da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18581da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1859ba6c0592STrond Myklebust switch (error) { 1860ba6c0592STrond Myklebust case 0: 18612b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1862ba6c0592STrond Myklebust break; 1863ba6c0592STrond Myklebust case -ENOENT: 1864d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1865ba6c0592STrond Myklebust } 1866884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1867ba6c0592STrond Myklebust } else 1868ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18691ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds return error; 18721da177e4SLinus Torvalds } 1873ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds /* 18761da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18771da177e4SLinus Torvalds * and after checking that the file has only one user. 18781da177e4SLinus Torvalds * 18791da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18801da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18811da177e4SLinus Torvalds */ 18821da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18831da177e4SLinus Torvalds { 18842b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18852b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18861da177e4SLinus Torvalds int error = -EBUSY; 18871da177e4SLinus Torvalds 18886de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18911da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18921da177e4SLinus Torvalds error = 0; 18931da177e4SLinus Torvalds goto out; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 18961ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 18971da177e4SLinus Torvalds if (inode != NULL) { 1898912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 18991da177e4SLinus Torvalds if (error == 0) 19001b83d707STrond Myklebust nfs_drop_nlink(inode); 19011da177e4SLinus Torvalds } else 1902912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 1903d45b9d8bSTrond Myklebust if (error == -ENOENT) 1904d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 19051ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 19061da177e4SLinus Torvalds out: 19071da177e4SLinus Torvalds return error; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 19111da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 19121da177e4SLinus Torvalds * 19131da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 19141da177e4SLinus Torvalds */ 1915597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 19161da177e4SLinus Torvalds { 19171da177e4SLinus Torvalds int error; 19181da177e4SLinus Torvalds int need_rehash = 0; 19191da177e4SLinus Torvalds 19201e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 19216de1472fSAl Viro dir->i_ino, dentry); 19221da177e4SLinus Torvalds 19231ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 19241da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 192584d08fa8SAl Viro if (d_count(dentry) > 1) { 19261da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1927ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 19282b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 19291da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 19301ca42382STrond Myklebust goto out; 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 19331da177e4SLinus Torvalds __d_drop(dentry); 19341da177e4SLinus Torvalds need_rehash = 1; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 19371da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1938d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 19391da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19401da177e4SLinus Torvalds } else if (need_rehash) 19411da177e4SLinus Torvalds d_rehash(dentry); 19421ca42382STrond Myklebust out: 19431ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 19441da177e4SLinus Torvalds return error; 19451da177e4SLinus Torvalds } 1946ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 19471da177e4SLinus Torvalds 1948873101b3SChuck Lever /* 1949873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1950873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1951873101b3SChuck Lever * using prepare_write/commit_write. 1952873101b3SChuck Lever * 1953873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1954873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1955873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1956873101b3SChuck Lever * symlink request has completed on the server. 1957873101b3SChuck Lever * 1958873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1959873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1960873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1961873101b3SChuck Lever * and move the raw page into its mapping. 1962873101b3SChuck Lever */ 1963597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19641da177e4SLinus Torvalds { 1965873101b3SChuck Lever struct page *page; 1966873101b3SChuck Lever char *kaddr; 19671da177e4SLinus Torvalds struct iattr attr; 1968873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19691da177e4SLinus Torvalds int error; 19701da177e4SLinus Torvalds 19711e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 19726de1472fSAl Viro dir->i_ino, dentry, symname); 19731da177e4SLinus Torvalds 1974873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1975873101b3SChuck Lever return -ENAMETOOLONG; 19761da177e4SLinus Torvalds 1977873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1978873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19791da177e4SLinus Torvalds 1980e8ecde25SAl Viro page = alloc_page(GFP_USER); 198176566991STrond Myklebust if (!page) 1982873101b3SChuck Lever return -ENOMEM; 1983873101b3SChuck Lever 1984e8ecde25SAl Viro kaddr = page_address(page); 1985873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1986873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1987873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1988873101b3SChuck Lever 19891ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 199094a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19911ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1992873101b3SChuck Lever if (error != 0) { 19931e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1994873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 19956de1472fSAl Viro dentry, symname, error); 19961da177e4SLinus Torvalds d_drop(dentry); 1997873101b3SChuck Lever __free_page(page); 19981da177e4SLinus Torvalds return error; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 2001873101b3SChuck Lever /* 2002873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2003873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2004873101b3SChuck Lever */ 20052b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2006873101b3SChuck Lever GFP_KERNEL)) { 2007873101b3SChuck Lever SetPageUptodate(page); 2008873101b3SChuck Lever unlock_page(page); 2009a0b54addSRafael Aquini /* 2010a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2011a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2012a0b54addSRafael Aquini */ 201309cbfeafSKirill A. Shutemov put_page(page); 2014873101b3SChuck Lever } else 2015873101b3SChuck Lever __free_page(page); 2016873101b3SChuck Lever 2017873101b3SChuck Lever return 0; 2018873101b3SChuck Lever } 2019ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2020873101b3SChuck Lever 2021597d9289SBryan Schumaker int 20221da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20231da177e4SLinus Torvalds { 20242b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 20251da177e4SLinus Torvalds int error; 20261da177e4SLinus Torvalds 20276de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 20286de1472fSAl Viro old_dentry, dentry); 20291da177e4SLinus Torvalds 20301fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 20319697d234STrond Myklebust d_drop(dentry); 20321da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2033cf809556STrond Myklebust if (error == 0) { 20347de9c6eeSAl Viro ihold(inode); 20359697d234STrond Myklebust d_add(dentry, inode); 2036cf809556STrond Myklebust } 20371fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 20381da177e4SLinus Torvalds return error; 20391da177e4SLinus Torvalds } 2040ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /* 20431da177e4SLinus Torvalds * RENAME 20441da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20451da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20461da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20471da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20481da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20491da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20501da177e4SLinus Torvalds * 20511da177e4SLinus Torvalds * FIXED. 20521da177e4SLinus Torvalds * 20531da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20541da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20551da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20561da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20571da177e4SLinus Torvalds * using the inode layer 20581da177e4SLinus Torvalds * 20591da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20601da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20611da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20621da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20631da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20641da177e4SLinus Torvalds * the rename. 20651da177e4SLinus Torvalds */ 2066597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 20671cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 20681cd66c93SMiklos Szeredi unsigned int flags) 20691da177e4SLinus Torvalds { 20702b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 20712b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2072d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 207380a491fdSJeff Layton struct rpc_task *task; 20741da177e4SLinus Torvalds int error = -EBUSY; 20751da177e4SLinus Torvalds 20761cd66c93SMiklos Szeredi if (flags) 20771cd66c93SMiklos Szeredi return -EINVAL; 20781cd66c93SMiklos Szeredi 20796de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20806de1472fSAl Viro old_dentry, new_dentry, 208184d08fa8SAl Viro d_count(new_dentry)); 20821da177e4SLinus Torvalds 208370ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20841da177e4SLinus Torvalds /* 208528f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 208628f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 208728f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 208828f79a1aSMiklos Szeredi * the new target. 20891da177e4SLinus Torvalds */ 209027226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 209127226104SMiklos Szeredi /* 209227226104SMiklos Szeredi * To prevent any new references to the target during the 209327226104SMiklos Szeredi * rename, we unhash the dentry in advance. 209427226104SMiklos Szeredi */ 2095d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 209627226104SMiklos Szeredi d_drop(new_dentry); 2097d9f29500SBenjamin Coddington rehash = new_dentry; 2098d9f29500SBenjamin Coddington } 209927226104SMiklos Szeredi 210084d08fa8SAl Viro if (d_count(new_dentry) > 2) { 21011da177e4SLinus Torvalds int err; 210227226104SMiklos Szeredi 21031da177e4SLinus Torvalds /* copy the target dentry's name */ 21041da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 21051da177e4SLinus Torvalds &new_dentry->d_name); 21061da177e4SLinus Torvalds if (!dentry) 21071da177e4SLinus Torvalds goto out; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds /* silly-rename the existing target ... */ 21101da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 211124e93025SMiklos Szeredi if (err) 21121da177e4SLinus Torvalds goto out; 211324e93025SMiklos Szeredi 211424e93025SMiklos Szeredi new_dentry = dentry; 2115d9f29500SBenjamin Coddington rehash = NULL; 211624e93025SMiklos Szeredi new_inode = NULL; 2117b1e4adf4STrond Myklebust } 211827226104SMiklos Szeredi } 21191da177e4SLinus Torvalds 2120d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 212180a491fdSJeff Layton if (IS_ERR(task)) { 212280a491fdSJeff Layton error = PTR_ERR(task); 212380a491fdSJeff Layton goto out; 212480a491fdSJeff Layton } 212580a491fdSJeff Layton 212680a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2127818a8dbeSBenjamin Coddington if (error != 0) { 2128818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2129818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2130818a8dbeSBenjamin Coddington smp_wmb(); 2131818a8dbeSBenjamin Coddington } else 213280a491fdSJeff Layton error = task->tk_status; 213380a491fdSJeff Layton rpc_put_task(task); 213459a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 213559a707b0STrond Myklebust if (error == 0) { 213659a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 213759a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 213859a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 213959a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 214059a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 214159a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 214259a707b0STrond Myklebust } 21431da177e4SLinus Torvalds out: 2144d9f29500SBenjamin Coddington if (rehash) 2145d9f29500SBenjamin Coddington d_rehash(rehash); 214670ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 214770ded201STrond Myklebust new_dir, new_dentry, error); 2148d9f29500SBenjamin Coddington if (!error) { 2149d9f29500SBenjamin Coddington if (new_inode != NULL) 2150d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2151d9f29500SBenjamin Coddington /* 2152d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2153d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2154d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2155d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2156d9f29500SBenjamin Coddington */ 2157d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2158d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2159d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2160d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2161d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2162d9f29500SBenjamin Coddington 21631da177e4SLinus Torvalds /* new dentry created? */ 21641da177e4SLinus Torvalds if (dentry) 21651da177e4SLinus Torvalds dput(dentry); 21661da177e4SLinus Torvalds return error; 21671da177e4SLinus Torvalds } 2168ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 21691da177e4SLinus Torvalds 2170cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2171cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2172cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2173cfcea3e8STrond Myklebust 21743a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 21753a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 21763a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 21773a505845STrond Myklebust 21781c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 21791c3c07e9STrond Myklebust { 2180b68572e0SNeilBrown put_cred(entry->cred); 2181f682a398SNeilBrown kfree_rcu(entry, rcu_head); 21824e857c58SPeter Zijlstra smp_mb__before_atomic(); 2183cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 21844e857c58SPeter Zijlstra smp_mb__after_atomic(); 21851c3c07e9STrond Myklebust } 21861c3c07e9STrond Myklebust 21871a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 21881a81bb8aSTrond Myklebust { 21891a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 21901a81bb8aSTrond Myklebust 21911a81bb8aSTrond Myklebust while (!list_empty(head)) { 21921a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 21931a81bb8aSTrond Myklebust list_del(&cache->lru); 21941a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 21951a81bb8aSTrond Myklebust } 21961a81bb8aSTrond Myklebust } 21971a81bb8aSTrond Myklebust 21983a505845STrond Myklebust static unsigned long 21993a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2200979df72eSTrond Myklebust { 2201979df72eSTrond Myklebust LIST_HEAD(head); 2202aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2203979df72eSTrond Myklebust struct nfs_access_entry *cache; 22041ab6c499SDave Chinner long freed = 0; 2205979df72eSTrond Myklebust 2206a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2207aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2208979df72eSTrond Myklebust struct inode *inode; 2209979df72eSTrond Myklebust 2210979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2211979df72eSTrond Myklebust break; 22129c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2213979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2214979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2215979df72eSTrond Myklebust goto remove_lru_entry; 2216979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2217979df72eSTrond Myklebust struct nfs_access_entry, lru); 2218979df72eSTrond Myklebust list_move(&cache->lru, &head); 2219979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 22201ab6c499SDave Chinner freed++; 2221979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2222979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2223979df72eSTrond Myklebust &nfs_access_lru_list); 2224979df72eSTrond Myklebust else { 2225979df72eSTrond Myklebust remove_lru_entry: 2226979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 22274e857c58SPeter Zijlstra smp_mb__before_atomic(); 2228979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 22294e857c58SPeter Zijlstra smp_mb__after_atomic(); 2230979df72eSTrond Myklebust } 223159844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2232979df72eSTrond Myklebust } 2233979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22341a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22351ab6c499SDave Chinner return freed; 22361ab6c499SDave Chinner } 22371ab6c499SDave Chinner 22381ab6c499SDave Chinner unsigned long 22393a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 22403a505845STrond Myklebust { 22413a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 22423a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 22433a505845STrond Myklebust 22443a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 22453a505845STrond Myklebust return SHRINK_STOP; 22463a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 22473a505845STrond Myklebust } 22483a505845STrond Myklebust 22493a505845STrond Myklebust 22503a505845STrond Myklebust unsigned long 22511ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 22521ab6c499SDave Chinner { 225355f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2254979df72eSTrond Myklebust } 2255979df72eSTrond Myklebust 22563a505845STrond Myklebust static void 22573a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22583a505845STrond Myklebust { 22593a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22603a505845STrond Myklebust unsigned long diff; 22613a505845STrond Myklebust unsigned int nr_to_scan; 22623a505845STrond Myklebust 22633a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22643a505845STrond Myklebust return; 22653a505845STrond Myklebust nr_to_scan = 100; 22663a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 22673a505845STrond Myklebust if (diff < nr_to_scan) 22683a505845STrond Myklebust nr_to_scan = diff; 22693a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 22703a505845STrond Myklebust } 22713a505845STrond Myklebust 22721a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 22731c3c07e9STrond Myklebust { 22741c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22751a81bb8aSTrond Myklebust struct rb_node *n; 22761c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22771c3c07e9STrond Myklebust 22781c3c07e9STrond Myklebust /* Unhook entries from the cache */ 22791c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 22801c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22811c3c07e9STrond Myklebust rb_erase(n, root_node); 22821a81bb8aSTrond Myklebust list_move(&entry->lru, head); 22831c3c07e9STrond Myklebust } 22841c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 22851c3c07e9STrond Myklebust } 22861c3c07e9STrond Myklebust 22871c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 22881c3c07e9STrond Myklebust { 22891a81bb8aSTrond Myklebust LIST_HEAD(head); 22901a81bb8aSTrond Myklebust 22911a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 22921a81bb8aSTrond Myklebust return; 2293cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2294cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22951a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2296cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2297cfcea3e8STrond Myklebust 22981c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22991a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 23001a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23011a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23021a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23031c3c07e9STrond Myklebust } 23041c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 23051c3c07e9STrond Myklebust 2306b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 23071c3c07e9STrond Myklebust { 23081c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 23091c3c07e9STrond Myklebust 23101c3c07e9STrond Myklebust while (n != NULL) { 2311b68572e0SNeilBrown struct nfs_access_entry *entry = 2312b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2313b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 23141c3c07e9STrond Myklebust 2315b68572e0SNeilBrown if (cmp < 0) 23161c3c07e9STrond Myklebust n = n->rb_left; 2317b68572e0SNeilBrown else if (cmp > 0) 23181c3c07e9STrond Myklebust n = n->rb_right; 23191c3c07e9STrond Myklebust else 23201c3c07e9STrond Myklebust return entry; 23211c3c07e9STrond Myklebust } 23221c3c07e9STrond Myklebust return NULL; 23231c3c07e9STrond Myklebust } 23241c3c07e9STrond Myklebust 2325b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 23261da177e4SLinus Torvalds { 232755296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 23281c3c07e9STrond Myklebust struct nfs_access_entry *cache; 232957b69181STrond Myklebust bool retry = true; 233057b69181STrond Myklebust int err; 23311da177e4SLinus Torvalds 23321c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 233357b69181STrond Myklebust for(;;) { 23341c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 23351c3c07e9STrond Myklebust goto out_zap; 23361c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 233757b69181STrond Myklebust err = -ENOENT; 23381c3c07e9STrond Myklebust if (cache == NULL) 23391c3c07e9STrond Myklebust goto out; 234057b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 234121c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 234257b69181STrond Myklebust break; 23435c965db8STrond Myklebust if (!retry) 23445c965db8STrond Myklebust break; 234557b69181STrond Myklebust err = -ECHILD; 234657b69181STrond Myklebust if (!may_block) 234757b69181STrond Myklebust goto out; 234857b69181STrond Myklebust spin_unlock(&inode->i_lock); 234957b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 235057b69181STrond Myklebust if (err) 235157b69181STrond Myklebust return err; 235257b69181STrond Myklebust spin_lock(&inode->i_lock); 235357b69181STrond Myklebust retry = false; 235457b69181STrond Myklebust } 23551c3c07e9STrond Myklebust res->cred = cache->cred; 23561c3c07e9STrond Myklebust res->mask = cache->mask; 2357cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23581c3c07e9STrond Myklebust err = 0; 23591c3c07e9STrond Myklebust out: 23601c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23611c3c07e9STrond Myklebust return err; 23621c3c07e9STrond Myklebust out_zap: 23631a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23641a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 23651c3c07e9STrond Myklebust return -ENOENT; 23661c3c07e9STrond Myklebust } 23671c3c07e9STrond Myklebust 2368b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2369f682a398SNeilBrown { 2370f682a398SNeilBrown /* Only check the most recently returned cache entry, 2371f682a398SNeilBrown * but do it without locking. 2372f682a398SNeilBrown */ 2373f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2374f682a398SNeilBrown struct nfs_access_entry *cache; 2375f682a398SNeilBrown int err = -ECHILD; 2376f682a398SNeilBrown struct list_head *lh; 2377f682a398SNeilBrown 2378f682a398SNeilBrown rcu_read_lock(); 2379f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2380f682a398SNeilBrown goto out; 2381f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2382f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2383f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 23849a206de2STrond Myklebust cred_fscmp(cred, cache->cred) != 0) 2385f682a398SNeilBrown cache = NULL; 2386f682a398SNeilBrown if (cache == NULL) 2387f682a398SNeilBrown goto out; 238821c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2389f682a398SNeilBrown goto out; 2390f682a398SNeilBrown res->cred = cache->cred; 2391f682a398SNeilBrown res->mask = cache->mask; 239221c3ba7eSTrond Myklebust err = 0; 2393f682a398SNeilBrown out: 2394f682a398SNeilBrown rcu_read_unlock(); 2395f682a398SNeilBrown return err; 2396f682a398SNeilBrown } 2397f682a398SNeilBrown 23981c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 23991c3c07e9STrond Myklebust { 2400cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2401cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24021c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 24031c3c07e9STrond Myklebust struct rb_node *parent = NULL; 24041c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2405b68572e0SNeilBrown int cmp; 24061c3c07e9STrond Myklebust 24071c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24081c3c07e9STrond Myklebust while (*p != NULL) { 24091c3c07e9STrond Myklebust parent = *p; 24101c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2411b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 24121c3c07e9STrond Myklebust 2413b68572e0SNeilBrown if (cmp < 0) 24141c3c07e9STrond Myklebust p = &parent->rb_left; 2415b68572e0SNeilBrown else if (cmp > 0) 24161c3c07e9STrond Myklebust p = &parent->rb_right; 24171c3c07e9STrond Myklebust else 24181c3c07e9STrond Myklebust goto found; 24191c3c07e9STrond Myklebust } 24201c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 24211c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2422cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 24231c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24241c3c07e9STrond Myklebust return; 24251c3c07e9STrond Myklebust found: 24261c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2427cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2428cfcea3e8STrond Myklebust list_del(&entry->lru); 24291c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24301c3c07e9STrond Myklebust nfs_access_free_entry(entry); 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds 24336168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 24341da177e4SLinus Torvalds { 24351c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 24361c3c07e9STrond Myklebust if (cache == NULL) 24371c3c07e9STrond Myklebust return; 24381c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2439b68572e0SNeilBrown cache->cred = get_cred(set->cred); 24401da177e4SLinus Torvalds cache->mask = set->mask; 24411c3c07e9STrond Myklebust 2442f682a398SNeilBrown /* The above field assignments must be visible 2443f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2444f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2445f682a398SNeilBrown */ 2446f682a398SNeilBrown smp_wmb(); 24471c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2448cfcea3e8STrond Myklebust 2449cfcea3e8STrond Myklebust /* Update accounting */ 24504e857c58SPeter Zijlstra smp_mb__before_atomic(); 2451cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 24524e857c58SPeter Zijlstra smp_mb__after_atomic(); 2453cfcea3e8STrond Myklebust 2454cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24551a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2456cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24571a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24581a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24591a81bb8aSTrond Myklebust &nfs_access_lru_list); 2460cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2461cfcea3e8STrond Myklebust } 24623a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24631da177e4SLinus Torvalds } 24646168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 24656168f62cSWeston Andros Adamson 24663c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 24673c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24683c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 24693c181827SAnna Schumaker NFS_ACCESS_DELETE) 24703c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24713c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2472ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 24733c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 24743c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 247515d4b73aSTrond Myklebust static int 2476ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 247715d4b73aSTrond Myklebust { 247815d4b73aSTrond Myklebust int mask = 0; 247915d4b73aSTrond Myklebust 248015d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 248115d4b73aSTrond Myklebust mask |= MAY_READ; 2482ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2483ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 248415d4b73aSTrond Myklebust mask |= MAY_WRITE; 2485ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 248615d4b73aSTrond Myklebust mask |= MAY_EXEC; 2487ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2488ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2489ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2490ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 249115d4b73aSTrond Myklebust mask |= MAY_EXEC; 2492ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2493ecbb903cSTrond Myklebust mask |= MAY_WRITE; 249415d4b73aSTrond Myklebust return mask; 249515d4b73aSTrond Myklebust } 249615d4b73aSTrond Myklebust 24976168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 24986168f62cSWeston Andros Adamson { 2499bd8b2441STrond Myklebust entry->mask = access_result; 25006168f62cSWeston Andros Adamson } 25016168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 25021da177e4SLinus Torvalds 2503b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds struct nfs_access_entry cache; 250657b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2507e8194b7dSTrond Myklebust int cache_mask = -1; 25081da177e4SLinus Torvalds int status; 25091da177e4SLinus Torvalds 2510f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2511f4ce1299STrond Myklebust 2512f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2513f682a398SNeilBrown if (status != 0) 251457b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 25151da177e4SLinus Torvalds if (status == 0) 2516f4ce1299STrond Myklebust goto out_cached; 25171da177e4SLinus Torvalds 2518f3324a2aSNeilBrown status = -ECHILD; 251957b69181STrond Myklebust if (!may_block) 2520f3324a2aSNeilBrown goto out; 2521f3324a2aSNeilBrown 25221750d929SAnna Schumaker /* 25231750d929SAnna Schumaker * Determine which access bits we want to ask for... 25241750d929SAnna Schumaker */ 25251750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 25261750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 25271750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 25281750d929SAnna Schumaker else 25291750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 25301da177e4SLinus Torvalds cache.cred = cred; 25311da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2532a71ee337SSuresh Jayaraman if (status != 0) { 2533a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2534a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2535a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2536a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2537a71ee337SSuresh Jayaraman } 2538f4ce1299STrond Myklebust goto out; 2539a71ee337SSuresh Jayaraman } 25401da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2541f4ce1299STrond Myklebust out_cached: 2542ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2543bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2544f4ce1299STrond Myklebust status = -EACCES; 25451da177e4SLinus Torvalds out: 2546e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status); 2547f4ce1299STrond Myklebust return status; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds 2550af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2551af22f94aSTrond Myklebust { 2552af22f94aSTrond Myklebust int mask = 0; 2553af22f94aSTrond Myklebust 2554f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2555f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2556f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2557f8d9a897SWeston Andros Adamson } else { 25588a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2559af22f94aSTrond Myklebust mask |= MAY_READ; 25608a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2561af22f94aSTrond Myklebust mask |= MAY_WRITE; 2562f8d9a897SWeston Andros Adamson } 2563f8d9a897SWeston Andros Adamson 2564af22f94aSTrond Myklebust return mask; 2565af22f94aSTrond Myklebust } 2566af22f94aSTrond Myklebust 2567b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2568af22f94aSTrond Myklebust { 2569af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2570af22f94aSTrond Myklebust } 257189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2572af22f94aSTrond Myklebust 25735c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 25745c5fc09aSTrond Myklebust { 25755c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 257621c3ba7eSTrond Myklebust int ret = 0; 25775c5fc09aSTrond Myklebust 25783825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 25793825827eSTrond Myklebust return 0; 2580cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 25815c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 258221c3ba7eSTrond Myklebust return -ECHILD; 258321c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 258421c3ba7eSTrond Myklebust } 25855c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 25865c5fc09aSTrond Myklebust ret = -EACCES; 25875c5fc09aSTrond Myklebust return ret; 25885c5fc09aSTrond Myklebust } 25895c5fc09aSTrond Myklebust 259010556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 25911da177e4SLinus Torvalds { 2592b68572e0SNeilBrown const struct cred *cred = current_cred(); 25931da177e4SLinus Torvalds int res = 0; 25941da177e4SLinus Torvalds 259591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 259691d5b470SChuck Lever 2597e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 25981da177e4SLinus Torvalds goto out; 25991da177e4SLinus Torvalds /* Is this sys_access() ? */ 26009cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 26011da177e4SLinus Torvalds goto force_lookup; 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 26041da177e4SLinus Torvalds case S_IFLNK: 26051da177e4SLinus Torvalds goto out; 26061da177e4SLinus Torvalds case S_IFREG: 2607762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2608762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2609762674f8STrond Myklebust return 0; 26101da177e4SLinus Torvalds break; 26111da177e4SLinus Torvalds case S_IFDIR: 26121da177e4SLinus Torvalds /* 26131da177e4SLinus Torvalds * Optimize away all write operations, since the server 26141da177e4SLinus Torvalds * will check permissions when we perform the op. 26151da177e4SLinus Torvalds */ 26161da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 26171da177e4SLinus Torvalds goto out; 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds force_lookup: 26211da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 26221da177e4SLinus Torvalds goto out_notsup; 26231da177e4SLinus Torvalds 2624f3324a2aSNeilBrown /* Always try fast lookups first */ 2625f3324a2aSNeilBrown rcu_read_lock(); 2626f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2627f3324a2aSNeilBrown rcu_read_unlock(); 2628f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2629f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 26301da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2631f3324a2aSNeilBrown } 26321da177e4SLinus Torvalds out: 26335c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 26345c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2635f696a365SMiklos Szeredi 26361e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 26371e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 26381da177e4SLinus Torvalds return res; 26391da177e4SLinus Torvalds out_notsup: 2640d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2641d51ac1a8SNeilBrown return -ECHILD; 2642d51ac1a8SNeilBrown 26431da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 26441da177e4SLinus Torvalds if (res == 0) 26452830ba7fSAl Viro res = generic_permission(inode, mask); 26461e7cb3dcSChuck Lever goto out; 26471da177e4SLinus Torvalds } 2648ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds /* 26511da177e4SLinus Torvalds * Local variables: 26521da177e4SLinus Torvalds * version-control: t 26531da177e4SLinus Torvalds * kept-new-versions: 5 26541da177e4SLinus Torvalds * End: 26551da177e4SLinus Torvalds */ 2656