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> 3656e4ebf8SBryan Schumaker #include <linux/vmalloc.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include "delegation.h" 3991d5b470SChuck Lever #include "iostat.h" 404c30d56eSAdrian Bunk #include "internal.h" 41cd9a1c0eSTrond Myklebust #include "fscache.h" 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 461da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 471da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 481da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 491da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 501da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 511da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 521da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 531da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 541da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 551da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 561da177e4SLinus Torvalds struct inode *, struct dentry *); 577ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int); 58f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 59d1bacf9eSBryan Schumaker static int nfs_readdir_clear_array(struct page*, gfp_t); 601da177e4SLinus Torvalds 614b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 62f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 631da177e4SLinus Torvalds .read = generic_read_dir, 641da177e4SLinus Torvalds .readdir = nfs_readdir, 651da177e4SLinus Torvalds .open = nfs_opendir, 661da177e4SLinus Torvalds .release = nfs_release, 671da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 681da177e4SLinus Torvalds }; 691da177e4SLinus Torvalds 7092e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = { 711da177e4SLinus Torvalds .create = nfs_create, 721da177e4SLinus Torvalds .lookup = nfs_lookup, 731da177e4SLinus Torvalds .link = nfs_link, 741da177e4SLinus Torvalds .unlink = nfs_unlink, 751da177e4SLinus Torvalds .symlink = nfs_symlink, 761da177e4SLinus Torvalds .mkdir = nfs_mkdir, 771da177e4SLinus Torvalds .rmdir = nfs_rmdir, 781da177e4SLinus Torvalds .mknod = nfs_mknod, 791da177e4SLinus Torvalds .rename = nfs_rename, 801da177e4SLinus Torvalds .permission = nfs_permission, 811da177e4SLinus Torvalds .getattr = nfs_getattr, 821da177e4SLinus Torvalds .setattr = nfs_setattr, 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 85d1bacf9eSBryan Schumaker const struct address_space_operations nfs_dir_addr_space_ops = { 86d1bacf9eSBryan Schumaker .releasepage = nfs_readdir_clear_array, 87d1bacf9eSBryan Schumaker }; 88d1bacf9eSBryan Schumaker 89b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 9092e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = { 91b7fa0554SAndreas Gruenbacher .create = nfs_create, 92b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 93b7fa0554SAndreas Gruenbacher .link = nfs_link, 94b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 95b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 96b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 97b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 98b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 99b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 100b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 101b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 102b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 103b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 104b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 105b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 106b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 107b7fa0554SAndreas Gruenbacher }; 108b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 109b7fa0554SAndreas Gruenbacher 1101da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 113c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd); 11492e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = { 115c0204fd2STrond Myklebust .create = nfs_open_create, 1161da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1171da177e4SLinus Torvalds .link = nfs_link, 1181da177e4SLinus Torvalds .unlink = nfs_unlink, 1191da177e4SLinus Torvalds .symlink = nfs_symlink, 1201da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1211da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1221da177e4SLinus Torvalds .mknod = nfs_mknod, 1231da177e4SLinus Torvalds .rename = nfs_rename, 1241da177e4SLinus Torvalds .permission = nfs_permission, 1251da177e4SLinus Torvalds .getattr = nfs_getattr, 1261da177e4SLinus Torvalds .setattr = nfs_setattr, 1276b3b5496SJ. Bruce Fields .getxattr = nfs4_getxattr, 1286b3b5496SJ. Bruce Fields .setxattr = nfs4_setxattr, 1296b3b5496SJ. Bruce Fields .listxattr = nfs4_listxattr, 1301da177e4SLinus Torvalds }; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * Open file 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds static int 1381da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1391da177e4SLinus Torvalds { 1407451c4f0SCarsten Otte int res; 1411da177e4SLinus Torvalds 1426da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 143cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 144cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 145cc0dd2d1SChuck Lever 146cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1471e7cb3dcSChuck Lever 1481da177e4SLinus Torvalds /* Call generic open code in order to cache credentials */ 1491da177e4SLinus Torvalds res = nfs_open(inode, filp); 150f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 151f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 152f5a73672SNeil Brown * have been called, so we need to refresh the 153f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 154f5a73672SNeil Brown */ 155f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 156f5a73672SNeil Brown } 1571da177e4SLinus Torvalds return res; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 161d1bacf9eSBryan Schumaker u64 cookie; 162d1bacf9eSBryan Schumaker u64 ino; 163d1bacf9eSBryan Schumaker struct qstr string; 164d1bacf9eSBryan Schumaker }; 165d1bacf9eSBryan Schumaker 166d1bacf9eSBryan Schumaker struct nfs_cache_array { 167d1bacf9eSBryan Schumaker unsigned int size; 168d1bacf9eSBryan Schumaker int eof_index; 169d1bacf9eSBryan Schumaker u64 last_cookie; 170d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 171d1bacf9eSBryan Schumaker }; 172d1bacf9eSBryan Schumaker 173d1bacf9eSBryan Schumaker #define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry)) 174d1bacf9eSBryan Schumaker 17582f2e547SBryan Schumaker typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, struct nfs_server *, int); 1761da177e4SLinus Torvalds typedef struct { 1771da177e4SLinus Torvalds struct file *file; 1781da177e4SLinus Torvalds struct page *page; 1791da177e4SLinus Torvalds unsigned long page_index; 180f0dd2136STrond Myklebust u64 *dir_cookie; 181f0dd2136STrond Myklebust loff_t current_index; 1821da177e4SLinus Torvalds decode_dirent_t decode; 183d1bacf9eSBryan Schumaker 1841f4eab7eSNeil Brown unsigned long timestamp; 1854704f0e2STrond Myklebust unsigned long gencount; 186d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 187d1bacf9eSBryan Schumaker unsigned int plus:1; 188d1bacf9eSBryan Schumaker unsigned int eof:1; 1891da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1901da177e4SLinus Torvalds 191d1bacf9eSBryan Schumaker /* 192d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds static 195d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 1961da177e4SLinus Torvalds { 197*8cd51a0cSTrond Myklebust void *ptr; 198d1bacf9eSBryan Schumaker if (page == NULL) 199d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 200*8cd51a0cSTrond Myklebust ptr = kmap(page); 201*8cd51a0cSTrond Myklebust if (ptr == NULL) 202*8cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 203*8cd51a0cSTrond Myklebust return ptr; 204d1bacf9eSBryan Schumaker } 205d1bacf9eSBryan Schumaker 206d1bacf9eSBryan Schumaker static 207d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 208d1bacf9eSBryan Schumaker { 209d1bacf9eSBryan Schumaker kunmap(page); 210d1bacf9eSBryan Schumaker } 211d1bacf9eSBryan Schumaker 212d1bacf9eSBryan Schumaker /* 213d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 214d1bacf9eSBryan Schumaker */ 215d1bacf9eSBryan Schumaker static 216d1bacf9eSBryan Schumaker int nfs_readdir_clear_array(struct page *page, gfp_t mask) 217d1bacf9eSBryan Schumaker { 218d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 219d1bacf9eSBryan Schumaker int i; 220*8cd51a0cSTrond Myklebust 221*8cd51a0cSTrond Myklebust if (IS_ERR(array)) 222*8cd51a0cSTrond Myklebust return PTR_ERR(array); 223d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 224d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 225d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 226d1bacf9eSBryan Schumaker return 0; 227d1bacf9eSBryan Schumaker } 228d1bacf9eSBryan Schumaker 229d1bacf9eSBryan Schumaker /* 230d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 231d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 232d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 233d1bacf9eSBryan Schumaker */ 234d1bacf9eSBryan Schumaker static 2354a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 236d1bacf9eSBryan Schumaker { 237d1bacf9eSBryan Schumaker string->len = len; 238d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2394a201d6eSTrond Myklebust if (string->name == NULL) 2404a201d6eSTrond Myklebust return -ENOMEM; 2414a201d6eSTrond Myklebust string->hash = full_name_hash(name, len); 2424a201d6eSTrond Myklebust return 0; 243d1bacf9eSBryan Schumaker } 244d1bacf9eSBryan Schumaker 245d1bacf9eSBryan Schumaker static 246d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 247d1bacf9eSBryan Schumaker { 248d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2494a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2504a201d6eSTrond Myklebust int ret; 2514a201d6eSTrond Myklebust 252d1bacf9eSBryan Schumaker if (IS_ERR(array)) 253d1bacf9eSBryan Schumaker return PTR_ERR(array); 254*8cd51a0cSTrond Myklebust ret = -ENOSPC; 2554a201d6eSTrond Myklebust if (array->size >= MAX_READDIR_ARRAY) 2564a201d6eSTrond Myklebust goto out; 257d1bacf9eSBryan Schumaker 2584a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2594a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2604a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2614a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2624a201d6eSTrond Myklebust if (ret) 2634a201d6eSTrond Myklebust goto out; 264d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 265*8cd51a0cSTrond Myklebust array->size++; 266d1bacf9eSBryan Schumaker if (entry->eof == 1) 267d1bacf9eSBryan Schumaker array->eof_index = array->size; 2684a201d6eSTrond Myklebust out: 269d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 2704a201d6eSTrond Myklebust return ret; 271d1bacf9eSBryan Schumaker } 272d1bacf9eSBryan Schumaker 273d1bacf9eSBryan Schumaker static 274d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 275d1bacf9eSBryan Schumaker { 276d1bacf9eSBryan Schumaker loff_t diff = desc->file->f_pos - desc->current_index; 277d1bacf9eSBryan Schumaker unsigned int index; 278d1bacf9eSBryan Schumaker 279d1bacf9eSBryan Schumaker if (diff < 0) 280d1bacf9eSBryan Schumaker goto out_eof; 281d1bacf9eSBryan Schumaker if (diff >= array->size) { 282*8cd51a0cSTrond Myklebust if (array->eof_index >= 0) 283d1bacf9eSBryan Schumaker goto out_eof; 284d1bacf9eSBryan Schumaker desc->current_index += array->size; 285d1bacf9eSBryan Schumaker return -EAGAIN; 286d1bacf9eSBryan Schumaker } 287d1bacf9eSBryan Schumaker 288d1bacf9eSBryan Schumaker index = (unsigned int)diff; 289d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 290d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 291d1bacf9eSBryan Schumaker return 0; 292d1bacf9eSBryan Schumaker out_eof: 293d1bacf9eSBryan Schumaker desc->eof = 1; 294d1bacf9eSBryan Schumaker return -EBADCOOKIE; 295d1bacf9eSBryan Schumaker } 296d1bacf9eSBryan Schumaker 297d1bacf9eSBryan Schumaker static 298d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 299d1bacf9eSBryan Schumaker { 300d1bacf9eSBryan Schumaker int i; 301d1bacf9eSBryan Schumaker int status = -EAGAIN; 302d1bacf9eSBryan Schumaker 303d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 304*8cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 305*8cd51a0cSTrond Myklebust desc->cache_entry_index = i; 306*8cd51a0cSTrond Myklebust status = 0; 307*8cd51a0cSTrond Myklebust goto out; 308*8cd51a0cSTrond Myklebust } 309*8cd51a0cSTrond Myklebust } 310d1bacf9eSBryan Schumaker if (i == array->eof_index) { 311d1bacf9eSBryan Schumaker desc->eof = 1; 312d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 313d1bacf9eSBryan Schumaker } 314*8cd51a0cSTrond Myklebust out: 315d1bacf9eSBryan Schumaker return status; 316d1bacf9eSBryan Schumaker } 317d1bacf9eSBryan Schumaker 318d1bacf9eSBryan Schumaker static 319d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 320d1bacf9eSBryan Schumaker { 321d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 322d1bacf9eSBryan Schumaker int status = -EBADCOOKIE; 323d1bacf9eSBryan Schumaker 324d1bacf9eSBryan Schumaker if (desc->dir_cookie == NULL) 325d1bacf9eSBryan Schumaker goto out; 326d1bacf9eSBryan Schumaker 327d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 328d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 329d1bacf9eSBryan Schumaker status = PTR_ERR(array); 330d1bacf9eSBryan Schumaker goto out; 331d1bacf9eSBryan Schumaker } 332d1bacf9eSBryan Schumaker 333d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 334d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 335d1bacf9eSBryan Schumaker else 336d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 337d1bacf9eSBryan Schumaker 338d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 339d1bacf9eSBryan Schumaker out: 340d1bacf9eSBryan Schumaker return status; 341d1bacf9eSBryan Schumaker } 342d1bacf9eSBryan Schumaker 343d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 344d1bacf9eSBryan Schumaker static 34556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 346d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 347d1bacf9eSBryan Schumaker { 3481da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 3494704f0e2STrond Myklebust unsigned long timestamp, gencount; 3501da177e4SLinus Torvalds int error; 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds again: 3531da177e4SLinus Torvalds timestamp = jiffies; 3544704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 35556e4ebf8SBryan Schumaker error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages, 3561da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3571da177e4SLinus Torvalds if (error < 0) { 3581da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3591da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3601da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3613a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 3621da177e4SLinus Torvalds desc->plus = 0; 3631da177e4SLinus Torvalds goto again; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds goto error; 3661da177e4SLinus Torvalds } 3671f4eab7eSNeil Brown desc->timestamp = timestamp; 3684704f0e2STrond Myklebust desc->gencount = gencount; 369d1bacf9eSBryan Schumaker error: 370d1bacf9eSBryan Schumaker return error; 371d1bacf9eSBryan Schumaker } 372d1bacf9eSBryan Schumaker 373d1bacf9eSBryan Schumaker /* Fill in an entry based on the xdr code stored in desc->page */ 374d1bacf9eSBryan Schumaker static 375babddc72SBryan Schumaker int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream) 376d1bacf9eSBryan Schumaker { 37782f2e547SBryan Schumaker __be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus); 378d1bacf9eSBryan Schumaker if (IS_ERR(p)) 379d1bacf9eSBryan Schumaker return PTR_ERR(p); 380d1bacf9eSBryan Schumaker 381d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 382d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 383d1bacf9eSBryan Schumaker return 0; 384d1bacf9eSBryan Schumaker } 385d1bacf9eSBryan Schumaker 386d39ab9deSBryan Schumaker static 387d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 388d39ab9deSBryan Schumaker { 389d39ab9deSBryan Schumaker struct nfs_inode *node; 390d39ab9deSBryan Schumaker if (dentry->d_inode == NULL) 391d39ab9deSBryan Schumaker goto different; 392d39ab9deSBryan Schumaker node = NFS_I(dentry->d_inode); 393d39ab9deSBryan Schumaker if (node->fh.size != entry->fh->size) 394d39ab9deSBryan Schumaker goto different; 395d39ab9deSBryan Schumaker if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0) 396d39ab9deSBryan Schumaker goto different; 397d39ab9deSBryan Schumaker return 1; 398d39ab9deSBryan Schumaker different: 399d39ab9deSBryan Schumaker return 0; 400d39ab9deSBryan Schumaker } 401d39ab9deSBryan Schumaker 402d39ab9deSBryan Schumaker static 403d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 404d39ab9deSBryan Schumaker { 4054a201d6eSTrond Myklebust struct qstr filename = { 4064a201d6eSTrond Myklebust .len = entry->len, 4074a201d6eSTrond Myklebust .name = entry->name, 4084a201d6eSTrond Myklebust }; 4094a201d6eSTrond Myklebust struct dentry *dentry; 4104a201d6eSTrond Myklebust struct dentry *alias; 411d39ab9deSBryan Schumaker struct inode *dir = parent->d_inode; 412d39ab9deSBryan Schumaker struct inode *inode; 413d39ab9deSBryan Schumaker 4144a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4154a201d6eSTrond Myklebust if (filename.len == 1) 4164a201d6eSTrond Myklebust return; 4174a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4184a201d6eSTrond Myklebust return; 4194a201d6eSTrond Myklebust } 4204a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 421d39ab9deSBryan Schumaker 4224a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 423d39ab9deSBryan Schumaker if (dentry != NULL) { 424d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 425d39ab9deSBryan Schumaker nfs_refresh_inode(dentry->d_inode, entry->fattr); 426d39ab9deSBryan Schumaker goto out; 427d39ab9deSBryan Schumaker } else { 428d39ab9deSBryan Schumaker d_drop(dentry); 429d39ab9deSBryan Schumaker dput(dentry); 430d39ab9deSBryan Schumaker } 431d39ab9deSBryan Schumaker } 432d39ab9deSBryan Schumaker 433d39ab9deSBryan Schumaker dentry = d_alloc(parent, &filename); 4344a201d6eSTrond Myklebust if (dentry == NULL) 4354a201d6eSTrond Myklebust return; 4364a201d6eSTrond Myklebust 437d39ab9deSBryan Schumaker dentry->d_op = NFS_PROTO(dir)->dentry_ops; 438d39ab9deSBryan Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 439d39ab9deSBryan Schumaker if (IS_ERR(inode)) 440d39ab9deSBryan Schumaker goto out; 441d39ab9deSBryan Schumaker 442d39ab9deSBryan Schumaker alias = d_materialise_unique(dentry, inode); 443d39ab9deSBryan Schumaker if (IS_ERR(alias)) 444d39ab9deSBryan Schumaker goto out; 445d39ab9deSBryan Schumaker else if (alias) { 446d39ab9deSBryan Schumaker nfs_set_verifier(alias, nfs_save_change_attribute(dir)); 447d39ab9deSBryan Schumaker dput(alias); 448d39ab9deSBryan Schumaker } else 449d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 450d39ab9deSBryan Schumaker 451d39ab9deSBryan Schumaker out: 452d39ab9deSBryan Schumaker dput(dentry); 453d39ab9deSBryan Schumaker } 454d39ab9deSBryan Schumaker 455d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 456d1bacf9eSBryan Schumaker static 457*8cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 45856e4ebf8SBryan Schumaker void *xdr_page, struct page *page, unsigned int buflen) 459d1bacf9eSBryan Schumaker { 460babddc72SBryan Schumaker struct xdr_stream stream; 461babddc72SBryan Schumaker struct xdr_buf buf; 46256e4ebf8SBryan Schumaker __be32 *ptr = xdr_page; 46399424380SBryan Schumaker int status; 46499424380SBryan Schumaker struct nfs_cache_array *array; 465babddc72SBryan Schumaker 466babddc72SBryan Schumaker buf.head->iov_base = xdr_page; 467babddc72SBryan Schumaker buf.head->iov_len = buflen; 468babddc72SBryan Schumaker buf.tail->iov_len = 0; 469babddc72SBryan Schumaker buf.page_base = 0; 470babddc72SBryan Schumaker buf.page_len = 0; 471babddc72SBryan Schumaker buf.buflen = buf.head->iov_len; 472babddc72SBryan Schumaker buf.len = buf.head->iov_len; 473babddc72SBryan Schumaker 474babddc72SBryan Schumaker xdr_init_decode(&stream, &buf, ptr); 475babddc72SBryan Schumaker 47699424380SBryan Schumaker 47799424380SBryan Schumaker do { 47899424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 479*8cd51a0cSTrond Myklebust if (status != 0) { 480*8cd51a0cSTrond Myklebust if (status == -EAGAIN) 481*8cd51a0cSTrond Myklebust status = 0; 48299424380SBryan Schumaker break; 483*8cd51a0cSTrond Myklebust } 48499424380SBryan Schumaker 485d39ab9deSBryan Schumaker if (desc->plus == 1) 486d39ab9deSBryan Schumaker nfs_prime_dcache(desc->file->f_path.dentry, entry); 487*8cd51a0cSTrond Myklebust 488*8cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 489*8cd51a0cSTrond Myklebust if (status != 0) 490*8cd51a0cSTrond Myklebust break; 49199424380SBryan Schumaker } while (!entry->eof); 49299424380SBryan Schumaker 49399424380SBryan Schumaker if (status == -EBADCOOKIE && entry->eof) { 49499424380SBryan Schumaker array = nfs_readdir_get_array(page); 495*8cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 496*8cd51a0cSTrond Myklebust array->eof_index = array->size; 49799424380SBryan Schumaker status = 0; 49899424380SBryan Schumaker nfs_readdir_release_array(page); 499d1bacf9eSBryan Schumaker } 50056e4ebf8SBryan Schumaker } 501*8cd51a0cSTrond Myklebust return status; 502*8cd51a0cSTrond Myklebust } 50356e4ebf8SBryan Schumaker 50456e4ebf8SBryan Schumaker static 50556e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages) 50656e4ebf8SBryan Schumaker { 50756e4ebf8SBryan Schumaker unsigned int i; 50856e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 50956e4ebf8SBryan Schumaker put_page(pages[i]); 51056e4ebf8SBryan Schumaker } 51156e4ebf8SBryan Schumaker 51256e4ebf8SBryan Schumaker static 51356e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages, 51456e4ebf8SBryan Schumaker unsigned int npages) 51556e4ebf8SBryan Schumaker { 51656e4ebf8SBryan Schumaker vm_unmap_ram(ptr, npages); 51756e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, npages); 51856e4ebf8SBryan Schumaker } 51956e4ebf8SBryan Schumaker 52056e4ebf8SBryan Schumaker /* 52156e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 52256e4ebf8SBryan Schumaker * to nfs_readdir_free_large_page 52356e4ebf8SBryan Schumaker */ 52456e4ebf8SBryan Schumaker static 52556e4ebf8SBryan Schumaker void *nfs_readdir_large_page(struct page **pages, unsigned int npages) 52656e4ebf8SBryan Schumaker { 52756e4ebf8SBryan Schumaker void *ptr; 52856e4ebf8SBryan Schumaker unsigned int i; 52956e4ebf8SBryan Schumaker 53056e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 53156e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 53256e4ebf8SBryan Schumaker if (page == NULL) 53356e4ebf8SBryan Schumaker goto out_freepages; 53456e4ebf8SBryan Schumaker pages[i] = page; 53556e4ebf8SBryan Schumaker } 53656e4ebf8SBryan Schumaker 5374a201d6eSTrond Myklebust ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL); 53856e4ebf8SBryan Schumaker if (!IS_ERR_OR_NULL(ptr)) 53956e4ebf8SBryan Schumaker return ptr; 54056e4ebf8SBryan Schumaker out_freepages: 54156e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, i); 54256e4ebf8SBryan Schumaker return NULL; 543d1bacf9eSBryan Schumaker } 544d1bacf9eSBryan Schumaker 545d1bacf9eSBryan Schumaker static 546d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 547d1bacf9eSBryan Schumaker { 54856e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 54956e4ebf8SBryan Schumaker void *pages_ptr = NULL; 550d1bacf9eSBryan Schumaker struct nfs_entry entry; 551d1bacf9eSBryan Schumaker struct file *file = desc->file; 552d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 553*8cd51a0cSTrond Myklebust int status = -ENOMEM; 55456e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 555d1bacf9eSBryan Schumaker 556d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 557d1bacf9eSBryan Schumaker entry.cookie = *desc->dir_cookie; 558d1bacf9eSBryan Schumaker entry.eof = 0; 559d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 560d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 561d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 562d1bacf9eSBryan Schumaker goto out; 563d1bacf9eSBryan Schumaker 564d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 565*8cd51a0cSTrond Myklebust if (IS_ERR(array)) { 566*8cd51a0cSTrond Myklebust status = PTR_ERR(array); 567*8cd51a0cSTrond Myklebust goto out; 568*8cd51a0cSTrond Myklebust } 569d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 570d1bacf9eSBryan Schumaker array->eof_index = -1; 571d1bacf9eSBryan Schumaker 57256e4ebf8SBryan Schumaker pages_ptr = nfs_readdir_large_page(pages, array_size); 57356e4ebf8SBryan Schumaker if (!pages_ptr) 574d1bacf9eSBryan Schumaker goto out_release_array; 575d1bacf9eSBryan Schumaker do { 57656e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 577babddc72SBryan Schumaker 578d1bacf9eSBryan Schumaker if (status < 0) 579d1bacf9eSBryan Schumaker break; 580*8cd51a0cSTrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, array_size * PAGE_SIZE); 581*8cd51a0cSTrond Myklebust if (status < 0) { 582*8cd51a0cSTrond Myklebust if (status == -ENOSPC) 583*8cd51a0cSTrond Myklebust status = 0; 584*8cd51a0cSTrond Myklebust break; 585*8cd51a0cSTrond Myklebust } 586*8cd51a0cSTrond Myklebust } while (array->eof_index < 0); 587d1bacf9eSBryan Schumaker 58856e4ebf8SBryan Schumaker nfs_readdir_free_large_page(pages_ptr, pages, array_size); 589d1bacf9eSBryan Schumaker out_release_array: 590d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 591d1bacf9eSBryan Schumaker out: 592d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 593d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 594d1bacf9eSBryan Schumaker return status; 595d1bacf9eSBryan Schumaker } 596d1bacf9eSBryan Schumaker 597d1bacf9eSBryan Schumaker /* 598d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 599d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 600d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 601d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6021da177e4SLinus Torvalds */ 603d1bacf9eSBryan Schumaker static 604d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 605d1bacf9eSBryan Schumaker { 606d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 607*8cd51a0cSTrond Myklebust int ret; 608d1bacf9eSBryan Schumaker 609*8cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 610*8cd51a0cSTrond Myklebust if (ret < 0) 611d1bacf9eSBryan Schumaker goto error; 612d1bacf9eSBryan Schumaker SetPageUptodate(page); 613d1bacf9eSBryan Schumaker 6142aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 615cd9ae2b6STrond Myklebust /* Should never happen */ 616cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 617cd9ae2b6STrond Myklebust } 6181da177e4SLinus Torvalds unlock_page(page); 6191da177e4SLinus Torvalds return 0; 6201da177e4SLinus Torvalds error: 6211da177e4SLinus Torvalds unlock_page(page); 622*8cd51a0cSTrond Myklebust return ret; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 625d1bacf9eSBryan Schumaker static 626d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds page_cache_release(desc->page); 6291da177e4SLinus Torvalds desc->page = NULL; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 632d1bacf9eSBryan Schumaker static 633d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 6341da177e4SLinus Torvalds { 635*8cd51a0cSTrond Myklebust return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping, 636d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /* 640d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 6411da177e4SLinus Torvalds */ 642d1bacf9eSBryan Schumaker static 643d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 644d1bacf9eSBryan Schumaker { 645d1bacf9eSBryan Schumaker int res; 646d1bacf9eSBryan Schumaker 647d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 648d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 649d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 650d1bacf9eSBryan Schumaker 651d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 652d1bacf9eSBryan Schumaker if (res == 0) 653d1bacf9eSBryan Schumaker return 0; 654d1bacf9eSBryan Schumaker cache_page_release(desc); 655d1bacf9eSBryan Schumaker return res; 656d1bacf9eSBryan Schumaker } 657d1bacf9eSBryan Schumaker 658d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 6591da177e4SLinus Torvalds static inline 6601da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 6611da177e4SLinus Torvalds { 662*8cd51a0cSTrond Myklebust int res; 663d1bacf9eSBryan Schumaker 664*8cd51a0cSTrond Myklebust if (desc->page_index == 0) 665*8cd51a0cSTrond Myklebust desc->current_index = 0; 666d1bacf9eSBryan Schumaker while (1) { 667d1bacf9eSBryan Schumaker res = find_cache_page(desc); 6681da177e4SLinus Torvalds if (res != -EAGAIN) 6691da177e4SLinus Torvalds break; 6701da177e4SLinus Torvalds desc->page_index++; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds return res; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* 6811da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 6821da177e4SLinus Torvalds */ 6831da177e4SLinus Torvalds static 6841da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 6851da177e4SLinus Torvalds filldir_t filldir) 6861da177e4SLinus Torvalds { 6871da177e4SLinus Torvalds struct file *file = desc->file; 688d1bacf9eSBryan Schumaker int i = 0; 689d1bacf9eSBryan Schumaker int res = 0; 690d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 691d1bacf9eSBryan Schumaker unsigned int d_type = DT_UNKNOWN; 6921da177e4SLinus Torvalds struct dentry *dentry = NULL; 6931da177e4SLinus Torvalds 694d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 695*8cd51a0cSTrond Myklebust if (IS_ERR(array)) 696*8cd51a0cSTrond Myklebust return PTR_ERR(array); 6971da177e4SLinus Torvalds 698d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 699d1bacf9eSBryan Schumaker d_type = DT_UNKNOWN; 7001da177e4SLinus Torvalds 701d1bacf9eSBryan Schumaker res = filldir(dirent, array->array[i].string.name, 702d1bacf9eSBryan Schumaker array->array[i].string.len, file->f_pos, 703d1bacf9eSBryan Schumaker nfs_compat_user_ino64(array->array[i].ino), d_type); 7041da177e4SLinus Torvalds if (res < 0) 7051da177e4SLinus Torvalds break; 70600a92642SOlivier Galibert file->f_pos++; 707d1bacf9eSBryan Schumaker desc->cache_entry_index = i; 708d1bacf9eSBryan Schumaker if (i < (array->size-1)) 709d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 710d1bacf9eSBryan Schumaker else 711d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 712*8cd51a0cSTrond Myklebust } 713*8cd51a0cSTrond Myklebust if (i == array->eof_index) 714d1bacf9eSBryan Schumaker desc->eof = 1; 715d1bacf9eSBryan Schumaker 716d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 717d1bacf9eSBryan Schumaker cache_page_release(desc); 7181da177e4SLinus Torvalds if (dentry != NULL) 7191da177e4SLinus Torvalds dput(dentry); 7201e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7211e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7221da177e4SLinus Torvalds return res; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds /* 7261da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 7271da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 7281da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 7291da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 7301da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 7311da177e4SLinus Torvalds * 7321da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 7331da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 7341da177e4SLinus Torvalds * we should already have a complete representation of the 7351da177e4SLinus Torvalds * directory in the page cache by the time we get here. 7361da177e4SLinus Torvalds */ 7371da177e4SLinus Torvalds static inline 7381da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 7391da177e4SLinus Torvalds filldir_t filldir) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds struct page *page = NULL; 7421da177e4SLinus Torvalds int status; 743d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 7441da177e4SLinus Torvalds 7451e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 7461e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 7491da177e4SLinus Torvalds if (!page) { 7501da177e4SLinus Torvalds status = -ENOMEM; 7511da177e4SLinus Torvalds goto out; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 754d1bacf9eSBryan Schumaker if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) { 755d1bacf9eSBryan Schumaker status = -EIO; 756d1bacf9eSBryan Schumaker goto out_release; 757d1bacf9eSBryan Schumaker } 758d1bacf9eSBryan Schumaker 759baf57a09STrond Myklebust desc->page_index = 0; 760d1bacf9eSBryan Schumaker desc->page = page; 7611da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds out: 7641e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 7653110ff80SHarvey Harrison __func__, status); 7661da177e4SLinus Torvalds return status; 7671da177e4SLinus Torvalds out_release: 768d1bacf9eSBryan Schumaker cache_page_release(desc); 7691da177e4SLinus Torvalds goto out; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 77200a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 77300a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 77400a92642SOlivier Galibert whole directory. 7751da177e4SLinus Torvalds */ 7761da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 7771da177e4SLinus Torvalds { 77801cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 7791da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 7801da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 7811da177e4SLinus Torvalds *desc = &my_desc; 782aa49b4cfSTrond Myklebust int res = -ENOMEM; 7831da177e4SLinus Torvalds 7846da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 7851e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 7861e7cb3dcSChuck Lever (long long)filp->f_pos); 78791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 78891d5b470SChuck Lever 7891da177e4SLinus Torvalds /* 79000a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 791f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 79200a92642SOlivier Galibert * to either find the entry with the appropriate number or 79300a92642SOlivier Galibert * revalidate the cookie. 7941da177e4SLinus Torvalds */ 7951da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds desc->file = filp; 798cd3758e3STrond Myklebust desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie; 7991da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 8001da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 8011da177e4SLinus Torvalds 802565277f6STrond Myklebust nfs_block_sillyrename(dentry); 8031cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 804fccca7fcSTrond Myklebust if (res < 0) 805fccca7fcSTrond Myklebust goto out; 806fccca7fcSTrond Myklebust 807d1bacf9eSBryan Schumaker while (desc->eof != 1) { 8081da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 80900a92642SOlivier Galibert 8101da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 8111da177e4SLinus Torvalds /* This means either end of directory */ 812d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 8131da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 8141da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 8151da177e4SLinus Torvalds if (res >= 0) 8161da177e4SLinus Torvalds continue; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds res = 0; 8191da177e4SLinus Torvalds break; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8223a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8231da177e4SLinus Torvalds nfs_zap_caches(inode); 824baf57a09STrond Myklebust desc->page_index = 0; 8251da177e4SLinus Torvalds desc->plus = 0; 826d1bacf9eSBryan Schumaker desc->eof = 0; 8271da177e4SLinus Torvalds continue; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds if (res < 0) 8301da177e4SLinus Torvalds break; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 8331da177e4SLinus Torvalds if (res < 0) { 8341da177e4SLinus Torvalds res = 0; 8351da177e4SLinus Torvalds break; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds } 838fccca7fcSTrond Myklebust out: 839565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 8401e7cb3dcSChuck Lever if (res > 0) 8411e7cb3dcSChuck Lever res = 0; 842aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 8431e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8441e7cb3dcSChuck Lever res); 8451da177e4SLinus Torvalds return res; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 84810afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 849f0dd2136STrond Myklebust { 850b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 851b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 852b84e06c5SChuck Lever 8536da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 854b84e06c5SChuck Lever dentry->d_parent->d_name.name, 855b84e06c5SChuck Lever dentry->d_name.name, 856b84e06c5SChuck Lever offset, origin); 857b84e06c5SChuck Lever 858b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 859f0dd2136STrond Myklebust switch (origin) { 860f0dd2136STrond Myklebust case 1: 861f0dd2136STrond Myklebust offset += filp->f_pos; 862f0dd2136STrond Myklebust case 0: 863f0dd2136STrond Myklebust if (offset >= 0) 864f0dd2136STrond Myklebust break; 865f0dd2136STrond Myklebust default: 866f0dd2136STrond Myklebust offset = -EINVAL; 867f0dd2136STrond Myklebust goto out; 868f0dd2136STrond Myklebust } 869f0dd2136STrond Myklebust if (offset != filp->f_pos) { 870f0dd2136STrond Myklebust filp->f_pos = offset; 871cd3758e3STrond Myklebust nfs_file_open_context(filp)->dir_cookie = 0; 872f0dd2136STrond Myklebust } 873f0dd2136STrond Myklebust out: 874b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 875f0dd2136STrond Myklebust return offset; 876f0dd2136STrond Myklebust } 877f0dd2136STrond Myklebust 8781da177e4SLinus Torvalds /* 8791da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 8801da177e4SLinus Torvalds * is a dummy operation. 8811da177e4SLinus Torvalds */ 8827ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync) 8831da177e4SLinus Torvalds { 8847ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 8857ea80859SChristoph Hellwig 8866da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 8871e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8881e7cb3dcSChuck Lever datasync); 8891e7cb3dcSChuck Lever 89054917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 8911da177e4SLinus Torvalds return 0; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 894bfc69a45STrond Myklebust /** 895bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 896bfc69a45STrond Myklebust * @dir - pointer to directory inode 897bfc69a45STrond Myklebust * 898bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 899bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 900bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 901bfc69a45STrond Myklebust * 902bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 903bfc69a45STrond Myklebust */ 904bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 905bfc69a45STrond Myklebust { 906011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 907bfc69a45STrond Myklebust } 908bfc69a45STrond Myklebust 9091da177e4SLinus Torvalds /* 9101da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9111da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9121da177e4SLinus Torvalds * and may need to be looked up again. 9131da177e4SLinus Torvalds */ 914c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 9151da177e4SLinus Torvalds { 9161da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9171da177e4SLinus Torvalds return 1; 9184eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 9194eec952eSTrond Myklebust return 0; 920f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 9216ecc5e8fSTrond Myklebust return 0; 922f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 923f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 924f2c77f4eSTrond Myklebust return 0; 925f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 926f2c77f4eSTrond Myklebust return 0; 927f2c77f4eSTrond Myklebust return 1; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds /* 9311d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 9321d6757fbSTrond Myklebust * 9331d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 9341d6757fbSTrond Myklebust * component of the path. 9351d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 9361d6757fbSTrond Myklebust */ 9371d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) 9381d6757fbSTrond Myklebust { 9391d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 9401d6757fbSTrond Myklebust return 0; 9411d6757fbSTrond Myklebust return nd->flags & mask; 9421d6757fbSTrond Myklebust } 9431d6757fbSTrond Myklebust 9441d6757fbSTrond Myklebust /* 945a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 946a12802caSTrond Myklebust * an O_EXCL create using this path component. 947a12802caSTrond Myklebust */ 948a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 949a12802caSTrond Myklebust { 950a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 951a12802caSTrond Myklebust return 0; 9523516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 953a12802caSTrond Myklebust } 954a12802caSTrond Myklebust 955a12802caSTrond Myklebust /* 9561d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 9571d6757fbSTrond Myklebust * 9581d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 9591d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 9601d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 9611d6757fbSTrond Myklebust * 9621d6757fbSTrond Myklebust */ 9631da177e4SLinus Torvalds static inline 9641da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 9671da177e4SLinus Torvalds 9684e99a1ffSTrond Myklebust if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags)) 9694e99a1ffSTrond Myklebust return 0; 9701da177e4SLinus Torvalds if (nd != NULL) { 9711da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 9721d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 9731da177e4SLinus Torvalds goto out_force; 9741da177e4SLinus Torvalds /* This is an open(2) */ 9751d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 9764e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 9774e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 9784e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 9791da177e4SLinus Torvalds goto out_force; 9804f48af45STrond Myklebust return 0; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 9831da177e4SLinus Torvalds out_force: 9841da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds /* 9881da177e4SLinus Torvalds * We judge how long we want to trust negative 9891da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 9901da177e4SLinus Torvalds * 9911da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 9921da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 9931da177e4SLinus Torvalds */ 9941da177e4SLinus Torvalds static inline 9951da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 9961da177e4SLinus Torvalds struct nameidata *nd) 9971da177e4SLinus Torvalds { 9981da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 9991d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 10001da177e4SLinus Torvalds return 0; 10014eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10024eec952eSTrond Myklebust return 1; 10031da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* 10071da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 10081da177e4SLinus Torvalds * and we should check whether we can really trust that 10091da177e4SLinus Torvalds * lookup. 10101da177e4SLinus Torvalds * 10111da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 10121da177e4SLinus Torvalds * we have an inode! 10131da177e4SLinus Torvalds * 10141da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 10151da177e4SLinus Torvalds * cached dentry and do a new lookup. 10161da177e4SLinus Torvalds */ 10171da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) 10181da177e4SLinus Torvalds { 10191da177e4SLinus Torvalds struct inode *dir; 10201da177e4SLinus Torvalds struct inode *inode; 10211da177e4SLinus Torvalds struct dentry *parent; 1022e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1023e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 10241da177e4SLinus Torvalds int error; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds parent = dget_parent(dentry); 10271da177e4SLinus Torvalds dir = parent->d_inode; 102891d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 10291da177e4SLinus Torvalds inode = dentry->d_inode; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds if (!inode) { 10321da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 10331da177e4SLinus Torvalds goto out_bad; 10341da177e4SLinus Torvalds goto out_valid; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds if (is_bad_inode(inode)) { 10381e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 10393110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 10401e7cb3dcSChuck Lever dentry->d_name.name); 10411da177e4SLinus Torvalds goto out_bad; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 104415860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 104515860ab1STrond Myklebust goto out_set_verifier; 104615860ab1STrond Myklebust 10471da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 1048a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 10491da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 10501da177e4SLinus Torvalds goto out_zap_parent; 10511da177e4SLinus Torvalds goto out_valid; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds if (NFS_STALE(inode)) 10551da177e4SLinus Torvalds goto out_bad; 10561da177e4SLinus Torvalds 1057e1fb4d05STrond Myklebust error = -ENOMEM; 1058e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1059e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1060e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1061e1fb4d05STrond Myklebust goto out_error; 1062e1fb4d05STrond Myklebust 1063e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 10641da177e4SLinus Torvalds if (error) 10651da177e4SLinus Torvalds goto out_bad; 1066e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 10671da177e4SLinus Torvalds goto out_bad; 1068e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 10691da177e4SLinus Torvalds goto out_bad; 10701da177e4SLinus Torvalds 1071e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1072e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 107315860ab1STrond Myklebust out_set_verifier: 1074cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 10751da177e4SLinus Torvalds out_valid: 10761da177e4SLinus Torvalds dput(parent); 10771e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 10783110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 10791e7cb3dcSChuck Lever dentry->d_name.name); 10801da177e4SLinus Torvalds return 1; 10811da177e4SLinus Torvalds out_zap_parent: 10821da177e4SLinus Torvalds nfs_zap_caches(dir); 10831da177e4SLinus Torvalds out_bad: 1084a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 10851da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 10861da177e4SLinus Torvalds /* Purge readdir caches. */ 10871da177e4SLinus Torvalds nfs_zap_caches(inode); 10881da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 10891da177e4SLinus Torvalds if (have_submounts(dentry)) 10901da177e4SLinus Torvalds goto out_valid; 1091d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 1092d9e80b7dSAl Viro goto out_valid; 10931da177e4SLinus Torvalds shrink_dcache_parent(dentry); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds d_drop(dentry); 1096e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1097e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 10981da177e4SLinus Torvalds dput(parent); 10991e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 11003110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11011e7cb3dcSChuck Lever dentry->d_name.name); 11021da177e4SLinus Torvalds return 0; 1103e1fb4d05STrond Myklebust out_error: 1104e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1105e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 1106e1fb4d05STrond Myklebust dput(parent); 1107e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 1108e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 1109e1fb4d05STrond Myklebust dentry->d_name.name, error); 1110e1fb4d05STrond Myklebust return error; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* 11141da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 11151da177e4SLinus Torvalds */ 11161da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry) 11171da177e4SLinus Torvalds { 11181da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 11191da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 11201da177e4SLinus Torvalds dentry->d_flags); 11211da177e4SLinus Torvalds 112277f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 112377f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 112477f11192STrond Myklebust return 1; 112577f11192STrond Myklebust 11261da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 11271da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 11281da177e4SLinus Torvalds return 1; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 11311da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 11321da177e4SLinus Torvalds * files will be cleaned up during umount */ 11331da177e4SLinus Torvalds return 1; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds return 0; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11391b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 11401b83d707STrond Myklebust { 11411b83d707STrond Myklebust spin_lock(&inode->i_lock); 11421b83d707STrond Myklebust if (inode->i_nlink > 0) 11431b83d707STrond Myklebust drop_nlink(inode); 11441b83d707STrond Myklebust spin_unlock(&inode->i_lock); 11451b83d707STrond Myklebust } 11461b83d707STrond Myklebust 11471da177e4SLinus Torvalds /* 11481da177e4SLinus Torvalds * Called when the dentry loses inode. 11491da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 11501da177e4SLinus Torvalds */ 11511da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 11521da177e4SLinus Torvalds { 115383672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 115483672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 115583672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 115683672d39SNeil Brown 11571da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 11589a53c3a7SDave Hansen drop_nlink(inode); 1159e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds iput(inode); 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 1164f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 11651da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 11661da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 11671da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 11681da177e4SLinus Torvalds }; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds struct dentry *res; 1173565277f6STrond Myklebust struct dentry *parent; 11741da177e4SLinus Torvalds struct inode *inode = NULL; 1175e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1176e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 11771da177e4SLinus Torvalds int error; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 11801da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 118191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 11841da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 11851da177e4SLinus Torvalds goto out; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 11881da177e4SLinus Torvalds 1189fd684071STrond Myklebust /* 1190fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1191fd684071STrond Myklebust * but don't hash the dentry. 1192fd684071STrond Myklebust */ 1193fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 1194fd684071STrond Myklebust d_instantiate(dentry, NULL); 1195fd684071STrond Myklebust res = NULL; 1196fc0f684cSTrond Myklebust goto out; 1197fd684071STrond Myklebust } 11981da177e4SLinus Torvalds 1199e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1200e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1201e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1202e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1203e1fb4d05STrond Myklebust goto out; 1204e1fb4d05STrond Myklebust 1205565277f6STrond Myklebust parent = dentry->d_parent; 1206565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 1207565277f6STrond Myklebust nfs_block_sillyrename(parent); 1208e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 12091da177e4SLinus Torvalds if (error == -ENOENT) 12101da177e4SLinus Torvalds goto no_entry; 12111da177e4SLinus Torvalds if (error < 0) { 12121da177e4SLinus Torvalds res = ERR_PTR(error); 1213565277f6STrond Myklebust goto out_unblock_sillyrename; 12141da177e4SLinus Torvalds } 1215e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 121603f28e3aSTrond Myklebust res = (struct dentry *)inode; 121703f28e3aSTrond Myklebust if (IS_ERR(res)) 1218565277f6STrond Myklebust goto out_unblock_sillyrename; 121954ceac45SDavid Howells 12201da177e4SLinus Torvalds no_entry: 122154ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 12229eaef27bSTrond Myklebust if (res != NULL) { 12239eaef27bSTrond Myklebust if (IS_ERR(res)) 1224565277f6STrond Myklebust goto out_unblock_sillyrename; 12251da177e4SLinus Torvalds dentry = res; 12269eaef27bSTrond Myklebust } 12271da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1228565277f6STrond Myklebust out_unblock_sillyrename: 1229565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 12301da177e4SLinus Torvalds out: 1231e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1232e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 12331da177e4SLinus Torvalds return res; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 12371da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 12381da177e4SLinus Torvalds 1239f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 12401da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 12411da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 12421da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 12431da177e4SLinus Torvalds }; 12441da177e4SLinus Torvalds 12451d6757fbSTrond Myklebust /* 12461d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 12471d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 12481d6757fbSTrond Myklebust */ 12495584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 12501da177e4SLinus Torvalds { 12511d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 12521da177e4SLinus Torvalds return 0; 12531da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 12541da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 12551da177e4SLinus Torvalds return 0; 12561da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 12572c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 12582c463e95SDave Hansen (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 12591da177e4SLinus Torvalds return 0; 12601da177e4SLinus Torvalds return 1; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 1263cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd) 1264cd9a1c0eSTrond Myklebust { 1265cd9a1c0eSTrond Myklebust struct path path = { 1266cd9a1c0eSTrond Myklebust .mnt = nd->path.mnt, 1267cd9a1c0eSTrond Myklebust .dentry = dentry, 1268cd9a1c0eSTrond Myklebust }; 1269cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1270cd9a1c0eSTrond Myklebust struct rpc_cred *cred; 1271cd9a1c0eSTrond Myklebust fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); 1272cd9a1c0eSTrond Myklebust 1273cd9a1c0eSTrond Myklebust cred = rpc_lookup_cred(); 1274cd9a1c0eSTrond Myklebust if (IS_ERR(cred)) 1275cd9a1c0eSTrond Myklebust return ERR_CAST(cred); 1276cd9a1c0eSTrond Myklebust ctx = alloc_nfs_open_context(&path, cred, fmode); 1277cd9a1c0eSTrond Myklebust put_rpccred(cred); 1278cd9a1c0eSTrond Myklebust if (ctx == NULL) 1279cd9a1c0eSTrond Myklebust return ERR_PTR(-ENOMEM); 1280cd9a1c0eSTrond Myklebust return ctx; 1281cd9a1c0eSTrond Myklebust } 1282cd9a1c0eSTrond Myklebust 1283cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1284cd9a1c0eSTrond Myklebust { 1285cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1286cd9a1c0eSTrond Myklebust return 0; 1287cd9a1c0eSTrond Myklebust } 1288cd9a1c0eSTrond Myklebust 1289cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) 1290cd9a1c0eSTrond Myklebust { 1291cd9a1c0eSTrond Myklebust struct file *filp; 1292cd9a1c0eSTrond Myklebust int ret = 0; 1293cd9a1c0eSTrond Myklebust 1294cd9a1c0eSTrond Myklebust /* If the open_intent is for execute, we have an extra check to make */ 1295cd9a1c0eSTrond Myklebust if (ctx->mode & FMODE_EXEC) { 1296cd9a1c0eSTrond Myklebust ret = nfs_may_open(ctx->path.dentry->d_inode, 1297cd9a1c0eSTrond Myklebust ctx->cred, 1298cd9a1c0eSTrond Myklebust nd->intent.open.flags); 1299cd9a1c0eSTrond Myklebust if (ret < 0) 1300cd9a1c0eSTrond Myklebust goto out; 1301cd9a1c0eSTrond Myklebust } 1302cd9a1c0eSTrond Myklebust filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open); 1303cd9a1c0eSTrond Myklebust if (IS_ERR(filp)) 1304cd9a1c0eSTrond Myklebust ret = PTR_ERR(filp); 1305cd9a1c0eSTrond Myklebust else 1306cd9a1c0eSTrond Myklebust nfs_file_set_open_context(filp, ctx); 1307cd9a1c0eSTrond Myklebust out: 1308cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1309cd9a1c0eSTrond Myklebust return ret; 1310cd9a1c0eSTrond Myklebust } 1311cd9a1c0eSTrond Myklebust 13121da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 13131da177e4SLinus Torvalds { 1314cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1315cd9a1c0eSTrond Myklebust struct iattr attr; 13161da177e4SLinus Torvalds struct dentry *res = NULL; 1317f46e0bd3STrond Myklebust struct inode *inode; 1318cd9a1c0eSTrond Myklebust int open_flags; 1319898f635cSTrond Myklebust int err; 13201da177e4SLinus Torvalds 13211e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 13221e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13231e7cb3dcSChuck Lever 13241da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 13255584c306STrond Myklebust if (!is_atomic_open(nd)) 13261da177e4SLinus Torvalds goto no_open; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 13291da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 13301da177e4SLinus Torvalds goto out; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 13331da177e4SLinus Torvalds 1334d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1335d4d9cdcbSTrond Myklebust * the dentry. */ 13363516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1337d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 133802a913a7STrond Myklebust goto out; 133902a913a7STrond Myklebust } 13401da177e4SLinus Torvalds 1341cd9a1c0eSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1342cd9a1c0eSTrond Myklebust res = ERR_CAST(ctx); 1343cd9a1c0eSTrond Myklebust if (IS_ERR(ctx)) 1344cd9a1c0eSTrond Myklebust goto out; 1345cd9a1c0eSTrond Myklebust 1346cd9a1c0eSTrond Myklebust open_flags = nd->intent.open.flags; 1347cd9a1c0eSTrond Myklebust if (nd->flags & LOOKUP_CREATE) { 1348cd9a1c0eSTrond Myklebust attr.ia_mode = nd->intent.open.create_mode; 1349cd9a1c0eSTrond Myklebust attr.ia_valid = ATTR_MODE; 1350cd9a1c0eSTrond Myklebust if (!IS_POSIXACL(dir)) 1351cd9a1c0eSTrond Myklebust attr.ia_mode &= ~current_umask(); 1352cd9a1c0eSTrond Myklebust } else { 1353898f635cSTrond Myklebust open_flags &= ~(O_EXCL | O_CREAT); 1354cd9a1c0eSTrond Myklebust attr.ia_valid = 0; 1355cd9a1c0eSTrond Myklebust } 1356cd9a1c0eSTrond Myklebust 13571da177e4SLinus Torvalds /* Open the file on the server */ 1358f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 13592b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr); 1360f46e0bd3STrond Myklebust if (IS_ERR(inode)) { 1361f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1362cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1363f46e0bd3STrond Myklebust switch (PTR_ERR(inode)) { 13641da177e4SLinus Torvalds /* Make a negative dentry */ 13651da177e4SLinus Torvalds case -ENOENT: 1366f46e0bd3STrond Myklebust d_add(dentry, NULL); 136702a913a7STrond Myklebust res = NULL; 136802a913a7STrond Myklebust goto out; 13691da177e4SLinus Torvalds /* This turned out not to be a regular file */ 13706f926b5bSTrond Myklebust case -ENOTDIR: 13716f926b5bSTrond Myklebust goto no_open; 13721da177e4SLinus Torvalds case -ELOOP: 13731da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 13741da177e4SLinus Torvalds goto no_open; 137523ebbd9aSTrond Myklebust /* case -EISDIR: */ 13761da177e4SLinus Torvalds /* case -EINVAL: */ 13771da177e4SLinus Torvalds default: 1378f46e0bd3STrond Myklebust res = ERR_CAST(inode); 13791da177e4SLinus Torvalds goto out; 13801da177e4SLinus Torvalds } 1381cd9a1c0eSTrond Myklebust } 1382f46e0bd3STrond Myklebust res = d_add_unique(dentry, inode); 1383898f635cSTrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1384f46e0bd3STrond Myklebust if (res != NULL) { 1385f46e0bd3STrond Myklebust dput(ctx->path.dentry); 1386f46e0bd3STrond Myklebust ctx->path.dentry = dget(res); 13871da177e4SLinus Torvalds dentry = res; 1388f46e0bd3STrond Myklebust } 1389898f635cSTrond Myklebust err = nfs_intent_set_file(nd, ctx); 1390898f635cSTrond Myklebust if (err < 0) { 1391898f635cSTrond Myklebust if (res != NULL) 1392898f635cSTrond Myklebust dput(res); 1393898f635cSTrond Myklebust return ERR_PTR(err); 1394898f635cSTrond Myklebust } 13951da177e4SLinus Torvalds out: 1396f46e0bd3STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 13971da177e4SLinus Torvalds return res; 13981da177e4SLinus Torvalds no_open: 13991da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 14031da177e4SLinus Torvalds { 14041da177e4SLinus Torvalds struct dentry *parent = NULL; 14051da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14061da177e4SLinus Torvalds struct inode *dir; 1407b8d4caddSTrond Myklebust struct nfs_open_context *ctx; 14081da177e4SLinus Torvalds int openflags, ret = 0; 14091da177e4SLinus Torvalds 14101f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 14115584c306STrond Myklebust goto no_open; 14122b484297STrond Myklebust 14131da177e4SLinus Torvalds parent = dget_parent(dentry); 14141da177e4SLinus Torvalds dir = parent->d_inode; 14152b484297STrond Myklebust 14161da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 14171da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 14181da177e4SLinus Torvalds */ 1419216d5d06STrond Myklebust if (inode == NULL) { 1420216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1421216d5d06STrond Myklebust ret = 1; 14221da177e4SLinus Torvalds goto out; 1423216d5d06STrond Myklebust } 1424216d5d06STrond Myklebust 14251da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 14261da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 14275584c306STrond Myklebust goto no_open_dput; 14281da177e4SLinus Torvalds openflags = nd->intent.open.flags; 14291da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 14301da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 14315584c306STrond Myklebust goto no_open_dput; 14321da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 14330a377cffSTrond Myklebust openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); 14341da177e4SLinus Torvalds 1435b8d4caddSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1436b8d4caddSTrond Myklebust ret = PTR_ERR(ctx); 1437b8d4caddSTrond Myklebust if (IS_ERR(ctx)) 1438b8d4caddSTrond Myklebust goto out; 14391da177e4SLinus Torvalds /* 14401b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 14411da177e4SLinus Torvalds * operations that change the directory. We therefore save the 14421da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 14431da177e4SLinus Torvalds */ 14442b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL); 1445535918f1STrond Myklebust if (IS_ERR(inode)) { 1446535918f1STrond Myklebust ret = PTR_ERR(inode); 1447535918f1STrond Myklebust switch (ret) { 1448535918f1STrond Myklebust case -EPERM: 1449535918f1STrond Myklebust case -EACCES: 1450535918f1STrond Myklebust case -EDQUOT: 1451535918f1STrond Myklebust case -ENOSPC: 1452535918f1STrond Myklebust case -EROFS: 1453535918f1STrond Myklebust goto out_put_ctx; 1454535918f1STrond Myklebust default: 1455535918f1STrond Myklebust goto out_drop; 1456535918f1STrond Myklebust } 1457535918f1STrond Myklebust } 1458535918f1STrond Myklebust iput(inode); 1459898f635cSTrond Myklebust if (inode != dentry->d_inode) 1460535918f1STrond Myklebust goto out_drop; 1461898f635cSTrond Myklebust 1462898f635cSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1463898f635cSTrond Myklebust ret = nfs_intent_set_file(nd, ctx); 1464898f635cSTrond Myklebust if (ret >= 0) 1465898f635cSTrond Myklebust ret = 1; 14661da177e4SLinus Torvalds out: 14671da177e4SLinus Torvalds dput(parent); 14681da177e4SLinus Torvalds return ret; 1469535918f1STrond Myklebust out_drop: 1470535918f1STrond Myklebust d_drop(dentry); 1471535918f1STrond Myklebust ret = 0; 1472535918f1STrond Myklebust out_put_ctx: 1473535918f1STrond Myklebust put_nfs_open_context(ctx); 1474535918f1STrond Myklebust goto out; 1475535918f1STrond Myklebust 14765584c306STrond Myklebust no_open_dput: 14771da177e4SLinus Torvalds dput(parent); 14785584c306STrond Myklebust no_open: 14791da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 14801da177e4SLinus Torvalds } 1481c0204fd2STrond Myklebust 1482c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, 1483c0204fd2STrond Myklebust struct nameidata *nd) 1484c0204fd2STrond Myklebust { 1485c0204fd2STrond Myklebust struct nfs_open_context *ctx = NULL; 1486c0204fd2STrond Myklebust struct iattr attr; 1487c0204fd2STrond Myklebust int error; 1488c0204fd2STrond Myklebust int open_flags = 0; 1489c0204fd2STrond Myklebust 1490c0204fd2STrond Myklebust dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 1491c0204fd2STrond Myklebust dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 1492c0204fd2STrond Myklebust 1493c0204fd2STrond Myklebust attr.ia_mode = mode; 1494c0204fd2STrond Myklebust attr.ia_valid = ATTR_MODE; 1495c0204fd2STrond Myklebust 1496c0204fd2STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) { 1497c0204fd2STrond Myklebust open_flags = nd->intent.open.flags; 1498c0204fd2STrond Myklebust 1499c0204fd2STrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1500c0204fd2STrond Myklebust error = PTR_ERR(ctx); 1501c0204fd2STrond Myklebust if (IS_ERR(ctx)) 1502898f635cSTrond Myklebust goto out_err_drop; 1503c0204fd2STrond Myklebust } 1504c0204fd2STrond Myklebust 1505c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx); 1506c0204fd2STrond Myklebust if (error != 0) 1507c0204fd2STrond Myklebust goto out_put_ctx; 1508898f635cSTrond Myklebust if (ctx != NULL) { 1509898f635cSTrond Myklebust error = nfs_intent_set_file(nd, ctx); 1510898f635cSTrond Myklebust if (error < 0) 1511898f635cSTrond Myklebust goto out_err; 1512898f635cSTrond Myklebust } 1513c0204fd2STrond Myklebust return 0; 1514c0204fd2STrond Myklebust out_put_ctx: 1515c0204fd2STrond Myklebust if (ctx != NULL) 1516c0204fd2STrond Myklebust put_nfs_open_context(ctx); 1517898f635cSTrond Myklebust out_err_drop: 1518c0204fd2STrond Myklebust d_drop(dentry); 1519898f635cSTrond Myklebust out_err: 1520c0204fd2STrond Myklebust return error; 1521c0204fd2STrond Myklebust } 1522c0204fd2STrond Myklebust 15231da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds /* 15261da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 15271da177e4SLinus Torvalds */ 15281da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 15291da177e4SLinus Torvalds struct nfs_fattr *fattr) 15301da177e4SLinus Torvalds { 1531fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1532fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 15331da177e4SLinus Torvalds struct inode *inode; 15341da177e4SLinus Torvalds int error = -EACCES; 15351da177e4SLinus Torvalds 1536fab728e1STrond Myklebust d_drop(dentry); 1537fab728e1STrond Myklebust 15381da177e4SLinus Torvalds /* We may have been initialized further down */ 15391da177e4SLinus Torvalds if (dentry->d_inode) 1540fab728e1STrond Myklebust goto out; 15411da177e4SLinus Torvalds if (fhandle->size == 0) { 15421da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 15431da177e4SLinus Torvalds if (error) 1544fab728e1STrond Myklebust goto out_error; 15451da177e4SLinus Torvalds } 15465724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15471da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 15481da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 15498fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 15501da177e4SLinus Torvalds if (error < 0) 1551fab728e1STrond Myklebust goto out_error; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 155403f28e3aSTrond Myklebust error = PTR_ERR(inode); 155503f28e3aSTrond Myklebust if (IS_ERR(inode)) 1556fab728e1STrond Myklebust goto out_error; 1557fab728e1STrond Myklebust d_add(dentry, inode); 1558fab728e1STrond Myklebust out: 1559fab728e1STrond Myklebust dput(parent); 15601da177e4SLinus Torvalds return 0; 1561fab728e1STrond Myklebust out_error: 1562fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1563fab728e1STrond Myklebust dput(parent); 1564fab728e1STrond Myklebust return error; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds /* 15681da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 15691da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 15701da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 15711da177e4SLinus Torvalds * reply path made it appear to have failed. 15721da177e4SLinus Torvalds */ 15731da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 15741da177e4SLinus Torvalds struct nameidata *nd) 15751da177e4SLinus Torvalds { 15761da177e4SLinus Torvalds struct iattr attr; 15771da177e4SLinus Torvalds int error; 15781da177e4SLinus Torvalds 15791e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 15801e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds attr.ia_mode = mode; 15831da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 15841da177e4SLinus Torvalds 1585c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL); 15861da177e4SLinus Torvalds if (error != 0) 15871da177e4SLinus Torvalds goto out_err; 15881da177e4SLinus Torvalds return 0; 15891da177e4SLinus Torvalds out_err: 15901da177e4SLinus Torvalds d_drop(dentry); 15911da177e4SLinus Torvalds return error; 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /* 15951da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 15961da177e4SLinus Torvalds */ 15971da177e4SLinus Torvalds static int 15981da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 15991da177e4SLinus Torvalds { 16001da177e4SLinus Torvalds struct iattr attr; 16011da177e4SLinus Torvalds int status; 16021da177e4SLinus Torvalds 16031e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 16041e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 16071da177e4SLinus Torvalds return -EINVAL; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds attr.ia_mode = mode; 16101da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 16131da177e4SLinus Torvalds if (status != 0) 16141da177e4SLinus Torvalds goto out_err; 16151da177e4SLinus Torvalds return 0; 16161da177e4SLinus Torvalds out_err: 16171da177e4SLinus Torvalds d_drop(dentry); 16181da177e4SLinus Torvalds return status; 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds /* 16221da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 16231da177e4SLinus Torvalds */ 16241da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 16251da177e4SLinus Torvalds { 16261da177e4SLinus Torvalds struct iattr attr; 16271da177e4SLinus Torvalds int error; 16281da177e4SLinus Torvalds 16291e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 16301e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16331da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 16361da177e4SLinus Torvalds if (error != 0) 16371da177e4SLinus Torvalds goto out_err; 16381da177e4SLinus Torvalds return 0; 16391da177e4SLinus Torvalds out_err: 16401da177e4SLinus Torvalds d_drop(dentry); 16411da177e4SLinus Torvalds return error; 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 1644d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1645d45b9d8bSTrond Myklebust { 1646d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1647d45b9d8bSTrond Myklebust d_delete(dentry); 1648d45b9d8bSTrond Myklebust } 1649d45b9d8bSTrond Myklebust 16501da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 16511da177e4SLinus Torvalds { 16521da177e4SLinus Torvalds int error; 16531da177e4SLinus Torvalds 16541e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 16551e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 16581da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 16591da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1660ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1661d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1662d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds return error; 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds /* 16681da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 16691da177e4SLinus Torvalds * and after checking that the file has only one user. 16701da177e4SLinus Torvalds * 16711da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 16721da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 16731da177e4SLinus Torvalds */ 16741da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 16751da177e4SLinus Torvalds { 16761da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 16771da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16781da177e4SLinus Torvalds int error = -EBUSY; 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 16811da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 16841da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 16851da177e4SLinus Torvalds error = 0; 16861da177e4SLinus Torvalds goto out; 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds if (inode != NULL) { 1690cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 16911da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 16921da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 16931da177e4SLinus Torvalds if (error == 0) 16941b83d707STrond Myklebust nfs_drop_nlink(inode); 16955ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 16961da177e4SLinus Torvalds } else 16971da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1698d45b9d8bSTrond Myklebust if (error == -ENOENT) 1699d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 17001da177e4SLinus Torvalds out: 17011da177e4SLinus Torvalds return error; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 17051da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 17061da177e4SLinus Torvalds * 17071da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 17081da177e4SLinus Torvalds */ 17091da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 17101da177e4SLinus Torvalds { 17111da177e4SLinus Torvalds int error; 17121da177e4SLinus Torvalds int need_rehash = 0; 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 17151da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds spin_lock(&dcache_lock); 17181da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 17191da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) > 1) { 17201da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 17211da177e4SLinus Torvalds spin_unlock(&dcache_lock); 1722ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1723ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 17241da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 17251da177e4SLinus Torvalds return error; 17261da177e4SLinus Torvalds } 17271da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 17281da177e4SLinus Torvalds __d_drop(dentry); 17291da177e4SLinus Torvalds need_rehash = 1; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 17321da177e4SLinus Torvalds spin_unlock(&dcache_lock); 17331da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1734d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 17351da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17361da177e4SLinus Torvalds } else if (need_rehash) 17371da177e4SLinus Torvalds d_rehash(dentry); 17381da177e4SLinus Torvalds return error; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 1741873101b3SChuck Lever /* 1742873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1743873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1744873101b3SChuck Lever * using prepare_write/commit_write. 1745873101b3SChuck Lever * 1746873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1747873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1748873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1749873101b3SChuck Lever * symlink request has completed on the server. 1750873101b3SChuck Lever * 1751873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1752873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1753873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1754873101b3SChuck Lever * and move the raw page into its mapping. 1755873101b3SChuck Lever */ 1756873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 17571da177e4SLinus Torvalds { 1758873101b3SChuck Lever struct pagevec lru_pvec; 1759873101b3SChuck Lever struct page *page; 1760873101b3SChuck Lever char *kaddr; 17611da177e4SLinus Torvalds struct iattr attr; 1762873101b3SChuck Lever unsigned int pathlen = strlen(symname); 17631da177e4SLinus Torvalds int error; 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 17661da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 17671da177e4SLinus Torvalds 1768873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1769873101b3SChuck Lever return -ENAMETOOLONG; 17701da177e4SLinus Torvalds 1771873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1772873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 17731da177e4SLinus Torvalds 177483d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 177576566991STrond Myklebust if (!page) 1776873101b3SChuck Lever return -ENOMEM; 1777873101b3SChuck Lever 1778873101b3SChuck Lever kaddr = kmap_atomic(page, KM_USER0); 1779873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1780873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1781873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1782873101b3SChuck Lever kunmap_atomic(kaddr, KM_USER0); 1783873101b3SChuck Lever 178494a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1785873101b3SChuck Lever if (error != 0) { 1786873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1787873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1788873101b3SChuck Lever dentry->d_name.name, symname, error); 17891da177e4SLinus Torvalds d_drop(dentry); 1790873101b3SChuck Lever __free_page(page); 17911da177e4SLinus Torvalds return error; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 1794873101b3SChuck Lever /* 1795873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1796873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1797873101b3SChuck Lever */ 1798873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1799873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1800873101b3SChuck Lever GFP_KERNEL)) { 180139cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 18024f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1803873101b3SChuck Lever SetPageUptodate(page); 1804873101b3SChuck Lever unlock_page(page); 1805873101b3SChuck Lever } else 1806873101b3SChuck Lever __free_page(page); 1807873101b3SChuck Lever 1808873101b3SChuck Lever return 0; 1809873101b3SChuck Lever } 1810873101b3SChuck Lever 18111da177e4SLinus Torvalds static int 18121da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 18151da177e4SLinus Torvalds int error; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 18181da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18191da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 18201da177e4SLinus Torvalds 18219a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 18229a3936aaSTrond Myklebust 18239697d234STrond Myklebust d_drop(dentry); 18241da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1825cf809556STrond Myklebust if (error == 0) { 18267de9c6eeSAl Viro ihold(inode); 18279697d234STrond Myklebust d_add(dentry, inode); 1828cf809556STrond Myklebust } 18291da177e4SLinus Torvalds return error; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* 18331da177e4SLinus Torvalds * RENAME 18341da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 18351da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 18361da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 18371da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 18381da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 18391da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 18401da177e4SLinus Torvalds * 18411da177e4SLinus Torvalds * FIXED. 18421da177e4SLinus Torvalds * 18431da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 18441da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 18451da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 18461da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 18471da177e4SLinus Torvalds * using the inode layer 18481da177e4SLinus Torvalds * 18491da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 18501da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 18511da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 18521da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 18531da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 18541da177e4SLinus Torvalds * the rename. 18551da177e4SLinus Torvalds */ 18561da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 18571da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 18581da177e4SLinus Torvalds { 18591da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 18601da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 18611da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 18621da177e4SLinus Torvalds int error = -EBUSY; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 18651da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18661da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 18671da177e4SLinus Torvalds atomic_read(&new_dentry->d_count)); 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds /* 187028f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 187128f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 187228f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 187328f79a1aSMiklos Szeredi * the new target. 18741da177e4SLinus Torvalds */ 187527226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 187627226104SMiklos Szeredi /* 187727226104SMiklos Szeredi * To prevent any new references to the target during the 187827226104SMiklos Szeredi * rename, we unhash the dentry in advance. 187927226104SMiklos Szeredi */ 188027226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 188127226104SMiklos Szeredi d_drop(new_dentry); 188227226104SMiklos Szeredi rehash = new_dentry; 188327226104SMiklos Szeredi } 188427226104SMiklos Szeredi 188527226104SMiklos Szeredi if (atomic_read(&new_dentry->d_count) > 2) { 18861da177e4SLinus Torvalds int err; 188727226104SMiklos Szeredi 18881da177e4SLinus Torvalds /* copy the target dentry's name */ 18891da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 18901da177e4SLinus Torvalds &new_dentry->d_name); 18911da177e4SLinus Torvalds if (!dentry) 18921da177e4SLinus Torvalds goto out; 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds /* silly-rename the existing target ... */ 18951da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 189624e93025SMiklos Szeredi if (err) 18971da177e4SLinus Torvalds goto out; 189824e93025SMiklos Szeredi 189924e93025SMiklos Szeredi new_dentry = dentry; 190056335936SOGAWA Hirofumi rehash = NULL; 190124e93025SMiklos Szeredi new_inode = NULL; 1902b1e4adf4STrond Myklebust } 190327226104SMiklos Szeredi } 19041da177e4SLinus Torvalds 1905cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 1906b1e4adf4STrond Myklebust if (new_inode != NULL) 190724174119STrond Myklebust nfs_inode_return_delegation(new_inode); 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 19101da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 19115ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 19121da177e4SLinus Torvalds out: 19131da177e4SLinus Torvalds if (rehash) 19141da177e4SLinus Torvalds d_rehash(rehash); 19151da177e4SLinus Torvalds if (!error) { 1916b1e4adf4STrond Myklebust if (new_inode != NULL) 1917b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 19181da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 19198fb559f8SChuck Lever nfs_set_verifier(new_dentry, 19208fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1921d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 1922d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds /* new dentry created? */ 19251da177e4SLinus Torvalds if (dentry) 19261da177e4SLinus Torvalds dput(dentry); 19271da177e4SLinus Torvalds return error; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds 1930cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 1931cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 1932cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 1933cfcea3e8STrond Myklebust 19341c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 19351c3c07e9STrond Myklebust { 19361c3c07e9STrond Myklebust put_rpccred(entry->cred); 19371c3c07e9STrond Myklebust kfree(entry); 1938cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 1939cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 1940cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 19411c3c07e9STrond Myklebust } 19421c3c07e9STrond Myklebust 19431a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 19441a81bb8aSTrond Myklebust { 19451a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 19461a81bb8aSTrond Myklebust 19471a81bb8aSTrond Myklebust while (!list_empty(head)) { 19481a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 19491a81bb8aSTrond Myklebust list_del(&cache->lru); 19501a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 19511a81bb8aSTrond Myklebust } 19521a81bb8aSTrond Myklebust } 19531a81bb8aSTrond Myklebust 19547f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) 1955979df72eSTrond Myklebust { 1956979df72eSTrond Myklebust LIST_HEAD(head); 1957aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 1958979df72eSTrond Myklebust struct nfs_access_entry *cache; 1959979df72eSTrond Myklebust 196061d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 196161d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 19629c7e7e23STrond Myklebust 1963a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 1964aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 1965979df72eSTrond Myklebust struct inode *inode; 1966979df72eSTrond Myklebust 1967979df72eSTrond Myklebust if (nr_to_scan-- == 0) 1968979df72eSTrond Myklebust break; 19699c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 1970979df72eSTrond Myklebust spin_lock(&inode->i_lock); 1971979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 1972979df72eSTrond Myklebust goto remove_lru_entry; 1973979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 1974979df72eSTrond Myklebust struct nfs_access_entry, lru); 1975979df72eSTrond Myklebust list_move(&cache->lru, &head); 1976979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1977979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 1978979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 1979979df72eSTrond Myklebust &nfs_access_lru_list); 1980979df72eSTrond Myklebust else { 1981979df72eSTrond Myklebust remove_lru_entry: 1982979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 19839c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 1984979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 19859c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 1986979df72eSTrond Myklebust } 198759844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 1988979df72eSTrond Myklebust } 1989979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 19901a81bb8aSTrond Myklebust nfs_access_free_list(&head); 1991979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 1992979df72eSTrond Myklebust } 1993979df72eSTrond Myklebust 19941a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 19951c3c07e9STrond Myklebust { 19961c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 19971a81bb8aSTrond Myklebust struct rb_node *n; 19981c3c07e9STrond Myklebust struct nfs_access_entry *entry; 19991c3c07e9STrond Myklebust 20001c3c07e9STrond Myklebust /* Unhook entries from the cache */ 20011c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 20021c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20031c3c07e9STrond Myklebust rb_erase(n, root_node); 20041a81bb8aSTrond Myklebust list_move(&entry->lru, head); 20051c3c07e9STrond Myklebust } 20061c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 20071c3c07e9STrond Myklebust } 20081c3c07e9STrond Myklebust 20091c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 20101c3c07e9STrond Myklebust { 20111a81bb8aSTrond Myklebust LIST_HEAD(head); 20121a81bb8aSTrond Myklebust 20131a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 20141a81bb8aSTrond Myklebust return; 2015cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2016cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 20171a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2018cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2019cfcea3e8STrond Myklebust 20201c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20211a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 20221a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 20231a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 20241a81bb8aSTrond Myklebust nfs_access_free_list(&head); 20251c3c07e9STrond Myklebust } 20261c3c07e9STrond Myklebust 20271c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 20281c3c07e9STrond Myklebust { 20291c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 20301c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20311c3c07e9STrond Myklebust 20321c3c07e9STrond Myklebust while (n != NULL) { 20331c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20341c3c07e9STrond Myklebust 20351c3c07e9STrond Myklebust if (cred < entry->cred) 20361c3c07e9STrond Myklebust n = n->rb_left; 20371c3c07e9STrond Myklebust else if (cred > entry->cred) 20381c3c07e9STrond Myklebust n = n->rb_right; 20391c3c07e9STrond Myklebust else 20401c3c07e9STrond Myklebust return entry; 20411c3c07e9STrond Myklebust } 20421c3c07e9STrond Myklebust return NULL; 20431c3c07e9STrond Myklebust } 20441c3c07e9STrond Myklebust 2045af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 20461da177e4SLinus Torvalds { 204755296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 20481c3c07e9STrond Myklebust struct nfs_access_entry *cache; 20491c3c07e9STrond Myklebust int err = -ENOENT; 20501da177e4SLinus Torvalds 20511c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20521c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 20531c3c07e9STrond Myklebust goto out_zap; 20541c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 20551c3c07e9STrond Myklebust if (cache == NULL) 20561c3c07e9STrond Myklebust goto out; 2057b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 205864672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 20591c3c07e9STrond Myklebust goto out_stale; 20601c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 20611c3c07e9STrond Myklebust res->cred = cache->cred; 20621c3c07e9STrond Myklebust res->mask = cache->mask; 2063cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 20641c3c07e9STrond Myklebust err = 0; 20651c3c07e9STrond Myklebust out: 20661c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20671c3c07e9STrond Myklebust return err; 20681c3c07e9STrond Myklebust out_stale: 20691c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2070cfcea3e8STrond Myklebust list_del(&cache->lru); 20711c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20721c3c07e9STrond Myklebust nfs_access_free_entry(cache); 20731da177e4SLinus Torvalds return -ENOENT; 20741c3c07e9STrond Myklebust out_zap: 20751a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 20761a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 20771c3c07e9STrond Myklebust return -ENOENT; 20781c3c07e9STrond Myklebust } 20791c3c07e9STrond Myklebust 20801c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 20811c3c07e9STrond Myklebust { 2082cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2083cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 20841c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 20851c3c07e9STrond Myklebust struct rb_node *parent = NULL; 20861c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20871c3c07e9STrond Myklebust 20881c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20891c3c07e9STrond Myklebust while (*p != NULL) { 20901c3c07e9STrond Myklebust parent = *p; 20911c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 20921c3c07e9STrond Myklebust 20931c3c07e9STrond Myklebust if (set->cred < entry->cred) 20941c3c07e9STrond Myklebust p = &parent->rb_left; 20951c3c07e9STrond Myklebust else if (set->cred > entry->cred) 20961c3c07e9STrond Myklebust p = &parent->rb_right; 20971c3c07e9STrond Myklebust else 20981c3c07e9STrond Myklebust goto found; 20991c3c07e9STrond Myklebust } 21001c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 21011c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2102cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 21031c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21041c3c07e9STrond Myklebust return; 21051c3c07e9STrond Myklebust found: 21061c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2107cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2108cfcea3e8STrond Myklebust list_del(&entry->lru); 21091c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21101c3c07e9STrond Myklebust nfs_access_free_entry(entry); 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 2113af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 21141da177e4SLinus Torvalds { 21151c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 21161c3c07e9STrond Myklebust if (cache == NULL) 21171c3c07e9STrond Myklebust return; 21181c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 21191da177e4SLinus Torvalds cache->jiffies = set->jiffies; 21201c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 21211da177e4SLinus Torvalds cache->mask = set->mask; 21221c3c07e9STrond Myklebust 21231c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2124cfcea3e8STrond Myklebust 2125cfcea3e8STrond Myklebust /* Update accounting */ 2126cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 2127cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 2128cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 2129cfcea3e8STrond Myklebust 2130cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 21311a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2132cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 21331a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 21341a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 21351a81bb8aSTrond Myklebust &nfs_access_lru_list); 2136cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2137cfcea3e8STrond Myklebust } 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 21411da177e4SLinus Torvalds { 21421da177e4SLinus Torvalds struct nfs_access_entry cache; 21431da177e4SLinus Torvalds int status; 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 21461da177e4SLinus Torvalds if (status == 0) 21471da177e4SLinus Torvalds goto out; 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 21501da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 21511da177e4SLinus Torvalds cache.cred = cred; 21521da177e4SLinus Torvalds cache.jiffies = jiffies; 21531da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2154a71ee337SSuresh Jayaraman if (status != 0) { 2155a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2156a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2157a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2158a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2159a71ee337SSuresh Jayaraman } 21601da177e4SLinus Torvalds return status; 2161a71ee337SSuresh Jayaraman } 21621da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 21631da177e4SLinus Torvalds out: 2164e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 21651da177e4SLinus Torvalds return 0; 21661da177e4SLinus Torvalds return -EACCES; 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 2169af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2170af22f94aSTrond Myklebust { 2171af22f94aSTrond Myklebust int mask = 0; 2172af22f94aSTrond Myklebust 2173af22f94aSTrond Myklebust if (openflags & FMODE_READ) 2174af22f94aSTrond Myklebust mask |= MAY_READ; 2175af22f94aSTrond Myklebust if (openflags & FMODE_WRITE) 2176af22f94aSTrond Myklebust mask |= MAY_WRITE; 2177af22f94aSTrond Myklebust if (openflags & FMODE_EXEC) 2178af22f94aSTrond Myklebust mask |= MAY_EXEC; 2179af22f94aSTrond Myklebust return mask; 2180af22f94aSTrond Myklebust } 2181af22f94aSTrond Myklebust 2182af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2183af22f94aSTrond Myklebust { 2184af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2185af22f94aSTrond Myklebust } 2186af22f94aSTrond Myklebust 2187e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask) 21881da177e4SLinus Torvalds { 21891da177e4SLinus Torvalds struct rpc_cred *cred; 21901da177e4SLinus Torvalds int res = 0; 21911da177e4SLinus Torvalds 219291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 219391d5b470SChuck Lever 2194e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 21951da177e4SLinus Torvalds goto out; 21961da177e4SLinus Torvalds /* Is this sys_access() ? */ 21979cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 21981da177e4SLinus Torvalds goto force_lookup; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 22011da177e4SLinus Torvalds case S_IFLNK: 22021da177e4SLinus Torvalds goto out; 22031da177e4SLinus Torvalds case S_IFREG: 22041da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 22051da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 22067ee2cb7fSFrank Filz && (mask & MAY_OPEN) 22077ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 22081da177e4SLinus Torvalds goto out; 22091da177e4SLinus Torvalds break; 22101da177e4SLinus Torvalds case S_IFDIR: 22111da177e4SLinus Torvalds /* 22121da177e4SLinus Torvalds * Optimize away all write operations, since the server 22131da177e4SLinus Torvalds * will check permissions when we perform the op. 22141da177e4SLinus Torvalds */ 22151da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 22161da177e4SLinus Torvalds goto out; 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds force_lookup: 22201da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 22211da177e4SLinus Torvalds goto out_notsup; 22221da177e4SLinus Torvalds 222398a8e323STrond Myklebust cred = rpc_lookup_cred(); 22241da177e4SLinus Torvalds if (!IS_ERR(cred)) { 22251da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 22261da177e4SLinus Torvalds put_rpccred(cred); 22271da177e4SLinus Torvalds } else 22281da177e4SLinus Torvalds res = PTR_ERR(cred); 22291da177e4SLinus Torvalds out: 2230f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2231f696a365SMiklos Szeredi res = -EACCES; 2232f696a365SMiklos Szeredi 22331e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 22341e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 22351da177e4SLinus Torvalds return res; 22361da177e4SLinus Torvalds out_notsup: 22371da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 22381da177e4SLinus Torvalds if (res == 0) 22391da177e4SLinus Torvalds res = generic_permission(inode, mask, NULL); 22401e7cb3dcSChuck Lever goto out; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds /* 22441da177e4SLinus Torvalds * Local variables: 22451da177e4SLinus Torvalds * version-control: t 22461da177e4SLinus Torvalds * kept-new-versions: 5 22471da177e4SLinus Torvalds * End: 22481da177e4SLinus Torvalds */ 2249