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 461da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 49480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 5023db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *); 5102c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 52f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 5311de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 541da177e4SLinus Torvalds 554b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 56f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 571da177e4SLinus Torvalds .read = generic_read_dir, 5823db8620SAl Viro .iterate = nfs_readdir, 591da177e4SLinus Torvalds .open = nfs_opendir, 60480c2006SBryan Schumaker .release = nfs_closedir, 611da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 621da177e4SLinus Torvalds }; 631da177e4SLinus Torvalds 6411de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 6511de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 66d1bacf9eSBryan Schumaker }; 67d1bacf9eSBryan Schumaker 680c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred) 69480c2006SBryan Schumaker { 70480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 71480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 72480c2006SBryan Schumaker if (ctx != NULL) { 738ef2ce3eSBryan Schumaker ctx->duped = 0; 740c030806STrond Myklebust ctx->attr_gencount = NFS_I(dir)->attr_gencount; 75480c2006SBryan Schumaker ctx->dir_cookie = 0; 768ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 77480c2006SBryan Schumaker ctx->cred = get_rpccred(cred); 78480c2006SBryan Schumaker return ctx; 79480c2006SBryan Schumaker } 800c030806STrond Myklebust return ERR_PTR(-ENOMEM); 810c030806STrond Myklebust } 82480c2006SBryan Schumaker 83480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx) 84480c2006SBryan Schumaker { 85480c2006SBryan Schumaker put_rpccred(ctx->cred); 86480c2006SBryan Schumaker kfree(ctx); 87480c2006SBryan Schumaker } 88480c2006SBryan Schumaker 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * Open file 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds static int 931da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 941da177e4SLinus Torvalds { 95480c2006SBryan Schumaker int res = 0; 96480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 97480c2006SBryan Schumaker struct rpc_cred *cred; 981da177e4SLinus Torvalds 996da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 100cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 101cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 102cc0dd2d1SChuck Lever 103cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1041e7cb3dcSChuck Lever 105480c2006SBryan Schumaker cred = rpc_lookup_cred(); 106480c2006SBryan Schumaker if (IS_ERR(cred)) 107480c2006SBryan Schumaker return PTR_ERR(cred); 1080c030806STrond Myklebust ctx = alloc_nfs_open_dir_context(inode, cred); 109480c2006SBryan Schumaker if (IS_ERR(ctx)) { 110480c2006SBryan Schumaker res = PTR_ERR(ctx); 111480c2006SBryan Schumaker goto out; 112480c2006SBryan Schumaker } 113480c2006SBryan Schumaker filp->private_data = ctx; 114f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 115f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 116f5a73672SNeil Brown * have been called, so we need to refresh the 117f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 118f5a73672SNeil Brown */ 119f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 120f5a73672SNeil Brown } 121480c2006SBryan Schumaker out: 122480c2006SBryan Schumaker put_rpccred(cred); 1231da177e4SLinus Torvalds return res; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126480c2006SBryan Schumaker static int 127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 128480c2006SBryan Schumaker { 129480c2006SBryan Schumaker put_nfs_open_dir_context(filp->private_data); 130480c2006SBryan Schumaker return 0; 131480c2006SBryan Schumaker } 132480c2006SBryan Schumaker 133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 134d1bacf9eSBryan Schumaker u64 cookie; 135d1bacf9eSBryan Schumaker u64 ino; 136d1bacf9eSBryan Schumaker struct qstr string; 1370b26a0bfSTrond Myklebust unsigned char d_type; 138d1bacf9eSBryan Schumaker }; 139d1bacf9eSBryan Schumaker 140d1bacf9eSBryan Schumaker struct nfs_cache_array { 14188b8e133SChuck Lever int size; 142d1bacf9eSBryan Schumaker int eof_index; 143d1bacf9eSBryan Schumaker u64 last_cookie; 144d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 145d1bacf9eSBryan Schumaker }; 146d1bacf9eSBryan Schumaker 147573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int); 1481da177e4SLinus Torvalds typedef struct { 1491da177e4SLinus Torvalds struct file *file; 1501da177e4SLinus Torvalds struct page *page; 15123db8620SAl Viro struct dir_context *ctx; 1521da177e4SLinus Torvalds unsigned long page_index; 153f0dd2136STrond Myklebust u64 *dir_cookie; 1540aded708STrond Myklebust u64 last_cookie; 155f0dd2136STrond Myklebust loff_t current_index; 1561da177e4SLinus Torvalds decode_dirent_t decode; 157d1bacf9eSBryan Schumaker 1581f4eab7eSNeil Brown unsigned long timestamp; 1594704f0e2STrond Myklebust unsigned long gencount; 160d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 161d1bacf9eSBryan Schumaker unsigned int plus:1; 162d1bacf9eSBryan Schumaker unsigned int eof:1; 1631da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1641da177e4SLinus Torvalds 165d1bacf9eSBryan Schumaker /* 166d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 1671da177e4SLinus Torvalds */ 1681da177e4SLinus Torvalds static 169d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 1701da177e4SLinus Torvalds { 1718cd51a0cSTrond Myklebust void *ptr; 172d1bacf9eSBryan Schumaker if (page == NULL) 173d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 1748cd51a0cSTrond Myklebust ptr = kmap(page); 1758cd51a0cSTrond Myklebust if (ptr == NULL) 1768cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 1778cd51a0cSTrond Myklebust return ptr; 178d1bacf9eSBryan Schumaker } 179d1bacf9eSBryan Schumaker 180d1bacf9eSBryan Schumaker static 181d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 182d1bacf9eSBryan Schumaker { 183d1bacf9eSBryan Schumaker kunmap(page); 184d1bacf9eSBryan Schumaker } 185d1bacf9eSBryan Schumaker 186d1bacf9eSBryan Schumaker /* 187d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 188d1bacf9eSBryan Schumaker */ 189d1bacf9eSBryan Schumaker static 19011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 191d1bacf9eSBryan Schumaker { 19211de3b11STrond Myklebust struct nfs_cache_array *array; 193d1bacf9eSBryan Schumaker int i; 1948cd51a0cSTrond Myklebust 1952b86ce2dSCong Wang array = kmap_atomic(page); 196d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 197d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 1982b86ce2dSCong Wang kunmap_atomic(array); 199d1bacf9eSBryan Schumaker } 200d1bacf9eSBryan Schumaker 201d1bacf9eSBryan Schumaker /* 202d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 203d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 204d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 205d1bacf9eSBryan Schumaker */ 206d1bacf9eSBryan Schumaker static 2074a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 208d1bacf9eSBryan Schumaker { 209d1bacf9eSBryan Schumaker string->len = len; 210d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2114a201d6eSTrond Myklebust if (string->name == NULL) 2124a201d6eSTrond Myklebust return -ENOMEM; 21304e4bd1cSCatalin Marinas /* 21404e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 21504e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 21604e4bd1cSCatalin Marinas */ 21704e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2184a201d6eSTrond Myklebust string->hash = full_name_hash(name, len); 2194a201d6eSTrond Myklebust return 0; 220d1bacf9eSBryan Schumaker } 221d1bacf9eSBryan Schumaker 222d1bacf9eSBryan Schumaker static 223d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 224d1bacf9eSBryan Schumaker { 225d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2264a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2274a201d6eSTrond Myklebust int ret; 2284a201d6eSTrond Myklebust 229d1bacf9eSBryan Schumaker if (IS_ERR(array)) 230d1bacf9eSBryan Schumaker return PTR_ERR(array); 231d1bacf9eSBryan Schumaker 2324a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2333020093fSTrond Myklebust 2343020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2353020093fSTrond Myklebust ret = -ENOSPC; 2363020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2373020093fSTrond Myklebust goto out; 2383020093fSTrond Myklebust 2394a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2404a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2410b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2424a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2434a201d6eSTrond Myklebust if (ret) 2444a201d6eSTrond Myklebust goto out; 245d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2468cd51a0cSTrond Myklebust array->size++; 24747c716cbSTrond Myklebust if (entry->eof != 0) 248d1bacf9eSBryan Schumaker array->eof_index = array->size; 2494a201d6eSTrond Myklebust out: 250d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 2514a201d6eSTrond Myklebust return ret; 252d1bacf9eSBryan Schumaker } 253d1bacf9eSBryan Schumaker 254d1bacf9eSBryan Schumaker static 255d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 256d1bacf9eSBryan Schumaker { 25723db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index; 258d1bacf9eSBryan Schumaker unsigned int index; 259d1bacf9eSBryan Schumaker 260d1bacf9eSBryan Schumaker if (diff < 0) 261d1bacf9eSBryan Schumaker goto out_eof; 262d1bacf9eSBryan Schumaker if (diff >= array->size) { 2638cd51a0cSTrond Myklebust if (array->eof_index >= 0) 264d1bacf9eSBryan Schumaker goto out_eof; 265d1bacf9eSBryan Schumaker return -EAGAIN; 266d1bacf9eSBryan Schumaker } 267d1bacf9eSBryan Schumaker 268d1bacf9eSBryan Schumaker index = (unsigned int)diff; 269d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 270d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 271d1bacf9eSBryan Schumaker return 0; 272d1bacf9eSBryan Schumaker out_eof: 273d1bacf9eSBryan Schumaker desc->eof = 1; 274d1bacf9eSBryan Schumaker return -EBADCOOKIE; 275d1bacf9eSBryan Schumaker } 276d1bacf9eSBryan Schumaker 277d1bacf9eSBryan Schumaker static 278d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 279d1bacf9eSBryan Schumaker { 280d1bacf9eSBryan Schumaker int i; 2818ef2ce3eSBryan Schumaker loff_t new_pos; 282d1bacf9eSBryan Schumaker int status = -EAGAIN; 283d1bacf9eSBryan Schumaker 284d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 2858cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 286496ad9aaSAl Viro struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 2870c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 2880c030806STrond Myklebust 2898ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 2900c030806STrond Myklebust if (ctx->attr_gencount != nfsi->attr_gencount 2910c030806STrond Myklebust || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) { 2920c030806STrond Myklebust ctx->duped = 0; 2930c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 29423db8620SAl Viro } else if (new_pos < desc->ctx->pos) { 2950c030806STrond Myklebust if (ctx->duped > 0 2960c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 2970c030806STrond Myklebust if (printk_ratelimit()) { 2980c030806STrond Myklebust pr_notice("NFS: directory %s/%s contains a readdir loop." 2990c030806STrond Myklebust "Please contact your server vendor. " 300374e4e3eSBryan Schumaker "The file: %s has duplicate cookie %llu\n", 3010c030806STrond Myklebust desc->file->f_dentry->d_parent->d_name.name, 3020c030806STrond Myklebust desc->file->f_dentry->d_name.name, 303374e4e3eSBryan Schumaker array->array[i].string.name, 3040c030806STrond Myklebust *desc->dir_cookie); 3050c030806STrond Myklebust } 3060c030806STrond Myklebust status = -ELOOP; 3070c030806STrond Myklebust goto out; 3080c030806STrond Myklebust } 3098ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3100c030806STrond Myklebust ctx->duped = -1; 3118ef2ce3eSBryan Schumaker } 31223db8620SAl Viro desc->ctx->pos = new_pos; 3138cd51a0cSTrond Myklebust desc->cache_entry_index = i; 31447c716cbSTrond Myklebust return 0; 3158cd51a0cSTrond Myklebust } 3168cd51a0cSTrond Myklebust } 31747c716cbSTrond Myklebust if (array->eof_index >= 0) { 318d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 31918fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 32018fb5fe4STrond Myklebust desc->eof = 1; 321d1bacf9eSBryan Schumaker } 3220c030806STrond Myklebust out: 323d1bacf9eSBryan Schumaker return status; 324d1bacf9eSBryan Schumaker } 325d1bacf9eSBryan Schumaker 326d1bacf9eSBryan Schumaker static 327d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 328d1bacf9eSBryan Schumaker { 329d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 33047c716cbSTrond Myklebust int status; 331d1bacf9eSBryan Schumaker 332d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 333d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 334d1bacf9eSBryan Schumaker status = PTR_ERR(array); 335d1bacf9eSBryan Schumaker goto out; 336d1bacf9eSBryan Schumaker } 337d1bacf9eSBryan Schumaker 338d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 339d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 340d1bacf9eSBryan Schumaker else 341d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 342d1bacf9eSBryan Schumaker 34347c716cbSTrond Myklebust if (status == -EAGAIN) { 3440aded708STrond Myklebust desc->last_cookie = array->last_cookie; 345e47c085aSTrond Myklebust desc->current_index += array->size; 34647c716cbSTrond Myklebust desc->page_index++; 34747c716cbSTrond Myklebust } 348d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 349d1bacf9eSBryan Schumaker out: 350d1bacf9eSBryan Schumaker return status; 351d1bacf9eSBryan Schumaker } 352d1bacf9eSBryan Schumaker 353d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 354d1bacf9eSBryan Schumaker static 35556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 356d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 357d1bacf9eSBryan Schumaker { 358480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 359480c2006SBryan Schumaker struct rpc_cred *cred = ctx->cred; 3604704f0e2STrond Myklebust unsigned long timestamp, gencount; 3611da177e4SLinus Torvalds int error; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds again: 3641da177e4SLinus Torvalds timestamp = jiffies; 3654704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 36656e4ebf8SBryan Schumaker error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages, 3671da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3681da177e4SLinus Torvalds if (error < 0) { 3691da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3701da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3711da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3723a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 3731da177e4SLinus Torvalds desc->plus = 0; 3741da177e4SLinus Torvalds goto again; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds goto error; 3771da177e4SLinus Torvalds } 3781f4eab7eSNeil Brown desc->timestamp = timestamp; 3794704f0e2STrond Myklebust desc->gencount = gencount; 380d1bacf9eSBryan Schumaker error: 381d1bacf9eSBryan Schumaker return error; 382d1bacf9eSBryan Schumaker } 383d1bacf9eSBryan Schumaker 384573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 385573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 386d1bacf9eSBryan Schumaker { 387573c4e1eSChuck Lever int error; 388d1bacf9eSBryan Schumaker 389573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 390573c4e1eSChuck Lever if (error) 391573c4e1eSChuck Lever return error; 392d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 393d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 394d1bacf9eSBryan Schumaker return 0; 395d1bacf9eSBryan Schumaker } 396d1bacf9eSBryan Schumaker 397d39ab9deSBryan Schumaker static 398d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 399d39ab9deSBryan Schumaker { 400d39ab9deSBryan Schumaker if (dentry->d_inode == NULL) 401d39ab9deSBryan Schumaker goto different; 40237a09f07STrond Myklebust if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0) 403d39ab9deSBryan Schumaker goto different; 404d39ab9deSBryan Schumaker return 1; 405d39ab9deSBryan Schumaker different: 406d39ab9deSBryan Schumaker return 0; 407d39ab9deSBryan Schumaker } 408d39ab9deSBryan Schumaker 409d39ab9deSBryan Schumaker static 41023db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 411d69ee9b8STrond Myklebust { 412d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 413d69ee9b8STrond Myklebust return false; 414d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 415d69ee9b8STrond Myklebust return true; 41623db8620SAl Viro if (ctx->pos == 0) 417d69ee9b8STrond Myklebust return true; 418d69ee9b8STrond Myklebust return false; 419d69ee9b8STrond Myklebust } 420d69ee9b8STrond Myklebust 421d69ee9b8STrond Myklebust /* 422d69ee9b8STrond Myklebust * This function is called by the lookup code to request the use of 423d69ee9b8STrond Myklebust * readdirplus to accelerate any future lookups in the same 424d69ee9b8STrond Myklebust * directory. 425d69ee9b8STrond Myklebust */ 426d69ee9b8STrond Myklebust static 427d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 428d69ee9b8STrond Myklebust { 429d69ee9b8STrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags); 430d69ee9b8STrond Myklebust } 431d69ee9b8STrond Myklebust 432d69ee9b8STrond Myklebust static 433d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 434d39ab9deSBryan Schumaker { 43526fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len); 4364a201d6eSTrond Myklebust struct dentry *dentry; 4374a201d6eSTrond Myklebust struct dentry *alias; 438d39ab9deSBryan Schumaker struct inode *dir = parent->d_inode; 439d39ab9deSBryan Schumaker struct inode *inode; 440aa9c2669SDavid Quigley int status; 441d39ab9deSBryan Schumaker 4424a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4434a201d6eSTrond Myklebust if (filename.len == 1) 4444a201d6eSTrond Myklebust return; 4454a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4464a201d6eSTrond Myklebust return; 4474a201d6eSTrond Myklebust } 4484a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 449d39ab9deSBryan Schumaker 4504a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 451d39ab9deSBryan Schumaker if (dentry != NULL) { 452d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 453cda57a1eSJeff Layton nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 454aa9c2669SDavid Quigley status = nfs_refresh_inode(dentry->d_inode, entry->fattr); 455aa9c2669SDavid Quigley if (!status) 456aa9c2669SDavid Quigley nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label); 457d39ab9deSBryan Schumaker goto out; 458d39ab9deSBryan Schumaker } else { 459696199f8SAl Viro if (d_invalidate(dentry) != 0) 460696199f8SAl Viro goto out; 461d39ab9deSBryan Schumaker dput(dentry); 462d39ab9deSBryan Schumaker } 463d39ab9deSBryan Schumaker } 464d39ab9deSBryan Schumaker 465d39ab9deSBryan Schumaker dentry = d_alloc(parent, &filename); 4664a201d6eSTrond Myklebust if (dentry == NULL) 4674a201d6eSTrond Myklebust return; 4684a201d6eSTrond Myklebust 4691775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 470d39ab9deSBryan Schumaker if (IS_ERR(inode)) 471d39ab9deSBryan Schumaker goto out; 472d39ab9deSBryan Schumaker 473d39ab9deSBryan Schumaker alias = d_materialise_unique(dentry, inode); 474d39ab9deSBryan Schumaker if (IS_ERR(alias)) 475d39ab9deSBryan Schumaker goto out; 476d39ab9deSBryan Schumaker else if (alias) { 477d39ab9deSBryan Schumaker nfs_set_verifier(alias, nfs_save_change_attribute(dir)); 478d39ab9deSBryan Schumaker dput(alias); 479d39ab9deSBryan Schumaker } else 480d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 481d39ab9deSBryan Schumaker 482d39ab9deSBryan Schumaker out: 483d39ab9deSBryan Schumaker dput(dentry); 484d39ab9deSBryan Schumaker } 485d39ab9deSBryan Schumaker 486d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 487d1bacf9eSBryan Schumaker static 4888cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 4896650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 490d1bacf9eSBryan Schumaker { 491babddc72SBryan Schumaker struct xdr_stream stream; 492f7da7a12SBenny Halevy struct xdr_buf buf; 4936650239aSTrond Myklebust struct page *scratch; 49499424380SBryan Schumaker struct nfs_cache_array *array; 4955c346854STrond Myklebust unsigned int count = 0; 4965c346854STrond Myklebust int status; 497babddc72SBryan Schumaker 4986650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 4996650239aSTrond Myklebust if (scratch == NULL) 5006650239aSTrond Myklebust return -ENOMEM; 501babddc72SBryan Schumaker 502f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5036650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 50499424380SBryan Schumaker 50599424380SBryan Schumaker do { 50699424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5078cd51a0cSTrond Myklebust if (status != 0) { 5088cd51a0cSTrond Myklebust if (status == -EAGAIN) 5098cd51a0cSTrond Myklebust status = 0; 51099424380SBryan Schumaker break; 5118cd51a0cSTrond Myklebust } 51299424380SBryan Schumaker 5135c346854STrond Myklebust count++; 5145c346854STrond Myklebust 51547c716cbSTrond Myklebust if (desc->plus != 0) 516d39ab9deSBryan Schumaker nfs_prime_dcache(desc->file->f_path.dentry, entry); 5178cd51a0cSTrond Myklebust 5188cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5198cd51a0cSTrond Myklebust if (status != 0) 5208cd51a0cSTrond Myklebust break; 52199424380SBryan Schumaker } while (!entry->eof); 52299424380SBryan Schumaker 52347c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 52499424380SBryan Schumaker array = nfs_readdir_get_array(page); 5258cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 5268cd51a0cSTrond Myklebust array->eof_index = array->size; 52799424380SBryan Schumaker status = 0; 52899424380SBryan Schumaker nfs_readdir_release_array(page); 5295c346854STrond Myklebust } else 5305c346854STrond Myklebust status = PTR_ERR(array); 53156e4ebf8SBryan Schumaker } 5326650239aSTrond Myklebust 5336650239aSTrond Myklebust put_page(scratch); 5348cd51a0cSTrond Myklebust return status; 5358cd51a0cSTrond Myklebust } 53656e4ebf8SBryan Schumaker 53756e4ebf8SBryan Schumaker static 53856e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages) 53956e4ebf8SBryan Schumaker { 54056e4ebf8SBryan Schumaker unsigned int i; 54156e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 54256e4ebf8SBryan Schumaker put_page(pages[i]); 54356e4ebf8SBryan Schumaker } 54456e4ebf8SBryan Schumaker 54556e4ebf8SBryan Schumaker static 54656e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages, 54756e4ebf8SBryan Schumaker unsigned int npages) 54856e4ebf8SBryan Schumaker { 54956e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, npages); 55056e4ebf8SBryan Schumaker } 55156e4ebf8SBryan Schumaker 55256e4ebf8SBryan Schumaker /* 55356e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 55456e4ebf8SBryan Schumaker * to nfs_readdir_free_large_page 55556e4ebf8SBryan Schumaker */ 55656e4ebf8SBryan Schumaker static 5576650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages) 55856e4ebf8SBryan Schumaker { 55956e4ebf8SBryan Schumaker unsigned int i; 56056e4ebf8SBryan Schumaker 56156e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 56256e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 56356e4ebf8SBryan Schumaker if (page == NULL) 56456e4ebf8SBryan Schumaker goto out_freepages; 56556e4ebf8SBryan Schumaker pages[i] = page; 56656e4ebf8SBryan Schumaker } 5676650239aSTrond Myklebust return 0; 56856e4ebf8SBryan Schumaker 56956e4ebf8SBryan Schumaker out_freepages: 57056e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, i); 5716650239aSTrond Myklebust return -ENOMEM; 572d1bacf9eSBryan Schumaker } 573d1bacf9eSBryan Schumaker 574d1bacf9eSBryan Schumaker static 575d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 576d1bacf9eSBryan Schumaker { 57756e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 57856e4ebf8SBryan Schumaker void *pages_ptr = NULL; 579d1bacf9eSBryan Schumaker struct nfs_entry entry; 580d1bacf9eSBryan Schumaker struct file *file = desc->file; 581d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 5828cd51a0cSTrond Myklebust int status = -ENOMEM; 58356e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 584d1bacf9eSBryan Schumaker 585d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 5860aded708STrond Myklebust entry.cookie = desc->last_cookie; 587d1bacf9eSBryan Schumaker entry.eof = 0; 588d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 589d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 590573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 591d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 592d1bacf9eSBryan Schumaker goto out; 593d1bacf9eSBryan Schumaker 59414c43f76SDavid Quigley entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 59514c43f76SDavid Quigley if (IS_ERR(entry.label)) { 59614c43f76SDavid Quigley status = PTR_ERR(entry.label); 59714c43f76SDavid Quigley goto out; 59814c43f76SDavid Quigley } 59914c43f76SDavid Quigley 600d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 6018cd51a0cSTrond Myklebust if (IS_ERR(array)) { 6028cd51a0cSTrond Myklebust status = PTR_ERR(array); 60314c43f76SDavid Quigley goto out_label_free; 6048cd51a0cSTrond Myklebust } 605d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 606d1bacf9eSBryan Schumaker array->eof_index = -1; 607d1bacf9eSBryan Schumaker 6086650239aSTrond Myklebust status = nfs_readdir_large_page(pages, array_size); 6096650239aSTrond Myklebust if (status < 0) 610d1bacf9eSBryan Schumaker goto out_release_array; 611d1bacf9eSBryan Schumaker do { 612ac396128STrond Myklebust unsigned int pglen; 61356e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 614babddc72SBryan Schumaker 615d1bacf9eSBryan Schumaker if (status < 0) 616d1bacf9eSBryan Schumaker break; 617ac396128STrond Myklebust pglen = status; 6186650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6198cd51a0cSTrond Myklebust if (status < 0) { 6208cd51a0cSTrond Myklebust if (status == -ENOSPC) 6218cd51a0cSTrond Myklebust status = 0; 6228cd51a0cSTrond Myklebust break; 6238cd51a0cSTrond Myklebust } 6248cd51a0cSTrond Myklebust } while (array->eof_index < 0); 625d1bacf9eSBryan Schumaker 62656e4ebf8SBryan Schumaker nfs_readdir_free_large_page(pages_ptr, pages, array_size); 627d1bacf9eSBryan Schumaker out_release_array: 628d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 62914c43f76SDavid Quigley out_label_free: 63014c43f76SDavid Quigley nfs4_label_free(entry.label); 631d1bacf9eSBryan Schumaker out: 632d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 633d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 634d1bacf9eSBryan Schumaker return status; 635d1bacf9eSBryan Schumaker } 636d1bacf9eSBryan Schumaker 637d1bacf9eSBryan Schumaker /* 638d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 639d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 640d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 641d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6421da177e4SLinus Torvalds */ 643d1bacf9eSBryan Schumaker static 644d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 645d1bacf9eSBryan Schumaker { 646496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 6478cd51a0cSTrond Myklebust int ret; 648d1bacf9eSBryan Schumaker 6498cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6508cd51a0cSTrond Myklebust if (ret < 0) 651d1bacf9eSBryan Schumaker goto error; 652d1bacf9eSBryan Schumaker SetPageUptodate(page); 653d1bacf9eSBryan Schumaker 6542aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 655cd9ae2b6STrond Myklebust /* Should never happen */ 656cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 657cd9ae2b6STrond Myklebust } 6581da177e4SLinus Torvalds unlock_page(page); 6591da177e4SLinus Torvalds return 0; 6601da177e4SLinus Torvalds error: 6611da177e4SLinus Torvalds unlock_page(page); 6628cd51a0cSTrond Myklebust return ret; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 665d1bacf9eSBryan Schumaker static 666d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 6671da177e4SLinus Torvalds { 66811de3b11STrond Myklebust if (!desc->page->mapping) 66911de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 6701da177e4SLinus Torvalds page_cache_release(desc->page); 6711da177e4SLinus Torvalds desc->page = NULL; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 674d1bacf9eSBryan Schumaker static 675d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 6761da177e4SLinus Torvalds { 677496ad9aaSAl Viro return read_cache_page(file_inode(desc->file)->i_mapping, 678d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* 682d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 6831da177e4SLinus Torvalds */ 684d1bacf9eSBryan Schumaker static 685d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 686d1bacf9eSBryan Schumaker { 687d1bacf9eSBryan Schumaker int res; 688d1bacf9eSBryan Schumaker 689d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 690d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 691d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 692d1bacf9eSBryan Schumaker 693d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 69447c716cbSTrond Myklebust if (res != 0) 695d1bacf9eSBryan Schumaker cache_page_release(desc); 696d1bacf9eSBryan Schumaker return res; 697d1bacf9eSBryan Schumaker } 698d1bacf9eSBryan Schumaker 699d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7001da177e4SLinus Torvalds static inline 7011da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7021da177e4SLinus Torvalds { 7038cd51a0cSTrond Myklebust int res; 704d1bacf9eSBryan Schumaker 7050aded708STrond Myklebust if (desc->page_index == 0) { 7068cd51a0cSTrond Myklebust desc->current_index = 0; 7070aded708STrond Myklebust desc->last_cookie = 0; 7080aded708STrond Myklebust } 70947c716cbSTrond Myklebust do { 710d1bacf9eSBryan Schumaker res = find_cache_page(desc); 71147c716cbSTrond Myklebust } while (res == -EAGAIN); 7121da177e4SLinus Torvalds return res; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds static 71923db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds struct file *file = desc->file; 722d1bacf9eSBryan Schumaker int i = 0; 723d1bacf9eSBryan Schumaker int res = 0; 724d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7258ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7268ef2ce3eSBryan Schumaker 727d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 728e7c58e97STrond Myklebust if (IS_ERR(array)) { 729e7c58e97STrond Myklebust res = PTR_ERR(array); 730e7c58e97STrond Myklebust goto out; 731e7c58e97STrond Myklebust } 7321da177e4SLinus Torvalds 733d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 734ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7351da177e4SLinus Torvalds 736ece0b423STrond Myklebust ent = &array->array[i]; 73723db8620SAl Viro if (!dir_emit(desc->ctx, ent->string.name, ent->string.len, 73823db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) { 739ece0b423STrond Myklebust desc->eof = 1; 7401da177e4SLinus Torvalds break; 741ece0b423STrond Myklebust } 74223db8620SAl Viro desc->ctx->pos++; 743d1bacf9eSBryan Schumaker if (i < (array->size-1)) 744d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 745d1bacf9eSBryan Schumaker else 746d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7470c030806STrond Myklebust if (ctx->duped != 0) 7480c030806STrond Myklebust ctx->duped = 1; 7498cd51a0cSTrond Myklebust } 75047c716cbSTrond Myklebust if (array->eof_index >= 0) 751d1bacf9eSBryan Schumaker desc->eof = 1; 752d1bacf9eSBryan Schumaker 753d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 754e7c58e97STrond Myklebust out: 755d1bacf9eSBryan Schumaker cache_page_release(desc); 7561e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7571e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7581da177e4SLinus Torvalds return res; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /* 7621da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 7631da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 7641da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 7651da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 7661da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 7671da177e4SLinus Torvalds * 7681da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 7691da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 7701da177e4SLinus Torvalds * we should already have a complete representation of the 7711da177e4SLinus Torvalds * directory in the page cache by the time we get here. 7721da177e4SLinus Torvalds */ 7731da177e4SLinus Torvalds static inline 77423db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds struct page *page = NULL; 7771da177e4SLinus Torvalds int status; 778496ad9aaSAl Viro struct inode *inode = file_inode(desc->file); 7790c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 7801da177e4SLinus Torvalds 7811e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 7821e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 7851da177e4SLinus Torvalds if (!page) { 7861da177e4SLinus Torvalds status = -ENOMEM; 7871da177e4SLinus Torvalds goto out; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7907a8e1dc3STrond Myklebust desc->page_index = 0; 7910aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 7927a8e1dc3STrond Myklebust desc->page = page; 7930c030806STrond Myklebust ctx->duped = 0; 7947a8e1dc3STrond Myklebust 79585f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 79685f8607eSTrond Myklebust if (status < 0) 797d1bacf9eSBryan Schumaker goto out_release; 798d1bacf9eSBryan Schumaker 79923db8620SAl Viro status = nfs_do_filldir(desc); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds out: 8021e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8033110ff80SHarvey Harrison __func__, status); 8041da177e4SLinus Torvalds return status; 8051da177e4SLinus Torvalds out_release: 806d1bacf9eSBryan Schumaker cache_page_release(desc); 8071da177e4SLinus Torvalds goto out; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 81000a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 81100a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 81200a92642SOlivier Galibert whole directory. 8131da177e4SLinus Torvalds */ 81423db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx) 8151da177e4SLinus Torvalds { 81623db8620SAl Viro struct dentry *dentry = file->f_path.dentry; 8171da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 8181da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8191da177e4SLinus Torvalds *desc = &my_desc; 82023db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data; 821*07b5ce8eSScott Mayhew int res = 0; 8221da177e4SLinus Torvalds 8236da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 8241e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 82523db8620SAl Viro (long long)ctx->pos); 82691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 82791d5b470SChuck Lever 8281da177e4SLinus Torvalds /* 82923db8620SAl Viro * ctx->pos points to the dirent entry number. 830f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 83100a92642SOlivier Galibert * to either find the entry with the appropriate number or 83200a92642SOlivier Galibert * revalidate the cookie. 8331da177e4SLinus Torvalds */ 8341da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8351da177e4SLinus Torvalds 83623db8620SAl Viro desc->file = file; 83723db8620SAl Viro desc->ctx = ctx; 838480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8391da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 84023db8620SAl Viro desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0; 8411da177e4SLinus Torvalds 842565277f6STrond Myklebust nfs_block_sillyrename(dentry); 843*07b5ce8eSScott Mayhew if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) 84423db8620SAl Viro res = nfs_revalidate_mapping(inode, file->f_mapping); 845fccca7fcSTrond Myklebust if (res < 0) 846fccca7fcSTrond Myklebust goto out; 847fccca7fcSTrond Myklebust 84847c716cbSTrond Myklebust do { 8491da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 85000a92642SOlivier Galibert 8511da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 852ece0b423STrond Myklebust res = 0; 8531da177e4SLinus Torvalds /* This means either end of directory */ 854d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 8551da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 85623db8620SAl Viro res = uncached_readdir(desc); 857ece0b423STrond Myklebust if (res == 0) 8581da177e4SLinus Torvalds continue; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds break; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8633a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8641da177e4SLinus Torvalds nfs_zap_caches(inode); 865baf57a09STrond Myklebust desc->page_index = 0; 8661da177e4SLinus Torvalds desc->plus = 0; 867d1bacf9eSBryan Schumaker desc->eof = 0; 8681da177e4SLinus Torvalds continue; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds if (res < 0) 8711da177e4SLinus Torvalds break; 8721da177e4SLinus Torvalds 87323db8620SAl Viro res = nfs_do_filldir(desc); 874ece0b423STrond Myklebust if (res < 0) 8751da177e4SLinus Torvalds break; 87647c716cbSTrond Myklebust } while (!desc->eof); 877fccca7fcSTrond Myklebust out: 878565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 8791e7cb3dcSChuck Lever if (res > 0) 8801e7cb3dcSChuck Lever res = 0; 881aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 8821e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8831e7cb3dcSChuck Lever res); 8841da177e4SLinus Torvalds return res; 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds 887965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 888f0dd2136STrond Myklebust { 889b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 890b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 891480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 892b84e06c5SChuck Lever 8936da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 894b84e06c5SChuck Lever dentry->d_parent->d_name.name, 895b84e06c5SChuck Lever dentry->d_name.name, 896965c8e59SAndrew Morton offset, whence); 897b84e06c5SChuck Lever 898b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 899965c8e59SAndrew Morton switch (whence) { 900f0dd2136STrond Myklebust case 1: 901f0dd2136STrond Myklebust offset += filp->f_pos; 902f0dd2136STrond Myklebust case 0: 903f0dd2136STrond Myklebust if (offset >= 0) 904f0dd2136STrond Myklebust break; 905f0dd2136STrond Myklebust default: 906f0dd2136STrond Myklebust offset = -EINVAL; 907f0dd2136STrond Myklebust goto out; 908f0dd2136STrond Myklebust } 909f0dd2136STrond Myklebust if (offset != filp->f_pos) { 910f0dd2136STrond Myklebust filp->f_pos = offset; 911480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9128ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 913f0dd2136STrond Myklebust } 914f0dd2136STrond Myklebust out: 915b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 916f0dd2136STrond Myklebust return offset; 917f0dd2136STrond Myklebust } 918f0dd2136STrond Myklebust 9191da177e4SLinus Torvalds /* 9201da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9211da177e4SLinus Torvalds * is a dummy operation. 9221da177e4SLinus Torvalds */ 92302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 92402c24a82SJosef Bacik int datasync) 9251da177e4SLinus Torvalds { 9267ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 92702c24a82SJosef Bacik struct inode *inode = dentry->d_inode; 9287ea80859SChristoph Hellwig 9296da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 9301e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 9311e7cb3dcSChuck Lever datasync); 9321e7cb3dcSChuck Lever 93302c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 93454917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 93502c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 9361da177e4SLinus Torvalds return 0; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 939bfc69a45STrond Myklebust /** 940bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 941bfc69a45STrond Myklebust * @dir - pointer to directory inode 942bfc69a45STrond Myklebust * 943bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 944bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 945bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 946bfc69a45STrond Myklebust * 947bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 948bfc69a45STrond Myklebust */ 949bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 950bfc69a45STrond Myklebust { 951011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 952bfc69a45STrond Myklebust } 95389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 954bfc69a45STrond Myklebust 9551da177e4SLinus Torvalds /* 9561da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9571da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9581da177e4SLinus Torvalds * and may need to be looked up again. 9591da177e4SLinus Torvalds */ 960c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 9611da177e4SLinus Torvalds { 9621da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9631da177e4SLinus Torvalds return 1; 9644eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 9654eec952eSTrond Myklebust return 0; 966f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 9676ecc5e8fSTrond Myklebust return 0; 968f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 969f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 970f2c77f4eSTrond Myklebust return 0; 971f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 972f2c77f4eSTrond Myklebust return 0; 973f2c77f4eSTrond Myklebust return 1; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 977a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 978a12802caSTrond Myklebust * an O_EXCL create using this path component. 979a12802caSTrond Myklebust */ 980fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 981a12802caSTrond Myklebust { 982a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 983a12802caSTrond Myklebust return 0; 984fa3c56bbSAl Viro return flags & LOOKUP_EXCL; 985a12802caSTrond Myklebust } 986a12802caSTrond Myklebust 987a12802caSTrond Myklebust /* 9881d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 9891d6757fbSTrond Myklebust * 9901d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 9911d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 9921d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 9931d6757fbSTrond Myklebust * 9941d6757fbSTrond Myklebust */ 99565a0c149STrond Myklebust static 996fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 9971da177e4SLinus Torvalds { 9981da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 99965a0c149STrond Myklebust int ret; 10001da177e4SLinus Torvalds 100136d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10024e99a1ffSTrond Myklebust return 0; 10031da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 1004fa3c56bbSAl Viro if (flags & LOOKUP_REVAL) 10051da177e4SLinus Torvalds goto out_force; 10061da177e4SLinus Torvalds /* This is an open(2) */ 1007fa3c56bbSAl Viro if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) && 1008fa3c56bbSAl Viro (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) 10091da177e4SLinus Torvalds goto out_force; 101065a0c149STrond Myklebust out: 101165a0c149STrond Myklebust return (inode->i_nlink == 0) ? -ENOENT : 0; 10121da177e4SLinus Torvalds out_force: 101365a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode); 101465a0c149STrond Myklebust if (ret != 0) 101565a0c149STrond Myklebust return ret; 101665a0c149STrond Myklebust goto out; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds /* 10201da177e4SLinus Torvalds * We judge how long we want to trust negative 10211da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10221da177e4SLinus Torvalds * 10231da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10241da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds static inline 10271da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1028fa3c56bbSAl Viro unsigned int flags) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 1031fa3c56bbSAl Viro if (flags & LOOKUP_CREATE) 10321da177e4SLinus Torvalds return 0; 10334eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10344eec952eSTrond Myklebust return 1; 10351da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /* 10391da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 10401da177e4SLinus Torvalds * and we should check whether we can really trust that 10411da177e4SLinus Torvalds * lookup. 10421da177e4SLinus Torvalds * 10431da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 10441da177e4SLinus Torvalds * we have an inode! 10451da177e4SLinus Torvalds * 10461da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 10471da177e4SLinus Torvalds * cached dentry and do a new lookup. 10481da177e4SLinus Torvalds */ 10490b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 10501da177e4SLinus Torvalds { 10511da177e4SLinus Torvalds struct inode *dir; 10521da177e4SLinus Torvalds struct inode *inode; 10531da177e4SLinus Torvalds struct dentry *parent; 1054e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1055e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 10561775fd3eSDavid Quigley struct nfs4_label *label = NULL; 10571da177e4SLinus Torvalds int error; 10581da177e4SLinus Torvalds 1059fa3c56bbSAl Viro if (flags & LOOKUP_RCU) 106034286d66SNick Piggin return -ECHILD; 106134286d66SNick Piggin 10621da177e4SLinus Torvalds parent = dget_parent(dentry); 10631da177e4SLinus Torvalds dir = parent->d_inode; 106491d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 10651da177e4SLinus Torvalds inode = dentry->d_inode; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds if (!inode) { 1068fa3c56bbSAl Viro if (nfs_neg_need_reval(dir, dentry, flags)) 10691da177e4SLinus Torvalds goto out_bad; 1070d69ee9b8STrond Myklebust goto out_valid_noent; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if (is_bad_inode(inode)) { 10741e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 10753110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 10761e7cb3dcSChuck Lever dentry->d_name.name); 10771da177e4SLinus Torvalds goto out_bad; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 1080011e2a7fSBryan Schumaker if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ)) 108115860ab1STrond Myklebust goto out_set_verifier; 108215860ab1STrond Myklebust 10831da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 1084fa3c56bbSAl Viro if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) { 1085fa3c56bbSAl Viro if (nfs_lookup_verify_inode(inode, flags)) 10861da177e4SLinus Torvalds goto out_zap_parent; 10871da177e4SLinus Torvalds goto out_valid; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (NFS_STALE(inode)) 10911da177e4SLinus Torvalds goto out_bad; 10921da177e4SLinus Torvalds 1093e1fb4d05STrond Myklebust error = -ENOMEM; 1094e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1095e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1096e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1097e1fb4d05STrond Myklebust goto out_error; 1098e1fb4d05STrond Myklebust 109914c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 110014c43f76SDavid Quigley if (IS_ERR(label)) 110114c43f76SDavid Quigley goto out_error; 110214c43f76SDavid Quigley 11031775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 11041da177e4SLinus Torvalds if (error) 11051da177e4SLinus Torvalds goto out_bad; 1106e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11071da177e4SLinus Torvalds goto out_bad; 1108e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 11091da177e4SLinus Torvalds goto out_bad; 11101da177e4SLinus Torvalds 1111aa9c2669SDavid Quigley nfs_setsecurity(inode, fattr, label); 1112aa9c2669SDavid Quigley 1113e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1114e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 111514c43f76SDavid Quigley nfs4_label_free(label); 111614c43f76SDavid Quigley 111715860ab1STrond Myklebust out_set_verifier: 1118cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11191da177e4SLinus Torvalds out_valid: 1120d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1121d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1122d69ee9b8STrond Myklebust out_valid_noent: 11231da177e4SLinus Torvalds dput(parent); 11241e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 11253110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11261e7cb3dcSChuck Lever dentry->d_name.name); 11271da177e4SLinus Torvalds return 1; 11281da177e4SLinus Torvalds out_zap_parent: 11291da177e4SLinus Torvalds nfs_zap_caches(dir); 11301da177e4SLinus Torvalds out_bad: 1131c44600c9SAl Viro nfs_free_fattr(fattr); 1132c44600c9SAl Viro nfs_free_fhandle(fhandle); 113314c43f76SDavid Quigley nfs4_label_free(label); 1134a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11351da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11361da177e4SLinus Torvalds /* Purge readdir caches. */ 11371da177e4SLinus Torvalds nfs_zap_caches(inode); 11381da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 11391da177e4SLinus Torvalds if (have_submounts(dentry)) 11401da177e4SLinus Torvalds goto out_valid; 1141d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 1142d9e80b7dSAl Viro goto out_valid; 11431da177e4SLinus Torvalds shrink_dcache_parent(dentry); 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds d_drop(dentry); 11461da177e4SLinus Torvalds dput(parent); 11471e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 11483110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11491e7cb3dcSChuck Lever dentry->d_name.name); 11501da177e4SLinus Torvalds return 0; 1151e1fb4d05STrond Myklebust out_error: 1152e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1153e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 115414c43f76SDavid Quigley nfs4_label_free(label); 1155e1fb4d05STrond Myklebust dput(parent); 1156e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 1157e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 1158e1fb4d05STrond Myklebust dentry->d_name.name, error); 1159e1fb4d05STrond Myklebust return error; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds /* 1163ecf3d1f1SJeff Layton * A weaker form of d_revalidate for revalidating just the dentry->d_inode 1164ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a 1165ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the 1166ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1167ecf3d1f1SJeff Layton * 1168ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK 1169ecf3d1f1SJeff Layton * since the dentry might have changed on the server. 1170ecf3d1f1SJeff Layton */ 1171ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1172ecf3d1f1SJeff Layton { 1173ecf3d1f1SJeff Layton int error; 1174ecf3d1f1SJeff Layton struct inode *inode = dentry->d_inode; 1175ecf3d1f1SJeff Layton 1176ecf3d1f1SJeff Layton /* 1177ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a 1178ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may 1179ecf3d1f1SJeff Layton * eventually need to do something more here. 1180ecf3d1f1SJeff Layton */ 1181ecf3d1f1SJeff Layton if (!inode) { 1182ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "%s: %s/%s has negative inode\n", 1183ecf3d1f1SJeff Layton __func__, dentry->d_parent->d_name.name, 1184ecf3d1f1SJeff Layton dentry->d_name.name); 1185ecf3d1f1SJeff Layton return 1; 1186ecf3d1f1SJeff Layton } 1187ecf3d1f1SJeff Layton 1188ecf3d1f1SJeff Layton if (is_bad_inode(inode)) { 1189ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 1190ecf3d1f1SJeff Layton __func__, dentry->d_parent->d_name.name, 1191ecf3d1f1SJeff Layton dentry->d_name.name); 1192ecf3d1f1SJeff Layton return 0; 1193ecf3d1f1SJeff Layton } 1194ecf3d1f1SJeff Layton 1195ecf3d1f1SJeff Layton error = nfs_revalidate_inode(NFS_SERVER(inode), inode); 1196ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1197ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid"); 1198ecf3d1f1SJeff Layton return !error; 1199ecf3d1f1SJeff Layton } 1200ecf3d1f1SJeff Layton 1201ecf3d1f1SJeff Layton /* 12021da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 12031da177e4SLinus Torvalds */ 1204fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 12071da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 12081da177e4SLinus Torvalds dentry->d_flags); 12091da177e4SLinus Torvalds 121077f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 121177f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 121277f11192STrond Myklebust return 1; 121377f11192STrond Myklebust 12141da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12151da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 12161da177e4SLinus Torvalds return 1; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 12191da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 12201da177e4SLinus Torvalds * files will be cleaned up during umount */ 12211da177e4SLinus Torvalds return 1; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds return 0; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12271f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */ 12281b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 12291b83d707STrond Myklebust { 12301b83d707STrond Myklebust spin_lock(&inode->i_lock); 12311f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */ 12321f018458STrond Myklebust if (inode->i_nlink == 1) 12331f018458STrond Myklebust clear_nlink(inode); 12341f018458STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; 12351b83d707STrond Myklebust spin_unlock(&inode->i_lock); 12361b83d707STrond Myklebust } 12371b83d707STrond Myklebust 12381da177e4SLinus Torvalds /* 12391da177e4SLinus Torvalds * Called when the dentry loses inode. 12401da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 12411da177e4SLinus Torvalds */ 12421da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 12431da177e4SLinus Torvalds { 124483672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 124583672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 124683672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 124783672d39SNeil Brown 12481da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1249e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 12501f018458STrond Myklebust nfs_drop_nlink(inode); 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds iput(inode); 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 1255b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1256b1942c5fSAl Viro { 1257b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1258b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1259b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1260b1942c5fSAl Viro WARN_ON(1); 1261b1942c5fSAl Viro else 1262b1942c5fSAl Viro kfree(dentry->d_fsdata); 1263b1942c5fSAl Viro } 1264b1942c5fSAl Viro } 1265b1942c5fSAl Viro 1266f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 12671da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 1268ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate, 12691da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 12701da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 127136d43a43SDavid Howells .d_automount = nfs_d_automount, 1272b1942c5fSAl Viro .d_release = nfs_d_release, 12731da177e4SLinus Torvalds }; 1274ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations); 12751da177e4SLinus Torvalds 1276597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 12771da177e4SLinus Torvalds { 12781da177e4SLinus Torvalds struct dentry *res; 1279565277f6STrond Myklebust struct dentry *parent; 12801da177e4SLinus Torvalds struct inode *inode = NULL; 1281e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1282e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 12831775fd3eSDavid Quigley struct nfs4_label *label = NULL; 12841da177e4SLinus Torvalds int error; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 12871da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 128891d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 12911da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 12921da177e4SLinus Torvalds goto out; 12931da177e4SLinus Torvalds 1294fd684071STrond Myklebust /* 1295fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1296fd684071STrond Myklebust * but don't hash the dentry. 1297fd684071STrond Myklebust */ 129800cd8dd3SAl Viro if (nfs_is_exclusive_create(dir, flags)) { 1299fd684071STrond Myklebust d_instantiate(dentry, NULL); 1300fd684071STrond Myklebust res = NULL; 1301fc0f684cSTrond Myklebust goto out; 1302fd684071STrond Myklebust } 13031da177e4SLinus Torvalds 1304e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1305e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1306e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1307e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1308e1fb4d05STrond Myklebust goto out; 1309e1fb4d05STrond Myklebust 131014c43f76SDavid Quigley label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 131114c43f76SDavid Quigley if (IS_ERR(label)) 131214c43f76SDavid Quigley goto out; 131314c43f76SDavid Quigley 1314565277f6STrond Myklebust parent = dentry->d_parent; 1315565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 1316565277f6STrond Myklebust nfs_block_sillyrename(parent); 13171775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label); 13181da177e4SLinus Torvalds if (error == -ENOENT) 13191da177e4SLinus Torvalds goto no_entry; 13201da177e4SLinus Torvalds if (error < 0) { 13211da177e4SLinus Torvalds res = ERR_PTR(error); 1322565277f6STrond Myklebust goto out_unblock_sillyrename; 13231da177e4SLinus Torvalds } 13241775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1325bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 132603f28e3aSTrond Myklebust if (IS_ERR(res)) 1327565277f6STrond Myklebust goto out_unblock_sillyrename; 132854ceac45SDavid Howells 1329d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1330d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1331d69ee9b8STrond Myklebust 13321da177e4SLinus Torvalds no_entry: 133354ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 13349eaef27bSTrond Myklebust if (res != NULL) { 13359eaef27bSTrond Myklebust if (IS_ERR(res)) 1336565277f6STrond Myklebust goto out_unblock_sillyrename; 13371da177e4SLinus Torvalds dentry = res; 13389eaef27bSTrond Myklebust } 13391da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1340565277f6STrond Myklebust out_unblock_sillyrename: 1341565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 134214c43f76SDavid Quigley nfs4_label_free(label); 13431da177e4SLinus Torvalds out: 1344e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1345e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 13461da177e4SLinus Torvalds return res; 13471da177e4SLinus Torvalds } 1348ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup); 13491da177e4SLinus Torvalds 135089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 13510b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 13521da177e4SLinus Torvalds 1353f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 13540ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate, 13551da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13561da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 135736d43a43SDavid Howells .d_automount = nfs_d_automount, 1358b1942c5fSAl Viro .d_release = nfs_d_release, 13591da177e4SLinus Torvalds }; 136089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 13611da177e4SLinus Torvalds 13628a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 13638a5e929dSAl Viro { 13648a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 13658a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 13668a5e929dSAl Viro res |= FMODE_READ; 13678a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 13688a5e929dSAl Viro res |= FMODE_WRITE; 13698a5e929dSAl Viro return res; 13708a5e929dSAl Viro } 13718a5e929dSAl Viro 137251141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags) 1373cd9a1c0eSTrond Myklebust { 13745ede7b1cSAl Viro return alloc_nfs_open_context(dentry, flags_to_mode(open_flags)); 1375cd9a1c0eSTrond Myklebust } 1376cd9a1c0eSTrond Myklebust 1377cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1378cd9a1c0eSTrond Myklebust { 1379cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1380cd9a1c0eSTrond Myklebust return 0; 1381cd9a1c0eSTrond Myklebust } 1382cd9a1c0eSTrond Myklebust 1383d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx, 13840dd2b474SMiklos Szeredi struct dentry *dentry, 138530d90494SAl Viro struct file *file, unsigned open_flags, 138647237687SAl Viro int *opened) 1387cd9a1c0eSTrond Myklebust { 13880dd2b474SMiklos Szeredi int err; 13890dd2b474SMiklos Szeredi 139030d90494SAl Viro err = finish_open(file, dentry, do_open, opened); 139130d90494SAl Viro if (err) 1392d9585277SAl Viro goto out; 139330d90494SAl Viro nfs_file_set_open_context(file, ctx); 13940dd2b474SMiklos Szeredi 1395cd9a1c0eSTrond Myklebust out: 1396cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1397d9585277SAl Viro return err; 1398cd9a1c0eSTrond Myklebust } 1399cd9a1c0eSTrond Myklebust 140073a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 140130d90494SAl Viro struct file *file, unsigned open_flags, 140247237687SAl Viro umode_t mode, int *opened) 14031da177e4SLinus Torvalds { 1404cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 14050dd2b474SMiklos Szeredi struct dentry *res; 14060dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN }; 1407f46e0bd3STrond Myklebust struct inode *inode; 1408898f635cSTrond Myklebust int err; 14091da177e4SLinus Torvalds 14100dd2b474SMiklos Szeredi /* Expect a negative dentry */ 14110dd2b474SMiklos Szeredi BUG_ON(dentry->d_inode); 14120dd2b474SMiklos Szeredi 14130dd2b474SMiklos Szeredi dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n", 14141e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14151e7cb3dcSChuck Lever 14160dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */ 14170dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) { 14180dd2b474SMiklos Szeredi if (!d_unhashed(dentry)) { 14190dd2b474SMiklos Szeredi /* 14200dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was 14210dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup 14220dd2b474SMiklos Szeredi * again 14230dd2b474SMiklos Szeredi */ 1424d9585277SAl Viro return -ENOENT; 14250dd2b474SMiklos Szeredi } 14261da177e4SLinus Torvalds goto no_open; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14290dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1430d9585277SAl Viro return -ENAMETOOLONG; 14311da177e4SLinus Torvalds 14320dd2b474SMiklos Szeredi if (open_flags & O_CREAT) { 1433536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 14340dd2b474SMiklos Szeredi attr.ia_mode = mode & ~current_umask(); 14350dd2b474SMiklos Szeredi } 1436536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1437536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1438536e43d1STrond Myklebust attr.ia_size = 0; 1439cd9a1c0eSTrond Myklebust } 1440cd9a1c0eSTrond Myklebust 14410dd2b474SMiklos Szeredi ctx = create_nfs_open_context(dentry, open_flags); 14420dd2b474SMiklos Szeredi err = PTR_ERR(ctx); 14430dd2b474SMiklos Szeredi if (IS_ERR(ctx)) 1444d9585277SAl Viro goto out; 14450dd2b474SMiklos Szeredi 1446f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 14472b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr); 1448f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1449275bb307STrond Myklebust if (IS_ERR(inode)) { 1450cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 14510dd2b474SMiklos Szeredi err = PTR_ERR(inode); 14520dd2b474SMiklos Szeredi switch (err) { 14531da177e4SLinus Torvalds case -ENOENT: 1454275bb307STrond Myklebust d_drop(dentry); 1455f46e0bd3STrond Myklebust d_add(dentry, NULL); 14560dd2b474SMiklos Szeredi break; 14571788ea6eSJeff Layton case -EISDIR: 14586f926b5bSTrond Myklebust case -ENOTDIR: 14596f926b5bSTrond Myklebust goto no_open; 14601da177e4SLinus Torvalds case -ELOOP: 14610dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW)) 14621da177e4SLinus Torvalds goto no_open; 14630dd2b474SMiklos Szeredi break; 14641da177e4SLinus Torvalds /* case -EINVAL: */ 14651da177e4SLinus Torvalds default: 14660dd2b474SMiklos Szeredi break; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds goto out; 14691da177e4SLinus Torvalds } 14700dd2b474SMiklos Szeredi 1471275bb307STrond Myklebust err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened); 1472d9585277SAl Viro out: 1473d9585277SAl Viro return err; 14740dd2b474SMiklos Szeredi 14751da177e4SLinus Torvalds no_open: 147600cd8dd3SAl Viro res = nfs_lookup(dir, dentry, 0); 14770dd2b474SMiklos Szeredi err = PTR_ERR(res); 14780dd2b474SMiklos Szeredi if (IS_ERR(res)) 1479d9585277SAl Viro goto out; 14800dd2b474SMiklos Szeredi 1481e45198a6SAl Viro return finish_no_open(file, res); 14821da177e4SLinus Torvalds } 148389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open); 14841da177e4SLinus Torvalds 14850b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 14861da177e4SLinus Torvalds { 14871da177e4SLinus Torvalds struct dentry *parent = NULL; 1488657e94b6SNick Piggin struct inode *inode; 14891da177e4SLinus Torvalds struct inode *dir; 149050de348cSMiklos Szeredi int ret = 0; 14911da177e4SLinus Torvalds 1492fa3c56bbSAl Viro if (flags & LOOKUP_RCU) 1493657e94b6SNick Piggin return -ECHILD; 1494657e94b6SNick Piggin 1495fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 1496eda72afbSMiklos Szeredi goto no_open; 1497eda72afbSMiklos Szeredi if (d_mountpoint(dentry)) 14985584c306STrond Myklebust goto no_open; 149949f9a0faSTrond Myklebust if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1) 150049f9a0faSTrond Myklebust goto no_open; 15012b484297STrond Myklebust 1502eda72afbSMiklos Szeredi inode = dentry->d_inode; 15031da177e4SLinus Torvalds parent = dget_parent(dentry); 15041da177e4SLinus Torvalds dir = parent->d_inode; 15052b484297STrond Myklebust 15061da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 15071da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 15081da177e4SLinus Torvalds */ 1509216d5d06STrond Myklebust if (inode == NULL) { 1510fa3c56bbSAl Viro if (!nfs_neg_need_reval(dir, dentry, flags)) 1511216d5d06STrond Myklebust ret = 1; 15121da177e4SLinus Torvalds goto out; 1513216d5d06STrond Myklebust } 1514216d5d06STrond Myklebust 15151da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 15161da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 15175584c306STrond Myklebust goto no_open_dput; 15181da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 1519fa3c56bbSAl Viro if (flags & LOOKUP_EXCL) 15205584c306STrond Myklebust goto no_open_dput; 15211da177e4SLinus Torvalds 15220ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */ 1523898f635cSTrond Myklebust ret = 1; 15240ef97dcfSMiklos Szeredi 15251da177e4SLinus Torvalds out: 15261da177e4SLinus Torvalds dput(parent); 15271da177e4SLinus Torvalds return ret; 1528535918f1STrond Myklebust 15295584c306STrond Myklebust no_open_dput: 15301da177e4SLinus Torvalds dput(parent); 15315584c306STrond Myklebust no_open: 15320b728e19SAl Viro return nfs_lookup_revalidate(dentry, flags); 1533c0204fd2STrond Myklebust } 1534c0204fd2STrond Myklebust 15351da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds /* 15381da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 15391da177e4SLinus Torvalds */ 15401da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 15411775fd3eSDavid Quigley struct nfs_fattr *fattr, 15421775fd3eSDavid Quigley struct nfs4_label *label) 15431da177e4SLinus Torvalds { 1544fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1545fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 15461da177e4SLinus Torvalds struct inode *inode; 15471da177e4SLinus Torvalds int error = -EACCES; 15481da177e4SLinus Torvalds 1549fab728e1STrond Myklebust d_drop(dentry); 1550fab728e1STrond Myklebust 15511da177e4SLinus Torvalds /* We may have been initialized further down */ 15521da177e4SLinus Torvalds if (dentry->d_inode) 1553fab728e1STrond Myklebust goto out; 15541da177e4SLinus Torvalds if (fhandle->size == 0) { 15551775fd3eSDavid Quigley error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL); 15561da177e4SLinus Torvalds if (error) 1557fab728e1STrond Myklebust goto out_error; 15581da177e4SLinus Torvalds } 15595724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15601da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 15611da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 15621775fd3eSDavid Quigley error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL); 15631da177e4SLinus Torvalds if (error < 0) 1564fab728e1STrond Myklebust goto out_error; 15651da177e4SLinus Torvalds } 15661775fd3eSDavid Quigley inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 156703f28e3aSTrond Myklebust error = PTR_ERR(inode); 156803f28e3aSTrond Myklebust if (IS_ERR(inode)) 1569fab728e1STrond Myklebust goto out_error; 1570fab728e1STrond Myklebust d_add(dentry, inode); 1571fab728e1STrond Myklebust out: 1572fab728e1STrond Myklebust dput(parent); 15731da177e4SLinus Torvalds return 0; 1574fab728e1STrond Myklebust out_error: 1575fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1576fab728e1STrond Myklebust dput(parent); 1577fab728e1STrond Myklebust return error; 15781da177e4SLinus Torvalds } 1579ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate); 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* 15821da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 15831da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 15841da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 15851da177e4SLinus Torvalds * reply path made it appear to have failed. 15861da177e4SLinus Torvalds */ 1587597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry, 1588ebfc3b49SAl Viro umode_t mode, bool excl) 15891da177e4SLinus Torvalds { 15901da177e4SLinus Torvalds struct iattr attr; 1591ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 15921da177e4SLinus Torvalds int error; 15931da177e4SLinus Torvalds 15941e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 15951e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds attr.ia_mode = mode; 15981da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 15991da177e4SLinus Torvalds 16008867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 16011da177e4SLinus Torvalds if (error != 0) 16021da177e4SLinus Torvalds goto out_err; 16031da177e4SLinus Torvalds return 0; 16041da177e4SLinus Torvalds out_err: 16051da177e4SLinus Torvalds d_drop(dentry); 16061da177e4SLinus Torvalds return error; 16071da177e4SLinus Torvalds } 1608ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create); 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds /* 16111da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 16121da177e4SLinus Torvalds */ 1613597d9289SBryan Schumaker int 16141a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds struct iattr attr; 16171da177e4SLinus Torvalds int status; 16181da177e4SLinus Torvalds 16191e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 16201e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 16231da177e4SLinus Torvalds return -EINVAL; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds attr.ia_mode = mode; 16261da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 16291da177e4SLinus Torvalds if (status != 0) 16301da177e4SLinus Torvalds goto out_err; 16311da177e4SLinus Torvalds return 0; 16321da177e4SLinus Torvalds out_err: 16331da177e4SLinus Torvalds d_drop(dentry); 16341da177e4SLinus Torvalds return status; 16351da177e4SLinus Torvalds } 1636ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod); 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds /* 16391da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 16401da177e4SLinus Torvalds */ 1641597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 16421da177e4SLinus Torvalds { 16431da177e4SLinus Torvalds struct iattr attr; 16441da177e4SLinus Torvalds int error; 16451da177e4SLinus Torvalds 16461e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 16471e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16501da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 16531da177e4SLinus Torvalds if (error != 0) 16541da177e4SLinus Torvalds goto out_err; 16551da177e4SLinus Torvalds return 0; 16561da177e4SLinus Torvalds out_err: 16571da177e4SLinus Torvalds d_drop(dentry); 16581da177e4SLinus Torvalds return error; 16591da177e4SLinus Torvalds } 1660ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir); 16611da177e4SLinus Torvalds 1662d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1663d45b9d8bSTrond Myklebust { 1664d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1665d45b9d8bSTrond Myklebust d_delete(dentry); 1666d45b9d8bSTrond Myklebust } 1667d45b9d8bSTrond Myklebust 1668597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds int error; 16711da177e4SLinus Torvalds 16721e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 16731e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 16761da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 16771da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1678ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1679d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1680d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds return error; 16831da177e4SLinus Torvalds } 1684ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir); 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 16881da177e4SLinus Torvalds * and after checking that the file has only one user. 16891da177e4SLinus Torvalds * 16901da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 16911da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 16921da177e4SLinus Torvalds */ 16931da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 16941da177e4SLinus Torvalds { 16951da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 16961da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16971da177e4SLinus Torvalds int error = -EBUSY; 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 17001da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 17031da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 17041da177e4SLinus Torvalds error = 0; 17051da177e4SLinus Torvalds goto out; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds if (inode != NULL) { 170957ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 17101da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 17111da177e4SLinus Torvalds if (error == 0) 17121b83d707STrond Myklebust nfs_drop_nlink(inode); 17131da177e4SLinus Torvalds } else 17141da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1715d45b9d8bSTrond Myklebust if (error == -ENOENT) 1716d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 17171da177e4SLinus Torvalds out: 17181da177e4SLinus Torvalds return error; 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 17221da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 17231da177e4SLinus Torvalds * 17241da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 17251da177e4SLinus Torvalds */ 1726597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry) 17271da177e4SLinus Torvalds { 17281da177e4SLinus Torvalds int error; 17291da177e4SLinus Torvalds int need_rehash = 0; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 17321da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 173584d08fa8SAl Viro if (d_count(dentry) > 1) { 17361da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1737ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1738ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 17391da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 17401da177e4SLinus Torvalds return error; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 17431da177e4SLinus Torvalds __d_drop(dentry); 17441da177e4SLinus Torvalds need_rehash = 1; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 17471da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1748d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 17491da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17501da177e4SLinus Torvalds } else if (need_rehash) 17511da177e4SLinus Torvalds d_rehash(dentry); 17521da177e4SLinus Torvalds return error; 17531da177e4SLinus Torvalds } 1754ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink); 17551da177e4SLinus Torvalds 1756873101b3SChuck Lever /* 1757873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1758873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1759873101b3SChuck Lever * using prepare_write/commit_write. 1760873101b3SChuck Lever * 1761873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1762873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1763873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1764873101b3SChuck Lever * symlink request has completed on the server. 1765873101b3SChuck Lever * 1766873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1767873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1768873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1769873101b3SChuck Lever * and move the raw page into its mapping. 1770873101b3SChuck Lever */ 1771597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 17721da177e4SLinus Torvalds { 1773873101b3SChuck Lever struct page *page; 1774873101b3SChuck Lever char *kaddr; 17751da177e4SLinus Torvalds struct iattr attr; 1776873101b3SChuck Lever unsigned int pathlen = strlen(symname); 17771da177e4SLinus Torvalds int error; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 17801da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 17811da177e4SLinus Torvalds 1782873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1783873101b3SChuck Lever return -ENAMETOOLONG; 17841da177e4SLinus Torvalds 1785873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1786873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 17871da177e4SLinus Torvalds 178883d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 178976566991STrond Myklebust if (!page) 1790873101b3SChuck Lever return -ENOMEM; 1791873101b3SChuck Lever 17922b86ce2dSCong Wang kaddr = kmap_atomic(page); 1793873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1794873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1795873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 17962b86ce2dSCong Wang kunmap_atomic(kaddr); 1797873101b3SChuck Lever 179894a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1799873101b3SChuck Lever if (error != 0) { 1800873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1801873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1802873101b3SChuck Lever dentry->d_name.name, symname, error); 18031da177e4SLinus Torvalds d_drop(dentry); 1804873101b3SChuck Lever __free_page(page); 18051da177e4SLinus Torvalds return error; 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 1808873101b3SChuck Lever /* 1809873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1810873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1811873101b3SChuck Lever */ 1812a0b8cab3SMel Gorman if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0, 1813873101b3SChuck Lever GFP_KERNEL)) { 1814873101b3SChuck Lever SetPageUptodate(page); 1815873101b3SChuck Lever unlock_page(page); 1816873101b3SChuck Lever } else 1817873101b3SChuck Lever __free_page(page); 1818873101b3SChuck Lever 1819873101b3SChuck Lever return 0; 1820873101b3SChuck Lever } 1821ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink); 1822873101b3SChuck Lever 1823597d9289SBryan Schumaker int 18241da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 18251da177e4SLinus Torvalds { 18261da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 18271da177e4SLinus Torvalds int error; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 18301da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18311da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 18321da177e4SLinus Torvalds 183357ec14c5SBryan Schumaker NFS_PROTO(inode)->return_delegation(inode); 18349a3936aaSTrond Myklebust 18359697d234STrond Myklebust d_drop(dentry); 18361da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1837cf809556STrond Myklebust if (error == 0) { 18387de9c6eeSAl Viro ihold(inode); 18399697d234STrond Myklebust d_add(dentry, inode); 1840cf809556STrond Myklebust } 18411da177e4SLinus Torvalds return error; 18421da177e4SLinus Torvalds } 1843ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link); 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds /* 18461da177e4SLinus Torvalds * RENAME 18471da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 18481da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 18491da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 18501da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 18511da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 18521da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 18531da177e4SLinus Torvalds * 18541da177e4SLinus Torvalds * FIXED. 18551da177e4SLinus Torvalds * 18561da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 18571da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 18581da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 18591da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 18601da177e4SLinus Torvalds * using the inode layer 18611da177e4SLinus Torvalds * 18621da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 18631da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 18641da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 18651da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 18661da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 18671da177e4SLinus Torvalds * the rename. 18681da177e4SLinus Torvalds */ 1869597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 18701da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 18711da177e4SLinus Torvalds { 18721da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 18731da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 18741da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 18751da177e4SLinus Torvalds int error = -EBUSY; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 18781da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18791da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 188084d08fa8SAl Viro d_count(new_dentry)); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* 188328f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 188428f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 188528f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 188628f79a1aSMiklos Szeredi * the new target. 18871da177e4SLinus Torvalds */ 188827226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 188927226104SMiklos Szeredi /* 189027226104SMiklos Szeredi * To prevent any new references to the target during the 189127226104SMiklos Szeredi * rename, we unhash the dentry in advance. 189227226104SMiklos Szeredi */ 189327226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 189427226104SMiklos Szeredi d_drop(new_dentry); 189527226104SMiklos Szeredi rehash = new_dentry; 189627226104SMiklos Szeredi } 189727226104SMiklos Szeredi 189884d08fa8SAl Viro if (d_count(new_dentry) > 2) { 18991da177e4SLinus Torvalds int err; 190027226104SMiklos Szeredi 19011da177e4SLinus Torvalds /* copy the target dentry's name */ 19021da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 19031da177e4SLinus Torvalds &new_dentry->d_name); 19041da177e4SLinus Torvalds if (!dentry) 19051da177e4SLinus Torvalds goto out; 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds /* silly-rename the existing target ... */ 19081da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 190924e93025SMiklos Szeredi if (err) 19101da177e4SLinus Torvalds goto out; 191124e93025SMiklos Szeredi 191224e93025SMiklos Szeredi new_dentry = dentry; 191356335936SOGAWA Hirofumi rehash = NULL; 191424e93025SMiklos Szeredi new_inode = NULL; 1915b1e4adf4STrond Myklebust } 191627226104SMiklos Szeredi } 19171da177e4SLinus Torvalds 191857ec14c5SBryan Schumaker NFS_PROTO(old_inode)->return_delegation(old_inode); 1919b1e4adf4STrond Myklebust if (new_inode != NULL) 192057ec14c5SBryan Schumaker NFS_PROTO(new_inode)->return_delegation(new_inode); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 19231da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 19245ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 19251da177e4SLinus Torvalds out: 19261da177e4SLinus Torvalds if (rehash) 19271da177e4SLinus Torvalds d_rehash(rehash); 19281da177e4SLinus Torvalds if (!error) { 1929b1e4adf4STrond Myklebust if (new_inode != NULL) 1930b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 19311da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 19328fb559f8SChuck Lever nfs_set_verifier(new_dentry, 19338fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1934d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 1935d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds /* new dentry created? */ 19381da177e4SLinus Torvalds if (dentry) 19391da177e4SLinus Torvalds dput(dentry); 19401da177e4SLinus Torvalds return error; 19411da177e4SLinus Torvalds } 1942ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename); 19431da177e4SLinus Torvalds 1944cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 1945cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 1946cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 1947cfcea3e8STrond Myklebust 19481c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 19491c3c07e9STrond Myklebust { 19501c3c07e9STrond Myklebust put_rpccred(entry->cred); 19511c3c07e9STrond Myklebust kfree(entry); 1952cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 1953cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 1954cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 19551c3c07e9STrond Myklebust } 19561c3c07e9STrond Myklebust 19571a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 19581a81bb8aSTrond Myklebust { 19591a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 19601a81bb8aSTrond Myklebust 19611a81bb8aSTrond Myklebust while (!list_empty(head)) { 19621a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 19631a81bb8aSTrond Myklebust list_del(&cache->lru); 19641a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 19651a81bb8aSTrond Myklebust } 19661a81bb8aSTrond Myklebust } 19671a81bb8aSTrond Myklebust 19681495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink, 19691495f230SYing Han struct shrink_control *sc) 1970979df72eSTrond Myklebust { 1971979df72eSTrond Myklebust LIST_HEAD(head); 1972aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 1973979df72eSTrond Myklebust struct nfs_access_entry *cache; 19741495f230SYing Han int nr_to_scan = sc->nr_to_scan; 19751495f230SYing Han gfp_t gfp_mask = sc->gfp_mask; 1976979df72eSTrond Myklebust 197761d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 197861d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 19799c7e7e23STrond Myklebust 1980a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 1981aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 1982979df72eSTrond Myklebust struct inode *inode; 1983979df72eSTrond Myklebust 1984979df72eSTrond Myklebust if (nr_to_scan-- == 0) 1985979df72eSTrond Myklebust break; 19869c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 1987979df72eSTrond Myklebust spin_lock(&inode->i_lock); 1988979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 1989979df72eSTrond Myklebust goto remove_lru_entry; 1990979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 1991979df72eSTrond Myklebust struct nfs_access_entry, lru); 1992979df72eSTrond Myklebust list_move(&cache->lru, &head); 1993979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1994979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 1995979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 1996979df72eSTrond Myklebust &nfs_access_lru_list); 1997979df72eSTrond Myklebust else { 1998979df72eSTrond Myklebust remove_lru_entry: 1999979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 20009c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 2001979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 20029c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 2003979df72eSTrond Myklebust } 200459844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2005979df72eSTrond Myklebust } 2006979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 20071a81bb8aSTrond Myklebust nfs_access_free_list(&head); 2008979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 2009979df72eSTrond Myklebust } 2010979df72eSTrond Myklebust 20111a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 20121c3c07e9STrond Myklebust { 20131c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 20141a81bb8aSTrond Myklebust struct rb_node *n; 20151c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20161c3c07e9STrond Myklebust 20171c3c07e9STrond Myklebust /* Unhook entries from the cache */ 20181c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 20191c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20201c3c07e9STrond Myklebust rb_erase(n, root_node); 20211a81bb8aSTrond Myklebust list_move(&entry->lru, head); 20221c3c07e9STrond Myklebust } 20231c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 20241c3c07e9STrond Myklebust } 20251c3c07e9STrond Myklebust 20261c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 20271c3c07e9STrond Myklebust { 20281a81bb8aSTrond Myklebust LIST_HEAD(head); 20291a81bb8aSTrond Myklebust 20301a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 20311a81bb8aSTrond Myklebust return; 2032cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2033cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 20341a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2035cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2036cfcea3e8STrond Myklebust 20371c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20381a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 20391a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 20401a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 20411a81bb8aSTrond Myklebust nfs_access_free_list(&head); 20421c3c07e9STrond Myklebust } 20431c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 20441c3c07e9STrond Myklebust 20451c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 20461c3c07e9STrond Myklebust { 20471c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 20481c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20491c3c07e9STrond Myklebust 20501c3c07e9STrond Myklebust while (n != NULL) { 20511c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20521c3c07e9STrond Myklebust 20531c3c07e9STrond Myklebust if (cred < entry->cred) 20541c3c07e9STrond Myklebust n = n->rb_left; 20551c3c07e9STrond Myklebust else if (cred > entry->cred) 20561c3c07e9STrond Myklebust n = n->rb_right; 20571c3c07e9STrond Myklebust else 20581c3c07e9STrond Myklebust return entry; 20591c3c07e9STrond Myklebust } 20601c3c07e9STrond Myklebust return NULL; 20611c3c07e9STrond Myklebust } 20621c3c07e9STrond Myklebust 2063af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 20641da177e4SLinus Torvalds { 206555296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 20661c3c07e9STrond Myklebust struct nfs_access_entry *cache; 20671c3c07e9STrond Myklebust int err = -ENOENT; 20681da177e4SLinus Torvalds 20691c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20701c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 20711c3c07e9STrond Myklebust goto out_zap; 20721c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 20731c3c07e9STrond Myklebust if (cache == NULL) 20741c3c07e9STrond Myklebust goto out; 2075b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 207664672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 20771c3c07e9STrond Myklebust goto out_stale; 20781c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 20791c3c07e9STrond Myklebust res->cred = cache->cred; 20801c3c07e9STrond Myklebust res->mask = cache->mask; 2081cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 20821c3c07e9STrond Myklebust err = 0; 20831c3c07e9STrond Myklebust out: 20841c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20851c3c07e9STrond Myklebust return err; 20861c3c07e9STrond Myklebust out_stale: 20871c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2088cfcea3e8STrond Myklebust list_del(&cache->lru); 20891c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20901c3c07e9STrond Myklebust nfs_access_free_entry(cache); 20911da177e4SLinus Torvalds return -ENOENT; 20921c3c07e9STrond Myklebust out_zap: 20931a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 20941a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 20951c3c07e9STrond Myklebust return -ENOENT; 20961c3c07e9STrond Myklebust } 20971c3c07e9STrond Myklebust 20981c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 20991c3c07e9STrond Myklebust { 2100cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2101cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 21021c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 21031c3c07e9STrond Myklebust struct rb_node *parent = NULL; 21041c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21051c3c07e9STrond Myklebust 21061c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 21071c3c07e9STrond Myklebust while (*p != NULL) { 21081c3c07e9STrond Myklebust parent = *p; 21091c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 21101c3c07e9STrond Myklebust 21111c3c07e9STrond Myklebust if (set->cred < entry->cred) 21121c3c07e9STrond Myklebust p = &parent->rb_left; 21131c3c07e9STrond Myklebust else if (set->cred > entry->cred) 21141c3c07e9STrond Myklebust p = &parent->rb_right; 21151c3c07e9STrond Myklebust else 21161c3c07e9STrond Myklebust goto found; 21171c3c07e9STrond Myklebust } 21181c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 21191c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2120cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 21211c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21221c3c07e9STrond Myklebust return; 21231c3c07e9STrond Myklebust found: 21241c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2125cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2126cfcea3e8STrond Myklebust list_del(&entry->lru); 21271c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21281c3c07e9STrond Myklebust nfs_access_free_entry(entry); 21291da177e4SLinus Torvalds } 21301da177e4SLinus Torvalds 21316168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 21321da177e4SLinus Torvalds { 21331c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 21341c3c07e9STrond Myklebust if (cache == NULL) 21351c3c07e9STrond Myklebust return; 21361c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 21371da177e4SLinus Torvalds cache->jiffies = set->jiffies; 21381c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 21391da177e4SLinus Torvalds cache->mask = set->mask; 21401c3c07e9STrond Myklebust 21411c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2142cfcea3e8STrond Myklebust 2143cfcea3e8STrond Myklebust /* Update accounting */ 2144cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 2145cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 2146cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 2147cfcea3e8STrond Myklebust 2148cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 21491a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2150cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 21511a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 21521a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 21531a81bb8aSTrond Myklebust &nfs_access_lru_list); 2154cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2155cfcea3e8STrond Myklebust } 21561da177e4SLinus Torvalds } 21576168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache); 21586168f62cSWeston Andros Adamson 21596168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 21606168f62cSWeston Andros Adamson { 21616168f62cSWeston Andros Adamson entry->mask = 0; 21626168f62cSWeston Andros Adamson if (access_result & NFS4_ACCESS_READ) 21636168f62cSWeston Andros Adamson entry->mask |= MAY_READ; 21646168f62cSWeston Andros Adamson if (access_result & 21656168f62cSWeston Andros Adamson (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) 21666168f62cSWeston Andros Adamson entry->mask |= MAY_WRITE; 21676168f62cSWeston Andros Adamson if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) 21686168f62cSWeston Andros Adamson entry->mask |= MAY_EXEC; 21696168f62cSWeston Andros Adamson } 21706168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask); 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 21731da177e4SLinus Torvalds { 21741da177e4SLinus Torvalds struct nfs_access_entry cache; 21751da177e4SLinus Torvalds int status; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 21781da177e4SLinus Torvalds if (status == 0) 21791da177e4SLinus Torvalds goto out; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 21821da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 21831da177e4SLinus Torvalds cache.cred = cred; 21841da177e4SLinus Torvalds cache.jiffies = jiffies; 21851da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2186a71ee337SSuresh Jayaraman if (status != 0) { 2187a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2188a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2189a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2190a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2191a71ee337SSuresh Jayaraman } 21921da177e4SLinus Torvalds return status; 2193a71ee337SSuresh Jayaraman } 21941da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 21951da177e4SLinus Torvalds out: 2196e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 21971da177e4SLinus Torvalds return 0; 21981da177e4SLinus Torvalds return -EACCES; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 2201af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2202af22f94aSTrond Myklebust { 2203af22f94aSTrond Myklebust int mask = 0; 2204af22f94aSTrond Myklebust 2205f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) { 2206f8d9a897SWeston Andros Adamson /* ONLY check exec rights */ 2207f8d9a897SWeston Andros Adamson mask = MAY_EXEC; 2208f8d9a897SWeston Andros Adamson } else { 22098a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2210af22f94aSTrond Myklebust mask |= MAY_READ; 22118a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2212af22f94aSTrond Myklebust mask |= MAY_WRITE; 2213f8d9a897SWeston Andros Adamson } 2214f8d9a897SWeston Andros Adamson 2215af22f94aSTrond Myklebust return mask; 2216af22f94aSTrond Myklebust } 2217af22f94aSTrond Myklebust 2218af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2219af22f94aSTrond Myklebust { 2220af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2221af22f94aSTrond Myklebust } 222289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open); 2223af22f94aSTrond Myklebust 222410556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 22251da177e4SLinus Torvalds { 22261da177e4SLinus Torvalds struct rpc_cred *cred; 22271da177e4SLinus Torvalds int res = 0; 22281da177e4SLinus Torvalds 222910556cb2SAl Viro if (mask & MAY_NOT_BLOCK) 2230b74c79e9SNick Piggin return -ECHILD; 2231b74c79e9SNick Piggin 223291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 223391d5b470SChuck Lever 2234e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 22351da177e4SLinus Torvalds goto out; 22361da177e4SLinus Torvalds /* Is this sys_access() ? */ 22379cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 22381da177e4SLinus Torvalds goto force_lookup; 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 22411da177e4SLinus Torvalds case S_IFLNK: 22421da177e4SLinus Torvalds goto out; 22431da177e4SLinus Torvalds case S_IFREG: 22441da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 22451da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 22467ee2cb7fSFrank Filz && (mask & MAY_OPEN) 22477ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 22481da177e4SLinus Torvalds goto out; 22491da177e4SLinus Torvalds break; 22501da177e4SLinus Torvalds case S_IFDIR: 22511da177e4SLinus Torvalds /* 22521da177e4SLinus Torvalds * Optimize away all write operations, since the server 22531da177e4SLinus Torvalds * will check permissions when we perform the op. 22541da177e4SLinus Torvalds */ 22551da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 22561da177e4SLinus Torvalds goto out; 22571da177e4SLinus Torvalds } 22581da177e4SLinus Torvalds 22591da177e4SLinus Torvalds force_lookup: 22601da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 22611da177e4SLinus Torvalds goto out_notsup; 22621da177e4SLinus Torvalds 226398a8e323STrond Myklebust cred = rpc_lookup_cred(); 22641da177e4SLinus Torvalds if (!IS_ERR(cred)) { 22651da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 22661da177e4SLinus Torvalds put_rpccred(cred); 22671da177e4SLinus Torvalds } else 22681da177e4SLinus Torvalds res = PTR_ERR(cred); 22691da177e4SLinus Torvalds out: 2270f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2271f696a365SMiklos Szeredi res = -EACCES; 2272f696a365SMiklos Szeredi 22731e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 22741e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 22751da177e4SLinus Torvalds return res; 22761da177e4SLinus Torvalds out_notsup: 22771da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 22781da177e4SLinus Torvalds if (res == 0) 22792830ba7fSAl Viro res = generic_permission(inode, mask); 22801e7cb3dcSChuck Lever goto out; 22811da177e4SLinus Torvalds } 2282ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission); 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* 22851da177e4SLinus Torvalds * Local variables: 22861da177e4SLinus Torvalds * version-control: t 22871da177e4SLinus Torvalds * kept-new-versions: 5 22881da177e4SLinus Torvalds * End: 22891da177e4SLinus Torvalds */ 2290