11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/dir.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * nfs directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 10 Apr 1996 Added silly rename for unlink --okir 91da177e4SLinus Torvalds * 28 Sep 1996 Improved directory cache --okir 101da177e4SLinus Torvalds * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de 111da177e4SLinus Torvalds * Re-implemented silly rename for unlink, newly implemented 121da177e4SLinus Torvalds * silly rename for nfs_rename() following the suggestions 131da177e4SLinus Torvalds * of Olaf Kirch (okir) found in this file. 141da177e4SLinus Torvalds * Following Linus comments on my original hack, this version 151da177e4SLinus Torvalds * depends only on the dcache stuff and doesn't touch the inode 161da177e4SLinus Torvalds * layer (iput() and friends). 171da177e4SLinus Torvalds * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 20ddda8e0aSBryan Schumaker #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/time.h> 221da177e4SLinus Torvalds #include <linux/errno.h> 231da177e4SLinus Torvalds #include <linux/stat.h> 241da177e4SLinus Torvalds #include <linux/fcntl.h> 251da177e4SLinus Torvalds #include <linux/string.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/mm.h> 291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 301da177e4SLinus Torvalds #include <linux/nfs_fs.h> 311da177e4SLinus Torvalds #include <linux/nfs_mount.h> 321da177e4SLinus Torvalds #include <linux/pagemap.h> 33873101b3SChuck Lever #include <linux/pagevec.h> 341da177e4SLinus Torvalds #include <linux/namei.h> 3554ceac45SDavid Howells #include <linux/mount.h> 36a0b8cab3SMel Gorman #include <linux/swap.h> 37e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include "delegation.h" 4291d5b470SChuck Lever #include "iostat.h" 434c30d56eSAdrian Bunk #include "internal.h" 44cd9a1c0eSTrond Myklebust #include "fscache.h" 451da177e4SLinus Torvalds 46f4ce1299STrond Myklebust #include "nfstrace.h" 47f4ce1299STrond Myklebust 481da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 51480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5223db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 54f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5511de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 561da177e4SLinus Torvalds 574b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 58f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 591da177e4SLinus Torvalds .read = generic_read_dir, 60b044f645SBenjamin Coddington .iterate = nfs_readdir, 611da177e4SLinus Torvalds .open = nfs_opendir, 62480c2006SBryan Schumaker .release = nfs_closedir, 631da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 6611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6711de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 68d1bacf9eSBryan Schumaker }; 69d1bacf9eSBryan Schumaker 70*684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred) 71480c2006SBryan Schumaker { 72311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 73480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 74480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 75480c2006SBryan Schumaker if (ctx != NULL) { 768ef2ce3eSBryan Schumaker ctx->duped = 0; 77311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 78480c2006SBryan Schumaker ctx->dir_cookie = 0; 798ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 80*684f39b4SNeilBrown ctx->cred = get_cred(cred); 81311324adSTrond Myklebust spin_lock(&dir->i_lock); 82311324adSTrond Myklebust list_add(&ctx->list, &nfsi->open_files); 83311324adSTrond Myklebust spin_unlock(&dir->i_lock); 84480c2006SBryan Schumaker return ctx; 85480c2006SBryan Schumaker } 860c030806STrond Myklebust return ERR_PTR(-ENOMEM); 870c030806STrond Myklebust } 88480c2006SBryan Schumaker 89311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 90480c2006SBryan Schumaker { 91311324adSTrond Myklebust spin_lock(&dir->i_lock); 92311324adSTrond Myklebust list_del(&ctx->list); 93311324adSTrond Myklebust spin_unlock(&dir->i_lock); 94*684f39b4SNeilBrown put_cred(ctx->cred); 95480c2006SBryan Schumaker kfree(ctx); 96480c2006SBryan Schumaker } 97480c2006SBryan Schumaker 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * Open file 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds static int 1021da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1031da177e4SLinus Torvalds { 104480c2006SBryan Schumaker int res = 0; 105480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 1061da177e4SLinus Torvalds 1076de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 108cc0dd2d1SChuck Lever 109cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1101e7cb3dcSChuck Lever 111*684f39b4SNeilBrown ctx = alloc_nfs_open_dir_context(inode, current_cred()); 112480c2006SBryan Schumaker if (IS_ERR(ctx)) { 113480c2006SBryan Schumaker res = PTR_ERR(ctx); 114480c2006SBryan Schumaker goto out; 115480c2006SBryan Schumaker } 116480c2006SBryan Schumaker filp->private_data = ctx; 117480c2006SBryan Schumaker out: 1181da177e4SLinus Torvalds return res; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 121480c2006SBryan Schumaker static int 122480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 123480c2006SBryan Schumaker { 124a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data); 125480c2006SBryan Schumaker return 0; 126480c2006SBryan Schumaker } 127480c2006SBryan Schumaker 128d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 129d1bacf9eSBryan Schumaker u64 cookie; 130d1bacf9eSBryan Schumaker u64 ino; 131d1bacf9eSBryan Schumaker struct qstr string; 1320b26a0bfSTrond Myklebust unsigned char d_type; 133d1bacf9eSBryan Schumaker }; 134d1bacf9eSBryan Schumaker 135d1bacf9eSBryan Schumaker struct nfs_cache_array { 13688b8e133SChuck Lever int size; 137d1bacf9eSBryan Schumaker int eof_index; 138d1bacf9eSBryan Schumaker u64 last_cookie; 139d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 140d1bacf9eSBryan Schumaker }; 141d1bacf9eSBryan Schumaker 142a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool); 1431da177e4SLinus Torvalds typedef struct { 1441da177e4SLinus Torvalds struct file *file; 1451da177e4SLinus Torvalds struct page *page; 14623db8620SAl Viro struct dir_context *ctx; 1471da177e4SLinus Torvalds unsigned long page_index; 148f0dd2136STrond Myklebust u64 *dir_cookie; 1490aded708STrond Myklebust u64 last_cookie; 150f0dd2136STrond Myklebust loff_t current_index; 1511da177e4SLinus Torvalds decode_dirent_t decode; 152d1bacf9eSBryan Schumaker 1531f4eab7eSNeil Brown unsigned long timestamp; 1544704f0e2STrond Myklebust unsigned long gencount; 155d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 156a7a3b1e9SBenjamin Coddington bool plus; 157a7a3b1e9SBenjamin Coddington bool eof; 1581da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1591da177e4SLinus Torvalds 160d1bacf9eSBryan Schumaker /* 161d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 162d1bacf9eSBryan Schumaker */ 163d1bacf9eSBryan Schumaker static 16411de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 165d1bacf9eSBryan Schumaker { 16611de3b11STrond Myklebust struct nfs_cache_array *array; 167d1bacf9eSBryan Schumaker int i; 1688cd51a0cSTrond Myklebust 1692b86ce2dSCong Wang array = kmap_atomic(page); 170d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 171d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1722b86ce2dSCong Wang kunmap_atomic(array); 173d1bacf9eSBryan Schumaker } 174d1bacf9eSBryan Schumaker 175d1bacf9eSBryan Schumaker /* 176d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 177d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 178d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 179d1bacf9eSBryan Schumaker */ 180d1bacf9eSBryan Schumaker static 1814a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 182d1bacf9eSBryan Schumaker { 183d1bacf9eSBryan Schumaker string->len = len; 184d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 1854a201d6eSTrond Myklebust if (string->name == NULL) 1864a201d6eSTrond Myklebust return -ENOMEM; 18704e4bd1cSCatalin Marinas /* 18804e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 18904e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 19004e4bd1cSCatalin Marinas */ 19104e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 1928387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 1934a201d6eSTrond Myklebust return 0; 194d1bacf9eSBryan Schumaker } 195d1bacf9eSBryan Schumaker 196d1bacf9eSBryan Schumaker static 197d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 198d1bacf9eSBryan Schumaker { 1990795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2004a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2014a201d6eSTrond Myklebust int ret; 2024a201d6eSTrond Myklebust 2034a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2043020093fSTrond Myklebust 2053020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2063020093fSTrond Myklebust ret = -ENOSPC; 2073020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2083020093fSTrond Myklebust goto out; 2093020093fSTrond Myklebust 2104a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2114a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2120b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2134a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2144a201d6eSTrond Myklebust if (ret) 2154a201d6eSTrond Myklebust goto out; 216d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2178cd51a0cSTrond Myklebust array->size++; 21847c716cbSTrond Myklebust if (entry->eof != 0) 219d1bacf9eSBryan Schumaker array->eof_index = array->size; 2204a201d6eSTrond Myklebust out: 2210795bf83SFabian Frederick kunmap(page); 2224a201d6eSTrond Myklebust return ret; 223d1bacf9eSBryan Schumaker } 224d1bacf9eSBryan Schumaker 225d1bacf9eSBryan Schumaker static 226d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 227d1bacf9eSBryan Schumaker { 22823db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 229d1bacf9eSBryan Schumaker unsigned int index; 230d1bacf9eSBryan Schumaker 231d1bacf9eSBryan Schumaker if (diff < 0) 232d1bacf9eSBryan Schumaker goto out_eof; 233d1bacf9eSBryan Schumaker if (diff >= array->size) { 2348cd51a0cSTrond Myklebust if (array->eof_index >= 0) 235d1bacf9eSBryan Schumaker goto out_eof; 236d1bacf9eSBryan Schumaker return -EAGAIN; 237d1bacf9eSBryan Schumaker } 238d1bacf9eSBryan Schumaker 239d1bacf9eSBryan Schumaker index = (unsigned int)diff; 240d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 241d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 242d1bacf9eSBryan Schumaker return 0; 243d1bacf9eSBryan Schumaker out_eof: 2446089dd0dSThomas Meyer desc->eof = true; 245d1bacf9eSBryan Schumaker return -EBADCOOKIE; 246d1bacf9eSBryan Schumaker } 247d1bacf9eSBryan Schumaker 2484db72b40SJeff Layton static bool 2494db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2504db72b40SJeff Layton { 2514db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2524db72b40SJeff Layton return false; 2534db72b40SJeff Layton smp_rmb(); 2544db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2554db72b40SJeff Layton } 2564db72b40SJeff Layton 257d1bacf9eSBryan Schumaker static 258d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 259d1bacf9eSBryan Schumaker { 260d1bacf9eSBryan Schumaker int i; 2618ef2ce3eSBryan Schumaker loff_t new_pos; 262d1bacf9eSBryan Schumaker int status = -EAGAIN; 263d1bacf9eSBryan Schumaker 264d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2658cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 266496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2670c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2680c030806STrond Myklebust 2698ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2704db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 2714db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 2720c030806STrond Myklebust ctx->duped = 0; 2730c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 27423db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2750c030806STrond Myklebust if (ctx->duped > 0 2760c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2770c030806STrond Myklebust if (printk_ratelimit()) { 2786de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 2790c030806STrond Myklebust "Please contact your server vendor. " 2809581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 2819581a4aeSJeff Layton desc->file, array->array[i].string.len, 2829581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 2830c030806STrond Myklebust } 2840c030806STrond Myklebust status = -ELOOP; 2850c030806STrond Myklebust goto out; 2860c030806STrond Myklebust } 2878ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 2880c030806STrond Myklebust ctx->duped = -1; 2898ef2ce3eSBryan Schumaker } 29023db8620SAl Viro desc->ctx->pos = new_pos; 2918cd51a0cSTrond Myklebust desc->cache_entry_index = i; 29247c716cbSTrond Myklebust return 0; 2938cd51a0cSTrond Myklebust } 2948cd51a0cSTrond Myklebust } 29547c716cbSTrond Myklebust if (array->eof_index >= 0) { 296d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 29718fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 2986089dd0dSThomas Meyer desc->eof = true; 299d1bacf9eSBryan Schumaker } 3000c030806STrond Myklebust out: 301d1bacf9eSBryan Schumaker return status; 302d1bacf9eSBryan Schumaker } 303d1bacf9eSBryan Schumaker 304d1bacf9eSBryan Schumaker static 305d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 306d1bacf9eSBryan Schumaker { 307d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 30847c716cbSTrond Myklebust int status; 309d1bacf9eSBryan Schumaker 3100795bf83SFabian Frederick array = kmap(desc->page); 311d1bacf9eSBryan Schumaker 312d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 313d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 314d1bacf9eSBryan Schumaker else 315d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 316d1bacf9eSBryan Schumaker 31747c716cbSTrond Myklebust if (status == -EAGAIN) { 3180aded708STrond Myklebust desc->last_cookie = array->last_cookie; 319e47c085aSTrond Myklebust desc->current_index += array->size; 32047c716cbSTrond Myklebust desc->page_index++; 32147c716cbSTrond Myklebust } 3220795bf83SFabian Frederick kunmap(desc->page); 323d1bacf9eSBryan Schumaker return status; 324d1bacf9eSBryan Schumaker } 325d1bacf9eSBryan Schumaker 326d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 327d1bacf9eSBryan Schumaker static 32856e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 329d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 330d1bacf9eSBryan Schumaker { 331480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 332*684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3334704f0e2STrond Myklebust unsigned long timestamp, gencount; 3341da177e4SLinus Torvalds int error; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds again: 3371da177e4SLinus Torvalds timestamp = jiffies; 3384704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 339be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3401da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3411da177e4SLinus Torvalds if (error < 0) { 3421da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3431da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3441da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3453a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 346a7a3b1e9SBenjamin Coddington desc->plus = false; 3471da177e4SLinus Torvalds goto again; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds goto error; 3501da177e4SLinus Torvalds } 3511f4eab7eSNeil Brown desc->timestamp = timestamp; 3524704f0e2STrond Myklebust desc->gencount = gencount; 353d1bacf9eSBryan Schumaker error: 354d1bacf9eSBryan Schumaker return error; 355d1bacf9eSBryan Schumaker } 356d1bacf9eSBryan Schumaker 357573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 358573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 359d1bacf9eSBryan Schumaker { 360573c4e1eSChuck Lever int error; 361d1bacf9eSBryan Schumaker 362573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 363573c4e1eSChuck Lever if (error) 364573c4e1eSChuck Lever return error; 365d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 366d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 367d1bacf9eSBryan Schumaker return 0; 368d1bacf9eSBryan Schumaker } 369d1bacf9eSBryan Schumaker 370fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 371fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 372fa923369STrond Myklebust */ 373d39ab9deSBryan Schumaker static 374d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 375d39ab9deSBryan Schumaker { 376d8fdb47fSTrond Myklebust struct inode *inode; 377fa923369STrond Myklebust struct nfs_inode *nfsi; 378fa923369STrond Myklebust 3792b0143b5SDavid Howells if (d_really_is_negative(dentry)) 3802b0143b5SDavid Howells return 0; 381fa923369STrond Myklebust 382d8fdb47fSTrond Myklebust inode = d_inode(dentry); 383d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 384d8fdb47fSTrond Myklebust return 0; 385d8fdb47fSTrond Myklebust 386d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 3877dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 388d39ab9deSBryan Schumaker return 0; 3897dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 3907dc72d5fSTrond Myklebust return 0; 3917dc72d5fSTrond Myklebust return 1; 392d39ab9deSBryan Schumaker } 393d39ab9deSBryan Schumaker 394d39ab9deSBryan Schumaker static 39523db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 396d69ee9b8STrond Myklebust { 397d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 398d69ee9b8STrond Myklebust return false; 399d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 400d69ee9b8STrond Myklebust return true; 40123db8620SAl Viro if (ctx->pos == 0) 402d69ee9b8STrond Myklebust return true; 403d69ee9b8STrond Myklebust return false; 404d69ee9b8STrond Myklebust } 405d69ee9b8STrond Myklebust 406d69ee9b8STrond Myklebust /* 40763519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 40863519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 409d69ee9b8STrond Myklebust * directory. 410d69ee9b8STrond Myklebust */ 411d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 412d69ee9b8STrond Myklebust { 41363519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 41463519fbcSTrond Myklebust 41563519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 41663519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 41763519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 418d69ee9b8STrond Myklebust } 419d69ee9b8STrond Myklebust 420311324adSTrond Myklebust /* 421311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 422311324adSTrond Myklebust * 423311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 424311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 425311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 426311324adSTrond Myklebust * cache flush. 427311324adSTrond Myklebust */ 428311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 429311324adSTrond 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); 43579f687a3STrond Myklebust invalidate_mapping_pages(dir->i_mapping, 0, -1); 436311324adSTrond Myklebust } 437311324adSTrond Myklebust } 438311324adSTrond Myklebust 439d69ee9b8STrond Myklebust static 440d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 441d39ab9deSBryan Schumaker { 44226fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4439ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4444a201d6eSTrond Myklebust struct dentry *dentry; 4454a201d6eSTrond Myklebust struct dentry *alias; 4462b0143b5SDavid Howells struct inode *dir = d_inode(parent); 447d39ab9deSBryan Schumaker struct inode *inode; 448aa9c2669SDavid Quigley int status; 449d39ab9deSBryan Schumaker 450fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 451fa923369STrond Myklebust return; 4526c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4536c441c25STrond Myklebust return; 45478d04af4STrond Myklebust if (filename.len == 0) 45578d04af4STrond Myklebust return; 45678d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 45778d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 45878d04af4STrond Myklebust return; 45978d04af4STrond Myklebust /* ...or '/' */ 46078d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 46178d04af4STrond Myklebust return; 4624a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4634a201d6eSTrond Myklebust if (filename.len == 1) 4644a201d6eSTrond Myklebust return; 4654a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4664a201d6eSTrond Myklebust return; 4674a201d6eSTrond Myklebust } 4688387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 469d39ab9deSBryan Schumaker 4704a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 4719ac3d3e8SAl Viro again: 4729ac3d3e8SAl Viro if (!dentry) { 4739ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 4749ac3d3e8SAl Viro if (IS_ERR(dentry)) 4759ac3d3e8SAl Viro return; 4769ac3d3e8SAl Viro } 4779ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 4786c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4796c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 4806c441c25STrond Myklebust &entry->fattr->fsid)) 4816c441c25STrond Myklebust goto out; 482d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 4837dc72d5fSTrond Myklebust if (!entry->fh->size) 4847dc72d5fSTrond Myklebust goto out; 485cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 4862b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 487aa9c2669SDavid Quigley if (!status) 4882b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 489d39ab9deSBryan Schumaker goto out; 490d39ab9deSBryan Schumaker } else { 4915542aa2fSEric W. Biederman d_invalidate(dentry); 492d39ab9deSBryan Schumaker dput(dentry); 4939ac3d3e8SAl Viro dentry = NULL; 4949ac3d3e8SAl Viro goto again; 495d39ab9deSBryan Schumaker } 496d39ab9deSBryan Schumaker } 4977dc72d5fSTrond Myklebust if (!entry->fh->size) { 4987dc72d5fSTrond Myklebust d_lookup_done(dentry); 4997dc72d5fSTrond Myklebust goto out; 5007dc72d5fSTrond Myklebust } 501d39ab9deSBryan Schumaker 5021775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 50341d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5049ac3d3e8SAl Viro d_lookup_done(dentry); 5059ac3d3e8SAl Viro if (alias) { 506d39ab9deSBryan Schumaker if (IS_ERR(alias)) 507d39ab9deSBryan Schumaker goto out; 5089ac3d3e8SAl Viro dput(dentry); 5099ac3d3e8SAl Viro dentry = alias; 5109ac3d3e8SAl Viro } 511d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 512d39ab9deSBryan Schumaker out: 513d39ab9deSBryan Schumaker dput(dentry); 514d39ab9deSBryan Schumaker } 515d39ab9deSBryan Schumaker 516d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 517d1bacf9eSBryan Schumaker static 5188cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5196650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 520d1bacf9eSBryan Schumaker { 521babddc72SBryan Schumaker struct xdr_stream stream; 522f7da7a12SBenny Halevy struct xdr_buf buf; 5236650239aSTrond Myklebust struct page *scratch; 52499424380SBryan Schumaker struct nfs_cache_array *array; 5255c346854STrond Myklebust unsigned int count = 0; 5265c346854STrond Myklebust int status; 527babddc72SBryan Schumaker 5286650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5296650239aSTrond Myklebust if (scratch == NULL) 5306650239aSTrond Myklebust return -ENOMEM; 531babddc72SBryan Schumaker 532ce85cfbeSBenjamin Coddington if (buflen == 0) 533ce85cfbeSBenjamin Coddington goto out_nopages; 534ce85cfbeSBenjamin Coddington 535f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5366650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 53799424380SBryan Schumaker 53899424380SBryan Schumaker do { 53999424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5408cd51a0cSTrond Myklebust if (status != 0) { 5418cd51a0cSTrond Myklebust if (status == -EAGAIN) 5428cd51a0cSTrond Myklebust status = 0; 54399424380SBryan Schumaker break; 5448cd51a0cSTrond Myklebust } 54599424380SBryan Schumaker 5465c346854STrond Myklebust count++; 5475c346854STrond Myklebust 548a7a3b1e9SBenjamin Coddington if (desc->plus) 549be62a1a8SMiklos Szeredi nfs_prime_dcache(file_dentry(desc->file), entry); 5508cd51a0cSTrond Myklebust 5518cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5528cd51a0cSTrond Myklebust if (status != 0) 5538cd51a0cSTrond Myklebust break; 55499424380SBryan Schumaker } while (!entry->eof); 55599424380SBryan Schumaker 556ce85cfbeSBenjamin Coddington out_nopages: 55747c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 5580795bf83SFabian Frederick array = kmap(page); 5598cd51a0cSTrond Myklebust array->eof_index = array->size; 56099424380SBryan Schumaker status = 0; 5610795bf83SFabian Frederick kunmap(page); 56256e4ebf8SBryan Schumaker } 5636650239aSTrond Myklebust 5646650239aSTrond Myklebust put_page(scratch); 5658cd51a0cSTrond Myklebust return status; 5668cd51a0cSTrond Myklebust } 56756e4ebf8SBryan Schumaker 56856e4ebf8SBryan Schumaker static 569c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 57056e4ebf8SBryan Schumaker { 57156e4ebf8SBryan Schumaker unsigned int i; 57256e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 57356e4ebf8SBryan Schumaker put_page(pages[i]); 57456e4ebf8SBryan Schumaker } 57556e4ebf8SBryan Schumaker 57656e4ebf8SBryan Schumaker /* 57756e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 5780b936e37SAnna Schumaker * to nfs_readdir_free_pagearray 57956e4ebf8SBryan Schumaker */ 58056e4ebf8SBryan Schumaker static 581c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 58256e4ebf8SBryan Schumaker { 58356e4ebf8SBryan Schumaker unsigned int i; 58456e4ebf8SBryan Schumaker 58556e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 58656e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 58756e4ebf8SBryan Schumaker if (page == NULL) 58856e4ebf8SBryan Schumaker goto out_freepages; 58956e4ebf8SBryan Schumaker pages[i] = page; 59056e4ebf8SBryan Schumaker } 5916650239aSTrond Myklebust return 0; 59256e4ebf8SBryan Schumaker 59356e4ebf8SBryan Schumaker out_freepages: 594c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 5956650239aSTrond Myklebust return -ENOMEM; 596d1bacf9eSBryan Schumaker } 597d1bacf9eSBryan Schumaker 598d1bacf9eSBryan Schumaker static 599d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 600d1bacf9eSBryan Schumaker { 60156e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 602d1bacf9eSBryan Schumaker struct nfs_entry entry; 603d1bacf9eSBryan Schumaker struct file *file = desc->file; 604d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6058cd51a0cSTrond Myklebust int status = -ENOMEM; 60656e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 607d1bacf9eSBryan Schumaker 608d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6090aded708STrond Myklebust entry.cookie = desc->last_cookie; 610d1bacf9eSBryan Schumaker entry.eof = 0; 611d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 612d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 613573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 614d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 615d1bacf9eSBryan Schumaker goto out; 616d1bacf9eSBryan Schumaker 61714c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 61814c43f76SDavid Quigley if (IS_ERR(entry.label)) { 61914c43f76SDavid Quigley status = PTR_ERR(entry.label); 62014c43f76SDavid Quigley goto out; 62114c43f76SDavid Quigley } 62214c43f76SDavid Quigley 6230795bf83SFabian Frederick array = kmap(page); 624d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 625d1bacf9eSBryan Schumaker array->eof_index = -1; 626d1bacf9eSBryan Schumaker 627c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6286650239aSTrond Myklebust if (status < 0) 629d1bacf9eSBryan Schumaker goto out_release_array; 630d1bacf9eSBryan Schumaker do { 631ac396128STrond Myklebust unsigned int pglen; 63256e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 633babddc72SBryan Schumaker 634d1bacf9eSBryan Schumaker if (status < 0) 635d1bacf9eSBryan Schumaker break; 636ac396128STrond Myklebust pglen = status; 6376650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6388cd51a0cSTrond Myklebust if (status < 0) { 6398cd51a0cSTrond Myklebust if (status == -ENOSPC) 6408cd51a0cSTrond Myklebust status = 0; 6418cd51a0cSTrond Myklebust break; 6428cd51a0cSTrond Myklebust } 6438cd51a0cSTrond Myklebust } while (array->eof_index < 0); 644d1bacf9eSBryan Schumaker 645c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 646d1bacf9eSBryan Schumaker out_release_array: 6470795bf83SFabian Frederick kunmap(page); 64814c43f76SDavid Quigley nfs4_label_free(entry.label); 649d1bacf9eSBryan Schumaker out: 650d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 651d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 652d1bacf9eSBryan Schumaker return status; 653d1bacf9eSBryan Schumaker } 654d1bacf9eSBryan Schumaker 655d1bacf9eSBryan Schumaker /* 656d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 657d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 658d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 659d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6601da177e4SLinus Torvalds */ 661d1bacf9eSBryan Schumaker static 662d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 663d1bacf9eSBryan Schumaker { 664496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6658cd51a0cSTrond Myklebust int ret; 666d1bacf9eSBryan Schumaker 6678cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6688cd51a0cSTrond Myklebust if (ret < 0) 669d1bacf9eSBryan Schumaker goto error; 670d1bacf9eSBryan Schumaker SetPageUptodate(page); 671d1bacf9eSBryan Schumaker 6722aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 673cd9ae2b6STrond Myklebust /* Should never happen */ 674cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 675cd9ae2b6STrond Myklebust } 6761da177e4SLinus Torvalds unlock_page(page); 6771da177e4SLinus Torvalds return 0; 6781da177e4SLinus Torvalds error: 6791da177e4SLinus Torvalds unlock_page(page); 6808cd51a0cSTrond Myklebust return ret; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 683d1bacf9eSBryan Schumaker static 684d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 6851da177e4SLinus Torvalds { 686b044f645SBenjamin Coddington if (!desc->page->mapping) 68711de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 68809cbfeafSKirill A. Shutemov put_page(desc->page); 6891da177e4SLinus Torvalds desc->page = NULL; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 692d1bacf9eSBryan Schumaker static 693d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 6941da177e4SLinus Torvalds { 695b044f645SBenjamin Coddington return read_cache_page(desc->file->f_mapping, 696d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds /* 700d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7011da177e4SLinus Torvalds */ 702d1bacf9eSBryan Schumaker static 703d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 704d1bacf9eSBryan Schumaker { 705d1bacf9eSBryan Schumaker int res; 706d1bacf9eSBryan Schumaker 707d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 708d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 709d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 710d1bacf9eSBryan Schumaker 711d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 71247c716cbSTrond Myklebust if (res != 0) 713d1bacf9eSBryan Schumaker cache_page_release(desc); 714d1bacf9eSBryan Schumaker return res; 715d1bacf9eSBryan Schumaker } 716d1bacf9eSBryan Schumaker 717d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7181da177e4SLinus Torvalds static inline 7191da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7201da177e4SLinus Torvalds { 7218cd51a0cSTrond Myklebust int res; 722d1bacf9eSBryan Schumaker 7230aded708STrond Myklebust if (desc->page_index == 0) { 7248cd51a0cSTrond Myklebust desc->current_index = 0; 7250aded708STrond Myklebust desc->last_cookie = 0; 7260aded708STrond Myklebust } 72747c716cbSTrond Myklebust do { 728d1bacf9eSBryan Schumaker res = find_cache_page(desc); 72947c716cbSTrond Myklebust } while (res == -EAGAIN); 7301da177e4SLinus Torvalds return res; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* 7341da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7351da177e4SLinus Torvalds */ 7361da177e4SLinus Torvalds static 73723db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds struct file *file = desc->file; 740d1bacf9eSBryan Schumaker int i = 0; 741d1bacf9eSBryan Schumaker int res = 0; 742d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7438ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7448ef2ce3eSBryan Schumaker 7450795bf83SFabian Frederick array = kmap(desc->page); 746d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 747ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7481da177e4SLinus Torvalds 749ece0b423STrond Myklebust ent = &array->array[i]; 75023db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 75123db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 7526089dd0dSThomas Meyer desc->eof = true; 7531da177e4SLinus Torvalds break; 754ece0b423STrond Myklebust } 75523db8620SAl Viro desc->ctx->pos++; 756d1bacf9eSBryan Schumaker if (i < (array->size-1)) 757d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 758d1bacf9eSBryan Schumaker else 759d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7600c030806STrond Myklebust if (ctx->duped != 0) 7610c030806STrond Myklebust ctx->duped = 1; 7628cd51a0cSTrond Myklebust } 76347c716cbSTrond Myklebust if (array->eof_index >= 0) 7646089dd0dSThomas Meyer desc->eof = true; 765d1bacf9eSBryan Schumaker 7660795bf83SFabian Frederick kunmap(desc->page); 767d1bacf9eSBryan Schumaker cache_page_release(desc); 7681e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7691e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7701da177e4SLinus Torvalds return res; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /* 7741da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 7751da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 7761da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 7771da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 7781da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 7791da177e4SLinus Torvalds * 7801da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 7811da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 7821da177e4SLinus Torvalds * we should already have a complete representation of the 7831da177e4SLinus Torvalds * directory in the page cache by the time we get here. 7841da177e4SLinus Torvalds */ 7851da177e4SLinus Torvalds static inline 78623db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 7871da177e4SLinus Torvalds { 7881da177e4SLinus Torvalds struct page *page = NULL; 7891da177e4SLinus Torvalds int status; 790496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7910c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 7921da177e4SLinus Torvalds 7931e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 7941e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 7971da177e4SLinus Torvalds if (!page) { 7981da177e4SLinus Torvalds status = -ENOMEM; 7991da177e4SLinus Torvalds goto out; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8027a8e1dc3STrond Myklebust desc->page_index = 0; 8030aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8047a8e1dc3STrond Myklebust desc->page = page; 8050c030806STrond Myklebust ctx->duped = 0; 8067a8e1dc3STrond Myklebust 80785f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 80885f8607eSTrond Myklebust if (status < 0) 809d1bacf9eSBryan Schumaker goto out_release; 810d1bacf9eSBryan Schumaker 81123db8620SAl Viro status = nfs_do_filldir(desc); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds out: 8141e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8153110ff80SHarvey Harrison __func__, status); 8161da177e4SLinus Torvalds return status; 8171da177e4SLinus Torvalds out_release: 818d1bacf9eSBryan Schumaker cache_page_release(desc); 8191da177e4SLinus Torvalds goto out; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 82200a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 82300a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 82400a92642SOlivier Galibert whole directory. 8251da177e4SLinus Torvalds */ 82623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8271da177e4SLinus Torvalds { 828be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8292b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8301da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8311da177e4SLinus Torvalds *desc = &my_desc; 83223db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 83307b5ce8eSScott Mayhew int res = 0; 8341da177e4SLinus Torvalds 8356de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 8366de1472fSAl Viro file, (long long)ctx->pos); 83791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 83891d5b470SChuck Lever 8391da177e4SLinus Torvalds /* 84023db8620SAl Viro * ctx->pos points to the dirent entry number. 841f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 84200a92642SOlivier Galibert * to either find the entry with the appropriate number or 84300a92642SOlivier Galibert * revalidate the cookie. 8441da177e4SLinus Torvalds */ 8451da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8461da177e4SLinus Torvalds 84723db8620SAl Viro desc->file = file; 84823db8620SAl Viro desc->ctx = ctx; 849480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8501da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 851a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 8521da177e4SLinus Torvalds 85379f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 85423db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 855fccca7fcSTrond Myklebust if (res < 0) 856fccca7fcSTrond Myklebust goto out; 857fccca7fcSTrond Myklebust 85847c716cbSTrond Myklebust do { 8591da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 86000a92642SOlivier Galibert 8611da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 862ece0b423STrond Myklebust res = 0; 8631da177e4SLinus Torvalds /* This means either end of directory */ 8646089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 8651da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 86623db8620SAl Viro res = uncached_readdir(desc); 867ece0b423STrond Myklebust if (res == 0) 8681da177e4SLinus Torvalds continue; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds break; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8733a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8741da177e4SLinus Torvalds nfs_zap_caches(inode); 875baf57a09STrond Myklebust desc->page_index = 0; 876a7a3b1e9SBenjamin Coddington desc->plus = false; 877a7a3b1e9SBenjamin Coddington desc->eof = false; 8781da177e4SLinus Torvalds continue; 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds if (res < 0) 8811da177e4SLinus Torvalds break; 8821da177e4SLinus Torvalds 88323db8620SAl Viro res = nfs_do_filldir(desc); 884ece0b423STrond Myklebust if (res < 0) 8851da177e4SLinus Torvalds break; 88647c716cbSTrond Myklebust } while (!desc->eof); 887fccca7fcSTrond Myklebust out: 8881e7cb3dcSChuck Lever if (res > 0) 8891e7cb3dcSChuck Lever res = 0; 8906de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 8911da177e4SLinus Torvalds return res; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 894965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 895f0dd2136STrond Myklebust { 896b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 897480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 898b84e06c5SChuck Lever 8996de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9006de1472fSAl Viro filp, offset, whence); 901b84e06c5SChuck Lever 902965c8e59SAndrew Morton switch (whence) { 903f0dd2136STrond Myklebust default: 904b2b1ff3dSTrond Myklebust return -EINVAL; 905b2b1ff3dSTrond Myklebust case SEEK_SET: 906b2b1ff3dSTrond Myklebust if (offset < 0) 907b2b1ff3dSTrond Myklebust return -EINVAL; 908b2b1ff3dSTrond Myklebust inode_lock(inode); 909b2b1ff3dSTrond Myklebust break; 910b2b1ff3dSTrond Myklebust case SEEK_CUR: 911b2b1ff3dSTrond Myklebust if (offset == 0) 912b2b1ff3dSTrond Myklebust return filp->f_pos; 913b2b1ff3dSTrond Myklebust inode_lock(inode); 914b2b1ff3dSTrond Myklebust offset += filp->f_pos; 915b2b1ff3dSTrond Myklebust if (offset < 0) { 916b2b1ff3dSTrond Myklebust inode_unlock(inode); 917b2b1ff3dSTrond Myklebust return -EINVAL; 918b2b1ff3dSTrond Myklebust } 919f0dd2136STrond Myklebust } 920f0dd2136STrond Myklebust if (offset != filp->f_pos) { 921f0dd2136STrond Myklebust filp->f_pos = offset; 922480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9238ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 924f0dd2136STrond Myklebust } 925b044f645SBenjamin Coddington inode_unlock(inode); 926f0dd2136STrond Myklebust return offset; 927f0dd2136STrond Myklebust } 928f0dd2136STrond Myklebust 9291da177e4SLinus Torvalds /* 9301da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9311da177e4SLinus Torvalds * is a dummy operation. 9321da177e4SLinus Torvalds */ 93302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 93402c24a82SJosef Bacik int datasync) 9351da177e4SLinus Torvalds { 9366de1472fSAl Viro struct inode *inode = file_inode(filp); 9377ea80859SChristoph Hellwig 9386de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 9391e7cb3dcSChuck Lever 9405955102cSAl Viro inode_lock(inode); 9416de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 9425955102cSAl Viro inode_unlock(inode); 9431da177e4SLinus Torvalds return 0; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 946bfc69a45STrond Myklebust /** 947bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 948bfc69a45STrond Myklebust * @dir - pointer to directory inode 949bfc69a45STrond Myklebust * 950bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 951bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 952bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 953bfc69a45STrond Myklebust * 954bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 955bfc69a45STrond Myklebust */ 956bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 957bfc69a45STrond Myklebust { 958011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 959bfc69a45STrond Myklebust } 96089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 961bfc69a45STrond Myklebust 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9641da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9651da177e4SLinus Torvalds * and may need to be looked up again. 966912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 9671da177e4SLinus Torvalds */ 968912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 969912a108dSNeilBrown int rcu_walk) 9701da177e4SLinus Torvalds { 9711da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9721da177e4SLinus Torvalds return 1; 9734eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 9744eec952eSTrond Myklebust return 0; 975f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 9766ecc5e8fSTrond Myklebust return 0; 977f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 9781cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 979912a108dSNeilBrown if (rcu_walk) 980f2c77f4eSTrond Myklebust return 0; 9811cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 9821cd9cb05STrond Myklebust return 0; 9831cd9cb05STrond Myklebust } 984f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 985f2c77f4eSTrond Myklebust return 0; 986f2c77f4eSTrond Myklebust return 1; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds /* 990a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 991a12802caSTrond Myklebust * an O_EXCL create using this path component. 992a12802caSTrond Myklebust */ 993fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 994a12802caSTrond Myklebust { 995a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 996a12802caSTrond Myklebust return 0; 997fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 998a12802caSTrond Myklebust } 999a12802caSTrond Myklebust 1000a12802caSTrond Myklebust /* 10011d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10021d6757fbSTrond Myklebust * 10031d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10041d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10051d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10061d6757fbSTrond Myklebust * 10071d6757fbSTrond Myklebust */ 100865a0c149STrond Myklebust static 1009fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10101da177e4SLinus Torvalds { 10111da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 101265a0c149STrond Myklebust int ret; 10131da177e4SLinus Torvalds 101436d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10154e99a1ffSTrond Myklebust return 0; 101647921921STrond Myklebust 101747921921STrond Myklebust if (flags & LOOKUP_OPEN) { 101847921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 101947921921STrond Myklebust case S_IFREG: 102047921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 102147921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 102247921921STrond Myklebust goto out; 102347921921STrond Myklebust /* Fallthrough */ 102447921921STrond Myklebust case S_IFDIR: 102547921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 102647921921STrond Myklebust break; 102747921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 102847921921STrond Myklebust goto out_force; 102947921921STrond Myklebust } 103047921921STrond Myklebust } 103147921921STrond Myklebust 10321da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1033fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10341da177e4SLinus Torvalds goto out_force; 103565a0c149STrond Myklebust out: 1036a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 10371da177e4SLinus Torvalds out_force: 10381fa1e384SNeilBrown if (flags & LOOKUP_RCU) 10391fa1e384SNeilBrown return -ECHILD; 104065a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 104165a0c149STrond Myklebust if (ret != 0) 104265a0c149STrond Myklebust return ret; 104365a0c149STrond Myklebust goto out; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /* 10471da177e4SLinus Torvalds * We judge how long we want to trust negative 10481da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10491da177e4SLinus Torvalds * 10501da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10511da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1052912a108dSNeilBrown * 1053912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1054912a108dSNeilBrown * suggesting a reval is needed. 10559f6d44d4STrond Myklebust * 10569f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 10579f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 10581da177e4SLinus Torvalds */ 10591da177e4SLinus Torvalds static inline 10601da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1061fa3c56bbSAl Viro unsigned int flags) 10621da177e4SLinus Torvalds { 10639f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 10641da177e4SLinus Torvalds return 0; 10654eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10664eec952eSTrond Myklebust return 1; 1067912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10705ceb9d7fSTrond Myklebust static int 10715ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 10725ceb9d7fSTrond Myklebust struct inode *inode, int error) 10731da177e4SLinus Torvalds { 10745ceb9d7fSTrond Myklebust switch (error) { 10755ceb9d7fSTrond Myklebust case 1: 10766de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 10776de1472fSAl Viro __func__, dentry); 10781da177e4SLinus Torvalds return 1; 10795ceb9d7fSTrond Myklebust case 0: 1080a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 10811da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 10821da177e4SLinus Torvalds /* Purge readdir caches. */ 10831da177e4SLinus Torvalds nfs_zap_caches(inode); 1084a3f432bfSJ. Bruce Fields /* 1085a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1086a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1087a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1088a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1089a3f432bfSJ. Bruce Fields */ 1090a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 10915ceb9d7fSTrond Myklebust return 1; 10921da177e4SLinus Torvalds } 10936de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 10946de1472fSAl Viro __func__, dentry); 10951da177e4SLinus Torvalds return 0; 10965ceb9d7fSTrond Myklebust } 10976de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 10986de1472fSAl Viro __func__, dentry, error); 1099e1fb4d05STrond Myklebust return error; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11025ceb9d7fSTrond Myklebust static int 11035ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 11045ceb9d7fSTrond Myklebust unsigned int flags) 11055ceb9d7fSTrond Myklebust { 11065ceb9d7fSTrond Myklebust int ret = 1; 11075ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 11085ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 11095ceb9d7fSTrond Myklebust return -ECHILD; 11105ceb9d7fSTrond Myklebust ret = 0; 11115ceb9d7fSTrond Myklebust } 11125ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 11135ceb9d7fSTrond Myklebust } 11145ceb9d7fSTrond Myklebust 11155ceb9d7fSTrond Myklebust static int 11165ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 11175ceb9d7fSTrond Myklebust struct inode *inode) 11185ceb9d7fSTrond Myklebust { 11195ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11205ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 11215ceb9d7fSTrond Myklebust } 11225ceb9d7fSTrond Myklebust 11235ceb9d7fSTrond Myklebust static int 11245ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 11255ceb9d7fSTrond Myklebust struct inode *inode) 11265ceb9d7fSTrond Myklebust { 11275ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 11285ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 11295ceb9d7fSTrond Myklebust struct nfs4_label *label; 11305ceb9d7fSTrond Myklebust int ret; 11315ceb9d7fSTrond Myklebust 11325ceb9d7fSTrond Myklebust ret = -ENOMEM; 11335ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 11345ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 11355ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 11365ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 11375ceb9d7fSTrond Myklebust goto out; 11385ceb9d7fSTrond Myklebust 11395ceb9d7fSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 11405ceb9d7fSTrond Myklebust if (ret < 0) { 11415ceb9d7fSTrond Myklebust if (ret == -ESTALE || ret == -ENOENT) 11425ceb9d7fSTrond Myklebust ret = 0; 11435ceb9d7fSTrond Myklebust goto out; 11445ceb9d7fSTrond Myklebust } 11455ceb9d7fSTrond Myklebust ret = 0; 11465ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11475ceb9d7fSTrond Myklebust goto out; 11485ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 11495ceb9d7fSTrond Myklebust goto out; 11505ceb9d7fSTrond Myklebust 11515ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 11525ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11535ceb9d7fSTrond Myklebust 11545ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 11555ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 11565ceb9d7fSTrond Myklebust ret = 1; 11575ceb9d7fSTrond Myklebust out: 11585ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 11595ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 11605ceb9d7fSTrond Myklebust nfs4_label_free(label); 11615ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 11625ceb9d7fSTrond Myklebust } 11635ceb9d7fSTrond Myklebust 11645ceb9d7fSTrond Myklebust /* 11655ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 11665ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 11675ceb9d7fSTrond Myklebust * lookup. 11685ceb9d7fSTrond Myklebust * 11695ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 11705ceb9d7fSTrond Myklebust * we have an inode! 11715ceb9d7fSTrond Myklebust * 11725ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 11735ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 11745ceb9d7fSTrond Myklebust */ 11755ceb9d7fSTrond Myklebust static int 11765ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 11775ceb9d7fSTrond Myklebust unsigned int flags) 11785ceb9d7fSTrond Myklebust { 11795ceb9d7fSTrond Myklebust struct inode *inode; 11805ceb9d7fSTrond Myklebust int error; 11815ceb9d7fSTrond Myklebust 11825ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 11835ceb9d7fSTrond Myklebust inode = d_inode(dentry); 11845ceb9d7fSTrond Myklebust 11855ceb9d7fSTrond Myklebust if (!inode) 11865ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 11875ceb9d7fSTrond Myklebust 11885ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 11895ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 11905ceb9d7fSTrond Myklebust __func__, dentry); 11915ceb9d7fSTrond Myklebust goto out_bad; 11925ceb9d7fSTrond Myklebust } 11935ceb9d7fSTrond Myklebust 11945ceb9d7fSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 11955ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 11965ceb9d7fSTrond Myklebust 11975ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 11985ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 11995ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 12005ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 12015ceb9d7fSTrond Myklebust if (error) { 12025ceb9d7fSTrond Myklebust if (error == -ESTALE) 12035ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 12045ceb9d7fSTrond Myklebust goto out_bad; 12055ceb9d7fSTrond Myklebust } 12065ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 12075ceb9d7fSTrond Myklebust goto out_valid; 12085ceb9d7fSTrond Myklebust } 12095ceb9d7fSTrond Myklebust 12105ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12115ceb9d7fSTrond Myklebust return -ECHILD; 12125ceb9d7fSTrond Myklebust 12135ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 12145ceb9d7fSTrond Myklebust goto out_bad; 12155ceb9d7fSTrond Myklebust 12165ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 12175ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 12185ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 12195ceb9d7fSTrond Myklebust return error; 12205ceb9d7fSTrond Myklebust out_valid: 12215ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 12225ceb9d7fSTrond Myklebust out_bad: 12235ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12245ceb9d7fSTrond Myklebust return -ECHILD; 12255ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 12265ceb9d7fSTrond Myklebust } 12275ceb9d7fSTrond Myklebust 12285ceb9d7fSTrond Myklebust static int 1229c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1230c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 12315ceb9d7fSTrond Myklebust { 12325ceb9d7fSTrond Myklebust struct dentry *parent; 12335ceb9d7fSTrond Myklebust struct inode *dir; 12345ceb9d7fSTrond Myklebust int ret; 12355ceb9d7fSTrond Myklebust 12365ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 12375ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 12385ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 12395ceb9d7fSTrond Myklebust if (!dir) 12405ceb9d7fSTrond Myklebust return -ECHILD; 1241c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 12425ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 12435ceb9d7fSTrond Myklebust return -ECHILD; 12445ceb9d7fSTrond Myklebust } else { 12455ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1246c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 12475ceb9d7fSTrond Myklebust dput(parent); 12485ceb9d7fSTrond Myklebust } 12495ceb9d7fSTrond Myklebust return ret; 12505ceb9d7fSTrond Myklebust } 12515ceb9d7fSTrond Myklebust 1252c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1253c7944ebbSTrond Myklebust { 1254c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1255c7944ebbSTrond Myklebust } 1256c7944ebbSTrond Myklebust 12571da177e4SLinus Torvalds /* 12582b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1259ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1260ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1261ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1262ecf3d1f1SJeff Layton * 1263ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1264ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1265ecf3d1f1SJeff Layton */ 1266ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1267ecf3d1f1SJeff Layton { 12682b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 12699cdd1d3fSTrond Myklebust int error = 0; 1270ecf3d1f1SJeff Layton 1271ecf3d1f1SJeff Layton /* 1272ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1273ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1274ecf3d1f1SJeff Layton * eventually need to do something more here. 1275ecf3d1f1SJeff Layton */ 1276ecf3d1f1SJeff Layton if (!inode) { 12776de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 12786de1472fSAl Viro __func__, dentry); 1279ecf3d1f1SJeff Layton return 1; 1280ecf3d1f1SJeff Layton } 1281ecf3d1f1SJeff Layton 1282ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 12836de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12846de1472fSAl Viro __func__, dentry); 1285ecf3d1f1SJeff Layton return 0; 1286ecf3d1f1SJeff Layton } 1287ecf3d1f1SJeff Layton 1288b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1289ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1290ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1291ecf3d1f1SJeff Layton return !error; 1292ecf3d1f1SJeff Layton } 1293ecf3d1f1SJeff Layton 1294ecf3d1f1SJeff Layton /* 12951da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 12961da177e4SLinus Torvalds */ 1297fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 12981da177e4SLinus Torvalds { 12996de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13006de1472fSAl Viro dentry, dentry->d_flags); 13011da177e4SLinus Torvalds 130277f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13032b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 130477f11192STrond Myklebust return 1; 130577f11192STrond Myklebust 13061da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13071da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13081da177e4SLinus Torvalds return 1; 13091da177e4SLinus Torvalds } 13101751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 13111da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13121da177e4SLinus Torvalds * files will be cleaned up during umount */ 13131da177e4SLinus Torvalds return 1; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds return 0; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13201b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13211b83d707STrond Myklebust { 13221b83d707STrond Myklebust spin_lock(&inode->i_lock); 13231f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 132459a707b0STrond Myklebust if (inode->i_nlink > 0) 132559a707b0STrond Myklebust drop_nlink(inode); 132659a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 132716e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 132816e14375STrond Myklebust | NFS_INO_INVALID_CTIME 132959a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 133059a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 13311b83d707STrond Myklebust spin_unlock(&inode->i_lock); 13321b83d707STrond Myklebust } 13331b83d707STrond Myklebust 13341da177e4SLinus Torvalds /* 13351da177e4SLinus Torvalds * Called when the dentry loses inode. 13361da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 13371da177e4SLinus Torvalds */ 13381da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 13391da177e4SLinus Torvalds { 134083672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 134183672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 134283672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 134383672d39SNeil Brown 13441da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1345e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 13461f018458STrond Myklebust nfs_drop_nlink(inode); 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds iput(inode); 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 1351b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1352b1942c5fSAl Viro { 1353b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1354b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1355b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1356b1942c5fSAl Viro WARN_ON(1); 1357b1942c5fSAl Viro else 1358b1942c5fSAl Viro kfree(dentry->d_fsdata); 1359b1942c5fSAl Viro } 1360b1942c5fSAl Viro } 1361b1942c5fSAl Viro 1362f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 13631da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1364ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 13651da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13661da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 136736d43a43SDavid Howells .d_automount = nfs_d_automount, 1368b1942c5fSAl Viro .d_release = nfs_d_release, 13691da177e4SLinus Torvalds }; 1370ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 13711da177e4SLinus Torvalds 1372597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct dentry *res; 13751da177e4SLinus Torvalds struct inode *inode = NULL; 1376e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1377e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 13781775fd3eSDavid Quigley struct nfs4_label *label = NULL; 13791da177e4SLinus Torvalds int error; 13801da177e4SLinus Torvalds 13816de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 138291d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 13831da177e4SLinus Torvalds 1384130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1385130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 13861da177e4SLinus Torvalds 1387fd684071STrond Myklebust /* 1388fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1389fd684071STrond Myklebust * but don't hash the dentry. 1390fd684071STrond Myklebust */ 13919f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1392130f9ab7SAl Viro return NULL; 13931da177e4SLinus Torvalds 1394e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1395e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1396e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1397e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1398e1fb4d05STrond Myklebust goto out; 1399e1fb4d05STrond Myklebust 140014c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 140114c43f76SDavid Quigley if (IS_ERR(label)) 140214c43f76SDavid Quigley goto out; 140314c43f76SDavid Quigley 14046e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 14051775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 14061da177e4SLinus Torvalds if (error == -ENOENT) 14071da177e4SLinus Torvalds goto no_entry; 14081da177e4SLinus Torvalds if (error < 0) { 14091da177e4SLinus Torvalds res = ERR_PTR(error); 1410bf130914SAl Viro goto out_label; 14111da177e4SLinus Torvalds } 14121775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1413bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 141403f28e3aSTrond Myklebust if (IS_ERR(res)) 1415bf130914SAl Viro goto out_label; 141654ceac45SDavid Howells 141763519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 141863519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1419d69ee9b8STrond Myklebust 14201da177e4SLinus Torvalds no_entry: 142141d28bcaSAl Viro res = d_splice_alias(inode, dentry); 14229eaef27bSTrond Myklebust if (res != NULL) { 14239eaef27bSTrond Myklebust if (IS_ERR(res)) 1424bf130914SAl Viro goto out_label; 14251da177e4SLinus Torvalds dentry = res; 14269eaef27bSTrond Myklebust } 14271da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1428bf130914SAl Viro out_label: 14296e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 143014c43f76SDavid Quigley nfs4_label_free(label); 14311da177e4SLinus Torvalds out: 1432e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1433e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 14341da177e4SLinus Torvalds return res; 14351da177e4SLinus Torvalds } 1436ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 14371da177e4SLinus Torvalds 143889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 14390b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 14401da177e4SLinus Torvalds 1441f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 14420ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1443b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 14441da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14451da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 144636d43a43SDavid Howells .d_automount = nfs_d_automount, 1447b1942c5fSAl Viro .d_release = nfs_d_release, 14481da177e4SLinus Torvalds }; 144989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 14501da177e4SLinus Torvalds 14518a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 14528a5e929dSAl Viro { 14538a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 14548a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 14558a5e929dSAl Viro res |= FMODE_READ; 14568a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 14578a5e929dSAl Viro res |= FMODE_WRITE; 14588a5e929dSAl Viro return res; 14598a5e929dSAl Viro } 14608a5e929dSAl Viro 1461532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1462cd9a1c0eSTrond Myklebust { 1463532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1464cd9a1c0eSTrond Myklebust } 1465cd9a1c0eSTrond Myklebust 1466cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1467cd9a1c0eSTrond Myklebust { 1468f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1469cd9a1c0eSTrond Myklebust return 0; 1470cd9a1c0eSTrond Myklebust } 1471cd9a1c0eSTrond Myklebust 1472d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 14730dd2b474SMiklos Szeredi struct dentry *dentry, 1474b452a458SAl Viro struct file *file, unsigned open_flags) 1475cd9a1c0eSTrond Myklebust { 14760dd2b474SMiklos Szeredi int err; 14770dd2b474SMiklos Szeredi 1478be12af3eSAl Viro err = finish_open(file, dentry, do_open); 147930d90494SAl Viro if (err) 1480d9585277SAl Viro goto out; 1481eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 148230d90494SAl Viro nfs_file_set_open_context(file, ctx); 1483eaa2b82cSNeilBrown else 1484eaa2b82cSNeilBrown err = -ESTALE; 1485cd9a1c0eSTrond Myklebust out: 1486d9585277SAl Viro return err; 1487cd9a1c0eSTrond Myklebust } 1488cd9a1c0eSTrond Myklebust 148973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 149030d90494SAl Viro struct file *file, unsigned open_flags, 149144907d79SAl Viro umode_t mode) 14921da177e4SLinus Torvalds { 1493c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1494cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 14950dd2b474SMiklos Szeredi struct dentry *res; 14960dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1497f46e0bd3STrond Myklebust struct inode *inode; 14981472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1499c94c0953SAl Viro bool switched = false; 150073a09dd9SAl Viro int created = 0; 1501898f635cSTrond Myklebust int err; 15021da177e4SLinus Torvalds 15030dd2b474SMiklos Szeredi /* Expect a negative dentry */ 15042b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 15050dd2b474SMiklos Szeredi 15061e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 15076de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15081e7cb3dcSChuck Lever 15099597c13bSJeff Layton err = nfs_check_flags(open_flags); 15109597c13bSJeff Layton if (err) 15119597c13bSJeff Layton return err; 15129597c13bSJeff Layton 15130dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15140dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 151500699ad8SAl Viro if (!d_in_lookup(dentry)) { 15160dd2b474SMiklos Szeredi /* 15170dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15180dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15190dd2b474SMiklos Szeredi * again 15200dd2b474SMiklos Szeredi */ 1521d9585277SAl Viro return -ENOENT; 15220dd2b474SMiklos Szeredi } 15231472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 15241da177e4SLinus Torvalds goto no_open; 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds 15270dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1528d9585277SAl Viro return -ENAMETOOLONG; 15291da177e4SLinus Torvalds 15300dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1531dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1532dff25ddbSAndreas Gruenbacher 1533dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1534dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1535dff25ddbSAndreas Gruenbacher 1536536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1537dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 15380dd2b474SMiklos Szeredi } 1539536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1540536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1541536e43d1STrond Myklebust attr.ia_size = 0; 1542cd9a1c0eSTrond Myklebust } 1543cd9a1c0eSTrond Myklebust 1544c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1545c94c0953SAl Viro d_drop(dentry); 1546c94c0953SAl Viro switched = true; 1547c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1548c94c0953SAl Viro &dentry->d_name, &wq); 1549c94c0953SAl Viro if (IS_ERR(dentry)) 1550c94c0953SAl Viro return PTR_ERR(dentry); 1551c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1552c94c0953SAl Viro return finish_no_open(file, dentry); 1553c94c0953SAl Viro } 1554c94c0953SAl Viro 1555532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 15560dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 15570dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1558d9585277SAl Viro goto out; 15590dd2b474SMiklos Szeredi 15606e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 156173a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 156273a09dd9SAl Viro if (created) 156373a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1564275bb307STrond Myklebust if (IS_ERR(inode)) { 15650dd2b474SMiklos Szeredi err = PTR_ERR(inode); 15666e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15672d9db750STrond Myklebust put_nfs_open_context(ctx); 1568d20cb71dSAl Viro d_drop(dentry); 15690dd2b474SMiklos Szeredi switch (err) { 15701da177e4SLinus Torvalds case -ENOENT: 1571774d9513SPeng Tao d_splice_alias(NULL, dentry); 1572809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15730dd2b474SMiklos Szeredi break; 15741788ea6eSJeff Layton case -EISDIR: 15756f926b5bSTrond Myklebust case -ENOTDIR: 15766f926b5bSTrond Myklebust goto no_open; 15771da177e4SLinus Torvalds case -ELOOP: 15780dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 15791da177e4SLinus Torvalds goto no_open; 15800dd2b474SMiklos Szeredi break; 15811da177e4SLinus Torvalds /* case -EINVAL: */ 15821da177e4SLinus Torvalds default: 15830dd2b474SMiklos Szeredi break; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds goto out; 15861da177e4SLinus Torvalds } 15870dd2b474SMiklos Szeredi 1588b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 15896e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 15902d9db750STrond Myklebust put_nfs_open_context(ctx); 1591d9585277SAl Viro out: 1592c94c0953SAl Viro if (unlikely(switched)) { 1593c94c0953SAl Viro d_lookup_done(dentry); 1594c94c0953SAl Viro dput(dentry); 1595c94c0953SAl Viro } 1596d9585277SAl Viro return err; 15970dd2b474SMiklos Szeredi 15981da177e4SLinus Torvalds no_open: 15991472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1600c94c0953SAl Viro if (switched) { 1601c94c0953SAl Viro d_lookup_done(dentry); 1602c94c0953SAl Viro if (!res) 1603c94c0953SAl Viro res = dentry; 1604c94c0953SAl Viro else 1605c94c0953SAl Viro dput(dentry); 1606c94c0953SAl Viro } 16070dd2b474SMiklos Szeredi if (IS_ERR(res)) 1608c94c0953SAl Viro return PTR_ERR(res); 1609e45198a6SAl Viro return finish_no_open(file, res); 16101da177e4SLinus Torvalds } 161189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 16121da177e4SLinus Torvalds 1613c7944ebbSTrond Myklebust static int 1614c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1615c7944ebbSTrond Myklebust unsigned int flags) 16161da177e4SLinus Torvalds { 1617657e94b6SNick Piggin struct inode *inode; 16181da177e4SLinus Torvalds 1619fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1620c7944ebbSTrond Myklebust goto full_reval; 1621eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1622c7944ebbSTrond Myklebust goto full_reval; 16232b484297STrond Myklebust 16242b0143b5SDavid Howells inode = d_inode(dentry); 16252b484297STrond Myklebust 16261da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 16271da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 16281da177e4SLinus Torvalds */ 1629c7944ebbSTrond Myklebust if (inode == NULL) 1630c7944ebbSTrond Myklebust goto full_reval; 163149317a7fSNeilBrown 1632c7944ebbSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 1633c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1634216d5d06STrond Myklebust 16351da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 16361da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1637c7944ebbSTrond Myklebust goto full_reval; 1638c7944ebbSTrond Myklebust 16391da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1640c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1641c7944ebbSTrond Myklebust goto reval_dentry; 1642c7944ebbSTrond Myklebust 1643c7944ebbSTrond Myklebust /* Check if the directory changed */ 1644c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1645c7944ebbSTrond Myklebust goto reval_dentry; 16461da177e4SLinus Torvalds 16470ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1648c7944ebbSTrond Myklebust return 1; 1649c7944ebbSTrond Myklebust reval_dentry: 1650c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1651c7944ebbSTrond Myklebust return -ECHILD; 1652c7944ebbSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode);; 16530ef97dcfSMiklos Szeredi 1654c7944ebbSTrond Myklebust full_reval: 1655c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1656c7944ebbSTrond Myklebust } 1657535918f1STrond Myklebust 1658c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1659c7944ebbSTrond Myklebust { 1660c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1661c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1662c0204fd2STrond Myklebust } 1663c0204fd2STrond Myklebust 16641da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* 16671da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 16681da177e4SLinus Torvalds */ 16691da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 16701775fd3eSDavid Quigley struct nfs_fattr *fattr, 16711775fd3eSDavid Quigley struct nfs4_label *label) 16721da177e4SLinus Torvalds { 1673fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 16742b0143b5SDavid Howells struct inode *dir = d_inode(parent); 16751da177e4SLinus Torvalds struct inode *inode; 1676b0c6108eSAl Viro struct dentry *d; 16771da177e4SLinus Torvalds int error = -EACCES; 16781da177e4SLinus Torvalds 1679fab728e1STrond Myklebust d_drop(dentry); 1680fab728e1STrond Myklebust 16811da177e4SLinus Torvalds /* We may have been initialized further down */ 16822b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1683fab728e1STrond Myklebust goto out; 16841da177e4SLinus Torvalds if (fhandle->size == 0) { 16851775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL); 16861da177e4SLinus Torvalds if (error) 1687fab728e1STrond Myklebust goto out_error; 16881da177e4SLinus Torvalds } 16895724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16901da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 16911da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1692a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1693a841b54dSTrond Myklebust fattr, NULL, NULL); 16941da177e4SLinus Torvalds if (error < 0) 1695fab728e1STrond Myklebust goto out_error; 16961da177e4SLinus Torvalds } 16971775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1698b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1699b0c6108eSAl Viro if (IS_ERR(d)) { 1700b0c6108eSAl Viro error = PTR_ERR(d); 1701fab728e1STrond Myklebust goto out_error; 1702b0c6108eSAl Viro } 1703b0c6108eSAl Viro dput(d); 1704fab728e1STrond Myklebust out: 1705fab728e1STrond Myklebust dput(parent); 17061da177e4SLinus Torvalds return 0; 1707fab728e1STrond Myklebust out_error: 1708fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1709fab728e1STrond Myklebust dput(parent); 1710fab728e1STrond Myklebust return error; 17111da177e4SLinus Torvalds } 1712ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds /* 17151da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17161da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17171da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17181da177e4SLinus Torvalds * reply path made it appear to have failed. 17191da177e4SLinus Torvalds */ 1720597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1721ebfc3b49SAl Viro umode_t mode, bool excl) 17221da177e4SLinus Torvalds { 17231da177e4SLinus Torvalds struct iattr attr; 1724ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 17251da177e4SLinus Torvalds int error; 17261da177e4SLinus Torvalds 17271e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 17286de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds attr.ia_mode = mode; 17311da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17321da177e4SLinus Torvalds 17338b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 17348867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 17358b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 17361da177e4SLinus Torvalds if (error != 0) 17371da177e4SLinus Torvalds goto out_err; 17381da177e4SLinus Torvalds return 0; 17391da177e4SLinus Torvalds out_err: 17401da177e4SLinus Torvalds d_drop(dentry); 17411da177e4SLinus Torvalds return error; 17421da177e4SLinus Torvalds } 1743ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /* 17461da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17471da177e4SLinus Torvalds */ 1748597d9289SBryan Schumaker int 17491a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17501da177e4SLinus Torvalds { 17511da177e4SLinus Torvalds struct iattr attr; 17521da177e4SLinus Torvalds int status; 17531da177e4SLinus Torvalds 17541e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 17556de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds attr.ia_mode = mode; 17581da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17591da177e4SLinus Torvalds 17601ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 17611da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 17621ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 17631da177e4SLinus Torvalds if (status != 0) 17641da177e4SLinus Torvalds goto out_err; 17651da177e4SLinus Torvalds return 0; 17661da177e4SLinus Torvalds out_err: 17671da177e4SLinus Torvalds d_drop(dentry); 17681da177e4SLinus Torvalds return status; 17691da177e4SLinus Torvalds } 1770ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds /* 17731da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17741da177e4SLinus Torvalds */ 1775597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 17761da177e4SLinus Torvalds { 17771da177e4SLinus Torvalds struct iattr attr; 17781da177e4SLinus Torvalds int error; 17791da177e4SLinus Torvalds 17801e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 17816de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17841da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 17851da177e4SLinus Torvalds 17861ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 17871da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 17881ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 17891da177e4SLinus Torvalds if (error != 0) 17901da177e4SLinus Torvalds goto out_err; 17911da177e4SLinus Torvalds return 0; 17921da177e4SLinus Torvalds out_err: 17931da177e4SLinus Torvalds d_drop(dentry); 17941da177e4SLinus Torvalds return error; 17951da177e4SLinus Torvalds } 1796ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 17971da177e4SLinus Torvalds 1798d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1799d45b9d8bSTrond Myklebust { 1800dc3f4198SAl Viro if (simple_positive(dentry)) 1801d45b9d8bSTrond Myklebust d_delete(dentry); 1802d45b9d8bSTrond Myklebust } 1803d45b9d8bSTrond Myklebust 1804597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 18051da177e4SLinus Torvalds { 18061da177e4SLinus Torvalds int error; 18071da177e4SLinus Torvalds 18081e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 18096de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18101da177e4SLinus Torvalds 18111ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18122b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1813884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18141da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18151da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1816ba6c0592STrond Myklebust switch (error) { 1817ba6c0592STrond Myklebust case 0: 18182b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1819ba6c0592STrond Myklebust break; 1820ba6c0592STrond Myklebust case -ENOENT: 1821d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1822ba6c0592STrond Myklebust } 1823884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1824ba6c0592STrond Myklebust } else 1825ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18261ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds return error; 18291da177e4SLinus Torvalds } 1830ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* 18331da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18341da177e4SLinus Torvalds * and after checking that the file has only one user. 18351da177e4SLinus Torvalds * 18361da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18371da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18381da177e4SLinus Torvalds */ 18391da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18401da177e4SLinus Torvalds { 18412b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 18422b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 18431da177e4SLinus Torvalds int error = -EBUSY; 18441da177e4SLinus Torvalds 18456de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18481da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18491da177e4SLinus Torvalds error = 0; 18501da177e4SLinus Torvalds goto out; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 18541da177e4SLinus Torvalds if (inode != NULL) { 1855912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 18561da177e4SLinus Torvalds if (error == 0) 18571b83d707STrond Myklebust nfs_drop_nlink(inode); 18581da177e4SLinus Torvalds } else 1859912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 1860d45b9d8bSTrond Myklebust if (error == -ENOENT) 1861d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18621ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 18631da177e4SLinus Torvalds out: 18641da177e4SLinus Torvalds return error; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 18681da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 18691da177e4SLinus Torvalds * 18701da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 18711da177e4SLinus Torvalds */ 1872597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 18731da177e4SLinus Torvalds { 18741da177e4SLinus Torvalds int error; 18751da177e4SLinus Torvalds int need_rehash = 0; 18761da177e4SLinus Torvalds 18771e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 18786de1472fSAl Viro dir->i_ino, dentry); 18791da177e4SLinus Torvalds 18801ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 18811da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 188284d08fa8SAl Viro if (d_count(dentry) > 1) { 18831da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1884ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 18852b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 18861da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 18871ca42382STrond Myklebust goto out; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 18901da177e4SLinus Torvalds __d_drop(dentry); 18911da177e4SLinus Torvalds need_rehash = 1; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 18941da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1895d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 18961da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18971da177e4SLinus Torvalds } else if (need_rehash) 18981da177e4SLinus Torvalds d_rehash(dentry); 18991ca42382STrond Myklebust out: 19001ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 19011da177e4SLinus Torvalds return error; 19021da177e4SLinus Torvalds } 1903ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 19041da177e4SLinus Torvalds 1905873101b3SChuck Lever /* 1906873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1907873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1908873101b3SChuck Lever * using prepare_write/commit_write. 1909873101b3SChuck Lever * 1910873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1911873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1912873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1913873101b3SChuck Lever * symlink request has completed on the server. 1914873101b3SChuck Lever * 1915873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1916873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1917873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1918873101b3SChuck Lever * and move the raw page into its mapping. 1919873101b3SChuck Lever */ 1920597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19211da177e4SLinus Torvalds { 1922873101b3SChuck Lever struct page *page; 1923873101b3SChuck Lever char *kaddr; 19241da177e4SLinus Torvalds struct iattr attr; 1925873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19261da177e4SLinus Torvalds int error; 19271da177e4SLinus Torvalds 19281e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 19296de1472fSAl Viro dir->i_ino, dentry, symname); 19301da177e4SLinus Torvalds 1931873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1932873101b3SChuck Lever return -ENAMETOOLONG; 19331da177e4SLinus Torvalds 1934873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1935873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19361da177e4SLinus Torvalds 1937e8ecde25SAl Viro page = alloc_page(GFP_USER); 193876566991STrond Myklebust if (!page) 1939873101b3SChuck Lever return -ENOMEM; 1940873101b3SChuck Lever 1941e8ecde25SAl Viro kaddr = page_address(page); 1942873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1943873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1944873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1945873101b3SChuck Lever 19461ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 194794a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 19481ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 1949873101b3SChuck Lever if (error != 0) { 19501e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 1951873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 19526de1472fSAl Viro dentry, symname, error); 19531da177e4SLinus Torvalds d_drop(dentry); 1954873101b3SChuck Lever __free_page(page); 19551da177e4SLinus Torvalds return error; 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds 1958873101b3SChuck Lever /* 1959873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1960873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1961873101b3SChuck Lever */ 19622b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 1963873101b3SChuck Lever GFP_KERNEL)) { 1964873101b3SChuck Lever SetPageUptodate(page); 1965873101b3SChuck Lever unlock_page(page); 1966a0b54addSRafael Aquini /* 1967a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 1968a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 1969a0b54addSRafael Aquini */ 197009cbfeafSKirill A. Shutemov put_page(page); 1971873101b3SChuck Lever } else 1972873101b3SChuck Lever __free_page(page); 1973873101b3SChuck Lever 1974873101b3SChuck Lever return 0; 1975873101b3SChuck Lever } 1976ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 1977873101b3SChuck Lever 1978597d9289SBryan Schumaker int 19791da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19801da177e4SLinus Torvalds { 19812b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 19821da177e4SLinus Torvalds int error; 19831da177e4SLinus Torvalds 19846de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 19856de1472fSAl Viro old_dentry, dentry); 19861da177e4SLinus Torvalds 19871fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 19889697d234STrond Myklebust d_drop(dentry); 19891da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1990cf809556STrond Myklebust if (error == 0) { 19917de9c6eeSAl Viro ihold(inode); 19929697d234STrond Myklebust d_add(dentry, inode); 1993cf809556STrond Myklebust } 19941fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 19951da177e4SLinus Torvalds return error; 19961da177e4SLinus Torvalds } 1997ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds /* 20001da177e4SLinus Torvalds * RENAME 20011da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20021da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20031da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20041da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20051da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20061da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20071da177e4SLinus Torvalds * 20081da177e4SLinus Torvalds * FIXED. 20091da177e4SLinus Torvalds * 20101da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20111da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20121da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20131da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20141da177e4SLinus Torvalds * using the inode layer 20151da177e4SLinus Torvalds * 20161da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20171da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20181da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20191da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20201da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20211da177e4SLinus Torvalds * the rename. 20221da177e4SLinus Torvalds */ 2023597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 20241cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 20251cd66c93SMiklos Szeredi unsigned int flags) 20261da177e4SLinus Torvalds { 20272b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 20282b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2029d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 203080a491fdSJeff Layton struct rpc_task *task; 20311da177e4SLinus Torvalds int error = -EBUSY; 20321da177e4SLinus Torvalds 20331cd66c93SMiklos Szeredi if (flags) 20341cd66c93SMiklos Szeredi return -EINVAL; 20351cd66c93SMiklos Szeredi 20366de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 20376de1472fSAl Viro old_dentry, new_dentry, 203884d08fa8SAl Viro d_count(new_dentry)); 20391da177e4SLinus Torvalds 204070ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 20411da177e4SLinus Torvalds /* 204228f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 204328f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 204428f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 204528f79a1aSMiklos Szeredi * the new target. 20461da177e4SLinus Torvalds */ 204727226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 204827226104SMiklos Szeredi /* 204927226104SMiklos Szeredi * To prevent any new references to the target during the 205027226104SMiklos Szeredi * rename, we unhash the dentry in advance. 205127226104SMiklos Szeredi */ 2052d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 205327226104SMiklos Szeredi d_drop(new_dentry); 2054d9f29500SBenjamin Coddington rehash = new_dentry; 2055d9f29500SBenjamin Coddington } 205627226104SMiklos Szeredi 205784d08fa8SAl Viro if (d_count(new_dentry) > 2) { 20581da177e4SLinus Torvalds int err; 205927226104SMiklos Szeredi 20601da177e4SLinus Torvalds /* copy the target dentry's name */ 20611da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 20621da177e4SLinus Torvalds &new_dentry->d_name); 20631da177e4SLinus Torvalds if (!dentry) 20641da177e4SLinus Torvalds goto out; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds /* silly-rename the existing target ... */ 20671da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 206824e93025SMiklos Szeredi if (err) 20691da177e4SLinus Torvalds goto out; 207024e93025SMiklos Szeredi 207124e93025SMiklos Szeredi new_dentry = dentry; 2072d9f29500SBenjamin Coddington rehash = NULL; 207324e93025SMiklos Szeredi new_inode = NULL; 2074b1e4adf4STrond Myklebust } 207527226104SMiklos Szeredi } 20761da177e4SLinus Torvalds 2077d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 207880a491fdSJeff Layton if (IS_ERR(task)) { 207980a491fdSJeff Layton error = PTR_ERR(task); 208080a491fdSJeff Layton goto out; 208180a491fdSJeff Layton } 208280a491fdSJeff Layton 208380a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2084818a8dbeSBenjamin Coddington if (error != 0) { 2085818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2086818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2087818a8dbeSBenjamin Coddington smp_wmb(); 2088818a8dbeSBenjamin Coddington } else 208980a491fdSJeff Layton error = task->tk_status; 209080a491fdSJeff Layton rpc_put_task(task); 209159a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 209259a707b0STrond Myklebust if (error == 0) { 209359a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 209459a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 209559a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 209659a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 209759a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 209859a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 209959a707b0STrond Myklebust } 21001da177e4SLinus Torvalds out: 2101d9f29500SBenjamin Coddington if (rehash) 2102d9f29500SBenjamin Coddington d_rehash(rehash); 210370ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 210470ded201STrond Myklebust new_dir, new_dentry, error); 2105d9f29500SBenjamin Coddington if (!error) { 2106d9f29500SBenjamin Coddington if (new_inode != NULL) 2107d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2108d9f29500SBenjamin Coddington /* 2109d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2110d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2111d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2112d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2113d9f29500SBenjamin Coddington */ 2114d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2115d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2116d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2117d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2118d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2119d9f29500SBenjamin Coddington 21201da177e4SLinus Torvalds /* new dentry created? */ 21211da177e4SLinus Torvalds if (dentry) 21221da177e4SLinus Torvalds dput(dentry); 21231da177e4SLinus Torvalds return error; 21241da177e4SLinus Torvalds } 2125ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 21261da177e4SLinus Torvalds 2127cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2128cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2129cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2130cfcea3e8STrond Myklebust 21313a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 21323a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 21333a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 21343a505845STrond Myklebust 21351c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 21361c3c07e9STrond Myklebust { 2137b68572e0SNeilBrown put_cred(entry->cred); 2138f682a398SNeilBrown kfree_rcu(entry, rcu_head); 21394e857c58SPeter Zijlstra smp_mb__before_atomic(); 2140cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 21414e857c58SPeter Zijlstra smp_mb__after_atomic(); 21421c3c07e9STrond Myklebust } 21431c3c07e9STrond Myklebust 21441a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 21451a81bb8aSTrond Myklebust { 21461a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 21471a81bb8aSTrond Myklebust 21481a81bb8aSTrond Myklebust while (!list_empty(head)) { 21491a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 21501a81bb8aSTrond Myklebust list_del(&cache->lru); 21511a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 21521a81bb8aSTrond Myklebust } 21531a81bb8aSTrond Myklebust } 21541a81bb8aSTrond Myklebust 21553a505845STrond Myklebust static unsigned long 21563a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2157979df72eSTrond Myklebust { 2158979df72eSTrond Myklebust LIST_HEAD(head); 2159aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2160979df72eSTrond Myklebust struct nfs_access_entry *cache; 21611ab6c499SDave Chinner long freed = 0; 2162979df72eSTrond Myklebust 2163a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2164aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2165979df72eSTrond Myklebust struct inode *inode; 2166979df72eSTrond Myklebust 2167979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2168979df72eSTrond Myklebust break; 21699c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2170979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2171979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2172979df72eSTrond Myklebust goto remove_lru_entry; 2173979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2174979df72eSTrond Myklebust struct nfs_access_entry, lru); 2175979df72eSTrond Myklebust list_move(&cache->lru, &head); 2176979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 21771ab6c499SDave Chinner freed++; 2178979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2179979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2180979df72eSTrond Myklebust &nfs_access_lru_list); 2181979df72eSTrond Myklebust else { 2182979df72eSTrond Myklebust remove_lru_entry: 2183979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 21844e857c58SPeter Zijlstra smp_mb__before_atomic(); 2185979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 21864e857c58SPeter Zijlstra smp_mb__after_atomic(); 2187979df72eSTrond Myklebust } 218859844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2189979df72eSTrond Myklebust } 2190979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21911a81bb8aSTrond Myklebust nfs_access_free_list(&head); 21921ab6c499SDave Chinner return freed; 21931ab6c499SDave Chinner } 21941ab6c499SDave Chinner 21951ab6c499SDave Chinner unsigned long 21963a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 21973a505845STrond Myklebust { 21983a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 21993a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 22003a505845STrond Myklebust 22013a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 22023a505845STrond Myklebust return SHRINK_STOP; 22033a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 22043a505845STrond Myklebust } 22053a505845STrond Myklebust 22063a505845STrond Myklebust 22073a505845STrond Myklebust unsigned long 22081ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 22091ab6c499SDave Chinner { 221055f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2211979df72eSTrond Myklebust } 2212979df72eSTrond Myklebust 22133a505845STrond Myklebust static void 22143a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22153a505845STrond Myklebust { 22163a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22173a505845STrond Myklebust unsigned long diff; 22183a505845STrond Myklebust unsigned int nr_to_scan; 22193a505845STrond Myklebust 22203a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22213a505845STrond Myklebust return; 22223a505845STrond Myklebust nr_to_scan = 100; 22233a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 22243a505845STrond Myklebust if (diff < nr_to_scan) 22253a505845STrond Myklebust nr_to_scan = diff; 22263a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 22273a505845STrond Myklebust } 22283a505845STrond Myklebust 22291a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 22301c3c07e9STrond Myklebust { 22311c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22321a81bb8aSTrond Myklebust struct rb_node *n; 22331c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22341c3c07e9STrond Myklebust 22351c3c07e9STrond Myklebust /* Unhook entries from the cache */ 22361c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 22371c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 22381c3c07e9STrond Myklebust rb_erase(n, root_node); 22391a81bb8aSTrond Myklebust list_move(&entry->lru, head); 22401c3c07e9STrond Myklebust } 22411c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 22421c3c07e9STrond Myklebust } 22431c3c07e9STrond Myklebust 22441c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 22451c3c07e9STrond Myklebust { 22461a81bb8aSTrond Myklebust LIST_HEAD(head); 22471a81bb8aSTrond Myklebust 22481a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 22491a81bb8aSTrond Myklebust return; 2250cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2251cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22521a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2253cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2254cfcea3e8STrond Myklebust 22551c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22561a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 22571a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22581a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22591a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22601c3c07e9STrond Myklebust } 22611c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 22621c3c07e9STrond Myklebust 2263b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 22641c3c07e9STrond Myklebust { 22651c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 22661c3c07e9STrond Myklebust 22671c3c07e9STrond Myklebust while (n != NULL) { 2268b68572e0SNeilBrown struct nfs_access_entry *entry = 2269b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2270b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 22711c3c07e9STrond Myklebust 2272b68572e0SNeilBrown if (cmp < 0) 22731c3c07e9STrond Myklebust n = n->rb_left; 2274b68572e0SNeilBrown else if (cmp > 0) 22751c3c07e9STrond Myklebust n = n->rb_right; 22761c3c07e9STrond Myklebust else 22771c3c07e9STrond Myklebust return entry; 22781c3c07e9STrond Myklebust } 22791c3c07e9STrond Myklebust return NULL; 22801c3c07e9STrond Myklebust } 22811c3c07e9STrond Myklebust 2282b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 22831da177e4SLinus Torvalds { 228455296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 22851c3c07e9STrond Myklebust struct nfs_access_entry *cache; 228657b69181STrond Myklebust bool retry = true; 228757b69181STrond Myklebust int err; 22881da177e4SLinus Torvalds 22891c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 229057b69181STrond Myklebust for(;;) { 22911c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 22921c3c07e9STrond Myklebust goto out_zap; 22931c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 229457b69181STrond Myklebust err = -ENOENT; 22951c3c07e9STrond Myklebust if (cache == NULL) 22961c3c07e9STrond Myklebust goto out; 229757b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 229821c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 229957b69181STrond Myklebust break; 230057b69181STrond Myklebust err = -ECHILD; 230157b69181STrond Myklebust if (!may_block) 230257b69181STrond Myklebust goto out; 230357b69181STrond Myklebust if (!retry) 230457b69181STrond Myklebust goto out_zap; 230557b69181STrond Myklebust spin_unlock(&inode->i_lock); 230657b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 230757b69181STrond Myklebust if (err) 230857b69181STrond Myklebust return err; 230957b69181STrond Myklebust spin_lock(&inode->i_lock); 231057b69181STrond Myklebust retry = false; 231157b69181STrond Myklebust } 23121c3c07e9STrond Myklebust res->cred = cache->cred; 23131c3c07e9STrond Myklebust res->mask = cache->mask; 2314cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23151c3c07e9STrond Myklebust err = 0; 23161c3c07e9STrond Myklebust out: 23171c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23181c3c07e9STrond Myklebust return err; 23191c3c07e9STrond Myklebust out_zap: 23201a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23211a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 23221c3c07e9STrond Myklebust return -ENOENT; 23231c3c07e9STrond Myklebust } 23241c3c07e9STrond Myklebust 2325b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2326f682a398SNeilBrown { 2327f682a398SNeilBrown /* Only check the most recently returned cache entry, 2328f682a398SNeilBrown * but do it without locking. 2329f682a398SNeilBrown */ 2330f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2331f682a398SNeilBrown struct nfs_access_entry *cache; 2332f682a398SNeilBrown int err = -ECHILD; 2333f682a398SNeilBrown struct list_head *lh; 2334f682a398SNeilBrown 2335f682a398SNeilBrown rcu_read_lock(); 2336f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2337f682a398SNeilBrown goto out; 2338f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2339f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2340f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 2341f682a398SNeilBrown cred != cache->cred) 2342f682a398SNeilBrown cache = NULL; 2343f682a398SNeilBrown if (cache == NULL) 2344f682a398SNeilBrown goto out; 234521c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2346f682a398SNeilBrown goto out; 2347f682a398SNeilBrown res->cred = cache->cred; 2348f682a398SNeilBrown res->mask = cache->mask; 234921c3ba7eSTrond Myklebust err = 0; 2350f682a398SNeilBrown out: 2351f682a398SNeilBrown rcu_read_unlock(); 2352f682a398SNeilBrown return err; 2353f682a398SNeilBrown } 2354f682a398SNeilBrown 23551c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 23561c3c07e9STrond Myklebust { 2357cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2358cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23591c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 23601c3c07e9STrond Myklebust struct rb_node *parent = NULL; 23611c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2362b68572e0SNeilBrown int cmp; 23631c3c07e9STrond Myklebust 23641c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23651c3c07e9STrond Myklebust while (*p != NULL) { 23661c3c07e9STrond Myklebust parent = *p; 23671c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2368b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 23691c3c07e9STrond Myklebust 2370b68572e0SNeilBrown if (cmp < 0) 23711c3c07e9STrond Myklebust p = &parent->rb_left; 2372b68572e0SNeilBrown else if (cmp > 0) 23731c3c07e9STrond Myklebust p = &parent->rb_right; 23741c3c07e9STrond Myklebust else 23751c3c07e9STrond Myklebust goto found; 23761c3c07e9STrond Myklebust } 23771c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 23781c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2379cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 23801c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23811c3c07e9STrond Myklebust return; 23821c3c07e9STrond Myklebust found: 23831c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2384cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2385cfcea3e8STrond Myklebust list_del(&entry->lru); 23861c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23871c3c07e9STrond Myklebust nfs_access_free_entry(entry); 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 23906168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 23911da177e4SLinus Torvalds { 23921c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 23931c3c07e9STrond Myklebust if (cache == NULL) 23941c3c07e9STrond Myklebust return; 23951c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2396b68572e0SNeilBrown cache->cred = get_cred(set->cred); 23971da177e4SLinus Torvalds cache->mask = set->mask; 23981c3c07e9STrond Myklebust 2399f682a398SNeilBrown /* The above field assignments must be visible 2400f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2401f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2402f682a398SNeilBrown */ 2403f682a398SNeilBrown smp_wmb(); 24041c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2405cfcea3e8STrond Myklebust 2406cfcea3e8STrond Myklebust /* Update accounting */ 24074e857c58SPeter Zijlstra smp_mb__before_atomic(); 2408cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 24094e857c58SPeter Zijlstra smp_mb__after_atomic(); 2410cfcea3e8STrond Myklebust 2411cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24121a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2413cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24141a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24151a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24161a81bb8aSTrond Myklebust &nfs_access_lru_list); 2417cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2418cfcea3e8STrond Myklebust } 24193a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24201da177e4SLinus Torvalds } 24216168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 24226168f62cSWeston Andros Adamson 24233c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 24243c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24253c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 24263c181827SAnna Schumaker NFS_ACCESS_DELETE) 24273c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 24283c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2429ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 24303c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 24313c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 243215d4b73aSTrond Myklebust static int 2433ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 243415d4b73aSTrond Myklebust { 243515d4b73aSTrond Myklebust int mask = 0; 243615d4b73aSTrond Myklebust 243715d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 243815d4b73aSTrond Myklebust mask |= MAY_READ; 2439ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2440ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 244115d4b73aSTrond Myklebust mask |= MAY_WRITE; 2442ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 244315d4b73aSTrond Myklebust mask |= MAY_EXEC; 2444ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2445ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2446ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2447ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 244815d4b73aSTrond Myklebust mask |= MAY_EXEC; 2449ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2450ecbb903cSTrond Myklebust mask |= MAY_WRITE; 245115d4b73aSTrond Myklebust return mask; 245215d4b73aSTrond Myklebust } 245315d4b73aSTrond Myklebust 24546168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 24556168f62cSWeston Andros Adamson { 2456bd8b2441STrond Myklebust entry->mask = access_result; 24576168f62cSWeston Andros Adamson } 24586168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 24591da177e4SLinus Torvalds 2460b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 24611da177e4SLinus Torvalds { 24621da177e4SLinus Torvalds struct nfs_access_entry cache; 246357b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2464bd8b2441STrond Myklebust int cache_mask; 24651da177e4SLinus Torvalds int status; 24661da177e4SLinus Torvalds 2467f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2468f4ce1299STrond Myklebust 2469f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2470f682a398SNeilBrown if (status != 0) 247157b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 24721da177e4SLinus Torvalds if (status == 0) 2473f4ce1299STrond Myklebust goto out_cached; 24741da177e4SLinus Torvalds 2475f3324a2aSNeilBrown status = -ECHILD; 247657b69181STrond Myklebust if (!may_block) 2477f3324a2aSNeilBrown goto out; 2478f3324a2aSNeilBrown 24791750d929SAnna Schumaker /* 24801750d929SAnna Schumaker * Determine which access bits we want to ask for... 24811750d929SAnna Schumaker */ 24821750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 24831750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 24841750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 24851750d929SAnna Schumaker else 24861750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 24871da177e4SLinus Torvalds cache.cred = cred; 24881da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2489a71ee337SSuresh Jayaraman if (status != 0) { 2490a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2491a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2492a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2493a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2494a71ee337SSuresh Jayaraman } 2495f4ce1299STrond Myklebust goto out; 2496a71ee337SSuresh Jayaraman } 24971da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2498f4ce1299STrond Myklebust out_cached: 2499ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2500bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2501f4ce1299STrond Myklebust status = -EACCES; 25021da177e4SLinus Torvalds out: 2503f4ce1299STrond Myklebust trace_nfs_access_exit(inode, status); 2504f4ce1299STrond Myklebust return status; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 2507af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2508af22f94aSTrond Myklebust { 2509af22f94aSTrond Myklebust int mask = 0; 2510af22f94aSTrond Myklebust 2511f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2512f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2513f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2514f8d9a897SWeston Andros Adamson } else { 25158a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2516af22f94aSTrond Myklebust mask |= MAY_READ; 25178a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2518af22f94aSTrond Myklebust mask |= MAY_WRITE; 2519f8d9a897SWeston Andros Adamson } 2520f8d9a897SWeston Andros Adamson 2521af22f94aSTrond Myklebust return mask; 2522af22f94aSTrond Myklebust } 2523af22f94aSTrond Myklebust 2524b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2525af22f94aSTrond Myklebust { 2526af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2527af22f94aSTrond Myklebust } 252889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2529af22f94aSTrond Myklebust 25305c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 25315c5fc09aSTrond Myklebust { 25325c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 253321c3ba7eSTrond Myklebust int ret = 0; 25345c5fc09aSTrond Myklebust 25353825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 25363825827eSTrond Myklebust return 0; 2537cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 25385c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 253921c3ba7eSTrond Myklebust return -ECHILD; 254021c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 254121c3ba7eSTrond Myklebust } 25425c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 25435c5fc09aSTrond Myklebust ret = -EACCES; 25445c5fc09aSTrond Myklebust return ret; 25455c5fc09aSTrond Myklebust } 25465c5fc09aSTrond Myklebust 254710556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 25481da177e4SLinus Torvalds { 2549b68572e0SNeilBrown const struct cred *cred = current_cred(); 25501da177e4SLinus Torvalds int res = 0; 25511da177e4SLinus Torvalds 255291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 255391d5b470SChuck Lever 2554e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 25551da177e4SLinus Torvalds goto out; 25561da177e4SLinus Torvalds /* Is this sys_access() ? */ 25579cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 25581da177e4SLinus Torvalds goto force_lookup; 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 25611da177e4SLinus Torvalds case S_IFLNK: 25621da177e4SLinus Torvalds goto out; 25631da177e4SLinus Torvalds case S_IFREG: 2564762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2565762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2566762674f8STrond Myklebust return 0; 25671da177e4SLinus Torvalds break; 25681da177e4SLinus Torvalds case S_IFDIR: 25691da177e4SLinus Torvalds /* 25701da177e4SLinus Torvalds * Optimize away all write operations, since the server 25711da177e4SLinus Torvalds * will check permissions when we perform the op. 25721da177e4SLinus Torvalds */ 25731da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 25741da177e4SLinus Torvalds goto out; 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds force_lookup: 25781da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 25791da177e4SLinus Torvalds goto out_notsup; 25801da177e4SLinus Torvalds 2581f3324a2aSNeilBrown /* Always try fast lookups first */ 2582f3324a2aSNeilBrown rcu_read_lock(); 2583f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2584f3324a2aSNeilBrown rcu_read_unlock(); 2585f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2586f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 25871da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2588f3324a2aSNeilBrown } 25891da177e4SLinus Torvalds out: 25905c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 25915c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2592f696a365SMiklos Szeredi 25931e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 25941e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 25951da177e4SLinus Torvalds return res; 25961da177e4SLinus Torvalds out_notsup: 2597d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2598d51ac1a8SNeilBrown return -ECHILD; 2599d51ac1a8SNeilBrown 26001da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 26011da177e4SLinus Torvalds if (res == 0) 26022830ba7fSAl Viro res = generic_permission(inode, mask); 26031e7cb3dcSChuck Lever goto out; 26041da177e4SLinus Torvalds } 2605ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds /* 26081da177e4SLinus Torvalds * Local variables: 26091da177e4SLinus Torvalds * version-control: t 26101da177e4SLinus Torvalds * kept-new-versions: 5 26111da177e4SLinus Torvalds * End: 26121da177e4SLinus Torvalds */ 2613