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 70684f39b4SNeilBrown 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; 80684f39b4SNeilBrown 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); 94684f39b4SNeilBrown 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 111684f39b4SNeilBrown 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 142be4c2d47Sluanshi struct readdirvec { 143be4c2d47Sluanshi unsigned long nr; 144be4c2d47Sluanshi unsigned long index; 145be4c2d47Sluanshi struct page *pages[NFS_MAX_READDIR_RAPAGES]; 146be4c2d47Sluanshi }; 147be4c2d47Sluanshi 148a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool); 1491da177e4SLinus Torvalds typedef struct { 1501da177e4SLinus Torvalds struct file *file; 1511da177e4SLinus Torvalds struct page *page; 15223db8620SAl Viro struct dir_context *ctx; 1531da177e4SLinus Torvalds unsigned long page_index; 154be4c2d47Sluanshi struct readdirvec pvec; 155f0dd2136STrond Myklebust u64 *dir_cookie; 1560aded708STrond Myklebust u64 last_cookie; 157f0dd2136STrond Myklebust loff_t current_index; 1581da177e4SLinus Torvalds decode_dirent_t decode; 159d1bacf9eSBryan Schumaker 1601f4eab7eSNeil Brown unsigned long timestamp; 1614704f0e2STrond Myklebust unsigned long gencount; 162d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 163a7a3b1e9SBenjamin Coddington bool plus; 164a7a3b1e9SBenjamin Coddington bool eof; 1651da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1661da177e4SLinus Torvalds 167d1bacf9eSBryan Schumaker /* 168d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 169d1bacf9eSBryan Schumaker */ 170d1bacf9eSBryan Schumaker static 17111de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 172d1bacf9eSBryan Schumaker { 17311de3b11STrond Myklebust struct nfs_cache_array *array; 174d1bacf9eSBryan Schumaker int i; 1758cd51a0cSTrond Myklebust 1762b86ce2dSCong Wang array = kmap_atomic(page); 177d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 178d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1792b86ce2dSCong Wang kunmap_atomic(array); 180d1bacf9eSBryan Schumaker } 181d1bacf9eSBryan Schumaker 182d1bacf9eSBryan Schumaker /* 183d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 184d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 185d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 186d1bacf9eSBryan Schumaker */ 187d1bacf9eSBryan Schumaker static 1884a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 189d1bacf9eSBryan Schumaker { 190d1bacf9eSBryan Schumaker string->len = len; 191d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 1924a201d6eSTrond Myklebust if (string->name == NULL) 1934a201d6eSTrond Myklebust return -ENOMEM; 19404e4bd1cSCatalin Marinas /* 19504e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 19604e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 19704e4bd1cSCatalin Marinas */ 19804e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 1998387ff25SLinus Torvalds string->hash = full_name_hash(NULL, name, len); 2004a201d6eSTrond Myklebust return 0; 201d1bacf9eSBryan Schumaker } 202d1bacf9eSBryan Schumaker 203d1bacf9eSBryan Schumaker static 204d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 205d1bacf9eSBryan Schumaker { 2060795bf83SFabian Frederick struct nfs_cache_array *array = kmap(page); 2074a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2084a201d6eSTrond Myklebust int ret; 2094a201d6eSTrond Myklebust 2104a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2113020093fSTrond Myklebust 2123020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2133020093fSTrond Myklebust ret = -ENOSPC; 2143020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2153020093fSTrond Myklebust goto out; 2163020093fSTrond Myklebust 2174a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2184a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2190b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2204a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2214a201d6eSTrond Myklebust if (ret) 2224a201d6eSTrond Myklebust goto out; 223d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2248cd51a0cSTrond Myklebust array->size++; 22547c716cbSTrond Myklebust if (entry->eof != 0) 226d1bacf9eSBryan Schumaker array->eof_index = array->size; 2274a201d6eSTrond Myklebust out: 2280795bf83SFabian Frederick kunmap(page); 2294a201d6eSTrond Myklebust return ret; 230d1bacf9eSBryan Schumaker } 231d1bacf9eSBryan Schumaker 232d1bacf9eSBryan Schumaker static 233d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 234d1bacf9eSBryan Schumaker { 23523db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 236d1bacf9eSBryan Schumaker unsigned int index; 237d1bacf9eSBryan Schumaker 238d1bacf9eSBryan Schumaker if (diff < 0) 239d1bacf9eSBryan Schumaker goto out_eof; 240d1bacf9eSBryan Schumaker if (diff >= array->size) { 2418cd51a0cSTrond Myklebust if (array->eof_index >= 0) 242d1bacf9eSBryan Schumaker goto out_eof; 243d1bacf9eSBryan Schumaker return -EAGAIN; 244d1bacf9eSBryan Schumaker } 245d1bacf9eSBryan Schumaker 246d1bacf9eSBryan Schumaker index = (unsigned int)diff; 247d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 248d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 249d1bacf9eSBryan Schumaker return 0; 250d1bacf9eSBryan Schumaker out_eof: 2516089dd0dSThomas Meyer desc->eof = true; 252d1bacf9eSBryan Schumaker return -EBADCOOKIE; 253d1bacf9eSBryan Schumaker } 254d1bacf9eSBryan Schumaker 2554db72b40SJeff Layton static bool 2564db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 2574db72b40SJeff Layton { 2584db72b40SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) 2594db72b40SJeff Layton return false; 2604db72b40SJeff Layton smp_rmb(); 2614db72b40SJeff Layton return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 2624db72b40SJeff Layton } 2634db72b40SJeff Layton 264d1bacf9eSBryan Schumaker static 265d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 266d1bacf9eSBryan Schumaker { 267d1bacf9eSBryan Schumaker int i; 2688ef2ce3eSBryan Schumaker loff_t new_pos; 269d1bacf9eSBryan Schumaker int status = -EAGAIN; 270d1bacf9eSBryan Schumaker 271d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2728cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 273496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2740c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2750c030806STrond Myklebust 2768ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2774db72b40SJeff Layton if (ctx->attr_gencount != nfsi->attr_gencount || 2784db72b40SJeff Layton !nfs_readdir_inode_mapping_valid(nfsi)) { 2790c030806STrond Myklebust ctx->duped = 0; 2800c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 28123db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2820c030806STrond Myklebust if (ctx->duped > 0 2830c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2840c030806STrond Myklebust if (printk_ratelimit()) { 2856de1472fSAl Viro pr_notice("NFS: directory %pD2 contains a readdir loop." 2860c030806STrond Myklebust "Please contact your server vendor. " 2879581a4aeSJeff Layton "The file: %.*s has duplicate cookie %llu\n", 2889581a4aeSJeff Layton desc->file, array->array[i].string.len, 2899581a4aeSJeff Layton array->array[i].string.name, *desc->dir_cookie); 2900c030806STrond Myklebust } 2910c030806STrond Myklebust status = -ELOOP; 2920c030806STrond Myklebust goto out; 2930c030806STrond Myklebust } 2948ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 2950c030806STrond Myklebust ctx->duped = -1; 2968ef2ce3eSBryan Schumaker } 29723db8620SAl Viro desc->ctx->pos = new_pos; 2988cd51a0cSTrond Myklebust desc->cache_entry_index = i; 29947c716cbSTrond Myklebust return 0; 3008cd51a0cSTrond Myklebust } 3018cd51a0cSTrond Myklebust } 30247c716cbSTrond Myklebust if (array->eof_index >= 0) { 303d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 30418fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 3056089dd0dSThomas Meyer desc->eof = true; 306d1bacf9eSBryan Schumaker } 3070c030806STrond Myklebust out: 308d1bacf9eSBryan Schumaker return status; 309d1bacf9eSBryan Schumaker } 310d1bacf9eSBryan Schumaker 311d1bacf9eSBryan Schumaker static 312d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 313d1bacf9eSBryan Schumaker { 314d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 31547c716cbSTrond Myklebust int status; 316d1bacf9eSBryan Schumaker 3170795bf83SFabian Frederick array = kmap(desc->page); 318d1bacf9eSBryan Schumaker 319d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 320d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 321d1bacf9eSBryan Schumaker else 322d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 323d1bacf9eSBryan Schumaker 32447c716cbSTrond Myklebust if (status == -EAGAIN) { 3250aded708STrond Myklebust desc->last_cookie = array->last_cookie; 326e47c085aSTrond Myklebust desc->current_index += array->size; 32747c716cbSTrond Myklebust desc->page_index++; 32847c716cbSTrond Myklebust } 3290795bf83SFabian Frederick kunmap(desc->page); 330d1bacf9eSBryan Schumaker return status; 331d1bacf9eSBryan Schumaker } 332d1bacf9eSBryan Schumaker 333d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 334d1bacf9eSBryan Schumaker static 33556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 336d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 337d1bacf9eSBryan Schumaker { 338480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 339684f39b4SNeilBrown const struct cred *cred = ctx->cred; 3404704f0e2STrond Myklebust unsigned long timestamp, gencount; 3411da177e4SLinus Torvalds int error; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds again: 3441da177e4SLinus Torvalds timestamp = jiffies; 3454704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 346be62a1a8SMiklos Szeredi error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages, 3471da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3481da177e4SLinus Torvalds if (error < 0) { 3491da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3501da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3511da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3523a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 353a7a3b1e9SBenjamin Coddington desc->plus = false; 3541da177e4SLinus Torvalds goto again; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds goto error; 3571da177e4SLinus Torvalds } 3581f4eab7eSNeil Brown desc->timestamp = timestamp; 3594704f0e2STrond Myklebust desc->gencount = gencount; 360d1bacf9eSBryan Schumaker error: 361d1bacf9eSBryan Schumaker return error; 362d1bacf9eSBryan Schumaker } 363d1bacf9eSBryan Schumaker 364573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 365573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 366d1bacf9eSBryan Schumaker { 367573c4e1eSChuck Lever int error; 368d1bacf9eSBryan Schumaker 369573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 370573c4e1eSChuck Lever if (error) 371573c4e1eSChuck Lever return error; 372d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 373d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 374d1bacf9eSBryan Schumaker return 0; 375d1bacf9eSBryan Schumaker } 376d1bacf9eSBryan Schumaker 377fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid 378fa923369STrond Myklebust * Note: caller is responsible for checking the fsid 379fa923369STrond Myklebust */ 380d39ab9deSBryan Schumaker static 381d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 382d39ab9deSBryan Schumaker { 383d8fdb47fSTrond Myklebust struct inode *inode; 384fa923369STrond Myklebust struct nfs_inode *nfsi; 385fa923369STrond Myklebust 3862b0143b5SDavid Howells if (d_really_is_negative(dentry)) 3872b0143b5SDavid Howells return 0; 388fa923369STrond Myklebust 389d8fdb47fSTrond Myklebust inode = d_inode(dentry); 390d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode)) 391d8fdb47fSTrond Myklebust return 0; 392d8fdb47fSTrond Myklebust 393d8fdb47fSTrond Myklebust nfsi = NFS_I(inode); 3947dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid) 395d39ab9deSBryan Schumaker return 0; 3967dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 3977dc72d5fSTrond Myklebust return 0; 3987dc72d5fSTrond Myklebust return 1; 399d39ab9deSBryan Schumaker } 400d39ab9deSBryan Schumaker 401d39ab9deSBryan Schumaker static 40223db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 403d69ee9b8STrond Myklebust { 404d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 405d69ee9b8STrond Myklebust return false; 406d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 407d69ee9b8STrond Myklebust return true; 40823db8620SAl Viro if (ctx->pos == 0) 409d69ee9b8STrond Myklebust return true; 410d69ee9b8STrond Myklebust return false; 411d69ee9b8STrond Myklebust } 412d69ee9b8STrond Myklebust 413d69ee9b8STrond Myklebust /* 41463519fbcSTrond Myklebust * This function is called by the lookup and getattr code to request the 41563519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same 416d69ee9b8STrond Myklebust * directory. 417d69ee9b8STrond Myklebust */ 418d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 419d69ee9b8STrond Myklebust { 42063519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 42163519fbcSTrond Myklebust 42263519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 42363519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) 42463519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 425d69ee9b8STrond Myklebust } 426d69ee9b8STrond Myklebust 427311324adSTrond Myklebust /* 428311324adSTrond Myklebust * This function is mainly for use by nfs_getattr(). 429311324adSTrond Myklebust * 430311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus. 431311324adSTrond Myklebust * Do this by checking if there is an active file descriptor 432311324adSTrond Myklebust * and calling nfs_advise_use_readdirplus, then forcing a 433311324adSTrond Myklebust * cache flush. 434311324adSTrond Myklebust */ 435311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir) 436311324adSTrond Myklebust { 43763519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir); 43863519fbcSTrond Myklebust 43963519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 44063519fbcSTrond Myklebust !list_empty(&nfsi->open_files)) { 44163519fbcSTrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 44279f687a3STrond Myklebust invalidate_mapping_pages(dir->i_mapping, 0, -1); 443311324adSTrond Myklebust } 444311324adSTrond Myklebust } 445311324adSTrond Myklebust 446d69ee9b8STrond Myklebust static 447d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 448d39ab9deSBryan Schumaker { 44926fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4509ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 4514a201d6eSTrond Myklebust struct dentry *dentry; 4524a201d6eSTrond Myklebust struct dentry *alias; 4532b0143b5SDavid Howells struct inode *dir = d_inode(parent); 454d39ab9deSBryan Schumaker struct inode *inode; 455aa9c2669SDavid Quigley int status; 456d39ab9deSBryan Schumaker 457fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 458fa923369STrond Myklebust return; 4596c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 4606c441c25STrond Myklebust return; 46178d04af4STrond Myklebust if (filename.len == 0) 46278d04af4STrond Myklebust return; 46378d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */ 46478d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len) 46578d04af4STrond Myklebust return; 46678d04af4STrond Myklebust /* ...or '/' */ 46778d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/')) 46878d04af4STrond Myklebust return; 4694a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4704a201d6eSTrond Myklebust if (filename.len == 1) 4714a201d6eSTrond Myklebust return; 4724a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4734a201d6eSTrond Myklebust return; 4744a201d6eSTrond Myklebust } 4758387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len); 476d39ab9deSBryan Schumaker 4774a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 4789ac3d3e8SAl Viro again: 4799ac3d3e8SAl Viro if (!dentry) { 4809ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq); 4819ac3d3e8SAl Viro if (IS_ERR(dentry)) 4829ac3d3e8SAl Viro return; 4839ac3d3e8SAl Viro } 4849ac3d3e8SAl Viro if (!d_in_lookup(dentry)) { 4856c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */ 4866c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 4876c441c25STrond Myklebust &entry->fattr->fsid)) 4886c441c25STrond Myklebust goto out; 489d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 4907dc72d5fSTrond Myklebust if (!entry->fh->size) 4917dc72d5fSTrond Myklebust goto out; 492cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 4932b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 494aa9c2669SDavid Quigley if (!status) 4952b0143b5SDavid Howells nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 496d39ab9deSBryan Schumaker goto out; 497d39ab9deSBryan Schumaker } else { 4985542aa2fSEric W. Biederman d_invalidate(dentry); 499d39ab9deSBryan Schumaker dput(dentry); 5009ac3d3e8SAl Viro dentry = NULL; 5019ac3d3e8SAl Viro goto again; 502d39ab9deSBryan Schumaker } 503d39ab9deSBryan Schumaker } 5047dc72d5fSTrond Myklebust if (!entry->fh->size) { 5057dc72d5fSTrond Myklebust d_lookup_done(dentry); 5067dc72d5fSTrond Myklebust goto out; 5077dc72d5fSTrond Myklebust } 508d39ab9deSBryan Schumaker 5091775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 51041d28bcaSAl Viro alias = d_splice_alias(inode, dentry); 5119ac3d3e8SAl Viro d_lookup_done(dentry); 5129ac3d3e8SAl Viro if (alias) { 513d39ab9deSBryan Schumaker if (IS_ERR(alias)) 514d39ab9deSBryan Schumaker goto out; 5159ac3d3e8SAl Viro dput(dentry); 5169ac3d3e8SAl Viro dentry = alias; 5179ac3d3e8SAl Viro } 518d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 519d39ab9deSBryan Schumaker out: 520d39ab9deSBryan Schumaker dput(dentry); 521d39ab9deSBryan Schumaker } 522d39ab9deSBryan Schumaker 523d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 524d1bacf9eSBryan Schumaker static 5258cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5266650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 527d1bacf9eSBryan Schumaker { 528babddc72SBryan Schumaker struct xdr_stream stream; 529f7da7a12SBenny Halevy struct xdr_buf buf; 5306650239aSTrond Myklebust struct page *scratch; 53199424380SBryan Schumaker struct nfs_cache_array *array; 5325c346854STrond Myklebust unsigned int count = 0; 5335c346854STrond Myklebust int status; 534be4c2d47Sluanshi int max_rapages = NFS_MAX_READDIR_RAPAGES; 535be4c2d47Sluanshi 536be4c2d47Sluanshi desc->pvec.index = desc->page_index; 537be4c2d47Sluanshi desc->pvec.nr = 0; 538babddc72SBryan Schumaker 5396650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5406650239aSTrond Myklebust if (scratch == NULL) 5416650239aSTrond Myklebust return -ENOMEM; 542babddc72SBryan Schumaker 543ce85cfbeSBenjamin Coddington if (buflen == 0) 544ce85cfbeSBenjamin Coddington goto out_nopages; 545ce85cfbeSBenjamin Coddington 546f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5476650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 54899424380SBryan Schumaker 54999424380SBryan Schumaker do { 55099424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5518cd51a0cSTrond Myklebust if (status != 0) { 5528cd51a0cSTrond Myklebust if (status == -EAGAIN) 5538cd51a0cSTrond Myklebust status = 0; 55499424380SBryan Schumaker break; 5558cd51a0cSTrond Myklebust } 55699424380SBryan Schumaker 5575c346854STrond Myklebust count++; 5585c346854STrond Myklebust 559a7a3b1e9SBenjamin Coddington if (desc->plus) 560be62a1a8SMiklos Szeredi nfs_prime_dcache(file_dentry(desc->file), entry); 5618cd51a0cSTrond Myklebust 562be4c2d47Sluanshi status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]); 563be4c2d47Sluanshi if (status == -ENOSPC) { 564be4c2d47Sluanshi desc->pvec.nr++; 565be4c2d47Sluanshi if (desc->pvec.nr == max_rapages) 566be4c2d47Sluanshi break; 567be4c2d47Sluanshi status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]); 568be4c2d47Sluanshi } 5698cd51a0cSTrond Myklebust if (status != 0) 5708cd51a0cSTrond Myklebust break; 57199424380SBryan Schumaker } while (!entry->eof); 57299424380SBryan Schumaker 573be4c2d47Sluanshi /* 574be4c2d47Sluanshi * page and desc->pvec.pages[0] are valid, don't need to check 575be4c2d47Sluanshi * whether or not to be NULL. 576be4c2d47Sluanshi */ 577be4c2d47Sluanshi copy_highpage(page, desc->pvec.pages[0]); 578be4c2d47Sluanshi 579ce85cfbeSBenjamin Coddington out_nopages: 58047c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 581be4c2d47Sluanshi array = kmap_atomic(desc->pvec.pages[desc->pvec.nr]); 5828cd51a0cSTrond Myklebust array->eof_index = array->size; 58399424380SBryan Schumaker status = 0; 584be4c2d47Sluanshi kunmap_atomic(array); 58556e4ebf8SBryan Schumaker } 5866650239aSTrond Myklebust 5876650239aSTrond Myklebust put_page(scratch); 588be4c2d47Sluanshi 589be4c2d47Sluanshi /* 590be4c2d47Sluanshi * desc->pvec.nr > 0 means at least one page was completely filled, 591be4c2d47Sluanshi * we should return -ENOSPC. Otherwise function 592be4c2d47Sluanshi * nfs_readdir_xdr_to_array will enter infinite loop. 593be4c2d47Sluanshi */ 594be4c2d47Sluanshi if (desc->pvec.nr > 0) 595be4c2d47Sluanshi return -ENOSPC; 5968cd51a0cSTrond Myklebust return status; 5978cd51a0cSTrond Myklebust } 59856e4ebf8SBryan Schumaker 59956e4ebf8SBryan Schumaker static 600c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages) 60156e4ebf8SBryan Schumaker { 60256e4ebf8SBryan Schumaker unsigned int i; 60356e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 60456e4ebf8SBryan Schumaker put_page(pages[i]); 60556e4ebf8SBryan Schumaker } 60656e4ebf8SBryan Schumaker 60756e4ebf8SBryan Schumaker /* 608*bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 609*bf211ca1Szhangliguang * to nfs_readdir_free_pages() 61056e4ebf8SBryan Schumaker */ 61156e4ebf8SBryan Schumaker static 612c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages) 61356e4ebf8SBryan Schumaker { 61456e4ebf8SBryan Schumaker unsigned int i; 61556e4ebf8SBryan Schumaker 61656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 61756e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 61856e4ebf8SBryan Schumaker if (page == NULL) 61956e4ebf8SBryan Schumaker goto out_freepages; 62056e4ebf8SBryan Schumaker pages[i] = page; 62156e4ebf8SBryan Schumaker } 6226650239aSTrond Myklebust return 0; 62356e4ebf8SBryan Schumaker 62456e4ebf8SBryan Schumaker out_freepages: 625c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i); 6266650239aSTrond Myklebust return -ENOMEM; 627d1bacf9eSBryan Schumaker } 628d1bacf9eSBryan Schumaker 629be4c2d47Sluanshi /* 630be4c2d47Sluanshi * nfs_readdir_rapages_init initialize rapages by nfs_cache_array structure. 631be4c2d47Sluanshi */ 632be4c2d47Sluanshi static 633be4c2d47Sluanshi void nfs_readdir_rapages_init(nfs_readdir_descriptor_t *desc) 634be4c2d47Sluanshi { 635be4c2d47Sluanshi struct nfs_cache_array *array; 636be4c2d47Sluanshi int max_rapages = NFS_MAX_READDIR_RAPAGES; 637be4c2d47Sluanshi int index; 638be4c2d47Sluanshi 639be4c2d47Sluanshi for (index = 0; index < max_rapages; index++) { 640be4c2d47Sluanshi array = kmap_atomic(desc->pvec.pages[index]); 641be4c2d47Sluanshi memset(array, 0, sizeof(struct nfs_cache_array)); 642be4c2d47Sluanshi array->eof_index = -1; 643be4c2d47Sluanshi kunmap_atomic(array); 644be4c2d47Sluanshi } 645be4c2d47Sluanshi } 646be4c2d47Sluanshi 647d1bacf9eSBryan Schumaker static 648d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 649d1bacf9eSBryan Schumaker { 65056e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 651d1bacf9eSBryan Schumaker struct nfs_entry entry; 652d1bacf9eSBryan Schumaker struct file *file = desc->file; 653d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6548cd51a0cSTrond Myklebust int status = -ENOMEM; 65556e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 656d1bacf9eSBryan Schumaker 657be4c2d47Sluanshi /* 658be4c2d47Sluanshi * This means we hit readdir rdpages miss, the preallocated rdpages 659be4c2d47Sluanshi * are useless, the preallocate rdpages should be reinitialized. 660be4c2d47Sluanshi */ 661be4c2d47Sluanshi nfs_readdir_rapages_init(desc); 662be4c2d47Sluanshi 663d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6640aded708STrond Myklebust entry.cookie = desc->last_cookie; 665d1bacf9eSBryan Schumaker entry.eof = 0; 666d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 667d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 668573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 669d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 670d1bacf9eSBryan Schumaker goto out; 671d1bacf9eSBryan Schumaker 67214c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 67314c43f76SDavid Quigley if (IS_ERR(entry.label)) { 67414c43f76SDavid Quigley status = PTR_ERR(entry.label); 67514c43f76SDavid Quigley goto out; 67614c43f76SDavid Quigley } 67714c43f76SDavid Quigley 6780795bf83SFabian Frederick array = kmap(page); 679d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 680d1bacf9eSBryan Schumaker array->eof_index = -1; 681d1bacf9eSBryan Schumaker 682c7e9668eSAnna Schumaker status = nfs_readdir_alloc_pages(pages, array_size); 6836650239aSTrond Myklebust if (status < 0) 684d1bacf9eSBryan Schumaker goto out_release_array; 685d1bacf9eSBryan Schumaker do { 686ac396128STrond Myklebust unsigned int pglen; 68756e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 688babddc72SBryan Schumaker 689d1bacf9eSBryan Schumaker if (status < 0) 690d1bacf9eSBryan Schumaker break; 691ac396128STrond Myklebust pglen = status; 6926650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6938cd51a0cSTrond Myklebust if (status < 0) { 6948cd51a0cSTrond Myklebust if (status == -ENOSPC) 6958cd51a0cSTrond Myklebust status = 0; 6968cd51a0cSTrond Myklebust break; 6978cd51a0cSTrond Myklebust } 6988cd51a0cSTrond Myklebust } while (array->eof_index < 0); 699d1bacf9eSBryan Schumaker 700c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size); 701d1bacf9eSBryan Schumaker out_release_array: 7020795bf83SFabian Frederick kunmap(page); 70314c43f76SDavid Quigley nfs4_label_free(entry.label); 704d1bacf9eSBryan Schumaker out: 705d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 706d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 707d1bacf9eSBryan Schumaker return status; 708d1bacf9eSBryan Schumaker } 709d1bacf9eSBryan Schumaker 710d1bacf9eSBryan Schumaker /* 711d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 712d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 713d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 714d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7151da177e4SLinus Torvalds */ 716d1bacf9eSBryan Schumaker static 717d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 718d1bacf9eSBryan Schumaker { 719496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7208cd51a0cSTrond Myklebust int ret; 721d1bacf9eSBryan Schumaker 722be4c2d47Sluanshi /* 723be4c2d47Sluanshi * If desc->page_index in range desc->pvec.index and 724be4c2d47Sluanshi * desc->pvec.index + desc->pvec.nr, we get readdir cache hit. 725be4c2d47Sluanshi */ 726be4c2d47Sluanshi if (desc->page_index >= desc->pvec.index && 727be4c2d47Sluanshi desc->page_index < (desc->pvec.index + desc->pvec.nr)) { 728be4c2d47Sluanshi /* 729be4c2d47Sluanshi * page and desc->pvec.pages[x] are valid, don't need to check 730be4c2d47Sluanshi * whether or not to be NULL. 731be4c2d47Sluanshi */ 732be4c2d47Sluanshi copy_highpage(page, desc->pvec.pages[desc->page_index - desc->pvec.index]); 733be4c2d47Sluanshi ret = 0; 734be4c2d47Sluanshi } else { 7358cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7368cd51a0cSTrond Myklebust if (ret < 0) 737d1bacf9eSBryan Schumaker goto error; 738be4c2d47Sluanshi } 739be4c2d47Sluanshi 740d1bacf9eSBryan Schumaker SetPageUptodate(page); 741d1bacf9eSBryan Schumaker 7422aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 743cd9ae2b6STrond Myklebust /* Should never happen */ 744cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 745cd9ae2b6STrond Myklebust } 7461da177e4SLinus Torvalds unlock_page(page); 7471da177e4SLinus Torvalds return 0; 7481da177e4SLinus Torvalds error: 7491da177e4SLinus Torvalds unlock_page(page); 7508cd51a0cSTrond Myklebust return ret; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753d1bacf9eSBryan Schumaker static 754d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7551da177e4SLinus Torvalds { 756b044f645SBenjamin Coddington if (!desc->page->mapping) 75711de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 75809cbfeafSKirill A. Shutemov put_page(desc->page); 7591da177e4SLinus Torvalds desc->page = NULL; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 762d1bacf9eSBryan Schumaker static 763d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7641da177e4SLinus Torvalds { 765b044f645SBenjamin Coddington return read_cache_page(desc->file->f_mapping, 766d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* 770d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7711da177e4SLinus Torvalds */ 772d1bacf9eSBryan Schumaker static 773d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 774d1bacf9eSBryan Schumaker { 775d1bacf9eSBryan Schumaker int res; 776d1bacf9eSBryan Schumaker 777d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 778d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 779d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 780d1bacf9eSBryan Schumaker 781d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 78247c716cbSTrond Myklebust if (res != 0) 783d1bacf9eSBryan Schumaker cache_page_release(desc); 784d1bacf9eSBryan Schumaker return res; 785d1bacf9eSBryan Schumaker } 786d1bacf9eSBryan Schumaker 787d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7881da177e4SLinus Torvalds static inline 7891da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7901da177e4SLinus Torvalds { 7918cd51a0cSTrond Myklebust int res; 792d1bacf9eSBryan Schumaker 7930aded708STrond Myklebust if (desc->page_index == 0) { 7948cd51a0cSTrond Myklebust desc->current_index = 0; 7950aded708STrond Myklebust desc->last_cookie = 0; 7960aded708STrond Myklebust } 79747c716cbSTrond Myklebust do { 798d1bacf9eSBryan Schumaker res = find_cache_page(desc); 79947c716cbSTrond Myklebust } while (res == -EAGAIN); 8001da177e4SLinus Torvalds return res; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 8051da177e4SLinus Torvalds */ 8061da177e4SLinus Torvalds static 80723db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 8081da177e4SLinus Torvalds { 8091da177e4SLinus Torvalds struct file *file = desc->file; 810d1bacf9eSBryan Schumaker int i = 0; 811d1bacf9eSBryan Schumaker int res = 0; 812d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 8138ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 8148ef2ce3eSBryan Schumaker 8150795bf83SFabian Frederick array = kmap(desc->page); 816d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 817ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 8181da177e4SLinus Torvalds 819ece0b423STrond Myklebust ent = &array->array[i]; 82023db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 82123db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 8226089dd0dSThomas Meyer desc->eof = true; 8231da177e4SLinus Torvalds break; 824ece0b423STrond Myklebust } 82523db8620SAl Viro desc->ctx->pos++; 826d1bacf9eSBryan Schumaker if (i < (array->size-1)) 827d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 828d1bacf9eSBryan Schumaker else 829d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 8300c030806STrond Myklebust if (ctx->duped != 0) 8310c030806STrond Myklebust ctx->duped = 1; 8328cd51a0cSTrond Myklebust } 83347c716cbSTrond Myklebust if (array->eof_index >= 0) 8346089dd0dSThomas Meyer desc->eof = true; 835d1bacf9eSBryan Schumaker 8360795bf83SFabian Frederick kunmap(desc->page); 837d1bacf9eSBryan Schumaker cache_page_release(desc); 8381e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8391e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8401da177e4SLinus Torvalds return res; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds /* 8441da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8451da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8461da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8471da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8481da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8491da177e4SLinus Torvalds * 8501da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8511da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8521da177e4SLinus Torvalds * we should already have a complete representation of the 8531da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8541da177e4SLinus Torvalds */ 8551da177e4SLinus Torvalds static inline 85623db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds struct page *page = NULL; 8591da177e4SLinus Torvalds int status; 860496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 8610c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8621da177e4SLinus Torvalds 8631e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8641e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8671da177e4SLinus Torvalds if (!page) { 8681da177e4SLinus Torvalds status = -ENOMEM; 8691da177e4SLinus Torvalds goto out; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8727a8e1dc3STrond Myklebust desc->page_index = 0; 8730aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8747a8e1dc3STrond Myklebust desc->page = page; 8750c030806STrond Myklebust ctx->duped = 0; 8767a8e1dc3STrond Myklebust 87785f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 87885f8607eSTrond Myklebust if (status < 0) 879d1bacf9eSBryan Schumaker goto out_release; 880d1bacf9eSBryan Schumaker 88123db8620SAl Viro status = nfs_do_filldir(desc); 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds out: 8841e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8853110ff80SHarvey Harrison __func__, status); 8861da177e4SLinus Torvalds return status; 8871da177e4SLinus Torvalds out_release: 888d1bacf9eSBryan Schumaker cache_page_release(desc); 8891da177e4SLinus Torvalds goto out; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 89200a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 89300a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 89400a92642SOlivier Galibert whole directory. 8951da177e4SLinus Torvalds */ 89623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8971da177e4SLinus Torvalds { 898be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file); 8992b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 9001da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 9011da177e4SLinus Torvalds *desc = &my_desc; 90223db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 90307b5ce8eSScott Mayhew int res = 0; 904be4c2d47Sluanshi int max_rapages = NFS_MAX_READDIR_RAPAGES; 9051da177e4SLinus Torvalds 9066de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 9076de1472fSAl Viro file, (long long)ctx->pos); 90891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 90991d5b470SChuck Lever 9101da177e4SLinus Torvalds /* 91123db8620SAl Viro * ctx->pos points to the dirent entry number. 912f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 91300a92642SOlivier Galibert * to either find the entry with the appropriate number or 91400a92642SOlivier Galibert * revalidate the cookie. 9151da177e4SLinus Torvalds */ 9161da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 9171da177e4SLinus Torvalds 91823db8620SAl Viro desc->file = file; 91923db8620SAl Viro desc->ctx = ctx; 920480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 9211da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 922a7a3b1e9SBenjamin Coddington desc->plus = nfs_use_readdirplus(inode, ctx); 9231da177e4SLinus Torvalds 924be4c2d47Sluanshi res = nfs_readdir_alloc_pages(desc->pvec.pages, max_rapages); 925be4c2d47Sluanshi if (res < 0) 926be4c2d47Sluanshi return -ENOMEM; 927be4c2d47Sluanshi 928be4c2d47Sluanshi nfs_readdir_rapages_init(desc); 929be4c2d47Sluanshi 93079f687a3STrond Myklebust if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 93123db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 932fccca7fcSTrond Myklebust if (res < 0) 933fccca7fcSTrond Myklebust goto out; 934fccca7fcSTrond Myklebust 93547c716cbSTrond Myklebust do { 9361da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 93700a92642SOlivier Galibert 9381da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 939ece0b423STrond Myklebust res = 0; 9401da177e4SLinus Torvalds /* This means either end of directory */ 9416089dd0dSThomas Meyer if (*desc->dir_cookie && !desc->eof) { 9421da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 94323db8620SAl Viro res = uncached_readdir(desc); 944ece0b423STrond Myklebust if (res == 0) 9451da177e4SLinus Torvalds continue; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds break; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9503a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9511da177e4SLinus Torvalds nfs_zap_caches(inode); 952baf57a09STrond Myklebust desc->page_index = 0; 953a7a3b1e9SBenjamin Coddington desc->plus = false; 954a7a3b1e9SBenjamin Coddington desc->eof = false; 9551da177e4SLinus Torvalds continue; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds if (res < 0) 9581da177e4SLinus Torvalds break; 9591da177e4SLinus Torvalds 96023db8620SAl Viro res = nfs_do_filldir(desc); 961ece0b423STrond Myklebust if (res < 0) 9621da177e4SLinus Torvalds break; 96347c716cbSTrond Myklebust } while (!desc->eof); 964fccca7fcSTrond Myklebust out: 965be4c2d47Sluanshi nfs_readdir_free_pages(desc->pvec.pages, max_rapages); 9661e7cb3dcSChuck Lever if (res > 0) 9671e7cb3dcSChuck Lever res = 0; 9686de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 9691da177e4SLinus Torvalds return res; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 972965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 973f0dd2136STrond Myklebust { 974b044f645SBenjamin Coddington struct inode *inode = file_inode(filp); 975480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 976b84e06c5SChuck Lever 9776de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 9786de1472fSAl Viro filp, offset, whence); 979b84e06c5SChuck Lever 980965c8e59SAndrew Morton switch (whence) { 981f0dd2136STrond Myklebust default: 982b2b1ff3dSTrond Myklebust return -EINVAL; 983b2b1ff3dSTrond Myklebust case SEEK_SET: 984b2b1ff3dSTrond Myklebust if (offset < 0) 985b2b1ff3dSTrond Myklebust return -EINVAL; 986b2b1ff3dSTrond Myklebust inode_lock(inode); 987b2b1ff3dSTrond Myklebust break; 988b2b1ff3dSTrond Myklebust case SEEK_CUR: 989b2b1ff3dSTrond Myklebust if (offset == 0) 990b2b1ff3dSTrond Myklebust return filp->f_pos; 991b2b1ff3dSTrond Myklebust inode_lock(inode); 992b2b1ff3dSTrond Myklebust offset += filp->f_pos; 993b2b1ff3dSTrond Myklebust if (offset < 0) { 994b2b1ff3dSTrond Myklebust inode_unlock(inode); 995b2b1ff3dSTrond Myklebust return -EINVAL; 996b2b1ff3dSTrond Myklebust } 997f0dd2136STrond Myklebust } 998f0dd2136STrond Myklebust if (offset != filp->f_pos) { 999f0dd2136STrond Myklebust filp->f_pos = offset; 1000480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 10018ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 1002f0dd2136STrond Myklebust } 1003b044f645SBenjamin Coddington inode_unlock(inode); 1004f0dd2136STrond Myklebust return offset; 1005f0dd2136STrond Myklebust } 1006f0dd2136STrond Myklebust 10071da177e4SLinus Torvalds /* 10081da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 10091da177e4SLinus Torvalds * is a dummy operation. 10101da177e4SLinus Torvalds */ 101102c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 101202c24a82SJosef Bacik int datasync) 10131da177e4SLinus Torvalds { 10146de1472fSAl Viro struct inode *inode = file_inode(filp); 10157ea80859SChristoph Hellwig 10166de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 10171e7cb3dcSChuck Lever 10185955102cSAl Viro inode_lock(inode); 10196de1472fSAl Viro nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 10205955102cSAl Viro inode_unlock(inode); 10211da177e4SLinus Torvalds return 0; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 1024bfc69a45STrond Myklebust /** 1025bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1026302fad7bSTrond Myklebust * @dir: pointer to directory inode 1027bfc69a45STrond Myklebust * 1028bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1029bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1030bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1031bfc69a45STrond Myklebust * 1032bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1033bfc69a45STrond Myklebust */ 1034bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1035bfc69a45STrond Myklebust { 1036011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 1037bfc69a45STrond Myklebust } 103889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1039bfc69a45STrond Myklebust 10401da177e4SLinus Torvalds /* 10411da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 10421da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10431da177e4SLinus Torvalds * and may need to be looked up again. 1044912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0. 10451da177e4SLinus Torvalds */ 1046912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1047912a108dSNeilBrown int rcu_walk) 10481da177e4SLinus Torvalds { 10491da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10501da177e4SLinus Torvalds return 1; 10514eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10524eec952eSTrond Myklebust return 0; 1053f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10546ecc5e8fSTrond Myklebust return 0; 1055f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 10561cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) { 1057912a108dSNeilBrown if (rcu_walk) 1058f2c77f4eSTrond Myklebust return 0; 10591cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 10601cd9cb05STrond Myklebust return 0; 10611cd9cb05STrond Myklebust } 1062f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1063f2c77f4eSTrond Myklebust return 0; 1064f2c77f4eSTrond Myklebust return 1; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds /* 1068a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1069a12802caSTrond Myklebust * an O_EXCL create using this path component. 1070a12802caSTrond Myklebust */ 1071fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1072a12802caSTrond Myklebust { 1073a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1074a12802caSTrond Myklebust return 0; 1075fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 1076a12802caSTrond Myklebust } 1077a12802caSTrond Myklebust 1078a12802caSTrond Myklebust /* 10791d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10801d6757fbSTrond Myklebust * 10811d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10821d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10831d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10841d6757fbSTrond Myklebust * 10851d6757fbSTrond Myklebust */ 108665a0c149STrond Myklebust static 1087fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 10881da177e4SLinus Torvalds { 10891da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 109065a0c149STrond Myklebust int ret; 10911da177e4SLinus Torvalds 109236d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10934e99a1ffSTrond Myklebust return 0; 109447921921STrond Myklebust 109547921921STrond Myklebust if (flags & LOOKUP_OPEN) { 109647921921STrond Myklebust switch (inode->i_mode & S_IFMT) { 109747921921STrond Myklebust case S_IFREG: 109847921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */ 109947921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN) 110047921921STrond Myklebust goto out; 110147921921STrond Myklebust /* Fallthrough */ 110247921921STrond Myklebust case S_IFDIR: 110347921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO) 110447921921STrond Myklebust break; 110547921921STrond Myklebust /* NFS close-to-open cache consistency validation */ 110647921921STrond Myklebust goto out_force; 110747921921STrond Myklebust } 110847921921STrond Myklebust } 110947921921STrond Myklebust 11101da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1111fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 11121da177e4SLinus Torvalds goto out_force; 111365a0c149STrond Myklebust out: 1114a61246c9SLance Shelton return (inode->i_nlink == 0) ? -ESTALE : 0; 11151da177e4SLinus Torvalds out_force: 11161fa1e384SNeilBrown if (flags & LOOKUP_RCU) 11171fa1e384SNeilBrown return -ECHILD; 111865a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 111965a0c149STrond Myklebust if (ret != 0) 112065a0c149STrond Myklebust return ret; 112165a0c149STrond Myklebust goto out; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /* 11251da177e4SLinus Torvalds * We judge how long we want to trust negative 11261da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 11271da177e4SLinus Torvalds * 11281da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 11291da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 1130912a108dSNeilBrown * 1131912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1 1132912a108dSNeilBrown * suggesting a reval is needed. 11339f6d44d4STrond Myklebust * 11349f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target, 11359f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry. 11361da177e4SLinus Torvalds */ 11371da177e4SLinus Torvalds static inline 11381da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1139fa3c56bbSAl Viro unsigned int flags) 11401da177e4SLinus Torvalds { 11419f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 11421da177e4SLinus Torvalds return 0; 11434eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 11444eec952eSTrond Myklebust return 1; 1145912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11485ceb9d7fSTrond Myklebust static int 11495ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 11505ceb9d7fSTrond Myklebust struct inode *inode, int error) 11511da177e4SLinus Torvalds { 11525ceb9d7fSTrond Myklebust switch (error) { 11535ceb9d7fSTrond Myklebust case 1: 11546de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 11556de1472fSAl Viro __func__, dentry); 11561da177e4SLinus Torvalds return 1; 11575ceb9d7fSTrond Myklebust case 0: 1158a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11591da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11601da177e4SLinus Torvalds /* Purge readdir caches. */ 11611da177e4SLinus Torvalds nfs_zap_caches(inode); 1162a3f432bfSJ. Bruce Fields /* 1163a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree: 1164a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide 1165a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy 1166a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses. 1167a3f432bfSJ. Bruce Fields */ 1168a3f432bfSJ. Bruce Fields if (IS_ROOT(dentry)) 11695ceb9d7fSTrond Myklebust return 1; 11701da177e4SLinus Torvalds } 11716de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 11726de1472fSAl Viro __func__, dentry); 11731da177e4SLinus Torvalds return 0; 11745ceb9d7fSTrond Myklebust } 11756de1472fSAl Viro dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 11766de1472fSAl Viro __func__, dentry, error); 1177e1fb4d05STrond Myklebust return error; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11805ceb9d7fSTrond Myklebust static int 11815ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 11825ceb9d7fSTrond Myklebust unsigned int flags) 11835ceb9d7fSTrond Myklebust { 11845ceb9d7fSTrond Myklebust int ret = 1; 11855ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) { 11865ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 11875ceb9d7fSTrond Myklebust return -ECHILD; 11885ceb9d7fSTrond Myklebust ret = 0; 11895ceb9d7fSTrond Myklebust } 11905ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 11915ceb9d7fSTrond Myklebust } 11925ceb9d7fSTrond Myklebust 11935ceb9d7fSTrond Myklebust static int 11945ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 11955ceb9d7fSTrond Myklebust struct inode *inode) 11965ceb9d7fSTrond Myklebust { 11975ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11985ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 11995ceb9d7fSTrond Myklebust } 12005ceb9d7fSTrond Myklebust 12015ceb9d7fSTrond Myklebust static int 12025ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 12035ceb9d7fSTrond Myklebust struct inode *inode) 12045ceb9d7fSTrond Myklebust { 12055ceb9d7fSTrond Myklebust struct nfs_fh *fhandle; 12065ceb9d7fSTrond Myklebust struct nfs_fattr *fattr; 12075ceb9d7fSTrond Myklebust struct nfs4_label *label; 12085ceb9d7fSTrond Myklebust int ret; 12095ceb9d7fSTrond Myklebust 12105ceb9d7fSTrond Myklebust ret = -ENOMEM; 12115ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle(); 12125ceb9d7fSTrond Myklebust fattr = nfs_alloc_fattr(); 12135ceb9d7fSTrond Myklebust label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 12145ceb9d7fSTrond Myklebust if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 12155ceb9d7fSTrond Myklebust goto out; 12165ceb9d7fSTrond Myklebust 12175ceb9d7fSTrond Myklebust ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 12185ceb9d7fSTrond Myklebust if (ret < 0) { 12195ceb9d7fSTrond Myklebust if (ret == -ESTALE || ret == -ENOENT) 12205ceb9d7fSTrond Myklebust ret = 0; 12215ceb9d7fSTrond Myklebust goto out; 12225ceb9d7fSTrond Myklebust } 12235ceb9d7fSTrond Myklebust ret = 0; 12245ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 12255ceb9d7fSTrond Myklebust goto out; 12265ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0) 12275ceb9d7fSTrond Myklebust goto out; 12285ceb9d7fSTrond Myklebust 12295ceb9d7fSTrond Myklebust nfs_setsecurity(inode, fattr, label); 12305ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12315ceb9d7fSTrond Myklebust 12325ceb9d7fSTrond Myklebust /* set a readdirplus hint that we had a cache miss */ 12335ceb9d7fSTrond Myklebust nfs_force_use_readdirplus(dir); 12345ceb9d7fSTrond Myklebust ret = 1; 12355ceb9d7fSTrond Myklebust out: 12365ceb9d7fSTrond Myklebust nfs_free_fattr(fattr); 12375ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle); 12385ceb9d7fSTrond Myklebust nfs4_label_free(label); 12395ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 12405ceb9d7fSTrond Myklebust } 12415ceb9d7fSTrond Myklebust 12425ceb9d7fSTrond Myklebust /* 12435ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit, 12445ceb9d7fSTrond Myklebust * and we should check whether we can really trust that 12455ceb9d7fSTrond Myklebust * lookup. 12465ceb9d7fSTrond Myklebust * 12475ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume 12485ceb9d7fSTrond Myklebust * we have an inode! 12495ceb9d7fSTrond Myklebust * 12505ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the 12515ceb9d7fSTrond Myklebust * cached dentry and do a new lookup. 12525ceb9d7fSTrond Myklebust */ 12535ceb9d7fSTrond Myklebust static int 12545ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 12555ceb9d7fSTrond Myklebust unsigned int flags) 12565ceb9d7fSTrond Myklebust { 12575ceb9d7fSTrond Myklebust struct inode *inode; 12585ceb9d7fSTrond Myklebust int error; 12595ceb9d7fSTrond Myklebust 12605ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 12615ceb9d7fSTrond Myklebust inode = d_inode(dentry); 12625ceb9d7fSTrond Myklebust 12635ceb9d7fSTrond Myklebust if (!inode) 12645ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags); 12655ceb9d7fSTrond Myklebust 12665ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) { 12675ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 12685ceb9d7fSTrond Myklebust __func__, dentry); 12695ceb9d7fSTrond Myklebust goto out_bad; 12705ceb9d7fSTrond Myklebust } 12715ceb9d7fSTrond Myklebust 12725ceb9d7fSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 12735ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 12745ceb9d7fSTrond Myklebust 12755ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */ 12765ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 12775ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 12785ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags); 12795ceb9d7fSTrond Myklebust if (error) { 12805ceb9d7fSTrond Myklebust if (error == -ESTALE) 12815ceb9d7fSTrond Myklebust nfs_zap_caches(dir); 12825ceb9d7fSTrond Myklebust goto out_bad; 12835ceb9d7fSTrond Myklebust } 12845ceb9d7fSTrond Myklebust nfs_advise_use_readdirplus(dir); 12855ceb9d7fSTrond Myklebust goto out_valid; 12865ceb9d7fSTrond Myklebust } 12875ceb9d7fSTrond Myklebust 12885ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 12895ceb9d7fSTrond Myklebust return -ECHILD; 12905ceb9d7fSTrond Myklebust 12915ceb9d7fSTrond Myklebust if (NFS_STALE(inode)) 12925ceb9d7fSTrond Myklebust goto out_bad; 12935ceb9d7fSTrond Myklebust 12945ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 12955ceb9d7fSTrond Myklebust error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 12965ceb9d7fSTrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 12975ceb9d7fSTrond Myklebust return error; 12985ceb9d7fSTrond Myklebust out_valid: 12995ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 13005ceb9d7fSTrond Myklebust out_bad: 13015ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) 13025ceb9d7fSTrond Myklebust return -ECHILD; 13035ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 13045ceb9d7fSTrond Myklebust } 13055ceb9d7fSTrond Myklebust 13065ceb9d7fSTrond Myklebust static int 1307c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1308c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int)) 13095ceb9d7fSTrond Myklebust { 13105ceb9d7fSTrond Myklebust struct dentry *parent; 13115ceb9d7fSTrond Myklebust struct inode *dir; 13125ceb9d7fSTrond Myklebust int ret; 13135ceb9d7fSTrond Myklebust 13145ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) { 13155ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent); 13165ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent); 13175ceb9d7fSTrond Myklebust if (!dir) 13185ceb9d7fSTrond Myklebust return -ECHILD; 1319c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags); 13205ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent)) 13215ceb9d7fSTrond Myklebust return -ECHILD; 13225ceb9d7fSTrond Myklebust } else { 13235ceb9d7fSTrond Myklebust parent = dget_parent(dentry); 1324c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags); 13255ceb9d7fSTrond Myklebust dput(parent); 13265ceb9d7fSTrond Myklebust } 13275ceb9d7fSTrond Myklebust return ret; 13285ceb9d7fSTrond Myklebust } 13295ceb9d7fSTrond Myklebust 1330c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1331c7944ebbSTrond Myklebust { 1332c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1333c7944ebbSTrond Myklebust } 1334c7944ebbSTrond Myklebust 13351da177e4SLinus Torvalds /* 13362b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1337ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1338ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1339ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1340ecf3d1f1SJeff Layton * 1341ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1342ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1343ecf3d1f1SJeff Layton */ 1344ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1345ecf3d1f1SJeff Layton { 13462b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 13479cdd1d3fSTrond Myklebust int error = 0; 1348ecf3d1f1SJeff Layton 1349ecf3d1f1SJeff Layton /* 1350ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1351ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1352ecf3d1f1SJeff Layton * eventually need to do something more here. 1353ecf3d1f1SJeff Layton */ 1354ecf3d1f1SJeff Layton if (!inode) { 13556de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 13566de1472fSAl Viro __func__, dentry); 1357ecf3d1f1SJeff Layton return 1; 1358ecf3d1f1SJeff Layton } 1359ecf3d1f1SJeff Layton 1360ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 13616de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 13626de1472fSAl Viro __func__, dentry); 1363ecf3d1f1SJeff Layton return 0; 1364ecf3d1f1SJeff Layton } 1365ecf3d1f1SJeff Layton 1366b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags); 1367ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1368ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1369ecf3d1f1SJeff Layton return !error; 1370ecf3d1f1SJeff Layton } 1371ecf3d1f1SJeff Layton 1372ecf3d1f1SJeff Layton /* 13731da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 13741da177e4SLinus Torvalds */ 1375fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 13761da177e4SLinus Torvalds { 13776de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 13786de1472fSAl Viro dentry, dentry->d_flags); 13791da177e4SLinus Torvalds 138077f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 13812b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 138277f11192STrond Myklebust return 1; 138377f11192STrond Myklebust 13841da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 13851da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 13861da177e4SLinus Torvalds return 1; 13871da177e4SLinus Torvalds } 13881751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 13891da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 13901da177e4SLinus Torvalds * files will be cleaned up during umount */ 13911da177e4SLinus Torvalds return 1; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds return 0; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13971f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 13981b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 13991b83d707STrond Myklebust { 14001b83d707STrond Myklebust spin_lock(&inode->i_lock); 14011f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 140259a707b0STrond Myklebust if (inode->i_nlink > 0) 140359a707b0STrond Myklebust drop_nlink(inode); 140459a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 140516e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE 140616e14375STrond Myklebust | NFS_INO_INVALID_CTIME 140759a707b0STrond Myklebust | NFS_INO_INVALID_OTHER 140859a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 14091b83d707STrond Myklebust spin_unlock(&inode->i_lock); 14101b83d707STrond Myklebust } 14111b83d707STrond Myklebust 14121da177e4SLinus Torvalds /* 14131da177e4SLinus Torvalds * Called when the dentry loses inode. 14141da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 14151da177e4SLinus Torvalds */ 14161da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 14171da177e4SLinus Torvalds { 141883672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 141983672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 142083672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 142183672d39SNeil Brown 14221da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1423e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 14241f018458STrond Myklebust nfs_drop_nlink(inode); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds iput(inode); 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 1429b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1430b1942c5fSAl Viro { 1431b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1432b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1433b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1434b1942c5fSAl Viro WARN_ON(1); 1435b1942c5fSAl Viro else 1436b1942c5fSAl Viro kfree(dentry->d_fsdata); 1437b1942c5fSAl Viro } 1438b1942c5fSAl Viro } 1439b1942c5fSAl Viro 1440f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 14411da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1442ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 14431da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 14441da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 144536d43a43SDavid Howells .d_automount = nfs_d_automount, 1446b1942c5fSAl Viro .d_release = nfs_d_release, 14471da177e4SLinus Torvalds }; 1448ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 14491da177e4SLinus Torvalds 1450597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 14511da177e4SLinus Torvalds { 14521da177e4SLinus Torvalds struct dentry *res; 14531da177e4SLinus Torvalds struct inode *inode = NULL; 1454e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1455e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 14561775fd3eSDavid Quigley struct nfs4_label *label = NULL; 14571da177e4SLinus Torvalds int error; 14581da177e4SLinus Torvalds 14596de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 146091d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 14611da177e4SLinus Torvalds 1462130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1463130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG); 14641da177e4SLinus Torvalds 1465fd684071STrond Myklebust /* 1466fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1467fd684071STrond Myklebust * but don't hash the dentry. 1468fd684071STrond Myklebust */ 14699f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1470130f9ab7SAl Viro return NULL; 14711da177e4SLinus Torvalds 1472e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1473e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1474e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1475e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1476e1fb4d05STrond Myklebust goto out; 1477e1fb4d05STrond Myklebust 147814c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 147914c43f76SDavid Quigley if (IS_ERR(label)) 148014c43f76SDavid Quigley goto out; 148114c43f76SDavid Quigley 14826e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags); 14831775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 14841da177e4SLinus Torvalds if (error == -ENOENT) 14851da177e4SLinus Torvalds goto no_entry; 14861da177e4SLinus Torvalds if (error < 0) { 14871da177e4SLinus Torvalds res = ERR_PTR(error); 1488bf130914SAl Viro goto out_label; 14891da177e4SLinus Torvalds } 14901775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1491bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 149203f28e3aSTrond Myklebust if (IS_ERR(res)) 1493bf130914SAl Viro goto out_label; 149454ceac45SDavid Howells 149563519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */ 149663519fbcSTrond Myklebust nfs_force_use_readdirplus(dir); 1497d69ee9b8STrond Myklebust 14981da177e4SLinus Torvalds no_entry: 149941d28bcaSAl Viro res = d_splice_alias(inode, dentry); 15009eaef27bSTrond Myklebust if (res != NULL) { 15019eaef27bSTrond Myklebust if (IS_ERR(res)) 1502bf130914SAl Viro goto out_label; 15031da177e4SLinus Torvalds dentry = res; 15049eaef27bSTrond Myklebust } 15051da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1506bf130914SAl Viro out_label: 15076e0d0be7STrond Myklebust trace_nfs_lookup_exit(dir, dentry, flags, error); 150814c43f76SDavid Quigley nfs4_label_free(label); 15091da177e4SLinus Torvalds out: 1510e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1511e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 15121da177e4SLinus Torvalds return res; 15131da177e4SLinus Torvalds } 1514ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 15151da177e4SLinus Torvalds 151689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 15170b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 15181da177e4SLinus Torvalds 1519f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 15200ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 1521b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate, 15221da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 15231da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 152436d43a43SDavid Howells .d_automount = nfs_d_automount, 1525b1942c5fSAl Viro .d_release = nfs_d_release, 15261da177e4SLinus Torvalds }; 152789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 15281da177e4SLinus Torvalds 15298a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 15308a5e929dSAl Viro { 15318a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 15328a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 15338a5e929dSAl Viro res |= FMODE_READ; 15348a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 15358a5e929dSAl Viro res |= FMODE_WRITE; 15368a5e929dSAl Viro return res; 15378a5e929dSAl Viro } 15388a5e929dSAl Viro 1539532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1540cd9a1c0eSTrond Myklebust { 1541532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1542cd9a1c0eSTrond Myklebust } 1543cd9a1c0eSTrond Myklebust 1544cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1545cd9a1c0eSTrond Myklebust { 1546f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp); 1547cd9a1c0eSTrond Myklebust return 0; 1548cd9a1c0eSTrond Myklebust } 1549cd9a1c0eSTrond Myklebust 1550d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 15510dd2b474SMiklos Szeredi struct dentry *dentry, 1552b452a458SAl Viro struct file *file, unsigned open_flags) 1553cd9a1c0eSTrond Myklebust { 15540dd2b474SMiklos Szeredi int err; 15550dd2b474SMiklos Szeredi 1556be12af3eSAl Viro err = finish_open(file, dentry, do_open); 155730d90494SAl Viro if (err) 1558d9585277SAl Viro goto out; 1559eaa2b82cSNeilBrown if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 156030d90494SAl Viro nfs_file_set_open_context(file, ctx); 1561eaa2b82cSNeilBrown else 1562eaa2b82cSNeilBrown err = -ESTALE; 1563cd9a1c0eSTrond Myklebust out: 1564d9585277SAl Viro return err; 1565cd9a1c0eSTrond Myklebust } 1566cd9a1c0eSTrond Myklebust 156773a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 156830d90494SAl Viro struct file *file, unsigned open_flags, 156944907d79SAl Viro umode_t mode) 15701da177e4SLinus Torvalds { 1571c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1572cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 15730dd2b474SMiklos Szeredi struct dentry *res; 15740dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1575f46e0bd3STrond Myklebust struct inode *inode; 15761472b83eSTrond Myklebust unsigned int lookup_flags = 0; 1577c94c0953SAl Viro bool switched = false; 157873a09dd9SAl Viro int created = 0; 1579898f635cSTrond Myklebust int err; 15801da177e4SLinus Torvalds 15810dd2b474SMiklos Szeredi /* Expect a negative dentry */ 15822b0143b5SDavid Howells BUG_ON(d_inode(dentry)); 15830dd2b474SMiklos Szeredi 15841e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 15856de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 15861e7cb3dcSChuck Lever 15879597c13bSJeff Layton err = nfs_check_flags(open_flags); 15889597c13bSJeff Layton if (err) 15899597c13bSJeff Layton return err; 15909597c13bSJeff Layton 15910dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 15920dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 159300699ad8SAl Viro if (!d_in_lookup(dentry)) { 15940dd2b474SMiklos Szeredi /* 15950dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 15960dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 15970dd2b474SMiklos Szeredi * again 15980dd2b474SMiklos Szeredi */ 1599d9585277SAl Viro return -ENOENT; 16000dd2b474SMiklos Szeredi } 16011472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 16021da177e4SLinus Torvalds goto no_open; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 16050dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1606d9585277SAl Viro return -ENAMETOOLONG; 16071da177e4SLinus Torvalds 16080dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1609dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir); 1610dff25ddbSAndreas Gruenbacher 1611dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1612dff25ddbSAndreas Gruenbacher mode &= ~current_umask(); 1613dff25ddbSAndreas Gruenbacher 1614536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1615dff25ddbSAndreas Gruenbacher attr.ia_mode = mode; 16160dd2b474SMiklos Szeredi } 1617536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1618536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1619536e43d1STrond Myklebust attr.ia_size = 0; 1620cd9a1c0eSTrond Myklebust } 1621cd9a1c0eSTrond Myklebust 1622c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1623c94c0953SAl Viro d_drop(dentry); 1624c94c0953SAl Viro switched = true; 1625c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent, 1626c94c0953SAl Viro &dentry->d_name, &wq); 1627c94c0953SAl Viro if (IS_ERR(dentry)) 1628c94c0953SAl Viro return PTR_ERR(dentry); 1629c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry))) 1630c94c0953SAl Viro return finish_no_open(file, dentry); 1631c94c0953SAl Viro } 1632c94c0953SAl Viro 1633532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file); 16340dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 16350dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1636d9585277SAl Viro goto out; 16370dd2b474SMiklos Szeredi 16386e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags); 163973a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 164073a09dd9SAl Viro if (created) 164173a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 1642275bb307STrond Myklebust if (IS_ERR(inode)) { 16430dd2b474SMiklos Szeredi err = PTR_ERR(inode); 16446e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16452d9db750STrond Myklebust put_nfs_open_context(ctx); 1646d20cb71dSAl Viro d_drop(dentry); 16470dd2b474SMiklos Szeredi switch (err) { 16481da177e4SLinus Torvalds case -ENOENT: 1649774d9513SPeng Tao d_splice_alias(NULL, dentry); 1650809fd143STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16510dd2b474SMiklos Szeredi break; 16521788ea6eSJeff Layton case -EISDIR: 16536f926b5bSTrond Myklebust case -ENOTDIR: 16546f926b5bSTrond Myklebust goto no_open; 16551da177e4SLinus Torvalds case -ELOOP: 16560dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 16571da177e4SLinus Torvalds goto no_open; 16580dd2b474SMiklos Szeredi break; 16591da177e4SLinus Torvalds /* case -EINVAL: */ 16601da177e4SLinus Torvalds default: 16610dd2b474SMiklos Szeredi break; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds goto out; 16641da177e4SLinus Torvalds } 16650dd2b474SMiklos Szeredi 1666b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 16676e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 16682d9db750STrond Myklebust put_nfs_open_context(ctx); 1669d9585277SAl Viro out: 1670c94c0953SAl Viro if (unlikely(switched)) { 1671c94c0953SAl Viro d_lookup_done(dentry); 1672c94c0953SAl Viro dput(dentry); 1673c94c0953SAl Viro } 1674d9585277SAl Viro return err; 16750dd2b474SMiklos Szeredi 16761da177e4SLinus Torvalds no_open: 16771472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags); 1678c94c0953SAl Viro if (switched) { 1679c94c0953SAl Viro d_lookup_done(dentry); 1680c94c0953SAl Viro if (!res) 1681c94c0953SAl Viro res = dentry; 1682c94c0953SAl Viro else 1683c94c0953SAl Viro dput(dentry); 1684c94c0953SAl Viro } 16850dd2b474SMiklos Szeredi if (IS_ERR(res)) 1686c94c0953SAl Viro return PTR_ERR(res); 1687e45198a6SAl Viro return finish_no_open(file, res); 16881da177e4SLinus Torvalds } 168989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 16901da177e4SLinus Torvalds 1691c7944ebbSTrond Myklebust static int 1692c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1693c7944ebbSTrond Myklebust unsigned int flags) 16941da177e4SLinus Torvalds { 1695657e94b6SNick Piggin struct inode *inode; 16961da177e4SLinus Torvalds 1697fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1698c7944ebbSTrond Myklebust goto full_reval; 1699eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 1700c7944ebbSTrond Myklebust goto full_reval; 17012b484297STrond Myklebust 17022b0143b5SDavid Howells inode = d_inode(dentry); 17032b484297STrond Myklebust 17041da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 17051da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 17061da177e4SLinus Torvalds */ 1707c7944ebbSTrond Myklebust if (inode == NULL) 1708c7944ebbSTrond Myklebust goto full_reval; 170949317a7fSNeilBrown 1710c7944ebbSTrond Myklebust if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 1711c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1712216d5d06STrond Myklebust 17131da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 17141da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 1715c7944ebbSTrond Myklebust goto full_reval; 1716c7944ebbSTrond Myklebust 17171da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1718c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 1719c7944ebbSTrond Myklebust goto reval_dentry; 1720c7944ebbSTrond Myklebust 1721c7944ebbSTrond Myklebust /* Check if the directory changed */ 1722c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 1723c7944ebbSTrond Myklebust goto reval_dentry; 17241da177e4SLinus Torvalds 17250ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1726c7944ebbSTrond Myklebust return 1; 1727c7944ebbSTrond Myklebust reval_dentry: 1728c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU) 1729c7944ebbSTrond Myklebust return -ECHILD; 173042f72cf3Szhangliguang return nfs_lookup_revalidate_dentry(dir, dentry, inode); 17310ef97dcfSMiklos Szeredi 1732c7944ebbSTrond Myklebust full_reval: 1733c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags); 1734c7944ebbSTrond Myklebust } 1735535918f1STrond Myklebust 1736c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1737c7944ebbSTrond Myklebust { 1738c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, 1739c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate); 1740c0204fd2STrond Myklebust } 1741c0204fd2STrond Myklebust 17421da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds /* 17451da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 17461da177e4SLinus Torvalds */ 17471da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 17481775fd3eSDavid Quigley struct nfs_fattr *fattr, 17491775fd3eSDavid Quigley struct nfs4_label *label) 17501da177e4SLinus Torvalds { 1751fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 17522b0143b5SDavid Howells struct inode *dir = d_inode(parent); 17531da177e4SLinus Torvalds struct inode *inode; 1754b0c6108eSAl Viro struct dentry *d; 17551da177e4SLinus Torvalds int error = -EACCES; 17561da177e4SLinus Torvalds 1757fab728e1STrond Myklebust d_drop(dentry); 1758fab728e1STrond Myklebust 17591da177e4SLinus Torvalds /* We may have been initialized further down */ 17602b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1761fab728e1STrond Myklebust goto out; 17621da177e4SLinus Torvalds if (fhandle->size == 0) { 17631775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL); 17641da177e4SLinus Torvalds if (error) 1765fab728e1STrond Myklebust goto out_error; 17661da177e4SLinus Torvalds } 17675724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17681da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 17691da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 1770a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle, 1771a841b54dSTrond Myklebust fattr, NULL, NULL); 17721da177e4SLinus Torvalds if (error < 0) 1773fab728e1STrond Myklebust goto out_error; 17741da177e4SLinus Torvalds } 17751775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1776b0c6108eSAl Viro d = d_splice_alias(inode, dentry); 1777b0c6108eSAl Viro if (IS_ERR(d)) { 1778b0c6108eSAl Viro error = PTR_ERR(d); 1779fab728e1STrond Myklebust goto out_error; 1780b0c6108eSAl Viro } 1781b0c6108eSAl Viro dput(d); 1782fab728e1STrond Myklebust out: 1783fab728e1STrond Myklebust dput(parent); 17841da177e4SLinus Torvalds return 0; 1785fab728e1STrond Myklebust out_error: 1786fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1787fab728e1STrond Myklebust dput(parent); 1788fab728e1STrond Myklebust return error; 17891da177e4SLinus Torvalds } 1790ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds /* 17931da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17941da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17951da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17961da177e4SLinus Torvalds * reply path made it appear to have failed. 17971da177e4SLinus Torvalds */ 1798597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1799ebfc3b49SAl Viro umode_t mode, bool excl) 18001da177e4SLinus Torvalds { 18011da177e4SLinus Torvalds struct iattr attr; 1802ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 18031da177e4SLinus Torvalds int error; 18041da177e4SLinus Torvalds 18051e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 18066de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds attr.ia_mode = mode; 18091da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18101da177e4SLinus Torvalds 18118b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags); 18128867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 18138b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error); 18141da177e4SLinus Torvalds if (error != 0) 18151da177e4SLinus Torvalds goto out_err; 18161da177e4SLinus Torvalds return 0; 18171da177e4SLinus Torvalds out_err: 18181da177e4SLinus Torvalds d_drop(dentry); 18191da177e4SLinus Torvalds return error; 18201da177e4SLinus Torvalds } 1821ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* 18241da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18251da177e4SLinus Torvalds */ 1826597d9289SBryan Schumaker int 18271a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 18281da177e4SLinus Torvalds { 18291da177e4SLinus Torvalds struct iattr attr; 18301da177e4SLinus Torvalds int status; 18311da177e4SLinus Torvalds 18321e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 18336de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds attr.ia_mode = mode; 18361da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18371da177e4SLinus Torvalds 18381ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry); 18391da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 18401ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status); 18411da177e4SLinus Torvalds if (status != 0) 18421da177e4SLinus Torvalds goto out_err; 18431da177e4SLinus Torvalds return 0; 18441da177e4SLinus Torvalds out_err: 18451da177e4SLinus Torvalds d_drop(dentry); 18461da177e4SLinus Torvalds return status; 18471da177e4SLinus Torvalds } 1848ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /* 18511da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 18521da177e4SLinus Torvalds */ 1853597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 18541da177e4SLinus Torvalds { 18551da177e4SLinus Torvalds struct iattr attr; 18561da177e4SLinus Torvalds int error; 18571da177e4SLinus Torvalds 18581e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 18596de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 18621da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 18631da177e4SLinus Torvalds 18641ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry); 18651da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 18661ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error); 18671da177e4SLinus Torvalds if (error != 0) 18681da177e4SLinus Torvalds goto out_err; 18691da177e4SLinus Torvalds return 0; 18701da177e4SLinus Torvalds out_err: 18711da177e4SLinus Torvalds d_drop(dentry); 18721da177e4SLinus Torvalds return error; 18731da177e4SLinus Torvalds } 1874ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 18751da177e4SLinus Torvalds 1876d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1877d45b9d8bSTrond Myklebust { 1878dc3f4198SAl Viro if (simple_positive(dentry)) 1879d45b9d8bSTrond Myklebust d_delete(dentry); 1880d45b9d8bSTrond Myklebust } 1881d45b9d8bSTrond Myklebust 1882597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 18831da177e4SLinus Torvalds { 18841da177e4SLinus Torvalds int error; 18851da177e4SLinus Torvalds 18861e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 18876de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry); 18881da177e4SLinus Torvalds 18891ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry); 18902b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1891884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 18921da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 18931da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 1894ba6c0592STrond Myklebust switch (error) { 1895ba6c0592STrond Myklebust case 0: 18962b0143b5SDavid Howells clear_nlink(d_inode(dentry)); 1897ba6c0592STrond Myklebust break; 1898ba6c0592STrond Myklebust case -ENOENT: 1899d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 1900ba6c0592STrond Myklebust } 1901884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 1902ba6c0592STrond Myklebust } else 1903ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 19041ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error); 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds return error; 19071da177e4SLinus Torvalds } 1908ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /* 19111da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 19121da177e4SLinus Torvalds * and after checking that the file has only one user. 19131da177e4SLinus Torvalds * 19141da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 19151da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 19161da177e4SLinus Torvalds */ 19171da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 19181da177e4SLinus Torvalds { 19192b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 19202b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 19211da177e4SLinus Torvalds int error = -EBUSY; 19221da177e4SLinus Torvalds 19236de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 19261da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 19271da177e4SLinus Torvalds error = 0; 19281da177e4SLinus Torvalds goto out; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 19311ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry); 19321da177e4SLinus Torvalds if (inode != NULL) { 1933912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 19341da177e4SLinus Torvalds if (error == 0) 19351b83d707STrond Myklebust nfs_drop_nlink(inode); 19361da177e4SLinus Torvalds } else 1937912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry); 1938d45b9d8bSTrond Myklebust if (error == -ENOENT) 1939d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 19401ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error); 19411da177e4SLinus Torvalds out: 19421da177e4SLinus Torvalds return error; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 19461da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 19471da177e4SLinus Torvalds * 19481da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 19491da177e4SLinus Torvalds */ 1950597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 19511da177e4SLinus Torvalds { 19521da177e4SLinus Torvalds int error; 19531da177e4SLinus Torvalds int need_rehash = 0; 19541da177e4SLinus Torvalds 19551e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 19566de1472fSAl Viro dir->i_ino, dentry); 19571da177e4SLinus Torvalds 19581ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry); 19591da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 196084d08fa8SAl Viro if (d_count(dentry) > 1) { 19611da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1962ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 19632b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0); 19641da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 19651ca42382STrond Myklebust goto out; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 19681da177e4SLinus Torvalds __d_drop(dentry); 19691da177e4SLinus Torvalds need_rehash = 1; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 19721da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1973d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 19741da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 19751da177e4SLinus Torvalds } else if (need_rehash) 19761da177e4SLinus Torvalds d_rehash(dentry); 19771ca42382STrond Myklebust out: 19781ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error); 19791da177e4SLinus Torvalds return error; 19801da177e4SLinus Torvalds } 1981ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 19821da177e4SLinus Torvalds 1983873101b3SChuck Lever /* 1984873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1985873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1986873101b3SChuck Lever * using prepare_write/commit_write. 1987873101b3SChuck Lever * 1988873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1989873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1990873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1991873101b3SChuck Lever * symlink request has completed on the server. 1992873101b3SChuck Lever * 1993873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1994873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1995873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1996873101b3SChuck Lever * and move the raw page into its mapping. 1997873101b3SChuck Lever */ 1998597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 19991da177e4SLinus Torvalds { 2000873101b3SChuck Lever struct page *page; 2001873101b3SChuck Lever char *kaddr; 20021da177e4SLinus Torvalds struct iattr attr; 2003873101b3SChuck Lever unsigned int pathlen = strlen(symname); 20041da177e4SLinus Torvalds int error; 20051da177e4SLinus Torvalds 20061e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 20076de1472fSAl Viro dir->i_ino, dentry, symname); 20081da177e4SLinus Torvalds 2009873101b3SChuck Lever if (pathlen > PAGE_SIZE) 2010873101b3SChuck Lever return -ENAMETOOLONG; 20111da177e4SLinus Torvalds 2012873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 2013873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 20141da177e4SLinus Torvalds 2015e8ecde25SAl Viro page = alloc_page(GFP_USER); 201676566991STrond Myklebust if (!page) 2017873101b3SChuck Lever return -ENOMEM; 2018873101b3SChuck Lever 2019e8ecde25SAl Viro kaddr = page_address(page); 2020873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 2021873101b3SChuck Lever if (pathlen < PAGE_SIZE) 2022873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2023873101b3SChuck Lever 20241ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry); 202594a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 20261ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error); 2027873101b3SChuck Lever if (error != 0) { 20281e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2029873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 20306de1472fSAl Viro dentry, symname, error); 20311da177e4SLinus Torvalds d_drop(dentry); 2032873101b3SChuck Lever __free_page(page); 20331da177e4SLinus Torvalds return error; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 2036873101b3SChuck Lever /* 2037873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 2038873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 2039873101b3SChuck Lever */ 20402b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2041873101b3SChuck Lever GFP_KERNEL)) { 2042873101b3SChuck Lever SetPageUptodate(page); 2043873101b3SChuck Lever unlock_page(page); 2044a0b54addSRafael Aquini /* 2045a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount. 2046a0b54addSRafael Aquini * Drop it here to avoid leaking this page later. 2047a0b54addSRafael Aquini */ 204809cbfeafSKirill A. Shutemov put_page(page); 2049873101b3SChuck Lever } else 2050873101b3SChuck Lever __free_page(page); 2051873101b3SChuck Lever 2052873101b3SChuck Lever return 0; 2053873101b3SChuck Lever } 2054ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 2055873101b3SChuck Lever 2056597d9289SBryan Schumaker int 20571da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20581da177e4SLinus Torvalds { 20592b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 20601da177e4SLinus Torvalds int error; 20611da177e4SLinus Torvalds 20626de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 20636de1472fSAl Viro old_dentry, dentry); 20641da177e4SLinus Torvalds 20651fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry); 20669697d234STrond Myklebust d_drop(dentry); 20671da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2068cf809556STrond Myklebust if (error == 0) { 20697de9c6eeSAl Viro ihold(inode); 20709697d234STrond Myklebust d_add(dentry, inode); 2071cf809556STrond Myklebust } 20721fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error); 20731da177e4SLinus Torvalds return error; 20741da177e4SLinus Torvalds } 2075ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds /* 20781da177e4SLinus Torvalds * RENAME 20791da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 20801da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 20811da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 20821da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 20831da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 20841da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 20851da177e4SLinus Torvalds * 20861da177e4SLinus Torvalds * FIXED. 20871da177e4SLinus Torvalds * 20881da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 20891da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 20901da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 20911da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 20921da177e4SLinus Torvalds * using the inode layer 20931da177e4SLinus Torvalds * 20941da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 20951da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 20961da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 20971da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 20981da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 20991da177e4SLinus Torvalds * the rename. 21001da177e4SLinus Torvalds */ 2101597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 21021cd66c93SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 21031cd66c93SMiklos Szeredi unsigned int flags) 21041da177e4SLinus Torvalds { 21052b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry); 21062b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry); 2107d9f29500SBenjamin Coddington struct dentry *dentry = NULL, *rehash = NULL; 210880a491fdSJeff Layton struct rpc_task *task; 21091da177e4SLinus Torvalds int error = -EBUSY; 21101da177e4SLinus Torvalds 21111cd66c93SMiklos Szeredi if (flags) 21121cd66c93SMiklos Szeredi return -EINVAL; 21131cd66c93SMiklos Szeredi 21146de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 21156de1472fSAl Viro old_dentry, new_dentry, 211684d08fa8SAl Viro d_count(new_dentry)); 21171da177e4SLinus Torvalds 211870ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 21191da177e4SLinus Torvalds /* 212028f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 212128f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 212228f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 212328f79a1aSMiklos Szeredi * the new target. 21241da177e4SLinus Torvalds */ 212527226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 212627226104SMiklos Szeredi /* 212727226104SMiklos Szeredi * To prevent any new references to the target during the 212827226104SMiklos Szeredi * rename, we unhash the dentry in advance. 212927226104SMiklos Szeredi */ 2130d9f29500SBenjamin Coddington if (!d_unhashed(new_dentry)) { 213127226104SMiklos Szeredi d_drop(new_dentry); 2132d9f29500SBenjamin Coddington rehash = new_dentry; 2133d9f29500SBenjamin Coddington } 213427226104SMiklos Szeredi 213584d08fa8SAl Viro if (d_count(new_dentry) > 2) { 21361da177e4SLinus Torvalds int err; 213727226104SMiklos Szeredi 21381da177e4SLinus Torvalds /* copy the target dentry's name */ 21391da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 21401da177e4SLinus Torvalds &new_dentry->d_name); 21411da177e4SLinus Torvalds if (!dentry) 21421da177e4SLinus Torvalds goto out; 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds /* silly-rename the existing target ... */ 21451da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 214624e93025SMiklos Szeredi if (err) 21471da177e4SLinus Torvalds goto out; 214824e93025SMiklos Szeredi 214924e93025SMiklos Szeredi new_dentry = dentry; 2150d9f29500SBenjamin Coddington rehash = NULL; 215124e93025SMiklos Szeredi new_inode = NULL; 2152b1e4adf4STrond Myklebust } 215327226104SMiklos Szeredi } 21541da177e4SLinus Torvalds 2155d9f29500SBenjamin Coddington task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 215680a491fdSJeff Layton if (IS_ERR(task)) { 215780a491fdSJeff Layton error = PTR_ERR(task); 215880a491fdSJeff Layton goto out; 215980a491fdSJeff Layton } 216080a491fdSJeff Layton 216180a491fdSJeff Layton error = rpc_wait_for_completion_task(task); 2162818a8dbeSBenjamin Coddington if (error != 0) { 2163818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2164818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2165818a8dbeSBenjamin Coddington smp_wmb(); 2166818a8dbeSBenjamin Coddington } else 216780a491fdSJeff Layton error = task->tk_status; 216880a491fdSJeff Layton rpc_put_task(task); 216959a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */ 217059a707b0STrond Myklebust if (error == 0) { 217159a707b0STrond Myklebust spin_lock(&old_inode->i_lock); 217259a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 217359a707b0STrond Myklebust NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE 217459a707b0STrond Myklebust | NFS_INO_INVALID_CTIME 217559a707b0STrond Myklebust | NFS_INO_REVAL_FORCED; 217659a707b0STrond Myklebust spin_unlock(&old_inode->i_lock); 217759a707b0STrond Myklebust } 21781da177e4SLinus Torvalds out: 2179d9f29500SBenjamin Coddington if (rehash) 2180d9f29500SBenjamin Coddington d_rehash(rehash); 218170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry, 218270ded201STrond Myklebust new_dir, new_dentry, error); 2183d9f29500SBenjamin Coddington if (!error) { 2184d9f29500SBenjamin Coddington if (new_inode != NULL) 2185d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode); 2186d9f29500SBenjamin Coddington /* 2187d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion 2188d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If 2189d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler 2190d9f29500SBenjamin Coddington * should mark the directories for revalidation. 2191d9f29500SBenjamin Coddington */ 2192d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry); 2193d803224cSTrond Myklebust nfs_set_verifier(old_dentry, 2194d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir)); 2195d9f29500SBenjamin Coddington } else if (error == -ENOENT) 2196d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry); 2197d9f29500SBenjamin Coddington 21981da177e4SLinus Torvalds /* new dentry created? */ 21991da177e4SLinus Torvalds if (dentry) 22001da177e4SLinus Torvalds dput(dentry); 22011da177e4SLinus Torvalds return error; 22021da177e4SLinus Torvalds } 2203ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 22041da177e4SLinus Torvalds 2205cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2206cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2207cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2208cfcea3e8STrond Myklebust 22093a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX; 22103a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644); 22113a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 22123a505845STrond Myklebust 22131c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 22141c3c07e9STrond Myklebust { 2215b68572e0SNeilBrown put_cred(entry->cred); 2216f682a398SNeilBrown kfree_rcu(entry, rcu_head); 22174e857c58SPeter Zijlstra smp_mb__before_atomic(); 2218cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 22194e857c58SPeter Zijlstra smp_mb__after_atomic(); 22201c3c07e9STrond Myklebust } 22211c3c07e9STrond Myklebust 22221a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 22231a81bb8aSTrond Myklebust { 22241a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 22251a81bb8aSTrond Myklebust 22261a81bb8aSTrond Myklebust while (!list_empty(head)) { 22271a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 22281a81bb8aSTrond Myklebust list_del(&cache->lru); 22291a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 22301a81bb8aSTrond Myklebust } 22311a81bb8aSTrond Myklebust } 22321a81bb8aSTrond Myklebust 22333a505845STrond Myklebust static unsigned long 22343a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan) 2235979df72eSTrond Myklebust { 2236979df72eSTrond Myklebust LIST_HEAD(head); 2237aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2238979df72eSTrond Myklebust struct nfs_access_entry *cache; 22391ab6c499SDave Chinner long freed = 0; 2240979df72eSTrond Myklebust 2241a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2242aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2243979df72eSTrond Myklebust struct inode *inode; 2244979df72eSTrond Myklebust 2245979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2246979df72eSTrond Myklebust break; 22479c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2248979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2249979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2250979df72eSTrond Myklebust goto remove_lru_entry; 2251979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2252979df72eSTrond Myklebust struct nfs_access_entry, lru); 2253979df72eSTrond Myklebust list_move(&cache->lru, &head); 2254979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 22551ab6c499SDave Chinner freed++; 2256979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2257979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2258979df72eSTrond Myklebust &nfs_access_lru_list); 2259979df72eSTrond Myklebust else { 2260979df72eSTrond Myklebust remove_lru_entry: 2261979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 22624e857c58SPeter Zijlstra smp_mb__before_atomic(); 2263979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 22644e857c58SPeter Zijlstra smp_mb__after_atomic(); 2265979df72eSTrond Myklebust } 226659844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2267979df72eSTrond Myklebust } 2268979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 22691a81bb8aSTrond Myklebust nfs_access_free_list(&head); 22701ab6c499SDave Chinner return freed; 22711ab6c499SDave Chinner } 22721ab6c499SDave Chinner 22731ab6c499SDave Chinner unsigned long 22743a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 22753a505845STrond Myklebust { 22763a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan; 22773a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask; 22783a505845STrond Myklebust 22793a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 22803a505845STrond Myklebust return SHRINK_STOP; 22813a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan); 22823a505845STrond Myklebust } 22833a505845STrond Myklebust 22843a505845STrond Myklebust 22853a505845STrond Myklebust unsigned long 22861ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 22871ab6c499SDave Chinner { 228855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2289979df72eSTrond Myklebust } 2290979df72eSTrond Myklebust 22913a505845STrond Myklebust static void 22923a505845STrond Myklebust nfs_access_cache_enforce_limit(void) 22933a505845STrond Myklebust { 22943a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries); 22953a505845STrond Myklebust unsigned long diff; 22963a505845STrond Myklebust unsigned int nr_to_scan; 22973a505845STrond Myklebust 22983a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 22993a505845STrond Myklebust return; 23003a505845STrond Myklebust nr_to_scan = 100; 23013a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize; 23023a505845STrond Myklebust if (diff < nr_to_scan) 23033a505845STrond Myklebust nr_to_scan = diff; 23043a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan); 23053a505845STrond Myklebust } 23063a505845STrond Myklebust 23071a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 23081c3c07e9STrond Myklebust { 23091c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 23101a81bb8aSTrond Myklebust struct rb_node *n; 23111c3c07e9STrond Myklebust struct nfs_access_entry *entry; 23121c3c07e9STrond Myklebust 23131c3c07e9STrond Myklebust /* Unhook entries from the cache */ 23141c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 23151c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 23161c3c07e9STrond Myklebust rb_erase(n, root_node); 23171a81bb8aSTrond Myklebust list_move(&entry->lru, head); 23181c3c07e9STrond Myklebust } 23191c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 23201c3c07e9STrond Myklebust } 23211c3c07e9STrond Myklebust 23221c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 23231c3c07e9STrond Myklebust { 23241a81bb8aSTrond Myklebust LIST_HEAD(head); 23251a81bb8aSTrond Myklebust 23261a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 23271a81bb8aSTrond Myklebust return; 2328cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2329cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 23301a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2331cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2332cfcea3e8STrond Myklebust 23331c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 23341a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 23351a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23361a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 23371a81bb8aSTrond Myklebust nfs_access_free_list(&head); 23381c3c07e9STrond Myklebust } 23391c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 23401c3c07e9STrond Myklebust 2341b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 23421c3c07e9STrond Myklebust { 23431c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 23441c3c07e9STrond Myklebust 23451c3c07e9STrond Myklebust while (n != NULL) { 2346b68572e0SNeilBrown struct nfs_access_entry *entry = 2347b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node); 2348b68572e0SNeilBrown int cmp = cred_fscmp(cred, entry->cred); 23491c3c07e9STrond Myklebust 2350b68572e0SNeilBrown if (cmp < 0) 23511c3c07e9STrond Myklebust n = n->rb_left; 2352b68572e0SNeilBrown else if (cmp > 0) 23531c3c07e9STrond Myklebust n = n->rb_right; 23541c3c07e9STrond Myklebust else 23551c3c07e9STrond Myklebust return entry; 23561c3c07e9STrond Myklebust } 23571c3c07e9STrond Myklebust return NULL; 23581c3c07e9STrond Myklebust } 23591c3c07e9STrond Myklebust 2360b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 23611da177e4SLinus Torvalds { 236255296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 23631c3c07e9STrond Myklebust struct nfs_access_entry *cache; 236457b69181STrond Myklebust bool retry = true; 236557b69181STrond Myklebust int err; 23661da177e4SLinus Torvalds 23671c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 236857b69181STrond Myklebust for(;;) { 23691c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 23701c3c07e9STrond Myklebust goto out_zap; 23711c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 237257b69181STrond Myklebust err = -ENOENT; 23731c3c07e9STrond Myklebust if (cache == NULL) 23741c3c07e9STrond Myklebust goto out; 237557b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */ 237621c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 237757b69181STrond Myklebust break; 237857b69181STrond Myklebust err = -ECHILD; 237957b69181STrond Myklebust if (!may_block) 238057b69181STrond Myklebust goto out; 238157b69181STrond Myklebust if (!retry) 238257b69181STrond Myklebust goto out_zap; 238357b69181STrond Myklebust spin_unlock(&inode->i_lock); 238457b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 238557b69181STrond Myklebust if (err) 238657b69181STrond Myklebust return err; 238757b69181STrond Myklebust spin_lock(&inode->i_lock); 238857b69181STrond Myklebust retry = false; 238957b69181STrond Myklebust } 23901c3c07e9STrond Myklebust res->cred = cache->cred; 23911c3c07e9STrond Myklebust res->mask = cache->mask; 2392cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 23931c3c07e9STrond Myklebust err = 0; 23941c3c07e9STrond Myklebust out: 23951c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 23961c3c07e9STrond Myklebust return err; 23971c3c07e9STrond Myklebust out_zap: 23981a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 23991a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 24001c3c07e9STrond Myklebust return -ENOENT; 24011c3c07e9STrond Myklebust } 24021c3c07e9STrond Myklebust 2403b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2404f682a398SNeilBrown { 2405f682a398SNeilBrown /* Only check the most recently returned cache entry, 2406f682a398SNeilBrown * but do it without locking. 2407f682a398SNeilBrown */ 2408f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode); 2409f682a398SNeilBrown struct nfs_access_entry *cache; 2410f682a398SNeilBrown int err = -ECHILD; 2411f682a398SNeilBrown struct list_head *lh; 2412f682a398SNeilBrown 2413f682a398SNeilBrown rcu_read_lock(); 2414f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2415f682a398SNeilBrown goto out; 2416f682a398SNeilBrown lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); 2417f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru); 2418f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru || 2419f682a398SNeilBrown cred != cache->cred) 2420f682a398SNeilBrown cache = NULL; 2421f682a398SNeilBrown if (cache == NULL) 2422f682a398SNeilBrown goto out; 242321c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2424f682a398SNeilBrown goto out; 2425f682a398SNeilBrown res->cred = cache->cred; 2426f682a398SNeilBrown res->mask = cache->mask; 242721c3ba7eSTrond Myklebust err = 0; 2428f682a398SNeilBrown out: 2429f682a398SNeilBrown rcu_read_unlock(); 2430f682a398SNeilBrown return err; 2431f682a398SNeilBrown } 2432f682a398SNeilBrown 24331c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 24341c3c07e9STrond Myklebust { 2435cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2436cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 24371c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 24381c3c07e9STrond Myklebust struct rb_node *parent = NULL; 24391c3c07e9STrond Myklebust struct nfs_access_entry *entry; 2440b68572e0SNeilBrown int cmp; 24411c3c07e9STrond Myklebust 24421c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 24431c3c07e9STrond Myklebust while (*p != NULL) { 24441c3c07e9STrond Myklebust parent = *p; 24451c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2446b68572e0SNeilBrown cmp = cred_fscmp(set->cred, entry->cred); 24471c3c07e9STrond Myklebust 2448b68572e0SNeilBrown if (cmp < 0) 24491c3c07e9STrond Myklebust p = &parent->rb_left; 2450b68572e0SNeilBrown else if (cmp > 0) 24511c3c07e9STrond Myklebust p = &parent->rb_right; 24521c3c07e9STrond Myklebust else 24531c3c07e9STrond Myklebust goto found; 24541c3c07e9STrond Myklebust } 24551c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 24561c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2457cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 24581c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24591c3c07e9STrond Myklebust return; 24601c3c07e9STrond Myklebust found: 24611c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2462cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2463cfcea3e8STrond Myklebust list_del(&entry->lru); 24641c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 24651c3c07e9STrond Myklebust nfs_access_free_entry(entry); 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 24686168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 24691da177e4SLinus Torvalds { 24701c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 24711c3c07e9STrond Myklebust if (cache == NULL) 24721c3c07e9STrond Myklebust return; 24731c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 2474b68572e0SNeilBrown cache->cred = get_cred(set->cred); 24751da177e4SLinus Torvalds cache->mask = set->mask; 24761c3c07e9STrond Myklebust 2477f682a398SNeilBrown /* The above field assignments must be visible 2478f682a398SNeilBrown * before this item appears on the lru. We cannot easily 2479f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier. 2480f682a398SNeilBrown */ 2481f682a398SNeilBrown smp_wmb(); 24821c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2483cfcea3e8STrond Myklebust 2484cfcea3e8STrond Myklebust /* Update accounting */ 24854e857c58SPeter Zijlstra smp_mb__before_atomic(); 2486cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 24874e857c58SPeter Zijlstra smp_mb__after_atomic(); 2488cfcea3e8STrond Myklebust 2489cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 24901a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2491cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 24921a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 24931a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 24941a81bb8aSTrond Myklebust &nfs_access_lru_list); 2495cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2496cfcea3e8STrond Myklebust } 24973a505845STrond Myklebust nfs_access_cache_enforce_limit(); 24981da177e4SLinus Torvalds } 24996168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 25006168f62cSWeston Andros Adamson 25013c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ) 25023c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 25033c181827SAnna Schumaker NFS_ACCESS_EXTEND | \ 25043c181827SAnna Schumaker NFS_ACCESS_DELETE) 25053c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 25063c181827SAnna Schumaker NFS_ACCESS_EXTEND) 2507ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 25083c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 25093c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 251015d4b73aSTrond Myklebust static int 2511ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode) 251215d4b73aSTrond Myklebust { 251315d4b73aSTrond Myklebust int mask = 0; 251415d4b73aSTrond Myklebust 251515d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ) 251615d4b73aSTrond Myklebust mask |= MAY_READ; 2517ecbb903cSTrond Myklebust if (S_ISDIR(umode)) { 2518ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 251915d4b73aSTrond Myklebust mask |= MAY_WRITE; 2520ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 252115d4b73aSTrond Myklebust mask |= MAY_EXEC; 2522ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) { 2523ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2524ecbb903cSTrond Myklebust mask |= MAY_WRITE; 2525ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 252615d4b73aSTrond Myklebust mask |= MAY_EXEC; 2527ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE) 2528ecbb903cSTrond Myklebust mask |= MAY_WRITE; 252915d4b73aSTrond Myklebust return mask; 253015d4b73aSTrond Myklebust } 253115d4b73aSTrond Myklebust 25326168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 25336168f62cSWeston Andros Adamson { 2534bd8b2441STrond Myklebust entry->mask = access_result; 25356168f62cSWeston Andros Adamson } 25366168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 25371da177e4SLinus Torvalds 2538b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 25391da177e4SLinus Torvalds { 25401da177e4SLinus Torvalds struct nfs_access_entry cache; 254157b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2542bd8b2441STrond Myklebust int cache_mask; 25431da177e4SLinus Torvalds int status; 25441da177e4SLinus Torvalds 2545f4ce1299STrond Myklebust trace_nfs_access_enter(inode); 2546f4ce1299STrond Myklebust 2547f682a398SNeilBrown status = nfs_access_get_cached_rcu(inode, cred, &cache); 2548f682a398SNeilBrown if (status != 0) 254957b69181STrond Myklebust status = nfs_access_get_cached(inode, cred, &cache, may_block); 25501da177e4SLinus Torvalds if (status == 0) 2551f4ce1299STrond Myklebust goto out_cached; 25521da177e4SLinus Torvalds 2553f3324a2aSNeilBrown status = -ECHILD; 255457b69181STrond Myklebust if (!may_block) 2555f3324a2aSNeilBrown goto out; 2556f3324a2aSNeilBrown 25571750d929SAnna Schumaker /* 25581750d929SAnna Schumaker * Determine which access bits we want to ask for... 25591750d929SAnna Schumaker */ 25601750d929SAnna Schumaker cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 25611750d929SAnna Schumaker if (S_ISDIR(inode->i_mode)) 25621750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 25631750d929SAnna Schumaker else 25641750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE; 25651da177e4SLinus Torvalds cache.cred = cred; 25661da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2567a71ee337SSuresh Jayaraman if (status != 0) { 2568a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2569a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2570a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2571a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2572a71ee337SSuresh Jayaraman } 2573f4ce1299STrond Myklebust goto out; 2574a71ee337SSuresh Jayaraman } 25751da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 2576f4ce1299STrond Myklebust out_cached: 2577ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2578bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2579f4ce1299STrond Myklebust status = -EACCES; 25801da177e4SLinus Torvalds out: 2581f4ce1299STrond Myklebust trace_nfs_access_exit(inode, status); 2582f4ce1299STrond Myklebust return status; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 2585af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2586af22f94aSTrond Myklebust { 2587af22f94aSTrond Myklebust int mask = 0; 2588af22f94aSTrond Myklebust 2589f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2590f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2591f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2592f8d9a897SWeston Andros Adamson } else { 25938a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2594af22f94aSTrond Myklebust mask |= MAY_READ; 25958a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2596af22f94aSTrond Myklebust mask |= MAY_WRITE; 2597f8d9a897SWeston Andros Adamson } 2598f8d9a897SWeston Andros Adamson 2599af22f94aSTrond Myklebust return mask; 2600af22f94aSTrond Myklebust } 2601af22f94aSTrond Myklebust 2602b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2603af22f94aSTrond Myklebust { 2604af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2605af22f94aSTrond Myklebust } 260689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2607af22f94aSTrond Myklebust 26085c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask) 26095c5fc09aSTrond Myklebust { 26105c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 261121c3ba7eSTrond Myklebust int ret = 0; 26125c5fc09aSTrond Myklebust 26133825827eSTrond Myklebust if (S_ISDIR(inode->i_mode)) 26143825827eSTrond Myklebust return 0; 2615cf834027STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { 26165c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK) 261721c3ba7eSTrond Myklebust return -ECHILD; 261821c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode); 261921c3ba7eSTrond Myklebust } 26205c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode)) 26215c5fc09aSTrond Myklebust ret = -EACCES; 26225c5fc09aSTrond Myklebust return ret; 26235c5fc09aSTrond Myklebust } 26245c5fc09aSTrond Myklebust 262510556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 26261da177e4SLinus Torvalds { 2627b68572e0SNeilBrown const struct cred *cred = current_cred(); 26281da177e4SLinus Torvalds int res = 0; 26291da177e4SLinus Torvalds 263091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 263191d5b470SChuck Lever 2632e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 26331da177e4SLinus Torvalds goto out; 26341da177e4SLinus Torvalds /* Is this sys_access() ? */ 26359cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 26361da177e4SLinus Torvalds goto force_lookup; 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 26391da177e4SLinus Torvalds case S_IFLNK: 26401da177e4SLinus Torvalds goto out; 26411da177e4SLinus Torvalds case S_IFREG: 2642762674f8STrond Myklebust if ((mask & MAY_OPEN) && 2643762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 2644762674f8STrond Myklebust return 0; 26451da177e4SLinus Torvalds break; 26461da177e4SLinus Torvalds case S_IFDIR: 26471da177e4SLinus Torvalds /* 26481da177e4SLinus Torvalds * Optimize away all write operations, since the server 26491da177e4SLinus Torvalds * will check permissions when we perform the op. 26501da177e4SLinus Torvalds */ 26511da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 26521da177e4SLinus Torvalds goto out; 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds force_lookup: 26561da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 26571da177e4SLinus Torvalds goto out_notsup; 26581da177e4SLinus Torvalds 2659f3324a2aSNeilBrown /* Always try fast lookups first */ 2660f3324a2aSNeilBrown rcu_read_lock(); 2661f3324a2aSNeilBrown res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK); 2662f3324a2aSNeilBrown rcu_read_unlock(); 2663f3324a2aSNeilBrown if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) { 2664f3324a2aSNeilBrown /* Fast lookup failed, try the slow way */ 26651da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 2666f3324a2aSNeilBrown } 26671da177e4SLinus Torvalds out: 26685c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC)) 26695c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask); 2670f696a365SMiklos Szeredi 26711e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 26721e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 26731da177e4SLinus Torvalds return res; 26741da177e4SLinus Torvalds out_notsup: 2675d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK) 2676d51ac1a8SNeilBrown return -ECHILD; 2677d51ac1a8SNeilBrown 26781da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 26791da177e4SLinus Torvalds if (res == 0) 26802830ba7fSAl Viro res = generic_permission(inode, mask); 26811e7cb3dcSChuck Lever goto out; 26821da177e4SLinus Torvalds } 2683ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds /* 26861da177e4SLinus Torvalds * Local variables: 26871da177e4SLinus Torvalds * version-control: t 26881da177e4SLinus Torvalds * kept-new-versions: 5 26891da177e4SLinus Torvalds * End: 26901da177e4SLinus Torvalds */ 2691