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 *); 504acdaf27SAl Viro static int nfs_create(struct inode *, struct dentry *, umode_t, struct nameidata *); 5118bb1db3SAl Viro static int nfs_mkdir(struct inode *, struct dentry *, umode_t); 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 *); 561a67aafbSAl Viro static int nfs_mknod(struct inode *, struct dentry *, umode_t, dev_t); 571da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 581da177e4SLinus Torvalds struct inode *, struct dentry *); 5902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, 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 *); 1154acdaf27SAl Viro static int nfs_open_create(struct inode *dir, struct dentry *dentry, umode_t 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 1370c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, 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) { 1428ef2ce3eSBryan Schumaker ctx->duped = 0; 1430c030806STrond Myklebust ctx->attr_gencount = NFS_I(dir)->attr_gencount; 144480c2006SBryan Schumaker ctx->dir_cookie = 0; 1458ef2ce3eSBryan Schumaker ctx->dup_cookie = 0; 146480c2006SBryan Schumaker ctx->cred = get_rpccred(cred); 147480c2006SBryan Schumaker return ctx; 148480c2006SBryan Schumaker } 1490c030806STrond Myklebust return ERR_PTR(-ENOMEM); 1500c030806STrond Myklebust } 151480c2006SBryan Schumaker 152480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx) 153480c2006SBryan Schumaker { 154480c2006SBryan Schumaker put_rpccred(ctx->cred); 155480c2006SBryan Schumaker kfree(ctx); 156480c2006SBryan Schumaker } 157480c2006SBryan Schumaker 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * Open file 1601da177e4SLinus Torvalds */ 1611da177e4SLinus Torvalds static int 1621da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1631da177e4SLinus Torvalds { 164480c2006SBryan Schumaker int res = 0; 165480c2006SBryan Schumaker struct nfs_open_dir_context *ctx; 166480c2006SBryan Schumaker struct rpc_cred *cred; 1671da177e4SLinus Torvalds 1686da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 169cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 170cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 171cc0dd2d1SChuck Lever 172cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1731e7cb3dcSChuck Lever 174480c2006SBryan Schumaker cred = rpc_lookup_cred(); 175480c2006SBryan Schumaker if (IS_ERR(cred)) 176480c2006SBryan Schumaker return PTR_ERR(cred); 1770c030806STrond Myklebust ctx = alloc_nfs_open_dir_context(inode, cred); 178480c2006SBryan Schumaker if (IS_ERR(ctx)) { 179480c2006SBryan Schumaker res = PTR_ERR(ctx); 180480c2006SBryan Schumaker goto out; 181480c2006SBryan Schumaker } 182480c2006SBryan Schumaker filp->private_data = ctx; 183f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 184f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 185f5a73672SNeil Brown * have been called, so we need to refresh the 186f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 187f5a73672SNeil Brown */ 188f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 189f5a73672SNeil Brown } 190480c2006SBryan Schumaker out: 191480c2006SBryan Schumaker put_rpccred(cred); 1921da177e4SLinus Torvalds return res; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195480c2006SBryan Schumaker static int 196480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp) 197480c2006SBryan Schumaker { 198480c2006SBryan Schumaker put_nfs_open_dir_context(filp->private_data); 199480c2006SBryan Schumaker return 0; 200480c2006SBryan Schumaker } 201480c2006SBryan Schumaker 202d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 203d1bacf9eSBryan Schumaker u64 cookie; 204d1bacf9eSBryan Schumaker u64 ino; 205d1bacf9eSBryan Schumaker struct qstr string; 2060b26a0bfSTrond Myklebust unsigned char d_type; 207d1bacf9eSBryan Schumaker }; 208d1bacf9eSBryan Schumaker 209d1bacf9eSBryan Schumaker struct nfs_cache_array { 21088b8e133SChuck Lever int size; 211d1bacf9eSBryan Schumaker int eof_index; 212d1bacf9eSBryan Schumaker u64 last_cookie; 213d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 214d1bacf9eSBryan Schumaker }; 215d1bacf9eSBryan Schumaker 216573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int); 2171da177e4SLinus Torvalds typedef struct { 2181da177e4SLinus Torvalds struct file *file; 2191da177e4SLinus Torvalds struct page *page; 2201da177e4SLinus Torvalds unsigned long page_index; 221f0dd2136STrond Myklebust u64 *dir_cookie; 2220aded708STrond Myklebust u64 last_cookie; 223f0dd2136STrond Myklebust loff_t current_index; 2241da177e4SLinus Torvalds decode_dirent_t decode; 225d1bacf9eSBryan Schumaker 2261f4eab7eSNeil Brown unsigned long timestamp; 2274704f0e2STrond Myklebust unsigned long gencount; 228d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 229d1bacf9eSBryan Schumaker unsigned int plus:1; 230d1bacf9eSBryan Schumaker unsigned int eof:1; 2311da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 2321da177e4SLinus Torvalds 233d1bacf9eSBryan Schumaker /* 234d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 2351da177e4SLinus Torvalds */ 2361da177e4SLinus Torvalds static 237d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 2381da177e4SLinus Torvalds { 2398cd51a0cSTrond Myklebust void *ptr; 240d1bacf9eSBryan Schumaker if (page == NULL) 241d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 2428cd51a0cSTrond Myklebust ptr = kmap(page); 2438cd51a0cSTrond Myklebust if (ptr == NULL) 2448cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 2458cd51a0cSTrond Myklebust return ptr; 246d1bacf9eSBryan Schumaker } 247d1bacf9eSBryan Schumaker 248d1bacf9eSBryan Schumaker static 249d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 250d1bacf9eSBryan Schumaker { 251d1bacf9eSBryan Schumaker kunmap(page); 252d1bacf9eSBryan Schumaker } 253d1bacf9eSBryan Schumaker 254d1bacf9eSBryan Schumaker /* 255d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 256d1bacf9eSBryan Schumaker */ 257d1bacf9eSBryan Schumaker static 25811de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 259d1bacf9eSBryan Schumaker { 26011de3b11STrond Myklebust struct nfs_cache_array *array; 261d1bacf9eSBryan Schumaker int i; 2628cd51a0cSTrond Myklebust 2632b86ce2dSCong Wang array = kmap_atomic(page); 264d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 265d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 2662b86ce2dSCong Wang kunmap_atomic(array); 267d1bacf9eSBryan Schumaker } 268d1bacf9eSBryan Schumaker 269d1bacf9eSBryan Schumaker /* 270d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 271d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 272d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 273d1bacf9eSBryan Schumaker */ 274d1bacf9eSBryan Schumaker static 2754a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 276d1bacf9eSBryan Schumaker { 277d1bacf9eSBryan Schumaker string->len = len; 278d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2794a201d6eSTrond Myklebust if (string->name == NULL) 2804a201d6eSTrond Myklebust return -ENOMEM; 28104e4bd1cSCatalin Marinas /* 28204e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 28304e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 28404e4bd1cSCatalin Marinas */ 28504e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2864a201d6eSTrond Myklebust string->hash = full_name_hash(name, len); 2874a201d6eSTrond Myklebust return 0; 288d1bacf9eSBryan Schumaker } 289d1bacf9eSBryan Schumaker 290d1bacf9eSBryan Schumaker static 291d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 292d1bacf9eSBryan Schumaker { 293d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2944a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2954a201d6eSTrond Myklebust int ret; 2964a201d6eSTrond Myklebust 297d1bacf9eSBryan Schumaker if (IS_ERR(array)) 298d1bacf9eSBryan Schumaker return PTR_ERR(array); 299d1bacf9eSBryan Schumaker 3004a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 3013020093fSTrond Myklebust 3023020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 3033020093fSTrond Myklebust ret = -ENOSPC; 3043020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 3053020093fSTrond Myklebust goto out; 3063020093fSTrond Myklebust 3074a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 3084a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 3090b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 3104a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 3114a201d6eSTrond Myklebust if (ret) 3124a201d6eSTrond Myklebust goto out; 313d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 3148cd51a0cSTrond Myklebust array->size++; 31547c716cbSTrond Myklebust if (entry->eof != 0) 316d1bacf9eSBryan Schumaker array->eof_index = array->size; 3174a201d6eSTrond Myklebust out: 318d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 3194a201d6eSTrond Myklebust return ret; 320d1bacf9eSBryan Schumaker } 321d1bacf9eSBryan Schumaker 322d1bacf9eSBryan Schumaker static 323d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 324d1bacf9eSBryan Schumaker { 325d1bacf9eSBryan Schumaker loff_t diff = desc->file->f_pos - desc->current_index; 326d1bacf9eSBryan Schumaker unsigned int index; 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; 339d1bacf9eSBryan Schumaker return 0; 340d1bacf9eSBryan Schumaker out_eof: 341d1bacf9eSBryan Schumaker desc->eof = 1; 342d1bacf9eSBryan Schumaker return -EBADCOOKIE; 343d1bacf9eSBryan Schumaker } 344d1bacf9eSBryan Schumaker 345d1bacf9eSBryan Schumaker static 346d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 347d1bacf9eSBryan Schumaker { 348d1bacf9eSBryan Schumaker int i; 3498ef2ce3eSBryan Schumaker loff_t new_pos; 350d1bacf9eSBryan Schumaker int status = -EAGAIN; 351d1bacf9eSBryan Schumaker 352d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3538cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 3540c030806STrond Myklebust struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode); 3550c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 3560c030806STrond Myklebust 3578ef2ce3eSBryan Schumaker new_pos = desc->current_index + i; 3580c030806STrond Myklebust if (ctx->attr_gencount != nfsi->attr_gencount 3590c030806STrond Myklebust || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) { 3600c030806STrond Myklebust ctx->duped = 0; 3610c030806STrond Myklebust ctx->attr_gencount = nfsi->attr_gencount; 3620c030806STrond Myklebust } else if (new_pos < desc->file->f_pos) { 3630c030806STrond Myklebust if (ctx->duped > 0 3640c030806STrond Myklebust && ctx->dup_cookie == *desc->dir_cookie) { 3650c030806STrond Myklebust if (printk_ratelimit()) { 3660c030806STrond Myklebust pr_notice("NFS: directory %s/%s contains a readdir loop." 3670c030806STrond Myklebust "Please contact your server vendor. " 368374e4e3eSBryan Schumaker "The file: %s has duplicate cookie %llu\n", 3690c030806STrond Myklebust desc->file->f_dentry->d_parent->d_name.name, 3700c030806STrond Myklebust desc->file->f_dentry->d_name.name, 371374e4e3eSBryan Schumaker array->array[i].string.name, 3720c030806STrond Myklebust *desc->dir_cookie); 3730c030806STrond Myklebust } 3740c030806STrond Myklebust status = -ELOOP; 3750c030806STrond Myklebust goto out; 3760c030806STrond Myklebust } 3778ef2ce3eSBryan Schumaker ctx->dup_cookie = *desc->dir_cookie; 3780c030806STrond Myklebust ctx->duped = -1; 3798ef2ce3eSBryan Schumaker } 3808ef2ce3eSBryan Schumaker desc->file->f_pos = new_pos; 3818cd51a0cSTrond Myklebust desc->cache_entry_index = i; 38247c716cbSTrond Myklebust return 0; 3838cd51a0cSTrond Myklebust } 3848cd51a0cSTrond Myklebust } 38547c716cbSTrond Myklebust if (array->eof_index >= 0) { 386d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 38718fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 38818fb5fe4STrond Myklebust desc->eof = 1; 389d1bacf9eSBryan Schumaker } 3900c030806STrond Myklebust out: 391d1bacf9eSBryan Schumaker return status; 392d1bacf9eSBryan Schumaker } 393d1bacf9eSBryan Schumaker 394d1bacf9eSBryan Schumaker static 395d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 396d1bacf9eSBryan Schumaker { 397d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 39847c716cbSTrond Myklebust int status; 399d1bacf9eSBryan Schumaker 400d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 401d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 402d1bacf9eSBryan Schumaker status = PTR_ERR(array); 403d1bacf9eSBryan Schumaker goto out; 404d1bacf9eSBryan Schumaker } 405d1bacf9eSBryan Schumaker 406d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 407d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 408d1bacf9eSBryan Schumaker else 409d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 410d1bacf9eSBryan Schumaker 41147c716cbSTrond Myklebust if (status == -EAGAIN) { 4120aded708STrond Myklebust desc->last_cookie = array->last_cookie; 413e47c085aSTrond Myklebust desc->current_index += array->size; 41447c716cbSTrond Myklebust desc->page_index++; 41547c716cbSTrond Myklebust } 416d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 417d1bacf9eSBryan Schumaker out: 418d1bacf9eSBryan Schumaker return status; 419d1bacf9eSBryan Schumaker } 420d1bacf9eSBryan Schumaker 421d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 422d1bacf9eSBryan Schumaker static 42356e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 424d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 425d1bacf9eSBryan Schumaker { 426480c2006SBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 427480c2006SBryan Schumaker struct rpc_cred *cred = ctx->cred; 4284704f0e2STrond Myklebust unsigned long timestamp, gencount; 4291da177e4SLinus Torvalds int error; 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds again: 4321da177e4SLinus Torvalds timestamp = jiffies; 4334704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 43456e4ebf8SBryan Schumaker error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages, 4351da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 4361da177e4SLinus Torvalds if (error < 0) { 4371da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 4381da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 4391da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 4403a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 4411da177e4SLinus Torvalds desc->plus = 0; 4421da177e4SLinus Torvalds goto again; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds goto error; 4451da177e4SLinus Torvalds } 4461f4eab7eSNeil Brown desc->timestamp = timestamp; 4474704f0e2STrond Myklebust desc->gencount = gencount; 448d1bacf9eSBryan Schumaker error: 449d1bacf9eSBryan Schumaker return error; 450d1bacf9eSBryan Schumaker } 451d1bacf9eSBryan Schumaker 452573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc, 453573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr) 454d1bacf9eSBryan Schumaker { 455573c4e1eSChuck Lever int error; 456d1bacf9eSBryan Schumaker 457573c4e1eSChuck Lever error = desc->decode(xdr, entry, desc->plus); 458573c4e1eSChuck Lever if (error) 459573c4e1eSChuck Lever return error; 460d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 461d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 462d1bacf9eSBryan Schumaker return 0; 463d1bacf9eSBryan Schumaker } 464d1bacf9eSBryan Schumaker 465d39ab9deSBryan Schumaker static 466d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 467d39ab9deSBryan Schumaker { 468d39ab9deSBryan Schumaker if (dentry->d_inode == NULL) 469d39ab9deSBryan Schumaker goto different; 47037a09f07STrond Myklebust if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0) 471d39ab9deSBryan Schumaker goto different; 472d39ab9deSBryan Schumaker return 1; 473d39ab9deSBryan Schumaker different: 474d39ab9deSBryan Schumaker return 0; 475d39ab9deSBryan Schumaker } 476d39ab9deSBryan Schumaker 477d39ab9deSBryan Schumaker static 478*d69ee9b8STrond Myklebust bool nfs_use_readdirplus(struct inode *dir, struct file *filp) 479*d69ee9b8STrond Myklebust { 480*d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 481*d69ee9b8STrond Myklebust return false; 482*d69ee9b8STrond Myklebust if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 483*d69ee9b8STrond Myklebust return true; 484*d69ee9b8STrond Myklebust if (filp->f_pos == 0) 485*d69ee9b8STrond Myklebust return true; 486*d69ee9b8STrond Myklebust return false; 487*d69ee9b8STrond Myklebust } 488*d69ee9b8STrond Myklebust 489*d69ee9b8STrond Myklebust /* 490*d69ee9b8STrond Myklebust * This function is called by the lookup code to request the use of 491*d69ee9b8STrond Myklebust * readdirplus to accelerate any future lookups in the same 492*d69ee9b8STrond Myklebust * directory. 493*d69ee9b8STrond Myklebust */ 494*d69ee9b8STrond Myklebust static 495*d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir) 496*d69ee9b8STrond Myklebust { 497*d69ee9b8STrond Myklebust set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags); 498*d69ee9b8STrond Myklebust } 499*d69ee9b8STrond Myklebust 500*d69ee9b8STrond Myklebust static 501d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 502d39ab9deSBryan Schumaker { 5034a201d6eSTrond Myklebust struct qstr filename = { 5044a201d6eSTrond Myklebust .len = entry->len, 5054a201d6eSTrond Myklebust .name = entry->name, 5064a201d6eSTrond Myklebust }; 5074a201d6eSTrond Myklebust struct dentry *dentry; 5084a201d6eSTrond Myklebust struct dentry *alias; 509d39ab9deSBryan Schumaker struct inode *dir = parent->d_inode; 510d39ab9deSBryan Schumaker struct inode *inode; 511d39ab9deSBryan Schumaker 5124a201d6eSTrond Myklebust if (filename.name[0] == '.') { 5134a201d6eSTrond Myklebust if (filename.len == 1) 5144a201d6eSTrond Myklebust return; 5154a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 5164a201d6eSTrond Myklebust return; 5174a201d6eSTrond Myklebust } 5184a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 519d39ab9deSBryan Schumaker 5204a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 521d39ab9deSBryan Schumaker if (dentry != NULL) { 522d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 523d39ab9deSBryan Schumaker nfs_refresh_inode(dentry->d_inode, entry->fattr); 524d39ab9deSBryan Schumaker goto out; 525d39ab9deSBryan Schumaker } else { 526d39ab9deSBryan Schumaker d_drop(dentry); 527d39ab9deSBryan Schumaker dput(dentry); 528d39ab9deSBryan Schumaker } 529d39ab9deSBryan Schumaker } 530d39ab9deSBryan Schumaker 531d39ab9deSBryan Schumaker dentry = d_alloc(parent, &filename); 5324a201d6eSTrond Myklebust if (dentry == NULL) 5334a201d6eSTrond Myklebust return; 5344a201d6eSTrond Myklebust 535d39ab9deSBryan Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 536d39ab9deSBryan Schumaker if (IS_ERR(inode)) 537d39ab9deSBryan Schumaker goto out; 538d39ab9deSBryan Schumaker 539d39ab9deSBryan Schumaker alias = d_materialise_unique(dentry, inode); 540d39ab9deSBryan Schumaker if (IS_ERR(alias)) 541d39ab9deSBryan Schumaker goto out; 542d39ab9deSBryan Schumaker else if (alias) { 543d39ab9deSBryan Schumaker nfs_set_verifier(alias, nfs_save_change_attribute(dir)); 544d39ab9deSBryan Schumaker dput(alias); 545d39ab9deSBryan Schumaker } else 546d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 547d39ab9deSBryan Schumaker 548d39ab9deSBryan Schumaker out: 549d39ab9deSBryan Schumaker dput(dentry); 550d39ab9deSBryan Schumaker } 551d39ab9deSBryan Schumaker 552d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 553d1bacf9eSBryan Schumaker static 5548cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 5556650239aSTrond Myklebust struct page **xdr_pages, struct page *page, unsigned int buflen) 556d1bacf9eSBryan Schumaker { 557babddc72SBryan Schumaker struct xdr_stream stream; 558f7da7a12SBenny Halevy struct xdr_buf buf; 5596650239aSTrond Myklebust struct page *scratch; 56099424380SBryan Schumaker struct nfs_cache_array *array; 5615c346854STrond Myklebust unsigned int count = 0; 5625c346854STrond Myklebust int status; 563babddc72SBryan Schumaker 5646650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL); 5656650239aSTrond Myklebust if (scratch == NULL) 5666650239aSTrond Myklebust return -ENOMEM; 567babddc72SBryan Schumaker 568f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 5696650239aSTrond Myklebust xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 57099424380SBryan Schumaker 57199424380SBryan Schumaker do { 57299424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 5738cd51a0cSTrond Myklebust if (status != 0) { 5748cd51a0cSTrond Myklebust if (status == -EAGAIN) 5758cd51a0cSTrond Myklebust status = 0; 57699424380SBryan Schumaker break; 5778cd51a0cSTrond Myklebust } 57899424380SBryan Schumaker 5795c346854STrond Myklebust count++; 5805c346854STrond Myklebust 58147c716cbSTrond Myklebust if (desc->plus != 0) 582d39ab9deSBryan Schumaker nfs_prime_dcache(desc->file->f_path.dentry, entry); 5838cd51a0cSTrond Myklebust 5848cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 5858cd51a0cSTrond Myklebust if (status != 0) 5868cd51a0cSTrond Myklebust break; 58799424380SBryan Schumaker } while (!entry->eof); 58899424380SBryan Schumaker 58947c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 59099424380SBryan Schumaker array = nfs_readdir_get_array(page); 5918cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 5928cd51a0cSTrond Myklebust array->eof_index = array->size; 59399424380SBryan Schumaker status = 0; 59499424380SBryan Schumaker nfs_readdir_release_array(page); 5955c346854STrond Myklebust } else 5965c346854STrond Myklebust status = PTR_ERR(array); 59756e4ebf8SBryan Schumaker } 5986650239aSTrond Myklebust 5996650239aSTrond Myklebust put_page(scratch); 6008cd51a0cSTrond Myklebust return status; 6018cd51a0cSTrond Myklebust } 60256e4ebf8SBryan Schumaker 60356e4ebf8SBryan Schumaker static 60456e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages) 60556e4ebf8SBryan Schumaker { 60656e4ebf8SBryan Schumaker unsigned int i; 60756e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 60856e4ebf8SBryan Schumaker put_page(pages[i]); 60956e4ebf8SBryan Schumaker } 61056e4ebf8SBryan Schumaker 61156e4ebf8SBryan Schumaker static 61256e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages, 61356e4ebf8SBryan Schumaker unsigned int npages) 61456e4ebf8SBryan Schumaker { 61556e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, npages); 61656e4ebf8SBryan Schumaker } 61756e4ebf8SBryan Schumaker 61856e4ebf8SBryan Schumaker /* 61956e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 62056e4ebf8SBryan Schumaker * to nfs_readdir_free_large_page 62156e4ebf8SBryan Schumaker */ 62256e4ebf8SBryan Schumaker static 6236650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages) 62456e4ebf8SBryan Schumaker { 62556e4ebf8SBryan Schumaker unsigned int i; 62656e4ebf8SBryan Schumaker 62756e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 62856e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 62956e4ebf8SBryan Schumaker if (page == NULL) 63056e4ebf8SBryan Schumaker goto out_freepages; 63156e4ebf8SBryan Schumaker pages[i] = page; 63256e4ebf8SBryan Schumaker } 6336650239aSTrond Myklebust return 0; 63456e4ebf8SBryan Schumaker 63556e4ebf8SBryan Schumaker out_freepages: 63656e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, i); 6376650239aSTrond Myklebust return -ENOMEM; 638d1bacf9eSBryan Schumaker } 639d1bacf9eSBryan Schumaker 640d1bacf9eSBryan Schumaker static 641d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 642d1bacf9eSBryan Schumaker { 64356e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 64456e4ebf8SBryan Schumaker void *pages_ptr = NULL; 645d1bacf9eSBryan Schumaker struct nfs_entry entry; 646d1bacf9eSBryan Schumaker struct file *file = desc->file; 647d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 6488cd51a0cSTrond Myklebust int status = -ENOMEM; 64956e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 650d1bacf9eSBryan Schumaker 651d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 6520aded708STrond Myklebust entry.cookie = desc->last_cookie; 653d1bacf9eSBryan Schumaker entry.eof = 0; 654d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 655d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 656573c4e1eSChuck Lever entry.server = NFS_SERVER(inode); 657d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 658d1bacf9eSBryan Schumaker goto out; 659d1bacf9eSBryan Schumaker 660d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 6618cd51a0cSTrond Myklebust if (IS_ERR(array)) { 6628cd51a0cSTrond Myklebust status = PTR_ERR(array); 6638cd51a0cSTrond Myklebust goto out; 6648cd51a0cSTrond Myklebust } 665d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 666d1bacf9eSBryan Schumaker array->eof_index = -1; 667d1bacf9eSBryan Schumaker 6686650239aSTrond Myklebust status = nfs_readdir_large_page(pages, array_size); 6696650239aSTrond Myklebust if (status < 0) 670d1bacf9eSBryan Schumaker goto out_release_array; 671d1bacf9eSBryan Schumaker do { 672ac396128STrond Myklebust unsigned int pglen; 67356e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 674babddc72SBryan Schumaker 675d1bacf9eSBryan Schumaker if (status < 0) 676d1bacf9eSBryan Schumaker break; 677ac396128STrond Myklebust pglen = status; 6786650239aSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen); 6798cd51a0cSTrond Myklebust if (status < 0) { 6808cd51a0cSTrond Myklebust if (status == -ENOSPC) 6818cd51a0cSTrond Myklebust status = 0; 6828cd51a0cSTrond Myklebust break; 6838cd51a0cSTrond Myklebust } 6848cd51a0cSTrond Myklebust } while (array->eof_index < 0); 685d1bacf9eSBryan Schumaker 68656e4ebf8SBryan Schumaker nfs_readdir_free_large_page(pages_ptr, pages, array_size); 687d1bacf9eSBryan Schumaker out_release_array: 688d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 689d1bacf9eSBryan Schumaker out: 690d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 691d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 692d1bacf9eSBryan Schumaker return status; 693d1bacf9eSBryan Schumaker } 694d1bacf9eSBryan Schumaker 695d1bacf9eSBryan Schumaker /* 696d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 697d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 698d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 699d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 7001da177e4SLinus Torvalds */ 701d1bacf9eSBryan Schumaker static 702d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 703d1bacf9eSBryan Schumaker { 704d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 7058cd51a0cSTrond Myklebust int ret; 706d1bacf9eSBryan Schumaker 7078cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 7088cd51a0cSTrond Myklebust if (ret < 0) 709d1bacf9eSBryan Schumaker goto error; 710d1bacf9eSBryan Schumaker SetPageUptodate(page); 711d1bacf9eSBryan Schumaker 7122aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 713cd9ae2b6STrond Myklebust /* Should never happen */ 714cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 715cd9ae2b6STrond Myklebust } 7161da177e4SLinus Torvalds unlock_page(page); 7171da177e4SLinus Torvalds return 0; 7181da177e4SLinus Torvalds error: 7191da177e4SLinus Torvalds unlock_page(page); 7208cd51a0cSTrond Myklebust return ret; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 723d1bacf9eSBryan Schumaker static 724d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 7251da177e4SLinus Torvalds { 72611de3b11STrond Myklebust if (!desc->page->mapping) 72711de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 7281da177e4SLinus Torvalds page_cache_release(desc->page); 7291da177e4SLinus Torvalds desc->page = NULL; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 732d1bacf9eSBryan Schumaker static 733d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 7341da177e4SLinus Torvalds { 7358cd51a0cSTrond Myklebust return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping, 736d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /* 740d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 7411da177e4SLinus Torvalds */ 742d1bacf9eSBryan Schumaker static 743d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 744d1bacf9eSBryan Schumaker { 745d1bacf9eSBryan Schumaker int res; 746d1bacf9eSBryan Schumaker 747d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 748d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 749d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 750d1bacf9eSBryan Schumaker 751d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 75247c716cbSTrond Myklebust if (res != 0) 753d1bacf9eSBryan Schumaker cache_page_release(desc); 754d1bacf9eSBryan Schumaker return res; 755d1bacf9eSBryan Schumaker } 756d1bacf9eSBryan Schumaker 757d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 7581da177e4SLinus Torvalds static inline 7591da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 7601da177e4SLinus Torvalds { 7618cd51a0cSTrond Myklebust int res; 762d1bacf9eSBryan Schumaker 7630aded708STrond Myklebust if (desc->page_index == 0) { 7648cd51a0cSTrond Myklebust desc->current_index = 0; 7650aded708STrond Myklebust desc->last_cookie = 0; 7660aded708STrond Myklebust } 76747c716cbSTrond Myklebust do { 768d1bacf9eSBryan Schumaker res = find_cache_page(desc); 76947c716cbSTrond Myklebust } while (res == -EAGAIN); 7701da177e4SLinus Torvalds return res; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /* 7741da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 7751da177e4SLinus Torvalds */ 7761da177e4SLinus Torvalds static 7771da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 7781da177e4SLinus Torvalds filldir_t filldir) 7791da177e4SLinus Torvalds { 7801da177e4SLinus Torvalds struct file *file = desc->file; 781d1bacf9eSBryan Schumaker int i = 0; 782d1bacf9eSBryan Schumaker int res = 0; 783d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 7848ef2ce3eSBryan Schumaker struct nfs_open_dir_context *ctx = file->private_data; 7858ef2ce3eSBryan Schumaker 786d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 787e7c58e97STrond Myklebust if (IS_ERR(array)) { 788e7c58e97STrond Myklebust res = PTR_ERR(array); 789e7c58e97STrond Myklebust goto out; 790e7c58e97STrond Myklebust } 7911da177e4SLinus Torvalds 792d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 793ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7941da177e4SLinus Torvalds 795ece0b423STrond Myklebust ent = &array->array[i]; 796ece0b423STrond Myklebust if (filldir(dirent, ent->string.name, ent->string.len, 7970b26a0bfSTrond Myklebust file->f_pos, nfs_compat_user_ino64(ent->ino), 7980b26a0bfSTrond Myklebust ent->d_type) < 0) { 799ece0b423STrond Myklebust desc->eof = 1; 8001da177e4SLinus Torvalds break; 801ece0b423STrond Myklebust } 80200a92642SOlivier Galibert file->f_pos++; 803d1bacf9eSBryan Schumaker if (i < (array->size-1)) 804d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 805d1bacf9eSBryan Schumaker else 806d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 8070c030806STrond Myklebust if (ctx->duped != 0) 8080c030806STrond Myklebust ctx->duped = 1; 8098cd51a0cSTrond Myklebust } 81047c716cbSTrond Myklebust if (array->eof_index >= 0) 811d1bacf9eSBryan Schumaker desc->eof = 1; 812d1bacf9eSBryan Schumaker 813d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 814e7c58e97STrond Myklebust out: 815d1bacf9eSBryan Schumaker cache_page_release(desc); 8161e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 8171e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 8181da177e4SLinus Torvalds return res; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds /* 8221da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 8231da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 8241da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 8251da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 8261da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 8271da177e4SLinus Torvalds * 8281da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 8291da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 8301da177e4SLinus Torvalds * we should already have a complete representation of the 8311da177e4SLinus Torvalds * directory in the page cache by the time we get here. 8321da177e4SLinus Torvalds */ 8331da177e4SLinus Torvalds static inline 8341da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 8351da177e4SLinus Torvalds filldir_t filldir) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds struct page *page = NULL; 8381da177e4SLinus Torvalds int status; 839d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 8400c030806STrond Myklebust struct nfs_open_dir_context *ctx = desc->file->private_data; 8411da177e4SLinus Torvalds 8421e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 8431e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 8461da177e4SLinus Torvalds if (!page) { 8471da177e4SLinus Torvalds status = -ENOMEM; 8481da177e4SLinus Torvalds goto out; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8517a8e1dc3STrond Myklebust desc->page_index = 0; 8520aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 8537a8e1dc3STrond Myklebust desc->page = page; 8540c030806STrond Myklebust ctx->duped = 0; 8557a8e1dc3STrond Myklebust 85685f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 85785f8607eSTrond Myklebust if (status < 0) 858d1bacf9eSBryan Schumaker goto out_release; 859d1bacf9eSBryan Schumaker 8601da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds out: 8631e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 8643110ff80SHarvey Harrison __func__, status); 8651da177e4SLinus Torvalds return status; 8661da177e4SLinus Torvalds out_release: 867d1bacf9eSBryan Schumaker cache_page_release(desc); 8681da177e4SLinus Torvalds goto out; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds 87100a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 87200a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 87300a92642SOlivier Galibert whole directory. 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 8761da177e4SLinus Torvalds { 87701cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 8781da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 8791da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 8801da177e4SLinus Torvalds *desc = &my_desc; 881480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 88247c716cbSTrond Myklebust int res; 8831da177e4SLinus Torvalds 8846da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 8851e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8861e7cb3dcSChuck Lever (long long)filp->f_pos); 88791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 88891d5b470SChuck Lever 8891da177e4SLinus Torvalds /* 89000a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 891f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 89200a92642SOlivier Galibert * to either find the entry with the appropriate number or 89300a92642SOlivier Galibert * revalidate the cookie. 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds desc->file = filp; 898480c2006SBryan Schumaker desc->dir_cookie = &dir_ctx->dir_cookie; 8991da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 900*d69ee9b8STrond Myklebust desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0; 9011da177e4SLinus Torvalds 902565277f6STrond Myklebust nfs_block_sillyrename(dentry); 9031cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 904fccca7fcSTrond Myklebust if (res < 0) 905fccca7fcSTrond Myklebust goto out; 906fccca7fcSTrond Myklebust 90747c716cbSTrond Myklebust do { 9081da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 90900a92642SOlivier Galibert 9101da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 911ece0b423STrond Myklebust res = 0; 9121da177e4SLinus Torvalds /* This means either end of directory */ 913d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 9141da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 9151da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 916ece0b423STrond Myklebust if (res == 0) 9171da177e4SLinus Torvalds continue; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds break; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 9223a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 9231da177e4SLinus Torvalds nfs_zap_caches(inode); 924baf57a09STrond Myklebust desc->page_index = 0; 9251da177e4SLinus Torvalds desc->plus = 0; 926d1bacf9eSBryan Schumaker desc->eof = 0; 9271da177e4SLinus Torvalds continue; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds if (res < 0) 9301da177e4SLinus Torvalds break; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 933ece0b423STrond Myklebust if (res < 0) 9341da177e4SLinus Torvalds break; 93547c716cbSTrond Myklebust } while (!desc->eof); 936fccca7fcSTrond Myklebust out: 937565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 9381e7cb3dcSChuck Lever if (res > 0) 9391e7cb3dcSChuck Lever res = 0; 940aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 9411e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 9421e7cb3dcSChuck Lever res); 9431da177e4SLinus Torvalds return res; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 94610afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 947f0dd2136STrond Myklebust { 948b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 949b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 950480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data; 951b84e06c5SChuck Lever 9526da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 953b84e06c5SChuck Lever dentry->d_parent->d_name.name, 954b84e06c5SChuck Lever dentry->d_name.name, 955b84e06c5SChuck Lever offset, origin); 956b84e06c5SChuck Lever 957b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 958f0dd2136STrond Myklebust switch (origin) { 959f0dd2136STrond Myklebust case 1: 960f0dd2136STrond Myklebust offset += filp->f_pos; 961f0dd2136STrond Myklebust case 0: 962f0dd2136STrond Myklebust if (offset >= 0) 963f0dd2136STrond Myklebust break; 964f0dd2136STrond Myklebust default: 965f0dd2136STrond Myklebust offset = -EINVAL; 966f0dd2136STrond Myklebust goto out; 967f0dd2136STrond Myklebust } 968f0dd2136STrond Myklebust if (offset != filp->f_pos) { 969f0dd2136STrond Myklebust filp->f_pos = offset; 970480c2006SBryan Schumaker dir_ctx->dir_cookie = 0; 9718ef2ce3eSBryan Schumaker dir_ctx->duped = 0; 972f0dd2136STrond Myklebust } 973f0dd2136STrond Myklebust out: 974b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 975f0dd2136STrond Myklebust return offset; 976f0dd2136STrond Myklebust } 977f0dd2136STrond Myklebust 9781da177e4SLinus Torvalds /* 9791da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 9801da177e4SLinus Torvalds * is a dummy operation. 9811da177e4SLinus Torvalds */ 98202c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 98302c24a82SJosef Bacik int datasync) 9841da177e4SLinus Torvalds { 9857ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 98602c24a82SJosef Bacik struct inode *inode = dentry->d_inode; 9877ea80859SChristoph Hellwig 9886da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 9891e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 9901e7cb3dcSChuck Lever datasync); 9911e7cb3dcSChuck Lever 99202c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 99354917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 99402c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 9951da177e4SLinus Torvalds return 0; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 998bfc69a45STrond Myklebust /** 999bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 1000bfc69a45STrond Myklebust * @dir - pointer to directory inode 1001bfc69a45STrond Myklebust * 1002bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 1003bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 1004bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 1005bfc69a45STrond Myklebust * 1006bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 1007bfc69a45STrond Myklebust */ 1008bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 1009bfc69a45STrond Myklebust { 1010011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 1011bfc69a45STrond Myklebust } 1012bfc69a45STrond Myklebust 10131da177e4SLinus Torvalds /* 10141da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 10151da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 10161da177e4SLinus Torvalds * and may need to be looked up again. 10171da177e4SLinus Torvalds */ 1018c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds if (IS_ROOT(dentry)) 10211da177e4SLinus Torvalds return 1; 10224eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 10234eec952eSTrond Myklebust return 0; 1024f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 10256ecc5e8fSTrond Myklebust return 0; 1026f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 1027f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 1028f2c77f4eSTrond Myklebust return 0; 1029f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1030f2c77f4eSTrond Myklebust return 0; 1031f2c77f4eSTrond Myklebust return 1; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /* 10351d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 10361d6757fbSTrond Myklebust * 10371d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 10388aeb376cSAl Viro * component of the path and none of them is set before that last 10398aeb376cSAl Viro * component. 10401d6757fbSTrond Myklebust */ 104134286d66SNick Piggin static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, 104234286d66SNick Piggin unsigned int mask) 10431d6757fbSTrond Myklebust { 10441d6757fbSTrond Myklebust return nd->flags & mask; 10451d6757fbSTrond Myklebust } 10461d6757fbSTrond Myklebust 10471d6757fbSTrond Myklebust /* 1048a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 1049a12802caSTrond Myklebust * an O_EXCL create using this path component. 1050a12802caSTrond Myklebust */ 1051a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 1052a12802caSTrond Myklebust { 1053a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 1054a12802caSTrond Myklebust return 0; 10553516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 1056a12802caSTrond Myklebust } 1057a12802caSTrond Myklebust 1058a12802caSTrond Myklebust /* 10591d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 10601d6757fbSTrond Myklebust * 10611d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 10621d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 10631d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 10641d6757fbSTrond Myklebust * 10651d6757fbSTrond Myklebust */ 10661da177e4SLinus Torvalds static inline 10671da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 10681da177e4SLinus Torvalds { 10691da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 10701da177e4SLinus Torvalds 107136d43a43SDavid Howells if (IS_AUTOMOUNT(inode)) 10724e99a1ffSTrond Myklebust return 0; 10731da177e4SLinus Torvalds if (nd != NULL) { 10741da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 10751d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 10761da177e4SLinus Torvalds goto out_force; 10771da177e4SLinus Torvalds /* This is an open(2) */ 10781d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 10794e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 10804e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 10814e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 10821da177e4SLinus Torvalds goto out_force; 10834f48af45STrond Myklebust return 0; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 10861da177e4SLinus Torvalds out_force: 10871da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* 10911da177e4SLinus Torvalds * We judge how long we want to trust negative 10921da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 10931da177e4SLinus Torvalds * 10941da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 10951da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 10961da177e4SLinus Torvalds */ 10971da177e4SLinus Torvalds static inline 10981da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 10991da177e4SLinus Torvalds struct nameidata *nd) 11001da177e4SLinus Torvalds { 11011da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 11021d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 11031da177e4SLinus Torvalds return 0; 11044eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 11054eec952eSTrond Myklebust return 1; 11061da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /* 11101da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 11111da177e4SLinus Torvalds * and we should check whether we can really trust that 11121da177e4SLinus Torvalds * lookup. 11131da177e4SLinus Torvalds * 11141da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 11151da177e4SLinus Torvalds * we have an inode! 11161da177e4SLinus Torvalds * 11171da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 11181da177e4SLinus Torvalds * cached dentry and do a new lookup. 11191da177e4SLinus Torvalds */ 11201da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd) 11211da177e4SLinus Torvalds { 11221da177e4SLinus Torvalds struct inode *dir; 11231da177e4SLinus Torvalds struct inode *inode; 11241da177e4SLinus Torvalds struct dentry *parent; 1125e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1126e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 11271da177e4SLinus Torvalds int error; 11281da177e4SLinus Torvalds 112934286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 113034286d66SNick Piggin return -ECHILD; 113134286d66SNick Piggin 11321da177e4SLinus Torvalds parent = dget_parent(dentry); 11331da177e4SLinus Torvalds dir = parent->d_inode; 113491d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 11351da177e4SLinus Torvalds inode = dentry->d_inode; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds if (!inode) { 11381da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 11391da177e4SLinus Torvalds goto out_bad; 1140*d69ee9b8STrond Myklebust goto out_valid_noent; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds if (is_bad_inode(inode)) { 11441e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 11453110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11461e7cb3dcSChuck Lever dentry->d_name.name); 11471da177e4SLinus Torvalds goto out_bad; 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds 115015860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 115115860ab1STrond Myklebust goto out_set_verifier; 115215860ab1STrond Myklebust 11531da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 1154a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 11551da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 11561da177e4SLinus Torvalds goto out_zap_parent; 11571da177e4SLinus Torvalds goto out_valid; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (NFS_STALE(inode)) 11611da177e4SLinus Torvalds goto out_bad; 11621da177e4SLinus Torvalds 1163e1fb4d05STrond Myklebust error = -ENOMEM; 1164e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1165e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1166e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1167e1fb4d05STrond Myklebust goto out_error; 1168e1fb4d05STrond Myklebust 116980a16b21SBryan Schumaker error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 11701da177e4SLinus Torvalds if (error) 11711da177e4SLinus Torvalds goto out_bad; 1172e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 11731da177e4SLinus Torvalds goto out_bad; 1174e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 11751da177e4SLinus Torvalds goto out_bad; 11761da177e4SLinus Torvalds 1177e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1178e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 117915860ab1STrond Myklebust out_set_verifier: 1180cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11811da177e4SLinus Torvalds out_valid: 1182*d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1183*d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1184*d69ee9b8STrond Myklebust out_valid_noent: 11851da177e4SLinus Torvalds dput(parent); 11861e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 11873110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11881e7cb3dcSChuck Lever dentry->d_name.name); 11891da177e4SLinus Torvalds return 1; 11901da177e4SLinus Torvalds out_zap_parent: 11911da177e4SLinus Torvalds nfs_zap_caches(dir); 11921da177e4SLinus Torvalds out_bad: 1193a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 11941da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 11951da177e4SLinus Torvalds /* Purge readdir caches. */ 11961da177e4SLinus Torvalds nfs_zap_caches(inode); 11971da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 11981da177e4SLinus Torvalds if (have_submounts(dentry)) 11991da177e4SLinus Torvalds goto out_valid; 1200d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 1201d9e80b7dSAl Viro goto out_valid; 12021da177e4SLinus Torvalds shrink_dcache_parent(dentry); 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds d_drop(dentry); 1205e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1206e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 12071da177e4SLinus Torvalds dput(parent); 12081e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 12093110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 12101e7cb3dcSChuck Lever dentry->d_name.name); 12111da177e4SLinus Torvalds return 0; 1212e1fb4d05STrond Myklebust out_error: 1213e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1214e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 1215e1fb4d05STrond Myklebust dput(parent); 1216e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 1217e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 1218e1fb4d05STrond Myklebust dentry->d_name.name, error); 1219e1fb4d05STrond Myklebust return error; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds /* 12231da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 12241da177e4SLinus Torvalds */ 1225fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 12281da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 12291da177e4SLinus Torvalds dentry->d_flags); 12301da177e4SLinus Torvalds 123177f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 123277f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 123377f11192STrond Myklebust return 1; 123477f11192STrond Myklebust 12351da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12361da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 12371da177e4SLinus Torvalds return 1; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 12401da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 12411da177e4SLinus Torvalds * files will be cleaned up during umount */ 12421da177e4SLinus Torvalds return 1; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds return 0; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 12491b83d707STrond Myklebust { 12501b83d707STrond Myklebust spin_lock(&inode->i_lock); 12511b83d707STrond Myklebust if (inode->i_nlink > 0) 12521b83d707STrond Myklebust drop_nlink(inode); 12531b83d707STrond Myklebust spin_unlock(&inode->i_lock); 12541b83d707STrond Myklebust } 12551b83d707STrond Myklebust 12561da177e4SLinus Torvalds /* 12571da177e4SLinus Torvalds * Called when the dentry loses inode. 12581da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 12591da177e4SLinus Torvalds */ 12601da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 12611da177e4SLinus Torvalds { 126283672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 126383672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 126483672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 126583672d39SNeil Brown 12661da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12679a53c3a7SDave Hansen drop_nlink(inode); 1268e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds iput(inode); 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 1273b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry) 1274b1942c5fSAl Viro { 1275b1942c5fSAl Viro /* free cached devname value, if it survived that far */ 1276b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) { 1277b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1278b1942c5fSAl Viro WARN_ON(1); 1279b1942c5fSAl Viro else 1280b1942c5fSAl Viro kfree(dentry->d_fsdata); 1281b1942c5fSAl Viro } 1282b1942c5fSAl Viro } 1283b1942c5fSAl Viro 1284f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 12851da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 12861da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 12871da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 128836d43a43SDavid Howells .d_automount = nfs_d_automount, 1289b1942c5fSAl Viro .d_release = nfs_d_release, 12901da177e4SLinus Torvalds }; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 12931da177e4SLinus Torvalds { 12941da177e4SLinus Torvalds struct dentry *res; 1295565277f6STrond Myklebust struct dentry *parent; 12961da177e4SLinus Torvalds struct inode *inode = NULL; 1297e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1298e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 12991da177e4SLinus Torvalds int error; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 13021da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 130391d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 13061da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 13071da177e4SLinus Torvalds goto out; 13081da177e4SLinus Torvalds 1309fd684071STrond Myklebust /* 1310fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1311fd684071STrond Myklebust * but don't hash the dentry. 1312fd684071STrond Myklebust */ 1313fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 1314fd684071STrond Myklebust d_instantiate(dentry, NULL); 1315fd684071STrond Myklebust res = NULL; 1316fc0f684cSTrond Myklebust goto out; 1317fd684071STrond Myklebust } 13181da177e4SLinus Torvalds 1319e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1320e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1321e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1322e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1323e1fb4d05STrond Myklebust goto out; 1324e1fb4d05STrond Myklebust 1325565277f6STrond Myklebust parent = dentry->d_parent; 1326565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 1327565277f6STrond Myklebust nfs_block_sillyrename(parent); 132880a16b21SBryan Schumaker error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 13291da177e4SLinus Torvalds if (error == -ENOENT) 13301da177e4SLinus Torvalds goto no_entry; 13311da177e4SLinus Torvalds if (error < 0) { 13321da177e4SLinus Torvalds res = ERR_PTR(error); 1333565277f6STrond Myklebust goto out_unblock_sillyrename; 13341da177e4SLinus Torvalds } 1335e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 1336bf0c84f1SNamhyung Kim res = ERR_CAST(inode); 133703f28e3aSTrond Myklebust if (IS_ERR(res)) 1338565277f6STrond Myklebust goto out_unblock_sillyrename; 133954ceac45SDavid Howells 1340*d69ee9b8STrond Myklebust /* Success: notify readdir to use READDIRPLUS */ 1341*d69ee9b8STrond Myklebust nfs_advise_use_readdirplus(dir); 1342*d69ee9b8STrond Myklebust 13431da177e4SLinus Torvalds no_entry: 134454ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 13459eaef27bSTrond Myklebust if (res != NULL) { 13469eaef27bSTrond Myklebust if (IS_ERR(res)) 1347565277f6STrond Myklebust goto out_unblock_sillyrename; 13481da177e4SLinus Torvalds dentry = res; 13499eaef27bSTrond Myklebust } 13501da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1351565277f6STrond Myklebust out_unblock_sillyrename: 1352565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 13531da177e4SLinus Torvalds out: 1354e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1355e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 13561da177e4SLinus Torvalds return res; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 13601da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 13611da177e4SLinus Torvalds 1362f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 13631da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 13641da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 13651da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 136636d43a43SDavid Howells .d_automount = nfs_d_automount, 1367b1942c5fSAl Viro .d_release = nfs_d_release, 13681da177e4SLinus Torvalds }; 13691da177e4SLinus Torvalds 13701d6757fbSTrond Myklebust /* 13711d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 13721d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 13731d6757fbSTrond Myklebust */ 13745584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 13751da177e4SLinus Torvalds { 13761d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 13771da177e4SLinus Torvalds return 0; 13781da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 13791da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 13801da177e4SLinus Torvalds return 0; 13811da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 13822c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 13838a5e929dSAl Viro (nd->intent.open.flags & (O_CREAT|O_TRUNC|O_ACCMODE))) 13841da177e4SLinus Torvalds return 0; 13851da177e4SLinus Torvalds return 1; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13888a5e929dSAl Viro static fmode_t flags_to_mode(int flags) 13898a5e929dSAl Viro { 13908a5e929dSAl Viro fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 13918a5e929dSAl Viro if ((flags & O_ACCMODE) != O_WRONLY) 13928a5e929dSAl Viro res |= FMODE_READ; 13938a5e929dSAl Viro if ((flags & O_ACCMODE) != O_RDONLY) 13948a5e929dSAl Viro res |= FMODE_WRITE; 13958a5e929dSAl Viro return res; 13968a5e929dSAl Viro } 13978a5e929dSAl Viro 139851141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags) 1399cd9a1c0eSTrond Myklebust { 14005ede7b1cSAl Viro return alloc_nfs_open_context(dentry, flags_to_mode(open_flags)); 1401cd9a1c0eSTrond Myklebust } 1402cd9a1c0eSTrond Myklebust 1403cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1404cd9a1c0eSTrond Myklebust { 1405cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1406cd9a1c0eSTrond Myklebust return 0; 1407cd9a1c0eSTrond Myklebust } 1408cd9a1c0eSTrond Myklebust 1409cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) 1410cd9a1c0eSTrond Myklebust { 1411cd9a1c0eSTrond Myklebust struct file *filp; 1412cd9a1c0eSTrond Myklebust int ret = 0; 1413cd9a1c0eSTrond Myklebust 1414cd9a1c0eSTrond Myklebust /* If the open_intent is for execute, we have an extra check to make */ 1415cd9a1c0eSTrond Myklebust if (ctx->mode & FMODE_EXEC) { 14163d4ff43dSAl Viro ret = nfs_may_open(ctx->dentry->d_inode, 1417cd9a1c0eSTrond Myklebust ctx->cred, 1418cd9a1c0eSTrond Myklebust nd->intent.open.flags); 1419cd9a1c0eSTrond Myklebust if (ret < 0) 1420cd9a1c0eSTrond Myklebust goto out; 1421cd9a1c0eSTrond Myklebust } 14223d4ff43dSAl Viro filp = lookup_instantiate_filp(nd, ctx->dentry, do_open); 1423cd9a1c0eSTrond Myklebust if (IS_ERR(filp)) 1424cd9a1c0eSTrond Myklebust ret = PTR_ERR(filp); 1425cd9a1c0eSTrond Myklebust else 1426cd9a1c0eSTrond Myklebust nfs_file_set_open_context(filp, ctx); 1427cd9a1c0eSTrond Myklebust out: 1428cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1429cd9a1c0eSTrond Myklebust return ret; 1430cd9a1c0eSTrond Myklebust } 1431cd9a1c0eSTrond Myklebust 14321da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 14331da177e4SLinus Torvalds { 1434cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1435cd9a1c0eSTrond Myklebust struct iattr attr; 14361da177e4SLinus Torvalds struct dentry *res = NULL; 1437f46e0bd3STrond Myklebust struct inode *inode; 1438cd9a1c0eSTrond Myklebust int open_flags; 1439898f635cSTrond Myklebust int err; 14401da177e4SLinus Torvalds 14411e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 14421e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14431e7cb3dcSChuck Lever 14441da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 14455584c306STrond Myklebust if (!is_atomic_open(nd)) 14461da177e4SLinus Torvalds goto no_open; 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 14491da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 14501da177e4SLinus Torvalds goto out; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 1453d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1454d4d9cdcbSTrond Myklebust * the dentry. */ 14553516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1456d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 145702a913a7STrond Myklebust goto out; 145802a913a7STrond Myklebust } 14591da177e4SLinus Torvalds 146051141598SAl Viro open_flags = nd->intent.open.flags; 1461451146beSTrond Myklebust attr.ia_valid = ATTR_OPEN; 146251141598SAl Viro 146351141598SAl Viro ctx = create_nfs_open_context(dentry, open_flags); 1464cd9a1c0eSTrond Myklebust res = ERR_CAST(ctx); 1465cd9a1c0eSTrond Myklebust if (IS_ERR(ctx)) 1466cd9a1c0eSTrond Myklebust goto out; 1467cd9a1c0eSTrond Myklebust 1468cd9a1c0eSTrond Myklebust if (nd->flags & LOOKUP_CREATE) { 1469cd9a1c0eSTrond Myklebust attr.ia_mode = nd->intent.open.create_mode; 1470536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE; 1471cd9a1c0eSTrond Myklebust attr.ia_mode &= ~current_umask(); 1472536e43d1STrond Myklebust } else 1473898f635cSTrond Myklebust open_flags &= ~(O_EXCL | O_CREAT); 1474536e43d1STrond Myklebust 1475536e43d1STrond Myklebust if (open_flags & O_TRUNC) { 1476536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1477536e43d1STrond Myklebust attr.ia_size = 0; 1478cd9a1c0eSTrond Myklebust } 1479cd9a1c0eSTrond Myklebust 14801da177e4SLinus Torvalds /* Open the file on the server */ 1481f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 14822b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr); 1483f46e0bd3STrond Myklebust if (IS_ERR(inode)) { 1484f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1485cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1486f46e0bd3STrond Myklebust switch (PTR_ERR(inode)) { 14871da177e4SLinus Torvalds /* Make a negative dentry */ 14881da177e4SLinus Torvalds case -ENOENT: 1489f46e0bd3STrond Myklebust d_add(dentry, NULL); 149002a913a7STrond Myklebust res = NULL; 149102a913a7STrond Myklebust goto out; 14921da177e4SLinus Torvalds /* This turned out not to be a regular file */ 14931788ea6eSJeff Layton case -EISDIR: 14946f926b5bSTrond Myklebust case -ENOTDIR: 14956f926b5bSTrond Myklebust goto no_open; 14961da177e4SLinus Torvalds case -ELOOP: 14971da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 14981da177e4SLinus Torvalds goto no_open; 14991da177e4SLinus Torvalds /* case -EINVAL: */ 15001da177e4SLinus Torvalds default: 1501f46e0bd3STrond Myklebust res = ERR_CAST(inode); 15021da177e4SLinus Torvalds goto out; 15031da177e4SLinus Torvalds } 1504cd9a1c0eSTrond Myklebust } 1505f46e0bd3STrond Myklebust res = d_add_unique(dentry, inode); 1506898f635cSTrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1507f46e0bd3STrond Myklebust if (res != NULL) { 15083d4ff43dSAl Viro dput(ctx->dentry); 15093d4ff43dSAl Viro ctx->dentry = dget(res); 15101da177e4SLinus Torvalds dentry = res; 1511f46e0bd3STrond Myklebust } 1512898f635cSTrond Myklebust err = nfs_intent_set_file(nd, ctx); 1513898f635cSTrond Myklebust if (err < 0) { 1514898f635cSTrond Myklebust if (res != NULL) 1515898f635cSTrond Myklebust dput(res); 1516898f635cSTrond Myklebust return ERR_PTR(err); 1517898f635cSTrond Myklebust } 15181da177e4SLinus Torvalds out: 1519f46e0bd3STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15201da177e4SLinus Torvalds return res; 15211da177e4SLinus Torvalds no_open: 15221da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct dentry *parent = NULL; 1528657e94b6SNick Piggin struct inode *inode; 15291da177e4SLinus Torvalds struct inode *dir; 1530b8d4caddSTrond Myklebust struct nfs_open_context *ctx; 1531536e43d1STrond Myklebust struct iattr attr; 15321da177e4SLinus Torvalds int openflags, ret = 0; 15331da177e4SLinus Torvalds 1534657e94b6SNick Piggin if (nd->flags & LOOKUP_RCU) 1535657e94b6SNick Piggin return -ECHILD; 1536657e94b6SNick Piggin 1537657e94b6SNick Piggin inode = dentry->d_inode; 15381f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 15395584c306STrond Myklebust goto no_open; 15402b484297STrond Myklebust 15411da177e4SLinus Torvalds parent = dget_parent(dentry); 15421da177e4SLinus Torvalds dir = parent->d_inode; 15432b484297STrond Myklebust 15441da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 15451da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 15461da177e4SLinus Torvalds */ 1547216d5d06STrond Myklebust if (inode == NULL) { 1548216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1549216d5d06STrond Myklebust ret = 1; 15501da177e4SLinus Torvalds goto out; 1551216d5d06STrond Myklebust } 1552216d5d06STrond Myklebust 15531da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 15541da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 15555584c306STrond Myklebust goto no_open_dput; 15561da177e4SLinus Torvalds openflags = nd->intent.open.flags; 15571da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 15581da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 15595584c306STrond Myklebust goto no_open_dput; 1560536e43d1STrond Myklebust /* We can't create new files here */ 1561536e43d1STrond Myklebust openflags &= ~(O_CREAT|O_EXCL); 15621da177e4SLinus Torvalds 156351141598SAl Viro ctx = create_nfs_open_context(dentry, openflags); 1564b8d4caddSTrond Myklebust ret = PTR_ERR(ctx); 1565b8d4caddSTrond Myklebust if (IS_ERR(ctx)) 1566b8d4caddSTrond Myklebust goto out; 1567536e43d1STrond Myklebust 1568451146beSTrond Myklebust attr.ia_valid = ATTR_OPEN; 1569536e43d1STrond Myklebust if (openflags & O_TRUNC) { 1570536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE; 1571536e43d1STrond Myklebust attr.ia_size = 0; 1572536e43d1STrond Myklebust nfs_wb_all(inode); 1573536e43d1STrond Myklebust } 1574536e43d1STrond Myklebust 15751da177e4SLinus Torvalds /* 15761b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 15771da177e4SLinus Torvalds * operations that change the directory. We therefore save the 15781da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 15791da177e4SLinus Torvalds */ 1580536e43d1STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr); 1581535918f1STrond Myklebust if (IS_ERR(inode)) { 1582535918f1STrond Myklebust ret = PTR_ERR(inode); 1583535918f1STrond Myklebust switch (ret) { 1584535918f1STrond Myklebust case -EPERM: 1585535918f1STrond Myklebust case -EACCES: 1586535918f1STrond Myklebust case -EDQUOT: 1587535918f1STrond Myklebust case -ENOSPC: 1588535918f1STrond Myklebust case -EROFS: 1589535918f1STrond Myklebust goto out_put_ctx; 1590535918f1STrond Myklebust default: 1591535918f1STrond Myklebust goto out_drop; 1592535918f1STrond Myklebust } 1593535918f1STrond Myklebust } 1594535918f1STrond Myklebust iput(inode); 1595898f635cSTrond Myklebust if (inode != dentry->d_inode) 1596535918f1STrond Myklebust goto out_drop; 1597898f635cSTrond Myklebust 1598898f635cSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1599898f635cSTrond Myklebust ret = nfs_intent_set_file(nd, ctx); 1600898f635cSTrond Myklebust if (ret >= 0) 1601898f635cSTrond Myklebust ret = 1; 16021da177e4SLinus Torvalds out: 16031da177e4SLinus Torvalds dput(parent); 16041da177e4SLinus Torvalds return ret; 1605535918f1STrond Myklebust out_drop: 1606535918f1STrond Myklebust d_drop(dentry); 1607535918f1STrond Myklebust ret = 0; 1608535918f1STrond Myklebust out_put_ctx: 1609535918f1STrond Myklebust put_nfs_open_context(ctx); 1610535918f1STrond Myklebust goto out; 1611535918f1STrond Myklebust 16125584c306STrond Myklebust no_open_dput: 16131da177e4SLinus Torvalds dput(parent); 16145584c306STrond Myklebust no_open: 16151da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 16161da177e4SLinus Torvalds } 1617c0204fd2STrond Myklebust 16184acdaf27SAl Viro static int nfs_open_create(struct inode *dir, struct dentry *dentry, 16194acdaf27SAl Viro umode_t mode, struct nameidata *nd) 1620c0204fd2STrond Myklebust { 1621c0204fd2STrond Myklebust struct nfs_open_context *ctx = NULL; 1622c0204fd2STrond Myklebust struct iattr attr; 1623c0204fd2STrond Myklebust int error; 16248a5e929dSAl Viro int open_flags = O_CREAT|O_EXCL; 1625c0204fd2STrond Myklebust 1626c0204fd2STrond Myklebust dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 1627c0204fd2STrond Myklebust dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 1628c0204fd2STrond Myklebust 1629c0204fd2STrond Myklebust attr.ia_mode = mode; 1630c0204fd2STrond Myklebust attr.ia_valid = ATTR_MODE; 1631c0204fd2STrond Myklebust 1632dd7dd556SAl Viro if (nd) 1633c0204fd2STrond Myklebust open_flags = nd->intent.open.flags; 1634c0204fd2STrond Myklebust 163551141598SAl Viro ctx = create_nfs_open_context(dentry, open_flags); 1636c0204fd2STrond Myklebust error = PTR_ERR(ctx); 1637c0204fd2STrond Myklebust if (IS_ERR(ctx)) 1638898f635cSTrond Myklebust goto out_err_drop; 1639c0204fd2STrond Myklebust 1640c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx); 1641c0204fd2STrond Myklebust if (error != 0) 1642c0204fd2STrond Myklebust goto out_put_ctx; 1643dd7dd556SAl Viro if (nd) { 1644898f635cSTrond Myklebust error = nfs_intent_set_file(nd, ctx); 1645898f635cSTrond Myklebust if (error < 0) 1646898f635cSTrond Myklebust goto out_err; 1647f7c85868SAl Viro } else { 1648f7c85868SAl Viro put_nfs_open_context(ctx); 1649898f635cSTrond Myklebust } 1650c0204fd2STrond Myklebust return 0; 1651c0204fd2STrond Myklebust out_put_ctx: 1652c0204fd2STrond Myklebust put_nfs_open_context(ctx); 1653898f635cSTrond Myklebust out_err_drop: 1654c0204fd2STrond Myklebust d_drop(dentry); 1655898f635cSTrond Myklebust out_err: 1656c0204fd2STrond Myklebust return error; 1657c0204fd2STrond Myklebust } 1658c0204fd2STrond Myklebust 16591da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds /* 16621da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 16631da177e4SLinus Torvalds */ 16641da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 16651da177e4SLinus Torvalds struct nfs_fattr *fattr) 16661da177e4SLinus Torvalds { 1667fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1668fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 16691da177e4SLinus Torvalds struct inode *inode; 16701da177e4SLinus Torvalds int error = -EACCES; 16711da177e4SLinus Torvalds 1672fab728e1STrond Myklebust d_drop(dentry); 1673fab728e1STrond Myklebust 16741da177e4SLinus Torvalds /* We may have been initialized further down */ 16751da177e4SLinus Torvalds if (dentry->d_inode) 1676fab728e1STrond Myklebust goto out; 16771da177e4SLinus Torvalds if (fhandle->size == 0) { 167880a16b21SBryan Schumaker error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 16791da177e4SLinus Torvalds if (error) 1680fab728e1STrond Myklebust goto out_error; 16811da177e4SLinus Torvalds } 16825724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16831da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 16841da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 16858fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 16861da177e4SLinus Torvalds if (error < 0) 1687fab728e1STrond Myklebust goto out_error; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 169003f28e3aSTrond Myklebust error = PTR_ERR(inode); 169103f28e3aSTrond Myklebust if (IS_ERR(inode)) 1692fab728e1STrond Myklebust goto out_error; 1693fab728e1STrond Myklebust d_add(dentry, inode); 1694fab728e1STrond Myklebust out: 1695fab728e1STrond Myklebust dput(parent); 16961da177e4SLinus Torvalds return 0; 1697fab728e1STrond Myklebust out_error: 1698fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1699fab728e1STrond Myklebust dput(parent); 1700fab728e1STrond Myklebust return error; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds /* 17041da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 17051da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 17061da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 17071da177e4SLinus Torvalds * reply path made it appear to have failed. 17081da177e4SLinus Torvalds */ 17094acdaf27SAl Viro static int nfs_create(struct inode *dir, struct dentry *dentry, 17104acdaf27SAl Viro umode_t mode, struct nameidata *nd) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds struct iattr attr; 17131da177e4SLinus Torvalds int error; 17148a5e929dSAl Viro int open_flags = O_CREAT|O_EXCL; 17151da177e4SLinus Torvalds 17161e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 17171e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds attr.ia_mode = mode; 17201da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17211da177e4SLinus Torvalds 1722dd7dd556SAl Viro if (nd) 17238a0eebf6STrond Myklebust open_flags = nd->intent.open.flags; 17248a0eebf6STrond Myklebust 17258a0eebf6STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL); 17261da177e4SLinus Torvalds if (error != 0) 17271da177e4SLinus Torvalds goto out_err; 17281da177e4SLinus Torvalds return 0; 17291da177e4SLinus Torvalds out_err: 17301da177e4SLinus Torvalds d_drop(dentry); 17311da177e4SLinus Torvalds return error; 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds /* 17351da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17361da177e4SLinus Torvalds */ 17371da177e4SLinus Torvalds static int 17381a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) 17391da177e4SLinus Torvalds { 17401da177e4SLinus Torvalds struct iattr attr; 17411da177e4SLinus Torvalds int status; 17421da177e4SLinus Torvalds 17431e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 17441e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 17471da177e4SLinus Torvalds return -EINVAL; 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds attr.ia_mode = mode; 17501da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 17531da177e4SLinus Torvalds if (status != 0) 17541da177e4SLinus Torvalds goto out_err; 17551da177e4SLinus Torvalds return 0; 17561da177e4SLinus Torvalds out_err: 17571da177e4SLinus Torvalds d_drop(dentry); 17581da177e4SLinus Torvalds return status; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* 17621da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 17631da177e4SLinus Torvalds */ 176418bb1db3SAl Viro static int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 17651da177e4SLinus Torvalds { 17661da177e4SLinus Torvalds struct iattr attr; 17671da177e4SLinus Torvalds int error; 17681da177e4SLinus Torvalds 17691e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 17701e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 17731da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 17761da177e4SLinus Torvalds if (error != 0) 17771da177e4SLinus Torvalds goto out_err; 17781da177e4SLinus Torvalds return 0; 17791da177e4SLinus Torvalds out_err: 17801da177e4SLinus Torvalds d_drop(dentry); 17811da177e4SLinus Torvalds return error; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 1784d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1785d45b9d8bSTrond Myklebust { 1786d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1787d45b9d8bSTrond Myklebust d_delete(dentry); 1788d45b9d8bSTrond Myklebust } 1789d45b9d8bSTrond Myklebust 17901da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 17911da177e4SLinus Torvalds { 17921da177e4SLinus Torvalds int error; 17931da177e4SLinus Torvalds 17941e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 17951e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 17981da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 17991da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1800ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1801d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1802d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds return error; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds /* 18081da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 18091da177e4SLinus Torvalds * and after checking that the file has only one user. 18101da177e4SLinus Torvalds * 18111da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 18121da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 18131da177e4SLinus Torvalds */ 18141da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 18151da177e4SLinus Torvalds { 18161da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 18171da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 18181da177e4SLinus Torvalds int error = -EBUSY; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 18211da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 18241da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 18251da177e4SLinus Torvalds error = 0; 18261da177e4SLinus Torvalds goto out; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds if (inode != NULL) { 1830cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 18311da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 18321da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 18331da177e4SLinus Torvalds if (error == 0) 18341b83d707STrond Myklebust nfs_drop_nlink(inode); 18355ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 18361da177e4SLinus Torvalds } else 18371da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1838d45b9d8bSTrond Myklebust if (error == -ENOENT) 1839d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 18401da177e4SLinus Torvalds out: 18411da177e4SLinus Torvalds return error; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 18451da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 18461da177e4SLinus Torvalds * 18471da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 18481da177e4SLinus Torvalds */ 18491da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 18501da177e4SLinus Torvalds { 18511da177e4SLinus Torvalds int error; 18521da177e4SLinus Torvalds int need_rehash = 0; 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 18551da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 1858b7ab39f6SNick Piggin if (dentry->d_count > 1) { 18591da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 1860ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1861ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 18621da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 18631da177e4SLinus Torvalds return error; 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 18661da177e4SLinus Torvalds __d_drop(dentry); 18671da177e4SLinus Torvalds need_rehash = 1; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 18701da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1871d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 18721da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 18731da177e4SLinus Torvalds } else if (need_rehash) 18741da177e4SLinus Torvalds d_rehash(dentry); 18751da177e4SLinus Torvalds return error; 18761da177e4SLinus Torvalds } 18771da177e4SLinus Torvalds 1878873101b3SChuck Lever /* 1879873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1880873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1881873101b3SChuck Lever * using prepare_write/commit_write. 1882873101b3SChuck Lever * 1883873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1884873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1885873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1886873101b3SChuck Lever * symlink request has completed on the server. 1887873101b3SChuck Lever * 1888873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1889873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1890873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1891873101b3SChuck Lever * and move the raw page into its mapping. 1892873101b3SChuck Lever */ 1893873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 18941da177e4SLinus Torvalds { 1895873101b3SChuck Lever struct pagevec lru_pvec; 1896873101b3SChuck Lever struct page *page; 1897873101b3SChuck Lever char *kaddr; 18981da177e4SLinus Torvalds struct iattr attr; 1899873101b3SChuck Lever unsigned int pathlen = strlen(symname); 19001da177e4SLinus Torvalds int error; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 19031da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 19041da177e4SLinus Torvalds 1905873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1906873101b3SChuck Lever return -ENAMETOOLONG; 19071da177e4SLinus Torvalds 1908873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1909873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 19101da177e4SLinus Torvalds 191183d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 191276566991STrond Myklebust if (!page) 1913873101b3SChuck Lever return -ENOMEM; 1914873101b3SChuck Lever 19152b86ce2dSCong Wang kaddr = kmap_atomic(page); 1916873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1917873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1918873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 19192b86ce2dSCong Wang kunmap_atomic(kaddr); 1920873101b3SChuck Lever 192194a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1922873101b3SChuck Lever if (error != 0) { 1923873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1924873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1925873101b3SChuck Lever dentry->d_name.name, symname, error); 19261da177e4SLinus Torvalds d_drop(dentry); 1927873101b3SChuck Lever __free_page(page); 19281da177e4SLinus Torvalds return error; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 1931873101b3SChuck Lever /* 1932873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1933873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1934873101b3SChuck Lever */ 1935873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1936873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1937873101b3SChuck Lever GFP_KERNEL)) { 193839cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 19394f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1940873101b3SChuck Lever SetPageUptodate(page); 1941873101b3SChuck Lever unlock_page(page); 1942873101b3SChuck Lever } else 1943873101b3SChuck Lever __free_page(page); 1944873101b3SChuck Lever 1945873101b3SChuck Lever return 0; 1946873101b3SChuck Lever } 1947873101b3SChuck Lever 19481da177e4SLinus Torvalds static int 19491da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 19521da177e4SLinus Torvalds int error; 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 19551da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 19561da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 19571da177e4SLinus Torvalds 19589a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 19599a3936aaSTrond Myklebust 19609697d234STrond Myklebust d_drop(dentry); 19611da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1962cf809556STrond Myklebust if (error == 0) { 19637de9c6eeSAl Viro ihold(inode); 19649697d234STrond Myklebust d_add(dentry, inode); 1965cf809556STrond Myklebust } 19661da177e4SLinus Torvalds return error; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds /* 19701da177e4SLinus Torvalds * RENAME 19711da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 19721da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 19731da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 19741da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 19751da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 19761da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 19771da177e4SLinus Torvalds * 19781da177e4SLinus Torvalds * FIXED. 19791da177e4SLinus Torvalds * 19801da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 19811da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 19821da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 19831da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 19841da177e4SLinus Torvalds * using the inode layer 19851da177e4SLinus Torvalds * 19861da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 19871da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 19881da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 19891da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 19901da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 19911da177e4SLinus Torvalds * the rename. 19921da177e4SLinus Torvalds */ 19931da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 19941da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 19951da177e4SLinus Torvalds { 19961da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 19971da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 19981da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 19991da177e4SLinus Torvalds int error = -EBUSY; 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 20021da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 20031da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 2004b7ab39f6SNick Piggin new_dentry->d_count); 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds /* 200728f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 200828f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 200928f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 201028f79a1aSMiklos Szeredi * the new target. 20111da177e4SLinus Torvalds */ 201227226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 201327226104SMiklos Szeredi /* 201427226104SMiklos Szeredi * To prevent any new references to the target during the 201527226104SMiklos Szeredi * rename, we unhash the dentry in advance. 201627226104SMiklos Szeredi */ 201727226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 201827226104SMiklos Szeredi d_drop(new_dentry); 201927226104SMiklos Szeredi rehash = new_dentry; 202027226104SMiklos Szeredi } 202127226104SMiklos Szeredi 2022b7ab39f6SNick Piggin if (new_dentry->d_count > 2) { 20231da177e4SLinus Torvalds int err; 202427226104SMiklos Szeredi 20251da177e4SLinus Torvalds /* copy the target dentry's name */ 20261da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 20271da177e4SLinus Torvalds &new_dentry->d_name); 20281da177e4SLinus Torvalds if (!dentry) 20291da177e4SLinus Torvalds goto out; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds /* silly-rename the existing target ... */ 20321da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 203324e93025SMiklos Szeredi if (err) 20341da177e4SLinus Torvalds goto out; 203524e93025SMiklos Szeredi 203624e93025SMiklos Szeredi new_dentry = dentry; 203756335936SOGAWA Hirofumi rehash = NULL; 203824e93025SMiklos Szeredi new_inode = NULL; 2039b1e4adf4STrond Myklebust } 204027226104SMiklos Szeredi } 20411da177e4SLinus Torvalds 2042cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 2043b1e4adf4STrond Myklebust if (new_inode != NULL) 204424174119STrond Myklebust nfs_inode_return_delegation(new_inode); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 20471da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 20485ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 20491da177e4SLinus Torvalds out: 20501da177e4SLinus Torvalds if (rehash) 20511da177e4SLinus Torvalds d_rehash(rehash); 20521da177e4SLinus Torvalds if (!error) { 2053b1e4adf4STrond Myklebust if (new_inode != NULL) 2054b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 20551da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 20568fb559f8SChuck Lever nfs_set_verifier(new_dentry, 20578fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 2058d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 2059d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds /* new dentry created? */ 20621da177e4SLinus Torvalds if (dentry) 20631da177e4SLinus Torvalds dput(dentry); 20641da177e4SLinus Torvalds return error; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 2067cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 2068cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 2069cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 2070cfcea3e8STrond Myklebust 20711c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 20721c3c07e9STrond Myklebust { 20731c3c07e9STrond Myklebust put_rpccred(entry->cred); 20741c3c07e9STrond Myklebust kfree(entry); 2075cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 2076cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 2077cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 20781c3c07e9STrond Myklebust } 20791c3c07e9STrond Myklebust 20801a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 20811a81bb8aSTrond Myklebust { 20821a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 20831a81bb8aSTrond Myklebust 20841a81bb8aSTrond Myklebust while (!list_empty(head)) { 20851a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 20861a81bb8aSTrond Myklebust list_del(&cache->lru); 20871a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 20881a81bb8aSTrond Myklebust } 20891a81bb8aSTrond Myklebust } 20901a81bb8aSTrond Myklebust 20911495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink, 20921495f230SYing Han struct shrink_control *sc) 2093979df72eSTrond Myklebust { 2094979df72eSTrond Myklebust LIST_HEAD(head); 2095aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 2096979df72eSTrond Myklebust struct nfs_access_entry *cache; 20971495f230SYing Han int nr_to_scan = sc->nr_to_scan; 20981495f230SYing Han gfp_t gfp_mask = sc->gfp_mask; 2099979df72eSTrond Myklebust 210061d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 210161d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 21029c7e7e23STrond Myklebust 2103a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 2104aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2105979df72eSTrond Myklebust struct inode *inode; 2106979df72eSTrond Myklebust 2107979df72eSTrond Myklebust if (nr_to_scan-- == 0) 2108979df72eSTrond Myklebust break; 21099c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 2110979df72eSTrond Myklebust spin_lock(&inode->i_lock); 2111979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 2112979df72eSTrond Myklebust goto remove_lru_entry; 2113979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 2114979df72eSTrond Myklebust struct nfs_access_entry, lru); 2115979df72eSTrond Myklebust list_move(&cache->lru, &head); 2116979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2117979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 2118979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 2119979df72eSTrond Myklebust &nfs_access_lru_list); 2120979df72eSTrond Myklebust else { 2121979df72eSTrond Myklebust remove_lru_entry: 2122979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 21239c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 2124979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 21259c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 2126979df72eSTrond Myklebust } 212759844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 2128979df72eSTrond Myklebust } 2129979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21301a81bb8aSTrond Myklebust nfs_access_free_list(&head); 2131979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 2132979df72eSTrond Myklebust } 2133979df72eSTrond Myklebust 21341a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 21351c3c07e9STrond Myklebust { 21361c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 21371a81bb8aSTrond Myklebust struct rb_node *n; 21381c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21391c3c07e9STrond Myklebust 21401c3c07e9STrond Myklebust /* Unhook entries from the cache */ 21411c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 21421c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 21431c3c07e9STrond Myklebust rb_erase(n, root_node); 21441a81bb8aSTrond Myklebust list_move(&entry->lru, head); 21451c3c07e9STrond Myklebust } 21461c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 21471c3c07e9STrond Myklebust } 21481c3c07e9STrond Myklebust 21491c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 21501c3c07e9STrond Myklebust { 21511a81bb8aSTrond Myklebust LIST_HEAD(head); 21521a81bb8aSTrond Myklebust 21531a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 21541a81bb8aSTrond Myklebust return; 2155cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2156cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 21571a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2158cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2159cfcea3e8STrond Myklebust 21601c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 21611a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 21621a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 21631a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 21641a81bb8aSTrond Myklebust nfs_access_free_list(&head); 21651c3c07e9STrond Myklebust } 21661c3c07e9STrond Myklebust 21671c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 21681c3c07e9STrond Myklebust { 21691c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 21701c3c07e9STrond Myklebust struct nfs_access_entry *entry; 21711c3c07e9STrond Myklebust 21721c3c07e9STrond Myklebust while (n != NULL) { 21731c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 21741c3c07e9STrond Myklebust 21751c3c07e9STrond Myklebust if (cred < entry->cred) 21761c3c07e9STrond Myklebust n = n->rb_left; 21771c3c07e9STrond Myklebust else if (cred > entry->cred) 21781c3c07e9STrond Myklebust n = n->rb_right; 21791c3c07e9STrond Myklebust else 21801c3c07e9STrond Myklebust return entry; 21811c3c07e9STrond Myklebust } 21821c3c07e9STrond Myklebust return NULL; 21831c3c07e9STrond Myklebust } 21841c3c07e9STrond Myklebust 2185af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 21861da177e4SLinus Torvalds { 218755296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 21881c3c07e9STrond Myklebust struct nfs_access_entry *cache; 21891c3c07e9STrond Myklebust int err = -ENOENT; 21901da177e4SLinus Torvalds 21911c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 21921c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 21931c3c07e9STrond Myklebust goto out_zap; 21941c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 21951c3c07e9STrond Myklebust if (cache == NULL) 21961c3c07e9STrond Myklebust goto out; 2197b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 219864672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 21991c3c07e9STrond Myklebust goto out_stale; 22001c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 22011c3c07e9STrond Myklebust res->cred = cache->cred; 22021c3c07e9STrond Myklebust res->mask = cache->mask; 2203cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 22041c3c07e9STrond Myklebust err = 0; 22051c3c07e9STrond Myklebust out: 22061c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22071c3c07e9STrond Myklebust return err; 22081c3c07e9STrond Myklebust out_stale: 22091c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2210cfcea3e8STrond Myklebust list_del(&cache->lru); 22111c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22121c3c07e9STrond Myklebust nfs_access_free_entry(cache); 22131da177e4SLinus Torvalds return -ENOENT; 22141c3c07e9STrond Myklebust out_zap: 22151a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 22161a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 22171c3c07e9STrond Myklebust return -ENOENT; 22181c3c07e9STrond Myklebust } 22191c3c07e9STrond Myklebust 22201c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 22211c3c07e9STrond Myklebust { 2222cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2223cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 22241c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 22251c3c07e9STrond Myklebust struct rb_node *parent = NULL; 22261c3c07e9STrond Myklebust struct nfs_access_entry *entry; 22271c3c07e9STrond Myklebust 22281c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 22291c3c07e9STrond Myklebust while (*p != NULL) { 22301c3c07e9STrond Myklebust parent = *p; 22311c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 22321c3c07e9STrond Myklebust 22331c3c07e9STrond Myklebust if (set->cred < entry->cred) 22341c3c07e9STrond Myklebust p = &parent->rb_left; 22351c3c07e9STrond Myklebust else if (set->cred > entry->cred) 22361c3c07e9STrond Myklebust p = &parent->rb_right; 22371c3c07e9STrond Myklebust else 22381c3c07e9STrond Myklebust goto found; 22391c3c07e9STrond Myklebust } 22401c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 22411c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2242cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 22431c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22441c3c07e9STrond Myklebust return; 22451c3c07e9STrond Myklebust found: 22461c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2247cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2248cfcea3e8STrond Myklebust list_del(&entry->lru); 22491c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 22501c3c07e9STrond Myklebust nfs_access_free_entry(entry); 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 2253af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 22541da177e4SLinus Torvalds { 22551c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 22561c3c07e9STrond Myklebust if (cache == NULL) 22571c3c07e9STrond Myklebust return; 22581c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 22591da177e4SLinus Torvalds cache->jiffies = set->jiffies; 22601c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 22611da177e4SLinus Torvalds cache->mask = set->mask; 22621c3c07e9STrond Myklebust 22631c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2264cfcea3e8STrond Myklebust 2265cfcea3e8STrond Myklebust /* Update accounting */ 2266cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 2267cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 2268cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 2269cfcea3e8STrond Myklebust 2270cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 22711a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2272cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 22731a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 22741a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 22751a81bb8aSTrond Myklebust &nfs_access_lru_list); 2276cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2277cfcea3e8STrond Myklebust } 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds struct nfs_access_entry cache; 22831da177e4SLinus Torvalds int status; 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 22861da177e4SLinus Torvalds if (status == 0) 22871da177e4SLinus Torvalds goto out; 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 22901da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 22911da177e4SLinus Torvalds cache.cred = cred; 22921da177e4SLinus Torvalds cache.jiffies = jiffies; 22931da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2294a71ee337SSuresh Jayaraman if (status != 0) { 2295a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2296a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2297a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2298a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2299a71ee337SSuresh Jayaraman } 23001da177e4SLinus Torvalds return status; 2301a71ee337SSuresh Jayaraman } 23021da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 23031da177e4SLinus Torvalds out: 2304e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 23051da177e4SLinus Torvalds return 0; 23061da177e4SLinus Torvalds return -EACCES; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 2309af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2310af22f94aSTrond Myklebust { 2311af22f94aSTrond Myklebust int mask = 0; 2312af22f94aSTrond Myklebust 23138a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY) 2314af22f94aSTrond Myklebust mask |= MAY_READ; 23158a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY) 2316af22f94aSTrond Myklebust mask |= MAY_WRITE; 23178a5e929dSAl Viro if (openflags & __FMODE_EXEC) 2318af22f94aSTrond Myklebust mask |= MAY_EXEC; 2319af22f94aSTrond Myklebust return mask; 2320af22f94aSTrond Myklebust } 2321af22f94aSTrond Myklebust 2322af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2323af22f94aSTrond Myklebust { 2324af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2325af22f94aSTrond Myklebust } 2326af22f94aSTrond Myklebust 232710556cb2SAl Viro int nfs_permission(struct inode *inode, int mask) 23281da177e4SLinus Torvalds { 23291da177e4SLinus Torvalds struct rpc_cred *cred; 23301da177e4SLinus Torvalds int res = 0; 23311da177e4SLinus Torvalds 233210556cb2SAl Viro if (mask & MAY_NOT_BLOCK) 2333b74c79e9SNick Piggin return -ECHILD; 2334b74c79e9SNick Piggin 233591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 233691d5b470SChuck Lever 2337e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 23381da177e4SLinus Torvalds goto out; 23391da177e4SLinus Torvalds /* Is this sys_access() ? */ 23409cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 23411da177e4SLinus Torvalds goto force_lookup; 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 23441da177e4SLinus Torvalds case S_IFLNK: 23451da177e4SLinus Torvalds goto out; 23461da177e4SLinus Torvalds case S_IFREG: 23471da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 23481da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 23497ee2cb7fSFrank Filz && (mask & MAY_OPEN) 23507ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 23511da177e4SLinus Torvalds goto out; 23521da177e4SLinus Torvalds break; 23531da177e4SLinus Torvalds case S_IFDIR: 23541da177e4SLinus Torvalds /* 23551da177e4SLinus Torvalds * Optimize away all write operations, since the server 23561da177e4SLinus Torvalds * will check permissions when we perform the op. 23571da177e4SLinus Torvalds */ 23581da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 23591da177e4SLinus Torvalds goto out; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds force_lookup: 23631da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 23641da177e4SLinus Torvalds goto out_notsup; 23651da177e4SLinus Torvalds 236698a8e323STrond Myklebust cred = rpc_lookup_cred(); 23671da177e4SLinus Torvalds if (!IS_ERR(cred)) { 23681da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 23691da177e4SLinus Torvalds put_rpccred(cred); 23701da177e4SLinus Torvalds } else 23711da177e4SLinus Torvalds res = PTR_ERR(cred); 23721da177e4SLinus Torvalds out: 2373f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2374f696a365SMiklos Szeredi res = -EACCES; 2375f696a365SMiklos Szeredi 23761e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 23771e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 23781da177e4SLinus Torvalds return res; 23791da177e4SLinus Torvalds out_notsup: 23801da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 23811da177e4SLinus Torvalds if (res == 0) 23822830ba7fSAl Viro res = generic_permission(inode, mask); 23831e7cb3dcSChuck Lever goto out; 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds /* 23871da177e4SLinus Torvalds * Local variables: 23881da177e4SLinus Torvalds * version-control: t 23891da177e4SLinus Torvalds * kept-new-versions: 5 23901da177e4SLinus Torvalds * End: 23911da177e4SLinus Torvalds */ 2392