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 201da177e4SLinus Torvalds #include <linux/time.h> 211da177e4SLinus Torvalds #include <linux/errno.h> 221da177e4SLinus Torvalds #include <linux/stat.h> 231da177e4SLinus Torvalds #include <linux/fcntl.h> 241da177e4SLinus Torvalds #include <linux/string.h> 251da177e4SLinus Torvalds #include <linux/kernel.h> 261da177e4SLinus Torvalds #include <linux/slab.h> 271da177e4SLinus Torvalds #include <linux/mm.h> 281da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 291da177e4SLinus Torvalds #include <linux/nfs_fs.h> 301da177e4SLinus Torvalds #include <linux/nfs_mount.h> 311da177e4SLinus Torvalds #include <linux/pagemap.h> 32873101b3SChuck Lever #include <linux/pagevec.h> 331da177e4SLinus Torvalds #include <linux/namei.h> 3454ceac45SDavid Howells #include <linux/mount.h> 35e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 3604e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 3764c2ce8bSAneesh Kumar K.V #include <linux/xattr.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include "delegation.h" 4091d5b470SChuck Lever #include "iostat.h" 414c30d56eSAdrian Bunk #include "internal.h" 42cd9a1c0eSTrond Myklebust #include "fscache.h" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 47480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *); 481da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 491da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 501da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 511da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 521da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 531da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 541da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 551da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 561da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 571da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 581da177e4SLinus Torvalds struct inode *, struct dentry *); 597ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int); 60f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 6111de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 621da177e4SLinus Torvalds 634b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 64f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 651da177e4SLinus Torvalds .read = generic_read_dir, 661da177e4SLinus Torvalds .readdir = nfs_readdir, 671da177e4SLinus Torvalds .open = nfs_opendir, 68480c2006SBryan Schumaker .release = nfs_closedir, 691da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 701da177e4SLinus Torvalds }; 711da177e4SLinus Torvalds 7292e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = { 731da177e4SLinus Torvalds .create = nfs_create, 741da177e4SLinus Torvalds .lookup = nfs_lookup, 751da177e4SLinus Torvalds .link = nfs_link, 761da177e4SLinus Torvalds .unlink = nfs_unlink, 771da177e4SLinus Torvalds .symlink = nfs_symlink, 781da177e4SLinus Torvalds .mkdir = nfs_mkdir, 791da177e4SLinus Torvalds .rmdir = nfs_rmdir, 801da177e4SLinus Torvalds .mknod = nfs_mknod, 811da177e4SLinus Torvalds .rename = nfs_rename, 821da177e4SLinus Torvalds .permission = nfs_permission, 831da177e4SLinus Torvalds .getattr = nfs_getattr, 841da177e4SLinus Torvalds .setattr = nfs_setattr, 851da177e4SLinus Torvalds }; 861da177e4SLinus Torvalds 8711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 8811de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 89d1bacf9eSBryan Schumaker }; 90d1bacf9eSBryan Schumaker 91b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 9292e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = { 93b7fa0554SAndreas Gruenbacher .create = nfs_create, 94b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 95b7fa0554SAndreas Gruenbacher .link = nfs_link, 96b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 97b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 98b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 99b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 100b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 101b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 102b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 103b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 104b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 105b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 106b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 107b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 108b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 109b7fa0554SAndreas Gruenbacher }; 110b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 111b7fa0554SAndreas Gruenbacher 1121da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 115c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd); 11692e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = { 117c0204fd2STrond Myklebust .create = nfs_open_create, 1181da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1191da177e4SLinus Torvalds .link = nfs_link, 1201da177e4SLinus Torvalds .unlink = nfs_unlink, 1211da177e4SLinus Torvalds .symlink = nfs_symlink, 1221da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1231da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1241da177e4SLinus Torvalds .mknod = nfs_mknod, 1251da177e4SLinus Torvalds .rename = nfs_rename, 1261da177e4SLinus Torvalds .permission = nfs_permission, 1271da177e4SLinus Torvalds .getattr = nfs_getattr, 1281da177e4SLinus Torvalds .setattr = nfs_setattr, 12964c2ce8bSAneesh Kumar K.V .getxattr = generic_getxattr, 13064c2ce8bSAneesh Kumar K.V .setxattr = generic_setxattr, 13164c2ce8bSAneesh Kumar K.V .listxattr = generic_listxattr, 13264c2ce8bSAneesh Kumar K.V .removexattr = generic_removexattr, 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1361da177e4SLinus Torvalds 137480c2006SBryan Schumaker static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct rpc_cred *cred) 138480c2006SBryan Schumaker { 139480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 140480c2006SBryan Schumaker ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 141480c2006SBryan Schumaker if (ctx != NULL) { 142*8ef2ce3eSBryan Schumaker ctx->duped = 0; 143480c2006SBryan Schumaker ctx->dir_cookie = 0; 144*8ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 145480c2006SBryan Schumaker ctx->cred = get_rpccred(cred); 146480c2006SBryan Schumaker } else 147480c2006SBryan Schumaker ctx = ERR_PTR(-ENOMEM); 148480c2006SBryan Schumaker return ctx; 149480c2006SBryan Schumaker } 150480c2006SBryan Schumaker 151480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx) 152480c2006SBryan Schumaker { 153480c2006SBryan Schumaker put_rpccred(ctx->cred); 154480c2006SBryan Schumaker kfree(ctx); 155480c2006SBryan Schumaker } 156480c2006SBryan Schumaker 1571da177e4SLinus Torvalds /* 1581da177e4SLinus Torvalds * Open file 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds static int 1611da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1621da177e4SLinus Torvalds { 163480c2006SBryan Schumaker int res = 0; 164480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 165480c2006SBryan Schumaker struct rpc_cred *cred; 1661da177e4SLinus Torvalds 1676da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 168cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 169cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 170cc0dd2d1SChuck Lever 171cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1721e7cb3dcSChuck Lever 173480c2006SBryan Schumaker cred = rpc_lookup_cred(); 174480c2006SBryan Schumaker if (IS_ERR(cred)) 175480c2006SBryan Schumaker return PTR_ERR(cred); 176480c2006SBryan Schumaker ctx = alloc_nfs_open_dir_context(cred); 177480c2006SBryan Schumaker if (IS_ERR(ctx)) { 178480c2006SBryan Schumaker res = PTR_ERR(ctx); 179480c2006SBryan Schumaker goto out; 180480c2006SBryan Schumaker } 181480c2006SBryan Schumaker filp->private_data = ctx; 182f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 183f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 184f5a73672SNeil Brown * have been called, so we need to refresh the 185f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 186f5a73672SNeil Brown */ 187f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 188f5a73672SNeil Brown } 189480c2006SBryan Schumaker out: 190480c2006SBryan Schumaker put_rpccred(cred); 1911da177e4SLinus Torvalds return res; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194480c2006SBryan Schumaker static int 195480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 196480c2006SBryan Schumaker { 197480c2006SBryan Schumaker put_nfs_open_dir_context(filp->private_data); 198480c2006SBryan Schumaker return 0; 199480c2006SBryan Schumaker } 200480c2006SBryan Schumaker 201d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 202d1bacf9eSBryan Schumaker u64 cookie; 203d1bacf9eSBryan Schumaker u64 ino; 204d1bacf9eSBryan Schumaker struct qstr string; 2050b26a0bfSTrond Myklebust unsigned char d_type; 206d1bacf9eSBryan Schumaker }; 207d1bacf9eSBryan Schumaker 208d1bacf9eSBryan Schumaker struct nfs_cache_array { 209d1bacf9eSBryan Schumaker unsigned int size; 210d1bacf9eSBryan Schumaker int eof_index; 211d1bacf9eSBryan Schumaker u64 last_cookie; 212d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 213d1bacf9eSBryan Schumaker }; 214d1bacf9eSBryan Schumaker 215573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int); 2161da177e4SLinus Torvalds typedef struct { 2171da177e4SLinus Torvalds struct file *file; 2181da177e4SLinus Torvalds struct page *page; 2191da177e4SLinus Torvalds unsigned long page_index; 220f0dd2136STrond Myklebust u64 *dir_cookie; 2210aded708STrond Myklebust u64 last_cookie; 222f0dd2136STrond Myklebust loff_t current_index; 2231da177e4SLinus Torvalds decode_dirent_t decode; 224d1bacf9eSBryan Schumaker 2251f4eab7eSNeil Brown unsigned long timestamp; 2264704f0e2STrond Myklebust unsigned long gencount; 227d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 228d1bacf9eSBryan Schumaker unsigned int plus:1; 229d1bacf9eSBryan Schumaker unsigned int eof:1; 2301da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 2311da177e4SLinus Torvalds 232d1bacf9eSBryan Schumaker /* 233d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 2341da177e4SLinus Torvalds */ 2351da177e4SLinus Torvalds static 236d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 2371da177e4SLinus Torvalds { 2388cd51a0cSTrond Myklebust void *ptr; 239d1bacf9eSBryan Schumaker if (page == NULL) 240d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 2418cd51a0cSTrond Myklebust ptr = kmap(page); 2428cd51a0cSTrond Myklebust if (ptr == NULL) 2438cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 2448cd51a0cSTrond Myklebust return ptr; 245d1bacf9eSBryan Schumaker } 246d1bacf9eSBryan Schumaker 247d1bacf9eSBryan Schumaker static 248d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 249d1bacf9eSBryan Schumaker { 250d1bacf9eSBryan Schumaker kunmap(page); 251d1bacf9eSBryan Schumaker } 252d1bacf9eSBryan Schumaker 253d1bacf9eSBryan Schumaker /* 254d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 255d1bacf9eSBryan Schumaker */ 256d1bacf9eSBryan Schumaker static 25711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 258d1bacf9eSBryan Schumaker { 25911de3b11STrond Myklebust struct nfs_cache_array *array; 260d1bacf9eSBryan Schumaker int i; 2618cd51a0cSTrond Myklebust 26211de3b11STrond Myklebust array = kmap_atomic(page, KM_USER0); 263d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 264d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 26511de3b11STrond Myklebust kunmap_atomic(array, KM_USER0); 266d1bacf9eSBryan Schumaker } 267d1bacf9eSBryan Schumaker 268d1bacf9eSBryan Schumaker /* 269d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 270d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 271d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 272d1bacf9eSBryan Schumaker */ 273d1bacf9eSBryan Schumaker static 2744a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 275d1bacf9eSBryan Schumaker { 276d1bacf9eSBryan Schumaker string->len = len; 277d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2784a201d6eSTrond Myklebust if (string->name == NULL) 2794a201d6eSTrond Myklebust return -ENOMEM; 28004e4bd1cSCatalin Marinas /* 28104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 28204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 28304e4bd1cSCatalin Marinas */ 28404e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2854a201d6eSTrond Myklebust string->hash = full_name_hash(name, len); 2864a201d6eSTrond Myklebust return 0; 287d1bacf9eSBryan Schumaker } 288d1bacf9eSBryan Schumaker 289d1bacf9eSBryan Schumaker static 290d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 291d1bacf9eSBryan Schumaker { 292d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2934a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2944a201d6eSTrond Myklebust int ret; 2954a201d6eSTrond Myklebust 296d1bacf9eSBryan Schumaker if (IS_ERR(array)) 297d1bacf9eSBryan Schumaker return PTR_ERR(array); 298d1bacf9eSBryan Schumaker 2994a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 3003020093fSTrond Myklebust 3013020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 3023020093fSTrond Myklebust ret = -ENOSPC; 3033020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 3043020093fSTrond Myklebust goto out; 3053020093fSTrond Myklebust 3064a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 3074a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 3080b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 3094a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 3104a201d6eSTrond Myklebust if (ret) 3114a201d6eSTrond Myklebust goto out; 312d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 3138cd51a0cSTrond Myklebust array->size++; 31447c716cbSTrond Myklebust if (entry->eof != 0) 315d1bacf9eSBryan Schumaker array->eof_index = array->size; 3164a201d6eSTrond Myklebust out: 317d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 3184a201d6eSTrond Myklebust return ret; 319d1bacf9eSBryan Schumaker } 320d1bacf9eSBryan Schumaker 321d1bacf9eSBryan Schumaker static 322d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 323d1bacf9eSBryan Schumaker { 324d1bacf9eSBryan Schumaker loff_t diff = desc->file->f_pos - desc->current_index; 325d1bacf9eSBryan Schumaker unsigned int index; 326*8ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = desc->file->private_data; 327d1bacf9eSBryan Schumaker 328d1bacf9eSBryan Schumaker if (diff < 0) 329d1bacf9eSBryan Schumaker goto out_eof; 330d1bacf9eSBryan Schumaker if (diff >= array->size) { 3318cd51a0cSTrond Myklebust if (array->eof_index >= 0) 332d1bacf9eSBryan Schumaker goto out_eof; 333d1bacf9eSBryan Schumaker return -EAGAIN; 334d1bacf9eSBryan Schumaker } 335d1bacf9eSBryan Schumaker 336d1bacf9eSBryan Schumaker index = (unsigned int)diff; 337d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 338d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 339*8ef2ce3eSBryan Schumaker ctx->duped = 0; 340d1bacf9eSBryan Schumaker return 0; 341d1bacf9eSBryan Schumaker out_eof: 342d1bacf9eSBryan Schumaker desc->eof = 1; 343d1bacf9eSBryan Schumaker return -EBADCOOKIE; 344d1bacf9eSBryan Schumaker } 345d1bacf9eSBryan Schumaker 346d1bacf9eSBryan Schumaker static 347d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 348d1bacf9eSBryan Schumaker { 349d1bacf9eSBryan Schumaker int i; 350*8ef2ce3eSBryan Schumaker loff_t new_pos; 351d1bacf9eSBryan Schumaker int status = -EAGAIN; 352*8ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = desc->file->private_data; 353d1bacf9eSBryan Schumaker 354d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3558cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 356*8ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 357*8ef2ce3eSBryan Schumaker if (new_pos < desc->file->f_pos) { 358*8ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 359*8ef2ce3eSBryan Schumaker ctx->duped = 1; 360*8ef2ce3eSBryan Schumaker } 361*8ef2ce3eSBryan Schumaker desc->file->f_pos = new_pos; 3628cd51a0cSTrond Myklebust desc->cache_entry_index = i; 36347c716cbSTrond Myklebust return 0; 3648cd51a0cSTrond Myklebust } 3658cd51a0cSTrond Myklebust } 36647c716cbSTrond Myklebust if (array->eof_index >= 0) { 367d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 36818fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 36918fb5fe4STrond Myklebust desc->eof = 1; 370d1bacf9eSBryan Schumaker } 371d1bacf9eSBryan Schumaker return status; 372d1bacf9eSBryan Schumaker } 373d1bacf9eSBryan Schumaker 374d1bacf9eSBryan Schumaker static 375d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 376d1bacf9eSBryan Schumaker { 377d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 37847c716cbSTrond Myklebust int status; 379d1bacf9eSBryan Schumaker 380d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 381d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 382d1bacf9eSBryan Schumaker status = PTR_ERR(array); 383d1bacf9eSBryan Schumaker goto out; 384d1bacf9eSBryan Schumaker } 385d1bacf9eSBryan Schumaker 386d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 387d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 388d1bacf9eSBryan Schumaker else 389d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 390d1bacf9eSBryan Schumaker 39147c716cbSTrond Myklebust if (status == -EAGAIN) { 3920aded708STrond Myklebust desc->last_cookie = array->last_cookie; 393e47c085aSTrond Myklebust desc->current_index += array->size; 39447c716cbSTrond Myklebust desc->page_index++; 39547c716cbSTrond Myklebust } 396d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 397d1bacf9eSBryan Schumaker out: 398d1bacf9eSBryan Schumaker return status; 399d1bacf9eSBryan Schumaker } 400d1bacf9eSBryan Schumaker 401d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 402d1bacf9eSBryan Schumaker static 40356e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 404d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 405d1bacf9eSBryan Schumaker { 406480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 407480c2006SBryan Schumaker struct rpc_cred *cred = ctx->cred; 4084704f0e2STrond Myklebust unsigned long timestamp, gencount; 4091da177e4SLinus Torvalds int error; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds again: 4121da177e4SLinus Torvalds timestamp = jiffies; 4134704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 41456e4ebf8SBryan Schumaker error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages, 4151da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 4161da177e4SLinus Torvalds if (error < 0) { 4171da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 4181da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 4191da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 4203a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 4211da177e4SLinus Torvalds desc->plus = 0; 4221da177e4SLinus Torvalds goto again; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds goto error; 4251da177e4SLinus Torvalds } 4261f4eab7eSNeil Brown desc->timestamp = timestamp; 4274704f0e2STrond Myklebust desc->gencount = gencount; 428d1bacf9eSBryan Schumaker error: 429d1bacf9eSBryan Schumaker return error; 430d1bacf9eSBryan Schumaker } 431d1bacf9eSBryan Schumaker 432573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 433573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 434d1bacf9eSBryan Schumaker { 435573c4e1eSChuck Lever int error; 436d1bacf9eSBryan Schumaker 437573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 438573c4e1eSChuck Lever if (error) 439573c4e1eSChuck Lever return error; 440d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 441d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 442d1bacf9eSBryan Schumaker return 0; 443d1bacf9eSBryan Schumaker } 444d1bacf9eSBryan Schumaker 445d39ab9deSBryan Schumaker static 446d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 447d39ab9deSBryan Schumaker { 448d39ab9deSBryan Schumaker if (dentry->d_inode == NULL) 449d39ab9deSBryan Schumaker goto different; 45037a09f07STrond Myklebust if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0) 451d39ab9deSBryan Schumaker goto different; 452d39ab9deSBryan Schumaker return 1; 453d39ab9deSBryan Schumaker different: 454d39ab9deSBryan Schumaker return 0; 455d39ab9deSBryan Schumaker } 456d39ab9deSBryan Schumaker 457d39ab9deSBryan Schumaker static 458d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 459d39ab9deSBryan Schumaker { 4604a201d6eSTrond Myklebust struct qstr filename = { 4614a201d6eSTrond Myklebust .len = entry->len, 4624a201d6eSTrond Myklebust .name = entry->name, 4634a201d6eSTrond Myklebust }; 4644a201d6eSTrond Myklebust struct dentry *dentry; 4654a201d6eSTrond Myklebust struct dentry *alias; 466d39ab9deSBryan Schumaker struct inode *dir = parent->d_inode; 467d39ab9deSBryan Schumaker struct inode *inode; 468d39ab9deSBryan Schumaker 4694a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4704a201d6eSTrond Myklebust if (filename.len == 1) 4714a201d6eSTrond Myklebust return; 4724a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4734a201d6eSTrond Myklebust return; 4744a201d6eSTrond Myklebust } 4754a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 476d39ab9deSBryan Schumaker 4774a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 478d39ab9deSBryan Schumaker if (dentry != NULL) { 479d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 480d39ab9deSBryan Schumaker nfs_refresh_inode(dentry->d_inode, entry->fattr); 481d39ab9deSBryan Schumaker goto out; 482d39ab9deSBryan Schumaker } else { 483d39ab9deSBryan Schumaker d_drop(dentry); 484d39ab9deSBryan Schumaker dput(dentry); 485d39ab9deSBryan Schumaker } 486d39ab9deSBryan Schumaker } 487d39ab9deSBryan Schumaker 488d39ab9deSBryan Schumaker dentry = d_alloc(parent, &filename); 4894a201d6eSTrond Myklebust if (dentry == NULL) 4904a201d6eSTrond Myklebust return; 4914a201d6eSTrond Myklebust 492d39ab9deSBryan Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 493d39ab9deSBryan Schumaker if (IS_ERR(inode)) 494d39ab9deSBryan Schumaker goto out; 495d39ab9deSBryan Schumaker 496d39ab9deSBryan Schumaker alias = d_materialise_unique(dentry, inode); 497d39ab9deSBryan Schumaker if (IS_ERR(alias)) 498d39ab9deSBryan Schumaker goto out; 499d39ab9deSBryan Schumaker else if (alias) { 500d39ab9deSBryan Schumaker nfs_set_verifier(alias, nfs_save_change_attribute(dir)); 501d39ab9deSBryan Schumaker dput(alias); 502d39ab9deSBryan Schumaker } else 503d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 504d39ab9deSBryan Schumaker 505d39ab9deSBryan Schumaker out: 506d39ab9deSBryan Schumaker dput(dentry); 507d39ab9deSBryan Schumaker } 508d39ab9deSBryan Schumaker 509d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 510d1bacf9eSBryan Schumaker static 5118cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5126650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 513d1bacf9eSBryan Schumaker { 514babddc72SBryan Schumaker struct xdr_stream stream; 5156650239aSTrond Myklebust struct xdr_buf buf = { 5166650239aSTrond Myklebust .pages = xdr_pages, 5176650239aSTrond Myklebust .page_len = buflen, 5186650239aSTrond Myklebust .buflen = buflen, 5196650239aSTrond Myklebust .len = buflen, 5206650239aSTrond Myklebust }; 5216650239aSTrond Myklebust struct page *scratch; 52299424380SBryan Schumaker struct nfs_cache_array *array; 5235c346854STrond Myklebust unsigned int count = 0; 5245c346854STrond Myklebust int status; 525babddc72SBryan Schumaker 5266650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5276650239aSTrond Myklebust if (scratch == NULL) 5286650239aSTrond Myklebust return -ENOMEM; 529babddc72SBryan Schumaker 5306650239aSTrond Myklebust xdr_init_decode(&stream, &buf, NULL); 5316650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 53299424380SBryan Schumaker 53399424380SBryan Schumaker do { 53499424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5358cd51a0cSTrond Myklebust if (status != 0) { 5368cd51a0cSTrond Myklebust if (status == -EAGAIN) 5378cd51a0cSTrond Myklebust status = 0; 53899424380SBryan Schumaker break; 5398cd51a0cSTrond Myklebust } 54099424380SBryan Schumaker 5415c346854STrond Myklebust count++; 5425c346854STrond Myklebust 54347c716cbSTrond Myklebust if (desc->plus != 0) 544d39ab9deSBryan Schumaker nfs_prime_dcache(desc->file->f_path.dentry, entry); 5458cd51a0cSTrond Myklebust 5468cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5478cd51a0cSTrond Myklebust if (status != 0) 5488cd51a0cSTrond Myklebust break; 54999424380SBryan Schumaker } while (!entry->eof); 55099424380SBryan Schumaker 55147c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 55299424380SBryan Schumaker array = nfs_readdir_get_array(page); 5538cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 5548cd51a0cSTrond Myklebust array->eof_index = array->size; 55599424380SBryan Schumaker status = 0; 55699424380SBryan Schumaker nfs_readdir_release_array(page); 5575c346854STrond Myklebust } else 5585c346854STrond Myklebust status = PTR_ERR(array); 55956e4ebf8SBryan Schumaker } 5606650239aSTrond Myklebust 5616650239aSTrond Myklebust put_page(scratch); 5628cd51a0cSTrond Myklebust return status; 5638cd51a0cSTrond Myklebust } 56456e4ebf8SBryan Schumaker 56556e4ebf8SBryan Schumaker static 56656e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages) 56756e4ebf8SBryan Schumaker { 56856e4ebf8SBryan Schumaker unsigned int i; 56956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 57056e4ebf8SBryan Schumaker put_page(pages[i]); 57156e4ebf8SBryan Schumaker } 57256e4ebf8SBryan Schumaker 57356e4ebf8SBryan Schumaker static 57456e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages, 57556e4ebf8SBryan Schumaker unsigned int npages) 57656e4ebf8SBryan Schumaker { 57756e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, npages); 57856e4ebf8SBryan Schumaker } 57956e4ebf8SBryan Schumaker 58056e4ebf8SBryan Schumaker /* 58156e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 58256e4ebf8SBryan Schumaker * to nfs_readdir_free_large_page 58356e4ebf8SBryan Schumaker */ 58456e4ebf8SBryan Schumaker static 5856650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages) 58656e4ebf8SBryan Schumaker { 58756e4ebf8SBryan Schumaker unsigned int i; 58856e4ebf8SBryan Schumaker 58956e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 59056e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 59156e4ebf8SBryan Schumaker if (page == NULL) 59256e4ebf8SBryan Schumaker goto out_freepages; 59356e4ebf8SBryan Schumaker pages[i] = page; 59456e4ebf8SBryan Schumaker } 5956650239aSTrond Myklebust return 0; 59656e4ebf8SBryan Schumaker 59756e4ebf8SBryan Schumaker out_freepages: 59856e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, i); 5996650239aSTrond Myklebust return -ENOMEM; 600d1bacf9eSBryan Schumaker } 601d1bacf9eSBryan Schumaker 602d1bacf9eSBryan Schumaker static 603d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 604d1bacf9eSBryan Schumaker { 60556e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 60656e4ebf8SBryan Schumaker void *pages_ptr = NULL; 607d1bacf9eSBryan Schumaker struct nfs_entry entry; 608d1bacf9eSBryan Schumaker struct file *file = desc->file; 609d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6108cd51a0cSTrond Myklebust int status = -ENOMEM; 61156e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 612d1bacf9eSBryan Schumaker 613d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6140aded708STrond Myklebust entry.cookie = desc->last_cookie; 615d1bacf9eSBryan Schumaker entry.eof = 0; 616d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 617d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 618573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 619d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 620d1bacf9eSBryan Schumaker goto out; 621d1bacf9eSBryan Schumaker 622d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 6238cd51a0cSTrond Myklebust if (IS_ERR(array)) { 6248cd51a0cSTrond Myklebust status = PTR_ERR(array); 6258cd51a0cSTrond Myklebust goto out; 6268cd51a0cSTrond Myklebust } 627d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 628d1bacf9eSBryan Schumaker array->eof_index = -1; 629d1bacf9eSBryan Schumaker 6306650239aSTrond Myklebust status = nfs_readdir_large_page(pages, array_size); 6316650239aSTrond Myklebust if (status < 0) 632d1bacf9eSBryan Schumaker goto out_release_array; 633d1bacf9eSBryan Schumaker do { 634ac396128STrond Myklebust unsigned int pglen; 63556e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 636babddc72SBryan Schumaker 637d1bacf9eSBryan Schumaker if (status < 0) 638d1bacf9eSBryan Schumaker break; 639ac396128STrond Myklebust pglen = status; 6406650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6418cd51a0cSTrond Myklebust if (status < 0) { 6428cd51a0cSTrond Myklebust if (status == -ENOSPC) 6438cd51a0cSTrond Myklebust status = 0; 6448cd51a0cSTrond Myklebust break; 6458cd51a0cSTrond Myklebust } 6468cd51a0cSTrond Myklebust } while (array->eof_index < 0); 647d1bacf9eSBryan Schumaker 64856e4ebf8SBryan Schumaker nfs_readdir_free_large_page(pages_ptr, pages, array_size); 649d1bacf9eSBryan Schumaker out_release_array: 650d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 651d1bacf9eSBryan Schumaker out: 652d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 653d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 654d1bacf9eSBryan Schumaker return status; 655d1bacf9eSBryan Schumaker } 656d1bacf9eSBryan Schumaker 657d1bacf9eSBryan Schumaker /* 658d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 659d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 660d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 661d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6621da177e4SLinus Torvalds */ 663d1bacf9eSBryan Schumaker static 664d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 665d1bacf9eSBryan Schumaker { 666d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 6678cd51a0cSTrond Myklebust int ret; 668d1bacf9eSBryan Schumaker 6698cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6708cd51a0cSTrond Myklebust if (ret < 0) 671d1bacf9eSBryan Schumaker goto error; 672d1bacf9eSBryan Schumaker SetPageUptodate(page); 673d1bacf9eSBryan Schumaker 6742aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 675cd9ae2b6STrond Myklebust /* Should never happen */ 676cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 677cd9ae2b6STrond Myklebust } 6781da177e4SLinus Torvalds unlock_page(page); 6791da177e4SLinus Torvalds return 0; 6801da177e4SLinus Torvalds error: 6811da177e4SLinus Torvalds unlock_page(page); 6828cd51a0cSTrond Myklebust return ret; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 685d1bacf9eSBryan Schumaker static 686d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 6871da177e4SLinus Torvalds { 68811de3b11STrond Myklebust if (!desc->page->mapping) 68911de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 6901da177e4SLinus Torvalds page_cache_release(desc->page); 6911da177e4SLinus Torvalds desc->page = NULL; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694d1bacf9eSBryan Schumaker static 695d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 6961da177e4SLinus Torvalds { 6978cd51a0cSTrond Myklebust return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping, 698d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* 702d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7031da177e4SLinus Torvalds */ 704d1bacf9eSBryan Schumaker static 705d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 706d1bacf9eSBryan Schumaker { 707d1bacf9eSBryan Schumaker int res; 708d1bacf9eSBryan Schumaker 709d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 710d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 711d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 712d1bacf9eSBryan Schumaker 713d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 71447c716cbSTrond Myklebust if (res != 0) 715d1bacf9eSBryan Schumaker cache_page_release(desc); 716d1bacf9eSBryan Schumaker return res; 717d1bacf9eSBryan Schumaker } 718d1bacf9eSBryan Schumaker 719d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7201da177e4SLinus Torvalds static inline 7211da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7221da177e4SLinus Torvalds { 7238cd51a0cSTrond Myklebust int res; 724d1bacf9eSBryan Schumaker 7250aded708STrond Myklebust if (desc->page_index == 0) { 7268cd51a0cSTrond Myklebust desc->current_index = 0; 7270aded708STrond Myklebust desc->last_cookie = 0; 7280aded708STrond Myklebust } 72947c716cbSTrond Myklebust do { 730d1bacf9eSBryan Schumaker res = find_cache_page(desc); 73147c716cbSTrond Myklebust } while (res == -EAGAIN); 7321da177e4SLinus Torvalds return res; 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /* 7361da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7371da177e4SLinus Torvalds */ 7381da177e4SLinus Torvalds static 7391da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 7401da177e4SLinus Torvalds filldir_t filldir) 7411da177e4SLinus Torvalds { 7421da177e4SLinus Torvalds struct file *file = desc->file; 743d1bacf9eSBryan Schumaker int i = 0; 744d1bacf9eSBryan Schumaker int res = 0; 745d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 746*8ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 747*8ef2ce3eSBryan Schumaker 748*8ef2ce3eSBryan Schumaker if (ctx->duped != 0 && ctx->dup_cookie == *desc->dir_cookie) { 749*8ef2ce3eSBryan Schumaker if (printk_ratelimit()) { 750*8ef2ce3eSBryan Schumaker pr_notice("NFS: directory %s/%s contains a readdir loop. " 751*8ef2ce3eSBryan Schumaker "Please contact your server vendor. " 752*8ef2ce3eSBryan Schumaker "Offending cookie: %llu\n", 753*8ef2ce3eSBryan Schumaker file->f_dentry->d_parent->d_name.name, 754*8ef2ce3eSBryan Schumaker file->f_dentry->d_name.name, 755*8ef2ce3eSBryan Schumaker *desc->dir_cookie); 756*8ef2ce3eSBryan Schumaker } 757*8ef2ce3eSBryan Schumaker res = -ELOOP; 758*8ef2ce3eSBryan Schumaker goto out; 759*8ef2ce3eSBryan Schumaker } 7601da177e4SLinus Torvalds 761d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 762e7c58e97STrond Myklebust if (IS_ERR(array)) { 763e7c58e97STrond Myklebust res = PTR_ERR(array); 764e7c58e97STrond Myklebust goto out; 765e7c58e97STrond Myklebust } 7661da177e4SLinus Torvalds 767d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 768ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7691da177e4SLinus Torvalds 770ece0b423STrond Myklebust ent = &array->array[i]; 771ece0b423STrond Myklebust if (filldir(dirent, ent->string.name, ent->string.len, 7720b26a0bfSTrond Myklebust file->f_pos, nfs_compat_user_ino64(ent->ino), 7730b26a0bfSTrond Myklebust ent->d_type) < 0) { 774ece0b423STrond Myklebust desc->eof = 1; 7751da177e4SLinus Torvalds break; 776ece0b423STrond Myklebust } 77700a92642SOlivier Galibert file->f_pos++; 778d1bacf9eSBryan Schumaker if (i < (array->size-1)) 779d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 780d1bacf9eSBryan Schumaker else 781d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7828cd51a0cSTrond Myklebust } 78347c716cbSTrond Myklebust if (array->eof_index >= 0) 784d1bacf9eSBryan Schumaker desc->eof = 1; 785d1bacf9eSBryan Schumaker 786d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 787e7c58e97STrond Myklebust out: 788d1bacf9eSBryan Schumaker cache_page_release(desc); 7891e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7901e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7911da177e4SLinus Torvalds return res; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 7961da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 7971da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 7981da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 7991da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8001da177e4SLinus Torvalds * 8011da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8021da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8031da177e4SLinus Torvalds * we should already have a complete representation of the 8041da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8051da177e4SLinus Torvalds */ 8061da177e4SLinus Torvalds static inline 8071da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 8081da177e4SLinus Torvalds filldir_t filldir) 8091da177e4SLinus Torvalds { 8101da177e4SLinus Torvalds struct page *page = NULL; 8111da177e4SLinus Torvalds int status; 812d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 8131da177e4SLinus Torvalds 8141e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8151e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8181da177e4SLinus Torvalds if (!page) { 8191da177e4SLinus Torvalds status = -ENOMEM; 8201da177e4SLinus Torvalds goto out; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8237a8e1dc3STrond Myklebust desc->page_index = 0; 8240aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8257a8e1dc3STrond Myklebust desc->page = page; 8267a8e1dc3STrond Myklebust 82785f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 82885f8607eSTrond Myklebust if (status < 0) 829d1bacf9eSBryan Schumaker goto out_release; 830d1bacf9eSBryan Schumaker 8311da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds out: 8341e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8353110ff80SHarvey Harrison __func__, status); 8361da177e4SLinus Torvalds return status; 8371da177e4SLinus Torvalds out_release: 838d1bacf9eSBryan Schumaker cache_page_release(desc); 8391da177e4SLinus Torvalds goto out; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 84200a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 84300a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 84400a92642SOlivier Galibert whole directory. 8451da177e4SLinus Torvalds */ 8461da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 8471da177e4SLinus Torvalds { 84801cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 8491da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 8501da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8511da177e4SLinus Torvalds *desc = &my_desc; 852480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 85347c716cbSTrond Myklebust int res; 8541da177e4SLinus Torvalds 8556da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 8561e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8571e7cb3dcSChuck Lever (long long)filp->f_pos); 85891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 85991d5b470SChuck Lever 8601da177e4SLinus Torvalds /* 86100a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 862f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 86300a92642SOlivier Galibert * to either find the entry with the appropriate number or 86400a92642SOlivier Galibert * revalidate the cookie. 8651da177e4SLinus Torvalds */ 8661da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds desc->file = filp; 869480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8701da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 8711da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 8721da177e4SLinus Torvalds 873565277f6STrond Myklebust nfs_block_sillyrename(dentry); 8741cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 875fccca7fcSTrond Myklebust if (res < 0) 876fccca7fcSTrond Myklebust goto out; 877fccca7fcSTrond Myklebust 87847c716cbSTrond Myklebust do { 8791da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 88000a92642SOlivier Galibert 8811da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 882ece0b423STrond Myklebust res = 0; 8831da177e4SLinus Torvalds /* This means either end of directory */ 884d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 8851da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 8861da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 887ece0b423STrond Myklebust if (res == 0) 8881da177e4SLinus Torvalds continue; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds break; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8933a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8941da177e4SLinus Torvalds nfs_zap_caches(inode); 895baf57a09STrond Myklebust desc->page_index = 0; 8961da177e4SLinus Torvalds desc->plus = 0; 897d1bacf9eSBryan Schumaker desc->eof = 0; 8981da177e4SLinus Torvalds continue; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds if (res < 0) 9011da177e4SLinus Torvalds break; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 904ece0b423STrond Myklebust if (res < 0) 9051da177e4SLinus Torvalds break; 90647c716cbSTrond Myklebust } while (!desc->eof); 907fccca7fcSTrond Myklebust out: 908565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 9091e7cb3dcSChuck Lever if (res > 0) 9101e7cb3dcSChuck Lever res = 0; 911aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 9121e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 9131e7cb3dcSChuck Lever res); 9141da177e4SLinus Torvalds return res; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 91710afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 918f0dd2136STrond Myklebust { 919b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 920b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 921480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 922b84e06c5SChuck Lever 9236da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 924b84e06c5SChuck Lever dentry->d_parent->d_name.name, 925b84e06c5SChuck Lever dentry->d_name.name, 926b84e06c5SChuck Lever offset, origin); 927b84e06c5SChuck Lever 928b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 929f0dd2136STrond Myklebust switch (origin) { 930f0dd2136STrond Myklebust case 1: 931f0dd2136STrond Myklebust offset += filp->f_pos; 932f0dd2136STrond Myklebust case 0: 933f0dd2136STrond Myklebust if (offset >= 0) 934f0dd2136STrond Myklebust break; 935f0dd2136STrond Myklebust default: 936f0dd2136STrond Myklebust offset = -EINVAL; 937f0dd2136STrond Myklebust goto out; 938f0dd2136STrond Myklebust } 939f0dd2136STrond Myklebust if (offset != filp->f_pos) { 940f0dd2136STrond Myklebust filp->f_pos = offset; 941480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 942*8ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 943f0dd2136STrond Myklebust } 944f0dd2136STrond Myklebust out: 945b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 946f0dd2136STrond Myklebust return offset; 947f0dd2136STrond Myklebust } 948f0dd2136STrond Myklebust 9491da177e4SLinus Torvalds /* 9501da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9511da177e4SLinus Torvalds * is a dummy operation. 9521da177e4SLinus Torvalds */ 9537ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync) 9541da177e4SLinus Torvalds { 9557ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 9567ea80859SChristoph Hellwig 9576da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 9581e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 9591e7cb3dcSChuck Lever datasync); 9601e7cb3dcSChuck Lever 96154917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 9621da177e4SLinus Torvalds return 0; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 965bfc69a45STrond Myklebust /** 966bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 967bfc69a45STrond Myklebust * @dir - pointer to directory inode 968bfc69a45STrond Myklebust * 969bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 970bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 971bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 972bfc69a45STrond Myklebust * 973bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 974bfc69a45STrond Myklebust */ 975bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 976bfc69a45STrond Myklebust { 977011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 978bfc69a45STrond Myklebust } 979bfc69a45STrond Myklebust 9801da177e4SLinus Torvalds /* 9811da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9821da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9831da177e4SLinus Torvalds * and may need to be looked up again. 9841da177e4SLinus Torvalds */ 985c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9881da177e4SLinus Torvalds return 1; 9894eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 9904eec952eSTrond Myklebust return 0; 991f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 9926ecc5e8fSTrond Myklebust return 0; 993f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 994f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 995f2c77f4eSTrond Myklebust return 0; 996f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 997f2c77f4eSTrond Myklebust return 0; 998f2c77f4eSTrond Myklebust return 1; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 10021d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 10031d6757fbSTrond Myklebust * 10041d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 10051d6757fbSTrond Myklebust * component of the path. 10061d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 10071d6757fbSTrond Myklebust */ 100834286d66SNick Piggin static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, 100934286d66SNick Piggin unsigned int mask) 10101d6757fbSTrond Myklebust { 10111d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 10121d6757fbSTrond Myklebust return 0; 10131d6757fbSTrond Myklebust return nd->flags & mask; 10141d6757fbSTrond Myklebust } 10151d6757fbSTrond Myklebust 10161d6757fbSTrond Myklebust /* 1017a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1018a12802caSTrond Myklebust * an O_EXCL create using this path component. 1019a12802caSTrond Myklebust */ 1020a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 1021a12802caSTrond Myklebust { 1022a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1023a12802caSTrond Myklebust return 0; 10243516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 1025a12802caSTrond Myklebust } 1026a12802caSTrond Myklebust 1027a12802caSTrond Myklebust /* 10281d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10291d6757fbSTrond Myklebust * 10301d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10311d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10321d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10331d6757fbSTrond Myklebust * 10341d6757fbSTrond Myklebust */ 10351da177e4SLinus Torvalds static inline 10361da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 10391da177e4SLinus Torvalds 104036d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10414e99a1ffSTrond Myklebust return 0; 10421da177e4SLinus Torvalds if (nd != NULL) { 10431da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 10441d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 10451da177e4SLinus Torvalds goto out_force; 10461da177e4SLinus Torvalds /* This is an open(2) */ 10471d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 10484e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 10494e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 10504e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 10511da177e4SLinus Torvalds goto out_force; 10524f48af45STrond Myklebust return 0; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 10551da177e4SLinus Torvalds out_force: 10561da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* 10601da177e4SLinus Torvalds * We judge how long we want to trust negative 10611da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10621da177e4SLinus Torvalds * 10631da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10641da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds static inline 10671da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 10681da177e4SLinus Torvalds struct nameidata *nd) 10691da177e4SLinus Torvalds { 10701da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 10711d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 10721da177e4SLinus Torvalds return 0; 10734eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10744eec952eSTrond Myklebust return 1; 10751da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds /* 10791da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 10801da177e4SLinus Torvalds * and we should check whether we can really trust that 10811da177e4SLinus Torvalds * lookup. 10821da177e4SLinus Torvalds * 10831da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 10841da177e4SLinus Torvalds * we have an inode! 10851da177e4SLinus Torvalds * 10861da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 10871da177e4SLinus Torvalds * cached dentry and do a new lookup. 10881da177e4SLinus Torvalds */ 10891da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd) 10901da177e4SLinus Torvalds { 10911da177e4SLinus Torvalds struct inode *dir; 10921da177e4SLinus Torvalds struct inode *inode; 10931da177e4SLinus Torvalds struct dentry *parent; 1094e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1095e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 10961da177e4SLinus Torvalds int error; 10971da177e4SLinus Torvalds 109834286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 109934286d66SNick Piggin return -ECHILD; 110034286d66SNick Piggin 11011da177e4SLinus Torvalds parent = dget_parent(dentry); 11021da177e4SLinus Torvalds dir = parent->d_inode; 110391d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 11041da177e4SLinus Torvalds inode = dentry->d_inode; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds if (!inode) { 11071da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 11081da177e4SLinus Torvalds goto out_bad; 11091da177e4SLinus Torvalds goto out_valid; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds if (is_bad_inode(inode)) { 11131e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 11143110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11151e7cb3dcSChuck Lever dentry->d_name.name); 11161da177e4SLinus Torvalds goto out_bad; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 111915860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 112015860ab1STrond Myklebust goto out_set_verifier; 112115860ab1STrond Myklebust 11221da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 1123a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 11241da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 11251da177e4SLinus Torvalds goto out_zap_parent; 11261da177e4SLinus Torvalds goto out_valid; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds if (NFS_STALE(inode)) 11301da177e4SLinus Torvalds goto out_bad; 11311da177e4SLinus Torvalds 1132e1fb4d05STrond Myklebust error = -ENOMEM; 1133e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1134e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1135e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1136e1fb4d05STrond Myklebust goto out_error; 1137e1fb4d05STrond Myklebust 1138e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 11391da177e4SLinus Torvalds if (error) 11401da177e4SLinus Torvalds goto out_bad; 1141e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11421da177e4SLinus Torvalds goto out_bad; 1143e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 11441da177e4SLinus Torvalds goto out_bad; 11451da177e4SLinus Torvalds 1146e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1147e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 114815860ab1STrond Myklebust out_set_verifier: 1149cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11501da177e4SLinus Torvalds out_valid: 11511da177e4SLinus Torvalds dput(parent); 11521e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 11533110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11541e7cb3dcSChuck Lever dentry->d_name.name); 11551da177e4SLinus Torvalds return 1; 11561da177e4SLinus Torvalds out_zap_parent: 11571da177e4SLinus Torvalds nfs_zap_caches(dir); 11581da177e4SLinus Torvalds out_bad: 1159a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11601da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11611da177e4SLinus Torvalds /* Purge readdir caches. */ 11621da177e4SLinus Torvalds nfs_zap_caches(inode); 11631da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 11641da177e4SLinus Torvalds if (have_submounts(dentry)) 11651da177e4SLinus Torvalds goto out_valid; 1166d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 1167d9e80b7dSAl Viro goto out_valid; 11681da177e4SLinus Torvalds shrink_dcache_parent(dentry); 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds d_drop(dentry); 1171e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1172e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 11731da177e4SLinus Torvalds dput(parent); 11741e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 11753110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11761e7cb3dcSChuck Lever dentry->d_name.name); 11771da177e4SLinus Torvalds return 0; 1178e1fb4d05STrond Myklebust out_error: 1179e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1180e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 1181e1fb4d05STrond Myklebust dput(parent); 1182e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 1183e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 1184e1fb4d05STrond Myklebust dentry->d_name.name, error); 1185e1fb4d05STrond Myklebust return error; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds /* 11891da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 11901da177e4SLinus Torvalds */ 1191fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 11921da177e4SLinus Torvalds { 11931da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 11941da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 11951da177e4SLinus Torvalds dentry->d_flags); 11961da177e4SLinus Torvalds 119777f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 119877f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 119977f11192STrond Myklebust return 1; 120077f11192STrond Myklebust 12011da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12021da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 12031da177e4SLinus Torvalds return 1; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 12061da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 12071da177e4SLinus Torvalds * files will be cleaned up during umount */ 12081da177e4SLinus Torvalds return 1; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds return 0; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 12141b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 12151b83d707STrond Myklebust { 12161b83d707STrond Myklebust spin_lock(&inode->i_lock); 12171b83d707STrond Myklebust if (inode->i_nlink > 0) 12181b83d707STrond Myklebust drop_nlink(inode); 12191b83d707STrond Myklebust spin_unlock(&inode->i_lock); 12201b83d707STrond Myklebust } 12211b83d707STrond Myklebust 12221da177e4SLinus Torvalds /* 12231da177e4SLinus Torvalds * Called when the dentry loses inode. 12241da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 12251da177e4SLinus Torvalds */ 12261da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 12271da177e4SLinus Torvalds { 122883672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 122983672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 123083672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 123183672d39SNeil Brown 12321da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12339a53c3a7SDave Hansen drop_nlink(inode); 1234e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds iput(inode); 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 1239f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 12401da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 12411da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 12421da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 124336d43a43SDavid Howells .d_automount = nfs_d_automount, 12441da177e4SLinus Torvalds }; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 12471da177e4SLinus Torvalds { 12481da177e4SLinus Torvalds struct dentry *res; 1249565277f6STrond Myklebust struct dentry *parent; 12501da177e4SLinus Torvalds struct inode *inode = NULL; 1251e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1252e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 12531da177e4SLinus Torvalds int error; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 12561da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 125791d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 12601da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 12611da177e4SLinus Torvalds goto out; 12621da177e4SLinus Torvalds 1263fd684071STrond Myklebust /* 1264fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1265fd684071STrond Myklebust * but don't hash the dentry. 1266fd684071STrond Myklebust */ 1267fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 1268fd684071STrond Myklebust d_instantiate(dentry, NULL); 1269fd684071STrond Myklebust res = NULL; 1270fc0f684cSTrond Myklebust goto out; 1271fd684071STrond Myklebust } 12721da177e4SLinus Torvalds 1273e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1274e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1275e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1276e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1277e1fb4d05STrond Myklebust goto out; 1278e1fb4d05STrond Myklebust 1279565277f6STrond Myklebust parent = dentry->d_parent; 1280565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 1281565277f6STrond Myklebust nfs_block_sillyrename(parent); 1282e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 12831da177e4SLinus Torvalds if (error == -ENOENT) 12841da177e4SLinus Torvalds goto no_entry; 12851da177e4SLinus Torvalds if (error < 0) { 12861da177e4SLinus Torvalds res = ERR_PTR(error); 1287565277f6STrond Myklebust goto out_unblock_sillyrename; 12881da177e4SLinus Torvalds } 1289e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1290bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 129103f28e3aSTrond Myklebust if (IS_ERR(res)) 1292565277f6STrond Myklebust goto out_unblock_sillyrename; 129354ceac45SDavid Howells 12941da177e4SLinus Torvalds no_entry: 129554ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 12969eaef27bSTrond Myklebust if (res != NULL) { 12979eaef27bSTrond Myklebust if (IS_ERR(res)) 1298565277f6STrond Myklebust goto out_unblock_sillyrename; 12991da177e4SLinus Torvalds dentry = res; 13009eaef27bSTrond Myklebust } 13011da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1302565277f6STrond Myklebust out_unblock_sillyrename: 1303565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 13041da177e4SLinus Torvalds out: 1305e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1306e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 13071da177e4SLinus Torvalds return res; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 13111da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 13121da177e4SLinus Torvalds 1313f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 13141da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 13151da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13161da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 131736d43a43SDavid Howells .d_automount = nfs_d_automount, 13181da177e4SLinus Torvalds }; 13191da177e4SLinus Torvalds 13201d6757fbSTrond Myklebust /* 13211d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 13221d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 13231d6757fbSTrond Myklebust */ 13245584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 13251da177e4SLinus Torvalds { 13261d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 13271da177e4SLinus Torvalds return 0; 13281da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 13291da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 13301da177e4SLinus Torvalds return 0; 13311da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 13322c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 13332c463e95SDave Hansen (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 13341da177e4SLinus Torvalds return 0; 13351da177e4SLinus Torvalds return 1; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 1338cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd) 1339cd9a1c0eSTrond Myklebust { 1340cd9a1c0eSTrond Myklebust struct path path = { 1341cd9a1c0eSTrond Myklebust .mnt = nd->path.mnt, 1342cd9a1c0eSTrond Myklebust .dentry = dentry, 1343cd9a1c0eSTrond Myklebust }; 1344cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1345cd9a1c0eSTrond Myklebust struct rpc_cred *cred; 1346cd9a1c0eSTrond Myklebust fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); 1347cd9a1c0eSTrond Myklebust 1348cd9a1c0eSTrond Myklebust cred = rpc_lookup_cred(); 1349cd9a1c0eSTrond Myklebust if (IS_ERR(cred)) 1350cd9a1c0eSTrond Myklebust return ERR_CAST(cred); 1351cd9a1c0eSTrond Myklebust ctx = alloc_nfs_open_context(&path, cred, fmode); 1352cd9a1c0eSTrond Myklebust put_rpccred(cred); 1353cd9a1c0eSTrond Myklebust if (ctx == NULL) 1354cd9a1c0eSTrond Myklebust return ERR_PTR(-ENOMEM); 1355cd9a1c0eSTrond Myklebust return ctx; 1356cd9a1c0eSTrond Myklebust } 1357cd9a1c0eSTrond Myklebust 1358cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1359cd9a1c0eSTrond Myklebust { 1360cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1361cd9a1c0eSTrond Myklebust return 0; 1362cd9a1c0eSTrond Myklebust } 1363cd9a1c0eSTrond Myklebust 1364cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) 1365cd9a1c0eSTrond Myklebust { 1366cd9a1c0eSTrond Myklebust struct file *filp; 1367cd9a1c0eSTrond Myklebust int ret = 0; 1368cd9a1c0eSTrond Myklebust 1369cd9a1c0eSTrond Myklebust /* If the open_intent is for execute, we have an extra check to make */ 1370cd9a1c0eSTrond Myklebust if (ctx->mode & FMODE_EXEC) { 1371cd9a1c0eSTrond Myklebust ret = nfs_may_open(ctx->path.dentry->d_inode, 1372cd9a1c0eSTrond Myklebust ctx->cred, 1373cd9a1c0eSTrond Myklebust nd->intent.open.flags); 1374cd9a1c0eSTrond Myklebust if (ret < 0) 1375cd9a1c0eSTrond Myklebust goto out; 1376cd9a1c0eSTrond Myklebust } 1377cd9a1c0eSTrond Myklebust filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open); 1378cd9a1c0eSTrond Myklebust if (IS_ERR(filp)) 1379cd9a1c0eSTrond Myklebust ret = PTR_ERR(filp); 1380cd9a1c0eSTrond Myklebust else 1381cd9a1c0eSTrond Myklebust nfs_file_set_open_context(filp, ctx); 1382cd9a1c0eSTrond Myklebust out: 1383cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1384cd9a1c0eSTrond Myklebust return ret; 1385cd9a1c0eSTrond Myklebust } 1386cd9a1c0eSTrond Myklebust 13871da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 13881da177e4SLinus Torvalds { 1389cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1390cd9a1c0eSTrond Myklebust struct iattr attr; 13911da177e4SLinus Torvalds struct dentry *res = NULL; 1392f46e0bd3STrond Myklebust struct inode *inode; 1393cd9a1c0eSTrond Myklebust int open_flags; 1394898f635cSTrond Myklebust int err; 13951da177e4SLinus Torvalds 13961e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 13971e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13981e7cb3dcSChuck Lever 13991da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 14005584c306STrond Myklebust if (!is_atomic_open(nd)) 14011da177e4SLinus Torvalds goto no_open; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 14041da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 14051da177e4SLinus Torvalds goto out; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 1408d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1409d4d9cdcbSTrond Myklebust * the dentry. */ 14103516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1411d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 141202a913a7STrond Myklebust goto out; 141302a913a7STrond Myklebust } 14141da177e4SLinus Torvalds 1415cd9a1c0eSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1416cd9a1c0eSTrond Myklebust res = ERR_CAST(ctx); 1417cd9a1c0eSTrond Myklebust if (IS_ERR(ctx)) 1418cd9a1c0eSTrond Myklebust goto out; 1419cd9a1c0eSTrond Myklebust 1420cd9a1c0eSTrond Myklebust open_flags = nd->intent.open.flags; 1421cd9a1c0eSTrond Myklebust if (nd->flags & LOOKUP_CREATE) { 1422cd9a1c0eSTrond Myklebust attr.ia_mode = nd->intent.open.create_mode; 1423cd9a1c0eSTrond Myklebust attr.ia_valid = ATTR_MODE; 1424cd9a1c0eSTrond Myklebust attr.ia_mode &= ~current_umask(); 1425cd9a1c0eSTrond Myklebust } else { 1426898f635cSTrond Myklebust open_flags &= ~(O_EXCL | O_CREAT); 1427cd9a1c0eSTrond Myklebust attr.ia_valid = 0; 1428cd9a1c0eSTrond Myklebust } 1429cd9a1c0eSTrond Myklebust 14301da177e4SLinus Torvalds /* Open the file on the server */ 1431f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 14322b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr); 1433f46e0bd3STrond Myklebust if (IS_ERR(inode)) { 1434f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1435cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1436f46e0bd3STrond Myklebust switch (PTR_ERR(inode)) { 14371da177e4SLinus Torvalds /* Make a negative dentry */ 14381da177e4SLinus Torvalds case -ENOENT: 1439f46e0bd3STrond Myklebust d_add(dentry, NULL); 144002a913a7STrond Myklebust res = NULL; 144102a913a7STrond Myklebust goto out; 14421da177e4SLinus Torvalds /* This turned out not to be a regular file */ 14436f926b5bSTrond Myklebust case -ENOTDIR: 14446f926b5bSTrond Myklebust goto no_open; 14451da177e4SLinus Torvalds case -ELOOP: 14461da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 14471da177e4SLinus Torvalds goto no_open; 144823ebbd9aSTrond Myklebust /* case -EISDIR: */ 14491da177e4SLinus Torvalds /* case -EINVAL: */ 14501da177e4SLinus Torvalds default: 1451f46e0bd3STrond Myklebust res = ERR_CAST(inode); 14521da177e4SLinus Torvalds goto out; 14531da177e4SLinus Torvalds } 1454cd9a1c0eSTrond Myklebust } 1455f46e0bd3STrond Myklebust res = d_add_unique(dentry, inode); 1456898f635cSTrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1457f46e0bd3STrond Myklebust if (res != NULL) { 1458f46e0bd3STrond Myklebust dput(ctx->path.dentry); 1459f46e0bd3STrond Myklebust ctx->path.dentry = dget(res); 14601da177e4SLinus Torvalds dentry = res; 1461f46e0bd3STrond Myklebust } 1462898f635cSTrond Myklebust err = nfs_intent_set_file(nd, ctx); 1463898f635cSTrond Myklebust if (err < 0) { 1464898f635cSTrond Myklebust if (res != NULL) 1465898f635cSTrond Myklebust dput(res); 1466898f635cSTrond Myklebust return ERR_PTR(err); 1467898f635cSTrond Myklebust } 14681da177e4SLinus Torvalds out: 1469f46e0bd3STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14701da177e4SLinus Torvalds return res; 14711da177e4SLinus Torvalds no_open: 14721da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 14761da177e4SLinus Torvalds { 14771da177e4SLinus Torvalds struct dentry *parent = NULL; 1478657e94b6SNick Piggin struct inode *inode; 14791da177e4SLinus Torvalds struct inode *dir; 1480b8d4caddSTrond Myklebust struct nfs_open_context *ctx; 14811da177e4SLinus Torvalds int openflags, ret = 0; 14821da177e4SLinus Torvalds 1483657e94b6SNick Piggin if (nd->flags & LOOKUP_RCU) 1484657e94b6SNick Piggin return -ECHILD; 1485657e94b6SNick Piggin 1486657e94b6SNick Piggin inode = dentry->d_inode; 14871f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 14885584c306STrond Myklebust goto no_open; 14892b484297STrond Myklebust 14901da177e4SLinus Torvalds parent = dget_parent(dentry); 14911da177e4SLinus Torvalds dir = parent->d_inode; 14922b484297STrond Myklebust 14931da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 14941da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 14951da177e4SLinus Torvalds */ 1496216d5d06STrond Myklebust if (inode == NULL) { 1497216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1498216d5d06STrond Myklebust ret = 1; 14991da177e4SLinus Torvalds goto out; 1500216d5d06STrond Myklebust } 1501216d5d06STrond Myklebust 15021da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 15031da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 15045584c306STrond Myklebust goto no_open_dput; 15051da177e4SLinus Torvalds openflags = nd->intent.open.flags; 15061da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 15071da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 15085584c306STrond Myklebust goto no_open_dput; 15091da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 15100a377cffSTrond Myklebust openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); 15111da177e4SLinus Torvalds 1512b8d4caddSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1513b8d4caddSTrond Myklebust ret = PTR_ERR(ctx); 1514b8d4caddSTrond Myklebust if (IS_ERR(ctx)) 1515b8d4caddSTrond Myklebust goto out; 15161da177e4SLinus Torvalds /* 15171b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 15181da177e4SLinus Torvalds * operations that change the directory. We therefore save the 15191da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 15201da177e4SLinus Torvalds */ 15212b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL); 1522535918f1STrond Myklebust if (IS_ERR(inode)) { 1523535918f1STrond Myklebust ret = PTR_ERR(inode); 1524535918f1STrond Myklebust switch (ret) { 1525535918f1STrond Myklebust case -EPERM: 1526535918f1STrond Myklebust case -EACCES: 1527535918f1STrond Myklebust case -EDQUOT: 1528535918f1STrond Myklebust case -ENOSPC: 1529535918f1STrond Myklebust case -EROFS: 1530535918f1STrond Myklebust goto out_put_ctx; 1531535918f1STrond Myklebust default: 1532535918f1STrond Myklebust goto out_drop; 1533535918f1STrond Myklebust } 1534535918f1STrond Myklebust } 1535535918f1STrond Myklebust iput(inode); 1536898f635cSTrond Myklebust if (inode != dentry->d_inode) 1537535918f1STrond Myklebust goto out_drop; 1538898f635cSTrond Myklebust 1539898f635cSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1540898f635cSTrond Myklebust ret = nfs_intent_set_file(nd, ctx); 1541898f635cSTrond Myklebust if (ret >= 0) 1542898f635cSTrond Myklebust ret = 1; 15431da177e4SLinus Torvalds out: 15441da177e4SLinus Torvalds dput(parent); 15451da177e4SLinus Torvalds return ret; 1546535918f1STrond Myklebust out_drop: 1547535918f1STrond Myklebust d_drop(dentry); 1548535918f1STrond Myklebust ret = 0; 1549535918f1STrond Myklebust out_put_ctx: 1550535918f1STrond Myklebust put_nfs_open_context(ctx); 1551535918f1STrond Myklebust goto out; 1552535918f1STrond Myklebust 15535584c306STrond Myklebust no_open_dput: 15541da177e4SLinus Torvalds dput(parent); 15555584c306STrond Myklebust no_open: 15561da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 15571da177e4SLinus Torvalds } 1558c0204fd2STrond Myklebust 1559c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, 1560c0204fd2STrond Myklebust struct nameidata *nd) 1561c0204fd2STrond Myklebust { 1562c0204fd2STrond Myklebust struct nfs_open_context *ctx = NULL; 1563c0204fd2STrond Myklebust struct iattr attr; 1564c0204fd2STrond Myklebust int error; 1565c0204fd2STrond Myklebust int open_flags = 0; 1566c0204fd2STrond Myklebust 1567c0204fd2STrond Myklebust dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 1568c0204fd2STrond Myklebust dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 1569c0204fd2STrond Myklebust 1570c0204fd2STrond Myklebust attr.ia_mode = mode; 1571c0204fd2STrond Myklebust attr.ia_valid = ATTR_MODE; 1572c0204fd2STrond Myklebust 1573c0204fd2STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) { 1574c0204fd2STrond Myklebust open_flags = nd->intent.open.flags; 1575c0204fd2STrond Myklebust 1576c0204fd2STrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1577c0204fd2STrond Myklebust error = PTR_ERR(ctx); 1578c0204fd2STrond Myklebust if (IS_ERR(ctx)) 1579898f635cSTrond Myklebust goto out_err_drop; 1580c0204fd2STrond Myklebust } 1581c0204fd2STrond Myklebust 1582c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx); 1583c0204fd2STrond Myklebust if (error != 0) 1584c0204fd2STrond Myklebust goto out_put_ctx; 1585898f635cSTrond Myklebust if (ctx != NULL) { 1586898f635cSTrond Myklebust error = nfs_intent_set_file(nd, ctx); 1587898f635cSTrond Myklebust if (error < 0) 1588898f635cSTrond Myklebust goto out_err; 1589898f635cSTrond Myklebust } 1590c0204fd2STrond Myklebust return 0; 1591c0204fd2STrond Myklebust out_put_ctx: 1592c0204fd2STrond Myklebust if (ctx != NULL) 1593c0204fd2STrond Myklebust put_nfs_open_context(ctx); 1594898f635cSTrond Myklebust out_err_drop: 1595c0204fd2STrond Myklebust d_drop(dentry); 1596898f635cSTrond Myklebust out_err: 1597c0204fd2STrond Myklebust return error; 1598c0204fd2STrond Myklebust } 1599c0204fd2STrond Myklebust 16001da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds /* 16031da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 16041da177e4SLinus Torvalds */ 16051da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 16061da177e4SLinus Torvalds struct nfs_fattr *fattr) 16071da177e4SLinus Torvalds { 1608fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1609fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 16101da177e4SLinus Torvalds struct inode *inode; 16111da177e4SLinus Torvalds int error = -EACCES; 16121da177e4SLinus Torvalds 1613fab728e1STrond Myklebust d_drop(dentry); 1614fab728e1STrond Myklebust 16151da177e4SLinus Torvalds /* We may have been initialized further down */ 16161da177e4SLinus Torvalds if (dentry->d_inode) 1617fab728e1STrond Myklebust goto out; 16181da177e4SLinus Torvalds if (fhandle->size == 0) { 16191da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 16201da177e4SLinus Torvalds if (error) 1621fab728e1STrond Myklebust goto out_error; 16221da177e4SLinus Torvalds } 16235724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16241da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 16251da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 16268fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 16271da177e4SLinus Torvalds if (error < 0) 1628fab728e1STrond Myklebust goto out_error; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 163103f28e3aSTrond Myklebust error = PTR_ERR(inode); 163203f28e3aSTrond Myklebust if (IS_ERR(inode)) 1633fab728e1STrond Myklebust goto out_error; 1634fab728e1STrond Myklebust d_add(dentry, inode); 1635fab728e1STrond Myklebust out: 1636fab728e1STrond Myklebust dput(parent); 16371da177e4SLinus Torvalds return 0; 1638fab728e1STrond Myklebust out_error: 1639fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1640fab728e1STrond Myklebust dput(parent); 1641fab728e1STrond Myklebust return error; 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds /* 16451da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 16461da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 16471da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 16481da177e4SLinus Torvalds * reply path made it appear to have failed. 16491da177e4SLinus Torvalds */ 16501da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 16511da177e4SLinus Torvalds struct nameidata *nd) 16521da177e4SLinus Torvalds { 16531da177e4SLinus Torvalds struct iattr attr; 16541da177e4SLinus Torvalds int error; 16558a0eebf6STrond Myklebust int open_flags = 0; 16561da177e4SLinus Torvalds 16571e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 16581e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds attr.ia_mode = mode; 16611da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16621da177e4SLinus Torvalds 16638a0eebf6STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) 16648a0eebf6STrond Myklebust open_flags = nd->intent.open.flags; 16658a0eebf6STrond Myklebust 16668a0eebf6STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL); 16671da177e4SLinus Torvalds if (error != 0) 16681da177e4SLinus Torvalds goto out_err; 16691da177e4SLinus Torvalds return 0; 16701da177e4SLinus Torvalds out_err: 16711da177e4SLinus Torvalds d_drop(dentry); 16721da177e4SLinus Torvalds return error; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds /* 16761da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 16771da177e4SLinus Torvalds */ 16781da177e4SLinus Torvalds static int 16791da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 16801da177e4SLinus Torvalds { 16811da177e4SLinus Torvalds struct iattr attr; 16821da177e4SLinus Torvalds int status; 16831da177e4SLinus Torvalds 16841e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 16851e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 16881da177e4SLinus Torvalds return -EINVAL; 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds attr.ia_mode = mode; 16911da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 16941da177e4SLinus Torvalds if (status != 0) 16951da177e4SLinus Torvalds goto out_err; 16961da177e4SLinus Torvalds return 0; 16971da177e4SLinus Torvalds out_err: 16981da177e4SLinus Torvalds d_drop(dentry); 16991da177e4SLinus Torvalds return status; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds /* 17031da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17041da177e4SLinus Torvalds */ 17051da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 17061da177e4SLinus Torvalds { 17071da177e4SLinus Torvalds struct iattr attr; 17081da177e4SLinus Torvalds int error; 17091da177e4SLinus Torvalds 17101e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 17111e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17141da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 17171da177e4SLinus Torvalds if (error != 0) 17181da177e4SLinus Torvalds goto out_err; 17191da177e4SLinus Torvalds return 0; 17201da177e4SLinus Torvalds out_err: 17211da177e4SLinus Torvalds d_drop(dentry); 17221da177e4SLinus Torvalds return error; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 1725d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1726d45b9d8bSTrond Myklebust { 1727d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1728d45b9d8bSTrond Myklebust d_delete(dentry); 1729d45b9d8bSTrond Myklebust } 1730d45b9d8bSTrond Myklebust 17311da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 17321da177e4SLinus Torvalds { 17331da177e4SLinus Torvalds int error; 17341da177e4SLinus Torvalds 17351e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 17361e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 17391da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 17401da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1741ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1742d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1743d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds return error; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds /* 17491da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 17501da177e4SLinus Torvalds * and after checking that the file has only one user. 17511da177e4SLinus Torvalds * 17521da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 17531da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 17541da177e4SLinus Torvalds */ 17551da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 17561da177e4SLinus Torvalds { 17571da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 17581da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 17591da177e4SLinus Torvalds int error = -EBUSY; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 17621da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 17651da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 17661da177e4SLinus Torvalds error = 0; 17671da177e4SLinus Torvalds goto out; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds if (inode != NULL) { 1771cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 17721da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 17731da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 17741da177e4SLinus Torvalds if (error == 0) 17751b83d707STrond Myklebust nfs_drop_nlink(inode); 17765ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 17771da177e4SLinus Torvalds } else 17781da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1779d45b9d8bSTrond Myklebust if (error == -ENOENT) 1780d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 17811da177e4SLinus Torvalds out: 17821da177e4SLinus Torvalds return error; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 17861da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 17871da177e4SLinus Torvalds * 17881da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 17891da177e4SLinus Torvalds */ 17901da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 17911da177e4SLinus Torvalds { 17921da177e4SLinus Torvalds int error; 17931da177e4SLinus Torvalds int need_rehash = 0; 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 17961da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 1799b7ab39f6SNick Piggin if (dentry->d_count > 1) { 18001da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1801ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1802ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 18031da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 18041da177e4SLinus Torvalds return error; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 18071da177e4SLinus Torvalds __d_drop(dentry); 18081da177e4SLinus Torvalds need_rehash = 1; 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 18111da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1812d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 18131da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18141da177e4SLinus Torvalds } else if (need_rehash) 18151da177e4SLinus Torvalds d_rehash(dentry); 18161da177e4SLinus Torvalds return error; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 1819873101b3SChuck Lever /* 1820873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1821873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1822873101b3SChuck Lever * using prepare_write/commit_write. 1823873101b3SChuck Lever * 1824873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1825873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1826873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1827873101b3SChuck Lever * symlink request has completed on the server. 1828873101b3SChuck Lever * 1829873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1830873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1831873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1832873101b3SChuck Lever * and move the raw page into its mapping. 1833873101b3SChuck Lever */ 1834873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 18351da177e4SLinus Torvalds { 1836873101b3SChuck Lever struct pagevec lru_pvec; 1837873101b3SChuck Lever struct page *page; 1838873101b3SChuck Lever char *kaddr; 18391da177e4SLinus Torvalds struct iattr attr; 1840873101b3SChuck Lever unsigned int pathlen = strlen(symname); 18411da177e4SLinus Torvalds int error; 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 18441da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 18451da177e4SLinus Torvalds 1846873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1847873101b3SChuck Lever return -ENAMETOOLONG; 18481da177e4SLinus Torvalds 1849873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1850873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 18511da177e4SLinus Torvalds 185283d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 185376566991STrond Myklebust if (!page) 1854873101b3SChuck Lever return -ENOMEM; 1855873101b3SChuck Lever 1856873101b3SChuck Lever kaddr = kmap_atomic(page, KM_USER0); 1857873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1858873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1859873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1860873101b3SChuck Lever kunmap_atomic(kaddr, KM_USER0); 1861873101b3SChuck Lever 186294a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1863873101b3SChuck Lever if (error != 0) { 1864873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1865873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1866873101b3SChuck Lever dentry->d_name.name, symname, error); 18671da177e4SLinus Torvalds d_drop(dentry); 1868873101b3SChuck Lever __free_page(page); 18691da177e4SLinus Torvalds return error; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds 1872873101b3SChuck Lever /* 1873873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1874873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1875873101b3SChuck Lever */ 1876873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1877873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1878873101b3SChuck Lever GFP_KERNEL)) { 187939cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 18804f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1881873101b3SChuck Lever SetPageUptodate(page); 1882873101b3SChuck Lever unlock_page(page); 1883873101b3SChuck Lever } else 1884873101b3SChuck Lever __free_page(page); 1885873101b3SChuck Lever 1886873101b3SChuck Lever return 0; 1887873101b3SChuck Lever } 1888873101b3SChuck Lever 18891da177e4SLinus Torvalds static int 18901da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 18911da177e4SLinus Torvalds { 18921da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 18931da177e4SLinus Torvalds int error; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 18961da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18971da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 18981da177e4SLinus Torvalds 18999a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 19009a3936aaSTrond Myklebust 19019697d234STrond Myklebust d_drop(dentry); 19021da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1903cf809556STrond Myklebust if (error == 0) { 19047de9c6eeSAl Viro ihold(inode); 19059697d234STrond Myklebust d_add(dentry, inode); 1906cf809556STrond Myklebust } 19071da177e4SLinus Torvalds return error; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /* 19111da177e4SLinus Torvalds * RENAME 19121da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 19131da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 19141da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 19151da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 19161da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 19171da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 19181da177e4SLinus Torvalds * 19191da177e4SLinus Torvalds * FIXED. 19201da177e4SLinus Torvalds * 19211da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 19221da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 19231da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 19241da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 19251da177e4SLinus Torvalds * using the inode layer 19261da177e4SLinus Torvalds * 19271da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 19281da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 19291da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 19301da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 19311da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 19321da177e4SLinus Torvalds * the rename. 19331da177e4SLinus Torvalds */ 19341da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 19351da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 19361da177e4SLinus Torvalds { 19371da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 19381da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 19391da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 19401da177e4SLinus Torvalds int error = -EBUSY; 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 19431da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 19441da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 1945b7ab39f6SNick Piggin new_dentry->d_count); 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* 194828f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 194928f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 195028f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 195128f79a1aSMiklos Szeredi * the new target. 19521da177e4SLinus Torvalds */ 195327226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 195427226104SMiklos Szeredi /* 195527226104SMiklos Szeredi * To prevent any new references to the target during the 195627226104SMiklos Szeredi * rename, we unhash the dentry in advance. 195727226104SMiklos Szeredi */ 195827226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 195927226104SMiklos Szeredi d_drop(new_dentry); 196027226104SMiklos Szeredi rehash = new_dentry; 196127226104SMiklos Szeredi } 196227226104SMiklos Szeredi 1963b7ab39f6SNick Piggin if (new_dentry->d_count > 2) { 19641da177e4SLinus Torvalds int err; 196527226104SMiklos Szeredi 19661da177e4SLinus Torvalds /* copy the target dentry's name */ 19671da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 19681da177e4SLinus Torvalds &new_dentry->d_name); 19691da177e4SLinus Torvalds if (!dentry) 19701da177e4SLinus Torvalds goto out; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds /* silly-rename the existing target ... */ 19731da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 197424e93025SMiklos Szeredi if (err) 19751da177e4SLinus Torvalds goto out; 197624e93025SMiklos Szeredi 197724e93025SMiklos Szeredi new_dentry = dentry; 197856335936SOGAWA Hirofumi rehash = NULL; 197924e93025SMiklos Szeredi new_inode = NULL; 1980b1e4adf4STrond Myklebust } 198127226104SMiklos Szeredi } 19821da177e4SLinus Torvalds 1983cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 1984b1e4adf4STrond Myklebust if (new_inode != NULL) 198524174119STrond Myklebust nfs_inode_return_delegation(new_inode); 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 19881da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 19895ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 19901da177e4SLinus Torvalds out: 19911da177e4SLinus Torvalds if (rehash) 19921da177e4SLinus Torvalds d_rehash(rehash); 19931da177e4SLinus Torvalds if (!error) { 1994b1e4adf4STrond Myklebust if (new_inode != NULL) 1995b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 19961da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 19978fb559f8SChuck Lever nfs_set_verifier(new_dentry, 19988fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1999d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 2000d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds /* new dentry created? */ 20031da177e4SLinus Torvalds if (dentry) 20041da177e4SLinus Torvalds dput(dentry); 20051da177e4SLinus Torvalds return error; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 2008cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2009cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2010cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2011cfcea3e8STrond Myklebust 20121c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 20131c3c07e9STrond Myklebust { 20141c3c07e9STrond Myklebust put_rpccred(entry->cred); 20151c3c07e9STrond Myklebust kfree(entry); 2016cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 2017cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 2018cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 20191c3c07e9STrond Myklebust } 20201c3c07e9STrond Myklebust 20211a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 20221a81bb8aSTrond Myklebust { 20231a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 20241a81bb8aSTrond Myklebust 20251a81bb8aSTrond Myklebust while (!list_empty(head)) { 20261a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 20271a81bb8aSTrond Myklebust list_del(&cache->lru); 20281a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 20291a81bb8aSTrond Myklebust } 20301a81bb8aSTrond Myklebust } 20311a81bb8aSTrond Myklebust 20327f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) 2033979df72eSTrond Myklebust { 2034979df72eSTrond Myklebust LIST_HEAD(head); 2035aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2036979df72eSTrond Myklebust struct nfs_access_entry *cache; 2037979df72eSTrond Myklebust 203861d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 203961d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 20409c7e7e23STrond Myklebust 2041a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2042aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2043979df72eSTrond Myklebust struct inode *inode; 2044979df72eSTrond Myklebust 2045979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2046979df72eSTrond Myklebust break; 20479c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2048979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2049979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2050979df72eSTrond Myklebust goto remove_lru_entry; 2051979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2052979df72eSTrond Myklebust struct nfs_access_entry, lru); 2053979df72eSTrond Myklebust list_move(&cache->lru, &head); 2054979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2055979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2056979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2057979df72eSTrond Myklebust &nfs_access_lru_list); 2058979df72eSTrond Myklebust else { 2059979df72eSTrond Myklebust remove_lru_entry: 2060979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 20619c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 2062979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 20639c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 2064979df72eSTrond Myklebust } 206559844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2066979df72eSTrond Myklebust } 2067979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 20681a81bb8aSTrond Myklebust nfs_access_free_list(&head); 2069979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 2070979df72eSTrond Myklebust } 2071979df72eSTrond Myklebust 20721a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 20731c3c07e9STrond Myklebust { 20741c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 20751a81bb8aSTrond Myklebust struct rb_node *n; 20761c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20771c3c07e9STrond Myklebust 20781c3c07e9STrond Myklebust /* Unhook entries from the cache */ 20791c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 20801c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20811c3c07e9STrond Myklebust rb_erase(n, root_node); 20821a81bb8aSTrond Myklebust list_move(&entry->lru, head); 20831c3c07e9STrond Myklebust } 20841c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 20851c3c07e9STrond Myklebust } 20861c3c07e9STrond Myklebust 20871c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 20881c3c07e9STrond Myklebust { 20891a81bb8aSTrond Myklebust LIST_HEAD(head); 20901a81bb8aSTrond Myklebust 20911a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 20921a81bb8aSTrond Myklebust return; 2093cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2094cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 20951a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2096cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2097cfcea3e8STrond Myklebust 20981c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20991a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 21001a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 21011a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21021a81bb8aSTrond Myklebust nfs_access_free_list(&head); 21031c3c07e9STrond Myklebust } 21041c3c07e9STrond Myklebust 21051c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 21061c3c07e9STrond Myklebust { 21071c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 21081c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21091c3c07e9STrond Myklebust 21101c3c07e9STrond Myklebust while (n != NULL) { 21111c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 21121c3c07e9STrond Myklebust 21131c3c07e9STrond Myklebust if (cred < entry->cred) 21141c3c07e9STrond Myklebust n = n->rb_left; 21151c3c07e9STrond Myklebust else if (cred > entry->cred) 21161c3c07e9STrond Myklebust n = n->rb_right; 21171c3c07e9STrond Myklebust else 21181c3c07e9STrond Myklebust return entry; 21191c3c07e9STrond Myklebust } 21201c3c07e9STrond Myklebust return NULL; 21211c3c07e9STrond Myklebust } 21221c3c07e9STrond Myklebust 2123af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 21241da177e4SLinus Torvalds { 212555296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 21261c3c07e9STrond Myklebust struct nfs_access_entry *cache; 21271c3c07e9STrond Myklebust int err = -ENOENT; 21281da177e4SLinus Torvalds 21291c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 21301c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 21311c3c07e9STrond Myklebust goto out_zap; 21321c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 21331c3c07e9STrond Myklebust if (cache == NULL) 21341c3c07e9STrond Myklebust goto out; 2135b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 213664672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 21371c3c07e9STrond Myklebust goto out_stale; 21381c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 21391c3c07e9STrond Myklebust res->cred = cache->cred; 21401c3c07e9STrond Myklebust res->mask = cache->mask; 2141cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 21421c3c07e9STrond Myklebust err = 0; 21431c3c07e9STrond Myklebust out: 21441c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21451c3c07e9STrond Myklebust return err; 21461c3c07e9STrond Myklebust out_stale: 21471c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2148cfcea3e8STrond Myklebust list_del(&cache->lru); 21491c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21501c3c07e9STrond Myklebust nfs_access_free_entry(cache); 21511da177e4SLinus Torvalds return -ENOENT; 21521c3c07e9STrond Myklebust out_zap: 21531a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 21541a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 21551c3c07e9STrond Myklebust return -ENOENT; 21561c3c07e9STrond Myklebust } 21571c3c07e9STrond Myklebust 21581c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 21591c3c07e9STrond Myklebust { 2160cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2161cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 21621c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 21631c3c07e9STrond Myklebust struct rb_node *parent = NULL; 21641c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21651c3c07e9STrond Myklebust 21661c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 21671c3c07e9STrond Myklebust while (*p != NULL) { 21681c3c07e9STrond Myklebust parent = *p; 21691c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 21701c3c07e9STrond Myklebust 21711c3c07e9STrond Myklebust if (set->cred < entry->cred) 21721c3c07e9STrond Myklebust p = &parent->rb_left; 21731c3c07e9STrond Myklebust else if (set->cred > entry->cred) 21741c3c07e9STrond Myklebust p = &parent->rb_right; 21751c3c07e9STrond Myklebust else 21761c3c07e9STrond Myklebust goto found; 21771c3c07e9STrond Myklebust } 21781c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 21791c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2180cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 21811c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21821c3c07e9STrond Myklebust return; 21831c3c07e9STrond Myklebust found: 21841c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2185cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2186cfcea3e8STrond Myklebust list_del(&entry->lru); 21871c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21881c3c07e9STrond Myklebust nfs_access_free_entry(entry); 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 2191af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 21921da177e4SLinus Torvalds { 21931c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 21941c3c07e9STrond Myklebust if (cache == NULL) 21951c3c07e9STrond Myklebust return; 21961c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 21971da177e4SLinus Torvalds cache->jiffies = set->jiffies; 21981c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 21991da177e4SLinus Torvalds cache->mask = set->mask; 22001c3c07e9STrond Myklebust 22011c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2202cfcea3e8STrond Myklebust 2203cfcea3e8STrond Myklebust /* Update accounting */ 2204cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 2205cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 2206cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 2207cfcea3e8STrond Myklebust 2208cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 22091a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2210cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22111a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 22121a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 22131a81bb8aSTrond Myklebust &nfs_access_lru_list); 2214cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2215cfcea3e8STrond Myklebust } 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 22191da177e4SLinus Torvalds { 22201da177e4SLinus Torvalds struct nfs_access_entry cache; 22211da177e4SLinus Torvalds int status; 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 22241da177e4SLinus Torvalds if (status == 0) 22251da177e4SLinus Torvalds goto out; 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 22281da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 22291da177e4SLinus Torvalds cache.cred = cred; 22301da177e4SLinus Torvalds cache.jiffies = jiffies; 22311da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2232a71ee337SSuresh Jayaraman if (status != 0) { 2233a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2234a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2235a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2236a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2237a71ee337SSuresh Jayaraman } 22381da177e4SLinus Torvalds return status; 2239a71ee337SSuresh Jayaraman } 22401da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 22411da177e4SLinus Torvalds out: 2242e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 22431da177e4SLinus Torvalds return 0; 22441da177e4SLinus Torvalds return -EACCES; 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 2247af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2248af22f94aSTrond Myklebust { 2249af22f94aSTrond Myklebust int mask = 0; 2250af22f94aSTrond Myklebust 2251af22f94aSTrond Myklebust if (openflags & FMODE_READ) 2252af22f94aSTrond Myklebust mask |= MAY_READ; 2253af22f94aSTrond Myklebust if (openflags & FMODE_WRITE) 2254af22f94aSTrond Myklebust mask |= MAY_WRITE; 2255af22f94aSTrond Myklebust if (openflags & FMODE_EXEC) 2256af22f94aSTrond Myklebust mask |= MAY_EXEC; 2257af22f94aSTrond Myklebust return mask; 2258af22f94aSTrond Myklebust } 2259af22f94aSTrond Myklebust 2260af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2261af22f94aSTrond Myklebust { 2262af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2263af22f94aSTrond Myklebust } 2264af22f94aSTrond Myklebust 2265b74c79e9SNick Piggin int nfs_permission(struct inode *inode, int mask, unsigned int flags) 22661da177e4SLinus Torvalds { 22671da177e4SLinus Torvalds struct rpc_cred *cred; 22681da177e4SLinus Torvalds int res = 0; 22691da177e4SLinus Torvalds 2270b74c79e9SNick Piggin if (flags & IPERM_FLAG_RCU) 2271b74c79e9SNick Piggin return -ECHILD; 2272b74c79e9SNick Piggin 227391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 227491d5b470SChuck Lever 2275e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 22761da177e4SLinus Torvalds goto out; 22771da177e4SLinus Torvalds /* Is this sys_access() ? */ 22789cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 22791da177e4SLinus Torvalds goto force_lookup; 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 22821da177e4SLinus Torvalds case S_IFLNK: 22831da177e4SLinus Torvalds goto out; 22841da177e4SLinus Torvalds case S_IFREG: 22851da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 22861da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 22877ee2cb7fSFrank Filz && (mask & MAY_OPEN) 22887ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 22891da177e4SLinus Torvalds goto out; 22901da177e4SLinus Torvalds break; 22911da177e4SLinus Torvalds case S_IFDIR: 22921da177e4SLinus Torvalds /* 22931da177e4SLinus Torvalds * Optimize away all write operations, since the server 22941da177e4SLinus Torvalds * will check permissions when we perform the op. 22951da177e4SLinus Torvalds */ 22961da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 22971da177e4SLinus Torvalds goto out; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds force_lookup: 23011da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 23021da177e4SLinus Torvalds goto out_notsup; 23031da177e4SLinus Torvalds 230498a8e323STrond Myklebust cred = rpc_lookup_cred(); 23051da177e4SLinus Torvalds if (!IS_ERR(cred)) { 23061da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 23071da177e4SLinus Torvalds put_rpccred(cred); 23081da177e4SLinus Torvalds } else 23091da177e4SLinus Torvalds res = PTR_ERR(cred); 23101da177e4SLinus Torvalds out: 2311f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2312f696a365SMiklos Szeredi res = -EACCES; 2313f696a365SMiklos Szeredi 23141e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 23151e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 23161da177e4SLinus Torvalds return res; 23171da177e4SLinus Torvalds out_notsup: 23181da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 23191da177e4SLinus Torvalds if (res == 0) 2320b74c79e9SNick Piggin res = generic_permission(inode, mask, flags, NULL); 23211e7cb3dcSChuck Lever goto out; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds /* 23251da177e4SLinus Torvalds * Local variables: 23261da177e4SLinus Torvalds * version-control: t 23271da177e4SLinus Torvalds * kept-new-versions: 5 23281da177e4SLinus Torvalds * End: 23291da177e4SLinus Torvalds */ 2330