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> 3704e4bd1cSCatalin Marinas #include <linux/kmemleak.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include "delegation.h" 4091d5b470SChuck Lever #include "iostat.h" 414c30d56eSAdrian Bunk #include "internal.h" 42cd9a1c0eSTrond Myklebust #include "fscache.h" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 471da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 481da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 491da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 501da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 511da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 521da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 531da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 541da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 551da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 561da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 571da177e4SLinus Torvalds struct inode *, struct dentry *); 587ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int); 59f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 6011de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*); 611da177e4SLinus Torvalds 624b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 63f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 641da177e4SLinus Torvalds .read = generic_read_dir, 651da177e4SLinus Torvalds .readdir = nfs_readdir, 661da177e4SLinus Torvalds .open = nfs_opendir, 671da177e4SLinus Torvalds .release = nfs_release, 681da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 691da177e4SLinus Torvalds }; 701da177e4SLinus Torvalds 7192e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = { 721da177e4SLinus Torvalds .create = nfs_create, 731da177e4SLinus Torvalds .lookup = nfs_lookup, 741da177e4SLinus Torvalds .link = nfs_link, 751da177e4SLinus Torvalds .unlink = nfs_unlink, 761da177e4SLinus Torvalds .symlink = nfs_symlink, 771da177e4SLinus Torvalds .mkdir = nfs_mkdir, 781da177e4SLinus Torvalds .rmdir = nfs_rmdir, 791da177e4SLinus Torvalds .mknod = nfs_mknod, 801da177e4SLinus Torvalds .rename = nfs_rename, 811da177e4SLinus Torvalds .permission = nfs_permission, 821da177e4SLinus Torvalds .getattr = nfs_getattr, 831da177e4SLinus Torvalds .setattr = nfs_setattr, 841da177e4SLinus Torvalds }; 851da177e4SLinus Torvalds 8611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = { 8711de3b11STrond Myklebust .freepage = nfs_readdir_clear_array, 88d1bacf9eSBryan Schumaker }; 89d1bacf9eSBryan Schumaker 90b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 9192e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = { 92b7fa0554SAndreas Gruenbacher .create = nfs_create, 93b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 94b7fa0554SAndreas Gruenbacher .link = nfs_link, 95b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 96b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 97b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 98b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 99b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 100b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 101b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 102b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 103b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 104b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 105b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 106b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 107b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 108b7fa0554SAndreas Gruenbacher }; 109b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 110b7fa0554SAndreas Gruenbacher 1111da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 114c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd); 11592e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = { 116c0204fd2STrond Myklebust .create = nfs_open_create, 1171da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1181da177e4SLinus Torvalds .link = nfs_link, 1191da177e4SLinus Torvalds .unlink = nfs_unlink, 1201da177e4SLinus Torvalds .symlink = nfs_symlink, 1211da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1221da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1231da177e4SLinus Torvalds .mknod = nfs_mknod, 1241da177e4SLinus Torvalds .rename = nfs_rename, 1251da177e4SLinus Torvalds .permission = nfs_permission, 1261da177e4SLinus Torvalds .getattr = nfs_getattr, 1271da177e4SLinus Torvalds .setattr = nfs_setattr, 1286b3b5496SJ. Bruce Fields .getxattr = nfs4_getxattr, 1296b3b5496SJ. Bruce Fields .setxattr = nfs4_setxattr, 1306b3b5496SJ. Bruce Fields .listxattr = nfs4_listxattr, 1311da177e4SLinus Torvalds }; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Open file 1371da177e4SLinus Torvalds */ 1381da177e4SLinus Torvalds static int 1391da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1401da177e4SLinus Torvalds { 1417451c4f0SCarsten Otte int res; 1421da177e4SLinus Torvalds 1436da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 144cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 145cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 146cc0dd2d1SChuck Lever 147cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1481e7cb3dcSChuck Lever 1491da177e4SLinus Torvalds /* Call generic open code in order to cache credentials */ 1501da177e4SLinus Torvalds res = nfs_open(inode, filp); 151f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 152f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 153f5a73672SNeil Brown * have been called, so we need to refresh the 154f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 155f5a73672SNeil Brown */ 156f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 157f5a73672SNeil Brown } 1581da177e4SLinus Torvalds return res; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 161d1bacf9eSBryan Schumaker struct nfs_cache_array_entry { 162d1bacf9eSBryan Schumaker u64 cookie; 163d1bacf9eSBryan Schumaker u64 ino; 164d1bacf9eSBryan Schumaker struct qstr string; 1650b26a0bfSTrond Myklebust unsigned char d_type; 166d1bacf9eSBryan Schumaker }; 167d1bacf9eSBryan Schumaker 168d1bacf9eSBryan Schumaker struct nfs_cache_array { 169d1bacf9eSBryan Schumaker unsigned int size; 170d1bacf9eSBryan Schumaker int eof_index; 171d1bacf9eSBryan Schumaker u64 last_cookie; 172d1bacf9eSBryan Schumaker struct nfs_cache_array_entry array[0]; 173d1bacf9eSBryan Schumaker }; 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; 1810aded708STrond Myklebust u64 last_cookie; 182f0dd2136STrond Myklebust loff_t current_index; 1831da177e4SLinus Torvalds decode_dirent_t decode; 184d1bacf9eSBryan Schumaker 1851f4eab7eSNeil Brown unsigned long timestamp; 1864704f0e2STrond Myklebust unsigned long gencount; 187d1bacf9eSBryan Schumaker unsigned int cache_entry_index; 188d1bacf9eSBryan Schumaker unsigned int plus:1; 189d1bacf9eSBryan Schumaker unsigned int eof:1; 1901da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1911da177e4SLinus Torvalds 192d1bacf9eSBryan Schumaker /* 193d1bacf9eSBryan Schumaker * The caller is responsible for calling nfs_readdir_release_array(page) 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds static 196d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page) 1971da177e4SLinus Torvalds { 1988cd51a0cSTrond Myklebust void *ptr; 199d1bacf9eSBryan Schumaker if (page == NULL) 200d1bacf9eSBryan Schumaker return ERR_PTR(-EIO); 2018cd51a0cSTrond Myklebust ptr = kmap(page); 2028cd51a0cSTrond Myklebust if (ptr == NULL) 2038cd51a0cSTrond Myklebust return ERR_PTR(-ENOMEM); 2048cd51a0cSTrond Myklebust return ptr; 205d1bacf9eSBryan Schumaker } 206d1bacf9eSBryan Schumaker 207d1bacf9eSBryan Schumaker static 208d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page) 209d1bacf9eSBryan Schumaker { 210d1bacf9eSBryan Schumaker kunmap(page); 211d1bacf9eSBryan Schumaker } 212d1bacf9eSBryan Schumaker 213d1bacf9eSBryan Schumaker /* 214d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array() 215d1bacf9eSBryan Schumaker */ 216d1bacf9eSBryan Schumaker static 21711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page) 218d1bacf9eSBryan Schumaker { 21911de3b11STrond Myklebust struct nfs_cache_array *array; 220d1bacf9eSBryan Schumaker int i; 2218cd51a0cSTrond Myklebust 22211de3b11STrond Myklebust array = kmap_atomic(page, KM_USER0); 223d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) 224d1bacf9eSBryan Schumaker kfree(array->array[i].string.name); 22511de3b11STrond Myklebust kunmap_atomic(array, KM_USER0); 226d1bacf9eSBryan Schumaker } 227d1bacf9eSBryan Schumaker 228d1bacf9eSBryan Schumaker /* 229d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name 230d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in 231d1bacf9eSBryan Schumaker * nfs_clear_readdir_array() 232d1bacf9eSBryan Schumaker */ 233d1bacf9eSBryan Schumaker static 2344a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len) 235d1bacf9eSBryan Schumaker { 236d1bacf9eSBryan Schumaker string->len = len; 237d1bacf9eSBryan Schumaker string->name = kmemdup(name, len, GFP_KERNEL); 2384a201d6eSTrond Myklebust if (string->name == NULL) 2394a201d6eSTrond Myklebust return -ENOMEM; 24004e4bd1cSCatalin Marinas /* 24104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored 24204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan. 24304e4bd1cSCatalin Marinas */ 24404e4bd1cSCatalin Marinas kmemleak_not_leak(string->name); 2454a201d6eSTrond Myklebust string->hash = full_name_hash(name, len); 2464a201d6eSTrond Myklebust return 0; 247d1bacf9eSBryan Schumaker } 248d1bacf9eSBryan Schumaker 249d1bacf9eSBryan Schumaker static 250d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 251d1bacf9eSBryan Schumaker { 252d1bacf9eSBryan Schumaker struct nfs_cache_array *array = nfs_readdir_get_array(page); 2534a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry; 2544a201d6eSTrond Myklebust int ret; 2554a201d6eSTrond Myklebust 256d1bacf9eSBryan Schumaker if (IS_ERR(array)) 257d1bacf9eSBryan Schumaker return PTR_ERR(array); 258d1bacf9eSBryan Schumaker 2594a201d6eSTrond Myklebust cache_entry = &array->array[array->size]; 2603020093fSTrond Myklebust 2613020093fSTrond Myklebust /* Check that this entry lies within the page bounds */ 2623020093fSTrond Myklebust ret = -ENOSPC; 2633020093fSTrond Myklebust if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE) 2643020093fSTrond Myklebust goto out; 2653020093fSTrond Myklebust 2664a201d6eSTrond Myklebust cache_entry->cookie = entry->prev_cookie; 2674a201d6eSTrond Myklebust cache_entry->ino = entry->ino; 2680b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type; 2694a201d6eSTrond Myklebust ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len); 2704a201d6eSTrond Myklebust if (ret) 2714a201d6eSTrond Myklebust goto out; 272d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie; 2738cd51a0cSTrond Myklebust array->size++; 27447c716cbSTrond Myklebust if (entry->eof != 0) 275d1bacf9eSBryan Schumaker array->eof_index = array->size; 2764a201d6eSTrond Myklebust out: 277d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 2784a201d6eSTrond Myklebust return ret; 279d1bacf9eSBryan Schumaker } 280d1bacf9eSBryan Schumaker 281d1bacf9eSBryan Schumaker static 282d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 283d1bacf9eSBryan Schumaker { 284d1bacf9eSBryan Schumaker loff_t diff = desc->file->f_pos - desc->current_index; 285d1bacf9eSBryan Schumaker unsigned int index; 286d1bacf9eSBryan Schumaker 287d1bacf9eSBryan Schumaker if (diff < 0) 288d1bacf9eSBryan Schumaker goto out_eof; 289d1bacf9eSBryan Schumaker if (diff >= array->size) { 2908cd51a0cSTrond Myklebust if (array->eof_index >= 0) 291d1bacf9eSBryan Schumaker goto out_eof; 292d1bacf9eSBryan Schumaker desc->current_index += array->size; 293d1bacf9eSBryan Schumaker return -EAGAIN; 294d1bacf9eSBryan Schumaker } 295d1bacf9eSBryan Schumaker 296d1bacf9eSBryan Schumaker index = (unsigned int)diff; 297d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[index].cookie; 298d1bacf9eSBryan Schumaker desc->cache_entry_index = index; 299d1bacf9eSBryan Schumaker return 0; 300d1bacf9eSBryan Schumaker out_eof: 301d1bacf9eSBryan Schumaker desc->eof = 1; 302d1bacf9eSBryan Schumaker return -EBADCOOKIE; 303d1bacf9eSBryan Schumaker } 304d1bacf9eSBryan Schumaker 305d1bacf9eSBryan Schumaker static 306d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc) 307d1bacf9eSBryan Schumaker { 308d1bacf9eSBryan Schumaker int i; 309d1bacf9eSBryan Schumaker int status = -EAGAIN; 310d1bacf9eSBryan Schumaker 311d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) { 3128cd51a0cSTrond Myklebust if (array->array[i].cookie == *desc->dir_cookie) { 3138cd51a0cSTrond Myklebust desc->cache_entry_index = i; 31447c716cbSTrond Myklebust return 0; 3158cd51a0cSTrond Myklebust } 3168cd51a0cSTrond Myklebust } 31747c716cbSTrond Myklebust if (array->eof_index >= 0) { 318d1bacf9eSBryan Schumaker status = -EBADCOOKIE; 31918fb5fe4STrond Myklebust if (*desc->dir_cookie == array->last_cookie) 32018fb5fe4STrond Myklebust desc->eof = 1; 321d1bacf9eSBryan Schumaker } 322d1bacf9eSBryan Schumaker return status; 323d1bacf9eSBryan Schumaker } 324d1bacf9eSBryan Schumaker 325d1bacf9eSBryan Schumaker static 326d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc) 327d1bacf9eSBryan Schumaker { 328d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 32947c716cbSTrond Myklebust int status; 330d1bacf9eSBryan Schumaker 331d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 332d1bacf9eSBryan Schumaker if (IS_ERR(array)) { 333d1bacf9eSBryan Schumaker status = PTR_ERR(array); 334d1bacf9eSBryan Schumaker goto out; 335d1bacf9eSBryan Schumaker } 336d1bacf9eSBryan Schumaker 337d1bacf9eSBryan Schumaker if (*desc->dir_cookie == 0) 338d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc); 339d1bacf9eSBryan Schumaker else 340d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc); 341d1bacf9eSBryan Schumaker 34247c716cbSTrond Myklebust if (status == -EAGAIN) { 3430aded708STrond Myklebust desc->last_cookie = array->last_cookie; 34447c716cbSTrond Myklebust desc->page_index++; 34547c716cbSTrond Myklebust } 346d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 347d1bacf9eSBryan Schumaker out: 348d1bacf9eSBryan Schumaker return status; 349d1bacf9eSBryan Schumaker } 350d1bacf9eSBryan Schumaker 351d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */ 352d1bacf9eSBryan Schumaker static 35356e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc, 354d1bacf9eSBryan Schumaker struct nfs_entry *entry, struct file *file, struct inode *inode) 355d1bacf9eSBryan Schumaker { 3561da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 3574704f0e2STrond Myklebust unsigned long timestamp, gencount; 3581da177e4SLinus Torvalds int error; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds again: 3611da177e4SLinus Torvalds timestamp = jiffies; 3624704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 36356e4ebf8SBryan Schumaker error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages, 3641da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 3651da177e4SLinus Torvalds if (error < 0) { 3661da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 3671da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 3681da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 3693a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 3701da177e4SLinus Torvalds desc->plus = 0; 3711da177e4SLinus Torvalds goto again; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds goto error; 3741da177e4SLinus Torvalds } 3751f4eab7eSNeil Brown desc->timestamp = timestamp; 3764704f0e2STrond Myklebust desc->gencount = gencount; 377d1bacf9eSBryan Schumaker error: 378d1bacf9eSBryan Schumaker return error; 379d1bacf9eSBryan Schumaker } 380d1bacf9eSBryan Schumaker 381d1bacf9eSBryan Schumaker /* Fill in an entry based on the xdr code stored in desc->page */ 382d1bacf9eSBryan Schumaker static 383babddc72SBryan Schumaker int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream) 384d1bacf9eSBryan Schumaker { 38582f2e547SBryan Schumaker __be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus); 386d1bacf9eSBryan Schumaker if (IS_ERR(p)) 387d1bacf9eSBryan Schumaker return PTR_ERR(p); 388d1bacf9eSBryan Schumaker 389d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp; 390d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount; 391d1bacf9eSBryan Schumaker return 0; 392d1bacf9eSBryan Schumaker } 393d1bacf9eSBryan Schumaker 394d39ab9deSBryan Schumaker static 395d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 396d39ab9deSBryan Schumaker { 397d39ab9deSBryan Schumaker if (dentry->d_inode == NULL) 398d39ab9deSBryan Schumaker goto different; 39937a09f07STrond Myklebust if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0) 400d39ab9deSBryan Schumaker goto different; 401d39ab9deSBryan Schumaker return 1; 402d39ab9deSBryan Schumaker different: 403d39ab9deSBryan Schumaker return 0; 404d39ab9deSBryan Schumaker } 405d39ab9deSBryan Schumaker 406d39ab9deSBryan Schumaker static 407d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry) 408d39ab9deSBryan Schumaker { 4094a201d6eSTrond Myklebust struct qstr filename = { 4104a201d6eSTrond Myklebust .len = entry->len, 4114a201d6eSTrond Myklebust .name = entry->name, 4124a201d6eSTrond Myklebust }; 4134a201d6eSTrond Myklebust struct dentry *dentry; 4144a201d6eSTrond Myklebust struct dentry *alias; 415d39ab9deSBryan Schumaker struct inode *dir = parent->d_inode; 416d39ab9deSBryan Schumaker struct inode *inode; 417d39ab9deSBryan Schumaker 4184a201d6eSTrond Myklebust if (filename.name[0] == '.') { 4194a201d6eSTrond Myklebust if (filename.len == 1) 4204a201d6eSTrond Myklebust return; 4214a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.') 4224a201d6eSTrond Myklebust return; 4234a201d6eSTrond Myklebust } 4244a201d6eSTrond Myklebust filename.hash = full_name_hash(filename.name, filename.len); 425d39ab9deSBryan Schumaker 4264a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename); 427d39ab9deSBryan Schumaker if (dentry != NULL) { 428d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) { 429d39ab9deSBryan Schumaker nfs_refresh_inode(dentry->d_inode, entry->fattr); 430d39ab9deSBryan Schumaker goto out; 431d39ab9deSBryan Schumaker } else { 432d39ab9deSBryan Schumaker d_drop(dentry); 433d39ab9deSBryan Schumaker dput(dentry); 434d39ab9deSBryan Schumaker } 435d39ab9deSBryan Schumaker } 436d39ab9deSBryan Schumaker 437d39ab9deSBryan Schumaker dentry = d_alloc(parent, &filename); 4384a201d6eSTrond Myklebust if (dentry == NULL) 4394a201d6eSTrond Myklebust return; 4404a201d6eSTrond Myklebust 441d39ab9deSBryan Schumaker dentry->d_op = NFS_PROTO(dir)->dentry_ops; 442d39ab9deSBryan Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 443d39ab9deSBryan Schumaker if (IS_ERR(inode)) 444d39ab9deSBryan Schumaker goto out; 445d39ab9deSBryan Schumaker 446d39ab9deSBryan Schumaker alias = d_materialise_unique(dentry, inode); 447d39ab9deSBryan Schumaker if (IS_ERR(alias)) 448d39ab9deSBryan Schumaker goto out; 449d39ab9deSBryan Schumaker else if (alias) { 450d39ab9deSBryan Schumaker nfs_set_verifier(alias, nfs_save_change_attribute(dir)); 451d39ab9deSBryan Schumaker dput(alias); 452d39ab9deSBryan Schumaker } else 453d39ab9deSBryan Schumaker nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 454d39ab9deSBryan Schumaker 455d39ab9deSBryan Schumaker out: 456d39ab9deSBryan Schumaker dput(dentry); 457d39ab9deSBryan Schumaker } 458d39ab9deSBryan Schumaker 459d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */ 460d1bacf9eSBryan Schumaker static 4618cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, 46256e4ebf8SBryan Schumaker void *xdr_page, struct page *page, unsigned int buflen) 463d1bacf9eSBryan Schumaker { 464babddc72SBryan Schumaker struct xdr_stream stream; 465babddc72SBryan Schumaker struct xdr_buf buf; 46656e4ebf8SBryan Schumaker __be32 *ptr = xdr_page; 46799424380SBryan Schumaker struct nfs_cache_array *array; 4685c346854STrond Myklebust unsigned int count = 0; 4695c346854STrond Myklebust int status; 470babddc72SBryan Schumaker 471babddc72SBryan Schumaker buf.head->iov_base = xdr_page; 472babddc72SBryan Schumaker buf.head->iov_len = buflen; 473babddc72SBryan Schumaker buf.tail->iov_len = 0; 474babddc72SBryan Schumaker buf.page_base = 0; 475babddc72SBryan Schumaker buf.page_len = 0; 476babddc72SBryan Schumaker buf.buflen = buf.head->iov_len; 477babddc72SBryan Schumaker buf.len = buf.head->iov_len; 478babddc72SBryan Schumaker 479babddc72SBryan Schumaker xdr_init_decode(&stream, &buf, ptr); 480babddc72SBryan Schumaker 48199424380SBryan Schumaker 48299424380SBryan Schumaker do { 48399424380SBryan Schumaker status = xdr_decode(desc, entry, &stream); 4848cd51a0cSTrond Myklebust if (status != 0) { 4858cd51a0cSTrond Myklebust if (status == -EAGAIN) 4868cd51a0cSTrond Myklebust status = 0; 48799424380SBryan Schumaker break; 4888cd51a0cSTrond Myklebust } 48999424380SBryan Schumaker 4905c346854STrond Myklebust count++; 4915c346854STrond Myklebust 49247c716cbSTrond Myklebust if (desc->plus != 0) 493d39ab9deSBryan Schumaker nfs_prime_dcache(desc->file->f_path.dentry, entry); 4948cd51a0cSTrond Myklebust 4958cd51a0cSTrond Myklebust status = nfs_readdir_add_to_array(entry, page); 4968cd51a0cSTrond Myklebust if (status != 0) 4978cd51a0cSTrond Myklebust break; 49899424380SBryan Schumaker } while (!entry->eof); 49999424380SBryan Schumaker 50047c716cbSTrond Myklebust if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) { 50199424380SBryan Schumaker array = nfs_readdir_get_array(page); 5028cd51a0cSTrond Myklebust if (!IS_ERR(array)) { 5038cd51a0cSTrond Myklebust array->eof_index = array->size; 50499424380SBryan Schumaker status = 0; 50599424380SBryan Schumaker nfs_readdir_release_array(page); 5065c346854STrond Myklebust } else 5075c346854STrond Myklebust status = PTR_ERR(array); 50856e4ebf8SBryan Schumaker } 5098cd51a0cSTrond Myklebust return status; 5108cd51a0cSTrond Myklebust } 51156e4ebf8SBryan Schumaker 51256e4ebf8SBryan Schumaker static 51356e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages) 51456e4ebf8SBryan Schumaker { 51556e4ebf8SBryan Schumaker unsigned int i; 51656e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) 51756e4ebf8SBryan Schumaker put_page(pages[i]); 51856e4ebf8SBryan Schumaker } 51956e4ebf8SBryan Schumaker 52056e4ebf8SBryan Schumaker static 52156e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages, 52256e4ebf8SBryan Schumaker unsigned int npages) 52356e4ebf8SBryan Schumaker { 52456e4ebf8SBryan Schumaker vm_unmap_ram(ptr, npages); 52556e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, npages); 52656e4ebf8SBryan Schumaker } 52756e4ebf8SBryan Schumaker 52856e4ebf8SBryan Schumaker /* 52956e4ebf8SBryan Schumaker * nfs_readdir_large_page will allocate pages that must be freed with a call 53056e4ebf8SBryan Schumaker * to nfs_readdir_free_large_page 53156e4ebf8SBryan Schumaker */ 53256e4ebf8SBryan Schumaker static 53356e4ebf8SBryan Schumaker void *nfs_readdir_large_page(struct page **pages, unsigned int npages) 53456e4ebf8SBryan Schumaker { 53556e4ebf8SBryan Schumaker void *ptr; 53656e4ebf8SBryan Schumaker unsigned int i; 53756e4ebf8SBryan Schumaker 53856e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) { 53956e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL); 54056e4ebf8SBryan Schumaker if (page == NULL) 54156e4ebf8SBryan Schumaker goto out_freepages; 54256e4ebf8SBryan Schumaker pages[i] = page; 54356e4ebf8SBryan Schumaker } 54456e4ebf8SBryan Schumaker 5454a201d6eSTrond Myklebust ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL); 54656e4ebf8SBryan Schumaker if (!IS_ERR_OR_NULL(ptr)) 54756e4ebf8SBryan Schumaker return ptr; 54856e4ebf8SBryan Schumaker out_freepages: 54956e4ebf8SBryan Schumaker nfs_readdir_free_pagearray(pages, i); 55056e4ebf8SBryan Schumaker return NULL; 551d1bacf9eSBryan Schumaker } 552d1bacf9eSBryan Schumaker 553d1bacf9eSBryan Schumaker static 554d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode) 555d1bacf9eSBryan Schumaker { 55656e4ebf8SBryan Schumaker struct page *pages[NFS_MAX_READDIR_PAGES]; 55756e4ebf8SBryan Schumaker void *pages_ptr = NULL; 558d1bacf9eSBryan Schumaker struct nfs_entry entry; 559d1bacf9eSBryan Schumaker struct file *file = desc->file; 560d1bacf9eSBryan Schumaker struct nfs_cache_array *array; 5618cd51a0cSTrond Myklebust int status = -ENOMEM; 56256e4ebf8SBryan Schumaker unsigned int array_size = ARRAY_SIZE(pages); 563d1bacf9eSBryan Schumaker 564d1bacf9eSBryan Schumaker entry.prev_cookie = 0; 5650aded708STrond Myklebust entry.cookie = desc->last_cookie; 566d1bacf9eSBryan Schumaker entry.eof = 0; 567d1bacf9eSBryan Schumaker entry.fh = nfs_alloc_fhandle(); 568d1bacf9eSBryan Schumaker entry.fattr = nfs_alloc_fattr(); 569d1bacf9eSBryan Schumaker if (entry.fh == NULL || entry.fattr == NULL) 570d1bacf9eSBryan Schumaker goto out; 571d1bacf9eSBryan Schumaker 572d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(page); 5738cd51a0cSTrond Myklebust if (IS_ERR(array)) { 5748cd51a0cSTrond Myklebust status = PTR_ERR(array); 5758cd51a0cSTrond Myklebust goto out; 5768cd51a0cSTrond Myklebust } 577d1bacf9eSBryan Schumaker memset(array, 0, sizeof(struct nfs_cache_array)); 578d1bacf9eSBryan Schumaker array->eof_index = -1; 579d1bacf9eSBryan Schumaker 58056e4ebf8SBryan Schumaker pages_ptr = nfs_readdir_large_page(pages, array_size); 58156e4ebf8SBryan Schumaker if (!pages_ptr) 582d1bacf9eSBryan Schumaker goto out_release_array; 583d1bacf9eSBryan Schumaker do { 584ac396128STrond Myklebust unsigned int pglen; 58556e4ebf8SBryan Schumaker status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode); 586babddc72SBryan Schumaker 587d1bacf9eSBryan Schumaker if (status < 0) 588d1bacf9eSBryan Schumaker break; 589ac396128STrond Myklebust pglen = status; 590ac396128STrond Myklebust status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, pglen); 5918cd51a0cSTrond Myklebust if (status < 0) { 5928cd51a0cSTrond Myklebust if (status == -ENOSPC) 5938cd51a0cSTrond Myklebust status = 0; 5948cd51a0cSTrond Myklebust break; 5958cd51a0cSTrond Myklebust } 5968cd51a0cSTrond Myklebust } while (array->eof_index < 0); 597d1bacf9eSBryan Schumaker 59856e4ebf8SBryan Schumaker nfs_readdir_free_large_page(pages_ptr, pages, array_size); 599d1bacf9eSBryan Schumaker out_release_array: 600d1bacf9eSBryan Schumaker nfs_readdir_release_array(page); 601d1bacf9eSBryan Schumaker out: 602d1bacf9eSBryan Schumaker nfs_free_fattr(entry.fattr); 603d1bacf9eSBryan Schumaker nfs_free_fhandle(entry.fh); 604d1bacf9eSBryan Schumaker return status; 605d1bacf9eSBryan Schumaker } 606d1bacf9eSBryan Schumaker 607d1bacf9eSBryan Schumaker /* 608d1bacf9eSBryan Schumaker * Now we cache directories properly, by converting xdr information 609d1bacf9eSBryan Schumaker * to an array that can be used for lookups later. This results in 610d1bacf9eSBryan Schumaker * fewer cache pages, since we can store more information on each page. 611d1bacf9eSBryan Schumaker * We only need to convert from xdr once so future lookups are much simpler 6121da177e4SLinus Torvalds */ 613d1bacf9eSBryan Schumaker static 614d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page) 615d1bacf9eSBryan Schumaker { 616d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 6178cd51a0cSTrond Myklebust int ret; 618d1bacf9eSBryan Schumaker 6198cd51a0cSTrond Myklebust ret = nfs_readdir_xdr_to_array(desc, page, inode); 6208cd51a0cSTrond Myklebust if (ret < 0) 621d1bacf9eSBryan Schumaker goto error; 622d1bacf9eSBryan Schumaker SetPageUptodate(page); 623d1bacf9eSBryan Schumaker 6242aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 625cd9ae2b6STrond Myklebust /* Should never happen */ 626cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 627cd9ae2b6STrond Myklebust } 6281da177e4SLinus Torvalds unlock_page(page); 6291da177e4SLinus Torvalds return 0; 6301da177e4SLinus Torvalds error: 6311da177e4SLinus Torvalds unlock_page(page); 6328cd51a0cSTrond Myklebust return ret; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 635d1bacf9eSBryan Schumaker static 636d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc) 6371da177e4SLinus Torvalds { 63811de3b11STrond Myklebust if (!desc->page->mapping) 63911de3b11STrond Myklebust nfs_readdir_clear_array(desc->page); 6401da177e4SLinus Torvalds page_cache_release(desc->page); 6411da177e4SLinus Torvalds desc->page = NULL; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 644d1bacf9eSBryan Schumaker static 645d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc) 6461da177e4SLinus Torvalds { 6478cd51a0cSTrond Myklebust return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping, 648d1bacf9eSBryan Schumaker desc->page_index, (filler_t *)nfs_readdir_filler, desc); 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* 652d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index 6531da177e4SLinus Torvalds */ 654d1bacf9eSBryan Schumaker static 655d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc) 656d1bacf9eSBryan Schumaker { 657d1bacf9eSBryan Schumaker int res; 658d1bacf9eSBryan Schumaker 659d1bacf9eSBryan Schumaker desc->page = get_cache_page(desc); 660d1bacf9eSBryan Schumaker if (IS_ERR(desc->page)) 661d1bacf9eSBryan Schumaker return PTR_ERR(desc->page); 662d1bacf9eSBryan Schumaker 663d1bacf9eSBryan Schumaker res = nfs_readdir_search_array(desc); 66447c716cbSTrond Myklebust if (res != 0) 665d1bacf9eSBryan Schumaker cache_page_release(desc); 666d1bacf9eSBryan Schumaker return res; 667d1bacf9eSBryan Schumaker } 668d1bacf9eSBryan Schumaker 669d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */ 6701da177e4SLinus Torvalds static inline 6711da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 6721da177e4SLinus Torvalds { 6738cd51a0cSTrond Myklebust int res; 674d1bacf9eSBryan Schumaker 6750aded708STrond Myklebust if (desc->page_index == 0) { 6768cd51a0cSTrond Myklebust desc->current_index = 0; 6770aded708STrond Myklebust desc->last_cookie = 0; 6780aded708STrond Myklebust } 67947c716cbSTrond Myklebust do { 680d1bacf9eSBryan Schumaker res = find_cache_page(desc); 68147c716cbSTrond Myklebust } while (res == -EAGAIN); 6821da177e4SLinus Torvalds return res; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /* 6861da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds static 6891da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 6901da177e4SLinus Torvalds filldir_t filldir) 6911da177e4SLinus Torvalds { 6921da177e4SLinus Torvalds struct file *file = desc->file; 693d1bacf9eSBryan Schumaker int i = 0; 694d1bacf9eSBryan Schumaker int res = 0; 695d1bacf9eSBryan Schumaker struct nfs_cache_array *array = NULL; 6961da177e4SLinus Torvalds 697d1bacf9eSBryan Schumaker array = nfs_readdir_get_array(desc->page); 698e7c58e97STrond Myklebust if (IS_ERR(array)) { 699e7c58e97STrond Myklebust res = PTR_ERR(array); 700e7c58e97STrond Myklebust goto out; 701e7c58e97STrond Myklebust } 7021da177e4SLinus Torvalds 703d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) { 704ece0b423STrond Myklebust struct nfs_cache_array_entry *ent; 7051da177e4SLinus Torvalds 706ece0b423STrond Myklebust ent = &array->array[i]; 707ece0b423STrond Myklebust if (filldir(dirent, ent->string.name, ent->string.len, 7080b26a0bfSTrond Myklebust file->f_pos, nfs_compat_user_ino64(ent->ino), 7090b26a0bfSTrond Myklebust ent->d_type) < 0) { 710ece0b423STrond Myklebust desc->eof = 1; 7111da177e4SLinus Torvalds break; 712ece0b423STrond Myklebust } 71300a92642SOlivier Galibert file->f_pos++; 714d1bacf9eSBryan Schumaker if (i < (array->size-1)) 715d1bacf9eSBryan Schumaker *desc->dir_cookie = array->array[i+1].cookie; 716d1bacf9eSBryan Schumaker else 717d1bacf9eSBryan Schumaker *desc->dir_cookie = array->last_cookie; 7188cd51a0cSTrond Myklebust } 71947c716cbSTrond Myklebust if (array->eof_index >= 0) 720d1bacf9eSBryan Schumaker desc->eof = 1; 721d1bacf9eSBryan Schumaker 722d1bacf9eSBryan Schumaker nfs_readdir_release_array(desc->page); 723e7c58e97STrond Myklebust out: 724d1bacf9eSBryan Schumaker cache_page_release(desc); 7251e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 7261e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 7271da177e4SLinus Torvalds return res; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* 7311da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 7321da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 7331da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 7341da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 7351da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 7361da177e4SLinus Torvalds * 7371da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 7381da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 7391da177e4SLinus Torvalds * we should already have a complete representation of the 7401da177e4SLinus Torvalds * directory in the page cache by the time we get here. 7411da177e4SLinus Torvalds */ 7421da177e4SLinus Torvalds static inline 7431da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 7441da177e4SLinus Torvalds filldir_t filldir) 7451da177e4SLinus Torvalds { 7461da177e4SLinus Torvalds struct page *page = NULL; 7471da177e4SLinus Torvalds int status; 748d1bacf9eSBryan Schumaker struct inode *inode = desc->file->f_path.dentry->d_inode; 7491da177e4SLinus Torvalds 7501e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 7511e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 7541da177e4SLinus Torvalds if (!page) { 7551da177e4SLinus Torvalds status = -ENOMEM; 7561da177e4SLinus Torvalds goto out; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7597a8e1dc3STrond Myklebust desc->page_index = 0; 7600aded708STrond Myklebust desc->last_cookie = *desc->dir_cookie; 7617a8e1dc3STrond Myklebust desc->page = page; 7627a8e1dc3STrond Myklebust 76385f8607eSTrond Myklebust status = nfs_readdir_xdr_to_array(desc, page, inode); 76485f8607eSTrond Myklebust if (status < 0) 765d1bacf9eSBryan Schumaker goto out_release; 766d1bacf9eSBryan Schumaker 7671da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds out: 7701e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 7713110ff80SHarvey Harrison __func__, status); 7721da177e4SLinus Torvalds return status; 7731da177e4SLinus Torvalds out_release: 774d1bacf9eSBryan Schumaker cache_page_release(desc); 7751da177e4SLinus Torvalds goto out; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 77800a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 77900a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 78000a92642SOlivier Galibert whole directory. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 7831da177e4SLinus Torvalds { 78401cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 7851da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 7861da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 7871da177e4SLinus Torvalds *desc = &my_desc; 78847c716cbSTrond Myklebust int res; 7891da177e4SLinus Torvalds 7906da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 7911e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 7921e7cb3dcSChuck Lever (long long)filp->f_pos); 79391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 79491d5b470SChuck Lever 7951da177e4SLinus Torvalds /* 79600a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 797f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 79800a92642SOlivier Galibert * to either find the entry with the appropriate number or 79900a92642SOlivier Galibert * revalidate the cookie. 8001da177e4SLinus Torvalds */ 8011da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds desc->file = filp; 804cd3758e3STrond Myklebust desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie; 8051da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 8061da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 8071da177e4SLinus Torvalds 808565277f6STrond Myklebust nfs_block_sillyrename(dentry); 8091cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 810fccca7fcSTrond Myklebust if (res < 0) 811fccca7fcSTrond Myklebust goto out; 812fccca7fcSTrond Myklebust 81347c716cbSTrond Myklebust do { 8141da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 81500a92642SOlivier Galibert 8161da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 817ece0b423STrond Myklebust res = 0; 8181da177e4SLinus Torvalds /* This means either end of directory */ 819d1bacf9eSBryan Schumaker if (*desc->dir_cookie && desc->eof == 0) { 8201da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 8211da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 822ece0b423STrond Myklebust if (res == 0) 8231da177e4SLinus Torvalds continue; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds break; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 8283a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 8291da177e4SLinus Torvalds nfs_zap_caches(inode); 830baf57a09STrond Myklebust desc->page_index = 0; 8311da177e4SLinus Torvalds desc->plus = 0; 832d1bacf9eSBryan Schumaker desc->eof = 0; 8331da177e4SLinus Torvalds continue; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds if (res < 0) 8361da177e4SLinus Torvalds break; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 839ece0b423STrond Myklebust if (res < 0) 8401da177e4SLinus Torvalds break; 84147c716cbSTrond Myklebust } while (!desc->eof); 842fccca7fcSTrond Myklebust out: 843565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 8441e7cb3dcSChuck Lever if (res > 0) 8451e7cb3dcSChuck Lever res = 0; 846aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 8471e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8481e7cb3dcSChuck Lever res); 8491da177e4SLinus Torvalds return res; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 85210afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 853f0dd2136STrond Myklebust { 854b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 855b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 856b84e06c5SChuck Lever 8576da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 858b84e06c5SChuck Lever dentry->d_parent->d_name.name, 859b84e06c5SChuck Lever dentry->d_name.name, 860b84e06c5SChuck Lever offset, origin); 861b84e06c5SChuck Lever 862b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 863f0dd2136STrond Myklebust switch (origin) { 864f0dd2136STrond Myklebust case 1: 865f0dd2136STrond Myklebust offset += filp->f_pos; 866f0dd2136STrond Myklebust case 0: 867f0dd2136STrond Myklebust if (offset >= 0) 868f0dd2136STrond Myklebust break; 869f0dd2136STrond Myklebust default: 870f0dd2136STrond Myklebust offset = -EINVAL; 871f0dd2136STrond Myklebust goto out; 872f0dd2136STrond Myklebust } 873f0dd2136STrond Myklebust if (offset != filp->f_pos) { 874f0dd2136STrond Myklebust filp->f_pos = offset; 875cd3758e3STrond Myklebust nfs_file_open_context(filp)->dir_cookie = 0; 876f0dd2136STrond Myklebust } 877f0dd2136STrond Myklebust out: 878b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 879f0dd2136STrond Myklebust return offset; 880f0dd2136STrond Myklebust } 881f0dd2136STrond Myklebust 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 8841da177e4SLinus Torvalds * is a dummy operation. 8851da177e4SLinus Torvalds */ 8867ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync) 8871da177e4SLinus Torvalds { 8887ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 8897ea80859SChristoph Hellwig 8906da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 8911e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 8921e7cb3dcSChuck Lever datasync); 8931e7cb3dcSChuck Lever 89454917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 8951da177e4SLinus Torvalds return 0; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 898bfc69a45STrond Myklebust /** 899bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 900bfc69a45STrond Myklebust * @dir - pointer to directory inode 901bfc69a45STrond Myklebust * 902bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 903bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 904bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 905bfc69a45STrond Myklebust * 906bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 907bfc69a45STrond Myklebust */ 908bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 909bfc69a45STrond Myklebust { 910011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 911bfc69a45STrond Myklebust } 912bfc69a45STrond Myklebust 9131da177e4SLinus Torvalds /* 9141da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 9151da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 9161da177e4SLinus Torvalds * and may need to be looked up again. 9171da177e4SLinus Torvalds */ 918c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds if (IS_ROOT(dentry)) 9211da177e4SLinus Torvalds return 1; 9224eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 9234eec952eSTrond Myklebust return 0; 924f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 9256ecc5e8fSTrond Myklebust return 0; 926f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 927f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 928f2c77f4eSTrond Myklebust return 0; 929f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 930f2c77f4eSTrond Myklebust return 0; 931f2c77f4eSTrond Myklebust return 1; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* 9351d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 9361d6757fbSTrond Myklebust * 9371d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 9381d6757fbSTrond Myklebust * component of the path. 9391d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 9401d6757fbSTrond Myklebust */ 9411d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) 9421d6757fbSTrond Myklebust { 9431d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 9441d6757fbSTrond Myklebust return 0; 9451d6757fbSTrond Myklebust return nd->flags & mask; 9461d6757fbSTrond Myklebust } 9471d6757fbSTrond Myklebust 9481d6757fbSTrond Myklebust /* 949a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 950a12802caSTrond Myklebust * an O_EXCL create using this path component. 951a12802caSTrond Myklebust */ 952a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 953a12802caSTrond Myklebust { 954a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 955a12802caSTrond Myklebust return 0; 9563516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 957a12802caSTrond Myklebust } 958a12802caSTrond Myklebust 959a12802caSTrond Myklebust /* 9601d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 9611d6757fbSTrond Myklebust * 9621d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 9631d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 9641d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 9651d6757fbSTrond Myklebust * 9661d6757fbSTrond Myklebust */ 9671da177e4SLinus Torvalds static inline 9681da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 9691da177e4SLinus Torvalds { 9701da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 9711da177e4SLinus Torvalds 9724e99a1ffSTrond Myklebust if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags)) 9734e99a1ffSTrond Myklebust return 0; 9741da177e4SLinus Torvalds if (nd != NULL) { 9751da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 9761d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 9771da177e4SLinus Torvalds goto out_force; 9781da177e4SLinus Torvalds /* This is an open(2) */ 9791d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 9804e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 9814e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 9824e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 9831da177e4SLinus Torvalds goto out_force; 9844f48af45STrond Myklebust return 0; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 9871da177e4SLinus Torvalds out_force: 9881da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * We judge how long we want to trust negative 9931da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 9941da177e4SLinus Torvalds * 9951da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 9961da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 9971da177e4SLinus Torvalds */ 9981da177e4SLinus Torvalds static inline 9991da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 10001da177e4SLinus Torvalds struct nameidata *nd) 10011da177e4SLinus Torvalds { 10021da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 10031d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 10041da177e4SLinus Torvalds return 0; 10054eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 10064eec952eSTrond Myklebust return 1; 10071da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds /* 10111da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 10121da177e4SLinus Torvalds * and we should check whether we can really trust that 10131da177e4SLinus Torvalds * lookup. 10141da177e4SLinus Torvalds * 10151da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 10161da177e4SLinus Torvalds * we have an inode! 10171da177e4SLinus Torvalds * 10181da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 10191da177e4SLinus Torvalds * cached dentry and do a new lookup. 10201da177e4SLinus Torvalds */ 10211da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) 10221da177e4SLinus Torvalds { 10231da177e4SLinus Torvalds struct inode *dir; 10241da177e4SLinus Torvalds struct inode *inode; 10251da177e4SLinus Torvalds struct dentry *parent; 1026e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1027e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 10281da177e4SLinus Torvalds int error; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds parent = dget_parent(dentry); 10311da177e4SLinus Torvalds dir = parent->d_inode; 103291d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 10331da177e4SLinus Torvalds inode = dentry->d_inode; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds if (!inode) { 10361da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 10371da177e4SLinus Torvalds goto out_bad; 10381da177e4SLinus Torvalds goto out_valid; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds if (is_bad_inode(inode)) { 10421e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 10433110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 10441e7cb3dcSChuck Lever dentry->d_name.name); 10451da177e4SLinus Torvalds goto out_bad; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 104815860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 104915860ab1STrond Myklebust goto out_set_verifier; 105015860ab1STrond Myklebust 10511da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 1052a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 10531da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 10541da177e4SLinus Torvalds goto out_zap_parent; 10551da177e4SLinus Torvalds goto out_valid; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds if (NFS_STALE(inode)) 10591da177e4SLinus Torvalds goto out_bad; 10601da177e4SLinus Torvalds 1061e1fb4d05STrond Myklebust error = -ENOMEM; 1062e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1063e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1064e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1065e1fb4d05STrond Myklebust goto out_error; 1066e1fb4d05STrond Myklebust 1067e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 10681da177e4SLinus Torvalds if (error) 10691da177e4SLinus Torvalds goto out_bad; 1070e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 10711da177e4SLinus Torvalds goto out_bad; 1072e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 10731da177e4SLinus Torvalds goto out_bad; 10741da177e4SLinus Torvalds 1075e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1076e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 107715860ab1STrond Myklebust out_set_verifier: 1078cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 10791da177e4SLinus Torvalds out_valid: 10801da177e4SLinus Torvalds dput(parent); 10811e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 10823110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 10831e7cb3dcSChuck Lever dentry->d_name.name); 10841da177e4SLinus Torvalds return 1; 10851da177e4SLinus Torvalds out_zap_parent: 10861da177e4SLinus Torvalds nfs_zap_caches(dir); 10871da177e4SLinus Torvalds out_bad: 1088a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 10891da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 10901da177e4SLinus Torvalds /* Purge readdir caches. */ 10911da177e4SLinus Torvalds nfs_zap_caches(inode); 10921da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 10931da177e4SLinus Torvalds if (have_submounts(dentry)) 10941da177e4SLinus Torvalds goto out_valid; 1095d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 1096d9e80b7dSAl Viro goto out_valid; 10971da177e4SLinus Torvalds shrink_dcache_parent(dentry); 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds d_drop(dentry); 1100e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1101e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 11021da177e4SLinus Torvalds dput(parent); 11031e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 11043110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 11051e7cb3dcSChuck Lever dentry->d_name.name); 11061da177e4SLinus Torvalds return 0; 1107e1fb4d05STrond Myklebust out_error: 1108e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1109e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 1110e1fb4d05STrond Myklebust dput(parent); 1111e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 1112e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 1113e1fb4d05STrond Myklebust dentry->d_name.name, error); 1114e1fb4d05STrond Myklebust return error; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds /* 11181da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 11191da177e4SLinus Torvalds */ 1120fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry) 11211da177e4SLinus Torvalds { 11221da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 11231da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 11241da177e4SLinus Torvalds dentry->d_flags); 11251da177e4SLinus Torvalds 112677f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 112777f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 112877f11192STrond Myklebust return 1; 112977f11192STrond Myklebust 11301da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 11311da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 11321da177e4SLinus Torvalds return 1; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 11351da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 11361da177e4SLinus Torvalds * files will be cleaned up during umount */ 11371da177e4SLinus Torvalds return 1; 11381da177e4SLinus Torvalds } 11391da177e4SLinus Torvalds return 0; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 11441b83d707STrond Myklebust { 11451b83d707STrond Myklebust spin_lock(&inode->i_lock); 11461b83d707STrond Myklebust if (inode->i_nlink > 0) 11471b83d707STrond Myklebust drop_nlink(inode); 11481b83d707STrond Myklebust spin_unlock(&inode->i_lock); 11491b83d707STrond Myklebust } 11501b83d707STrond Myklebust 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * Called when the dentry loses inode. 11531da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 11541da177e4SLinus Torvalds */ 11551da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 11561da177e4SLinus Torvalds { 115783672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 115883672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 115983672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 116083672d39SNeil Brown 11611da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 11629a53c3a7SDave Hansen drop_nlink(inode); 1163e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds iput(inode); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 1168f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 11691da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 11701da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 11711da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 11721da177e4SLinus Torvalds }; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 11751da177e4SLinus Torvalds { 11761da177e4SLinus Torvalds struct dentry *res; 1177565277f6STrond Myklebust struct dentry *parent; 11781da177e4SLinus Torvalds struct inode *inode = NULL; 1179e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 1180e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 11811da177e4SLinus Torvalds int error; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 11841da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 118591d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 11881da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 11891da177e4SLinus Torvalds goto out; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 11921da177e4SLinus Torvalds 1193fd684071STrond Myklebust /* 1194fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 1195fd684071STrond Myklebust * but don't hash the dentry. 1196fd684071STrond Myklebust */ 1197fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 1198fd684071STrond Myklebust d_instantiate(dentry, NULL); 1199fd684071STrond Myklebust res = NULL; 1200fc0f684cSTrond Myklebust goto out; 1201fd684071STrond Myklebust } 12021da177e4SLinus Torvalds 1203e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 1204e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 1205e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 1206e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 1207e1fb4d05STrond Myklebust goto out; 1208e1fb4d05STrond Myklebust 1209565277f6STrond Myklebust parent = dentry->d_parent; 1210565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 1211565277f6STrond Myklebust nfs_block_sillyrename(parent); 1212e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 12131da177e4SLinus Torvalds if (error == -ENOENT) 12141da177e4SLinus Torvalds goto no_entry; 12151da177e4SLinus Torvalds if (error < 0) { 12161da177e4SLinus Torvalds res = ERR_PTR(error); 1217565277f6STrond Myklebust goto out_unblock_sillyrename; 12181da177e4SLinus Torvalds } 1219e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 122003f28e3aSTrond Myklebust res = (struct dentry *)inode; 122103f28e3aSTrond Myklebust if (IS_ERR(res)) 1222565277f6STrond Myklebust goto out_unblock_sillyrename; 122354ceac45SDavid Howells 12241da177e4SLinus Torvalds no_entry: 122554ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 12269eaef27bSTrond Myklebust if (res != NULL) { 12279eaef27bSTrond Myklebust if (IS_ERR(res)) 1228565277f6STrond Myklebust goto out_unblock_sillyrename; 12291da177e4SLinus Torvalds dentry = res; 12309eaef27bSTrond Myklebust } 12311da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1232565277f6STrond Myklebust out_unblock_sillyrename: 1233565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 12341da177e4SLinus Torvalds out: 1235e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1236e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 12371da177e4SLinus Torvalds return res; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 12411da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 12421da177e4SLinus Torvalds 1243f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 12441da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 12451da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 12461da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 12471da177e4SLinus Torvalds }; 12481da177e4SLinus Torvalds 12491d6757fbSTrond Myklebust /* 12501d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 12511d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 12521d6757fbSTrond Myklebust */ 12535584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 12541da177e4SLinus Torvalds { 12551d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 12561da177e4SLinus Torvalds return 0; 12571da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 12581da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 12591da177e4SLinus Torvalds return 0; 12601da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 12612c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 12622c463e95SDave Hansen (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 12631da177e4SLinus Torvalds return 0; 12641da177e4SLinus Torvalds return 1; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 1267cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd) 1268cd9a1c0eSTrond Myklebust { 1269cd9a1c0eSTrond Myklebust struct path path = { 1270cd9a1c0eSTrond Myklebust .mnt = nd->path.mnt, 1271cd9a1c0eSTrond Myklebust .dentry = dentry, 1272cd9a1c0eSTrond Myklebust }; 1273cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1274cd9a1c0eSTrond Myklebust struct rpc_cred *cred; 1275cd9a1c0eSTrond Myklebust fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); 1276cd9a1c0eSTrond Myklebust 1277cd9a1c0eSTrond Myklebust cred = rpc_lookup_cred(); 1278cd9a1c0eSTrond Myklebust if (IS_ERR(cred)) 1279cd9a1c0eSTrond Myklebust return ERR_CAST(cred); 1280cd9a1c0eSTrond Myklebust ctx = alloc_nfs_open_context(&path, cred, fmode); 1281cd9a1c0eSTrond Myklebust put_rpccred(cred); 1282cd9a1c0eSTrond Myklebust if (ctx == NULL) 1283cd9a1c0eSTrond Myklebust return ERR_PTR(-ENOMEM); 1284cd9a1c0eSTrond Myklebust return ctx; 1285cd9a1c0eSTrond Myklebust } 1286cd9a1c0eSTrond Myklebust 1287cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1288cd9a1c0eSTrond Myklebust { 1289cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1290cd9a1c0eSTrond Myklebust return 0; 1291cd9a1c0eSTrond Myklebust } 1292cd9a1c0eSTrond Myklebust 1293cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) 1294cd9a1c0eSTrond Myklebust { 1295cd9a1c0eSTrond Myklebust struct file *filp; 1296cd9a1c0eSTrond Myklebust int ret = 0; 1297cd9a1c0eSTrond Myklebust 1298cd9a1c0eSTrond Myklebust /* If the open_intent is for execute, we have an extra check to make */ 1299cd9a1c0eSTrond Myklebust if (ctx->mode & FMODE_EXEC) { 1300cd9a1c0eSTrond Myklebust ret = nfs_may_open(ctx->path.dentry->d_inode, 1301cd9a1c0eSTrond Myklebust ctx->cred, 1302cd9a1c0eSTrond Myklebust nd->intent.open.flags); 1303cd9a1c0eSTrond Myklebust if (ret < 0) 1304cd9a1c0eSTrond Myklebust goto out; 1305cd9a1c0eSTrond Myklebust } 1306cd9a1c0eSTrond Myklebust filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open); 1307cd9a1c0eSTrond Myklebust if (IS_ERR(filp)) 1308cd9a1c0eSTrond Myklebust ret = PTR_ERR(filp); 1309cd9a1c0eSTrond Myklebust else 1310cd9a1c0eSTrond Myklebust nfs_file_set_open_context(filp, ctx); 1311cd9a1c0eSTrond Myklebust out: 1312cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1313cd9a1c0eSTrond Myklebust return ret; 1314cd9a1c0eSTrond Myklebust } 1315cd9a1c0eSTrond Myklebust 13161da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 13171da177e4SLinus Torvalds { 1318cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1319cd9a1c0eSTrond Myklebust struct iattr attr; 13201da177e4SLinus Torvalds struct dentry *res = NULL; 1321f46e0bd3STrond Myklebust struct inode *inode; 1322cd9a1c0eSTrond Myklebust int open_flags; 1323898f635cSTrond Myklebust int err; 13241da177e4SLinus Torvalds 13251e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 13261e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13271e7cb3dcSChuck Lever 13281da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 13295584c306STrond Myklebust if (!is_atomic_open(nd)) 13301da177e4SLinus Torvalds goto no_open; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 13331da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 13341da177e4SLinus Torvalds goto out; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 13371da177e4SLinus Torvalds 1338d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1339d4d9cdcbSTrond Myklebust * the dentry. */ 13403516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1341d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 134202a913a7STrond Myklebust goto out; 134302a913a7STrond Myklebust } 13441da177e4SLinus Torvalds 1345cd9a1c0eSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1346cd9a1c0eSTrond Myklebust res = ERR_CAST(ctx); 1347cd9a1c0eSTrond Myklebust if (IS_ERR(ctx)) 1348cd9a1c0eSTrond Myklebust goto out; 1349cd9a1c0eSTrond Myklebust 1350cd9a1c0eSTrond Myklebust open_flags = nd->intent.open.flags; 1351cd9a1c0eSTrond Myklebust if (nd->flags & LOOKUP_CREATE) { 1352cd9a1c0eSTrond Myklebust attr.ia_mode = nd->intent.open.create_mode; 1353cd9a1c0eSTrond Myklebust attr.ia_valid = ATTR_MODE; 1354cd9a1c0eSTrond Myklebust if (!IS_POSIXACL(dir)) 1355cd9a1c0eSTrond Myklebust attr.ia_mode &= ~current_umask(); 1356cd9a1c0eSTrond Myklebust } else { 1357898f635cSTrond Myklebust open_flags &= ~(O_EXCL | O_CREAT); 1358cd9a1c0eSTrond Myklebust attr.ia_valid = 0; 1359cd9a1c0eSTrond Myklebust } 1360cd9a1c0eSTrond Myklebust 13611da177e4SLinus Torvalds /* Open the file on the server */ 1362f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 13632b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr); 1364f46e0bd3STrond Myklebust if (IS_ERR(inode)) { 1365f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1366cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1367f46e0bd3STrond Myklebust switch (PTR_ERR(inode)) { 13681da177e4SLinus Torvalds /* Make a negative dentry */ 13691da177e4SLinus Torvalds case -ENOENT: 1370f46e0bd3STrond Myklebust d_add(dentry, NULL); 137102a913a7STrond Myklebust res = NULL; 137202a913a7STrond Myklebust goto out; 13731da177e4SLinus Torvalds /* This turned out not to be a regular file */ 13746f926b5bSTrond Myklebust case -ENOTDIR: 13756f926b5bSTrond Myklebust goto no_open; 13761da177e4SLinus Torvalds case -ELOOP: 13771da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 13781da177e4SLinus Torvalds goto no_open; 137923ebbd9aSTrond Myklebust /* case -EISDIR: */ 13801da177e4SLinus Torvalds /* case -EINVAL: */ 13811da177e4SLinus Torvalds default: 1382f46e0bd3STrond Myklebust res = ERR_CAST(inode); 13831da177e4SLinus Torvalds goto out; 13841da177e4SLinus Torvalds } 1385cd9a1c0eSTrond Myklebust } 1386f46e0bd3STrond Myklebust res = d_add_unique(dentry, inode); 1387898f635cSTrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1388f46e0bd3STrond Myklebust if (res != NULL) { 1389f46e0bd3STrond Myklebust dput(ctx->path.dentry); 1390f46e0bd3STrond Myklebust ctx->path.dentry = dget(res); 13911da177e4SLinus Torvalds dentry = res; 1392f46e0bd3STrond Myklebust } 1393898f635cSTrond Myklebust err = nfs_intent_set_file(nd, ctx); 1394898f635cSTrond Myklebust if (err < 0) { 1395898f635cSTrond Myklebust if (res != NULL) 1396898f635cSTrond Myklebust dput(res); 1397898f635cSTrond Myklebust return ERR_PTR(err); 1398898f635cSTrond Myklebust } 13991da177e4SLinus Torvalds out: 1400f46e0bd3STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14011da177e4SLinus Torvalds return res; 14021da177e4SLinus Torvalds no_open: 14031da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 14071da177e4SLinus Torvalds { 14081da177e4SLinus Torvalds struct dentry *parent = NULL; 14091da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14101da177e4SLinus Torvalds struct inode *dir; 1411b8d4caddSTrond Myklebust struct nfs_open_context *ctx; 14121da177e4SLinus Torvalds int openflags, ret = 0; 14131da177e4SLinus Torvalds 14141f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 14155584c306STrond Myklebust goto no_open; 14162b484297STrond Myklebust 14171da177e4SLinus Torvalds parent = dget_parent(dentry); 14181da177e4SLinus Torvalds dir = parent->d_inode; 14192b484297STrond Myklebust 14201da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 14211da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 14221da177e4SLinus Torvalds */ 1423216d5d06STrond Myklebust if (inode == NULL) { 1424216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1425216d5d06STrond Myklebust ret = 1; 14261da177e4SLinus Torvalds goto out; 1427216d5d06STrond Myklebust } 1428216d5d06STrond Myklebust 14291da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 14301da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 14315584c306STrond Myklebust goto no_open_dput; 14321da177e4SLinus Torvalds openflags = nd->intent.open.flags; 14331da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 14341da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 14355584c306STrond Myklebust goto no_open_dput; 14361da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 14370a377cffSTrond Myklebust openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); 14381da177e4SLinus Torvalds 1439b8d4caddSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1440b8d4caddSTrond Myklebust ret = PTR_ERR(ctx); 1441b8d4caddSTrond Myklebust if (IS_ERR(ctx)) 1442b8d4caddSTrond Myklebust goto out; 14431da177e4SLinus Torvalds /* 14441b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 14451da177e4SLinus Torvalds * operations that change the directory. We therefore save the 14461da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 14471da177e4SLinus Torvalds */ 14482b484297STrond Myklebust inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL); 1449535918f1STrond Myklebust if (IS_ERR(inode)) { 1450535918f1STrond Myklebust ret = PTR_ERR(inode); 1451535918f1STrond Myklebust switch (ret) { 1452535918f1STrond Myklebust case -EPERM: 1453535918f1STrond Myklebust case -EACCES: 1454535918f1STrond Myklebust case -EDQUOT: 1455535918f1STrond Myklebust case -ENOSPC: 1456535918f1STrond Myklebust case -EROFS: 1457535918f1STrond Myklebust goto out_put_ctx; 1458535918f1STrond Myklebust default: 1459535918f1STrond Myklebust goto out_drop; 1460535918f1STrond Myklebust } 1461535918f1STrond Myklebust } 1462535918f1STrond Myklebust iput(inode); 1463898f635cSTrond Myklebust if (inode != dentry->d_inode) 1464535918f1STrond Myklebust goto out_drop; 1465898f635cSTrond Myklebust 1466898f635cSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1467898f635cSTrond Myklebust ret = nfs_intent_set_file(nd, ctx); 1468898f635cSTrond Myklebust if (ret >= 0) 1469898f635cSTrond Myklebust ret = 1; 14701da177e4SLinus Torvalds out: 14711da177e4SLinus Torvalds dput(parent); 14721da177e4SLinus Torvalds return ret; 1473535918f1STrond Myklebust out_drop: 1474535918f1STrond Myklebust d_drop(dentry); 1475535918f1STrond Myklebust ret = 0; 1476535918f1STrond Myklebust out_put_ctx: 1477535918f1STrond Myklebust put_nfs_open_context(ctx); 1478535918f1STrond Myklebust goto out; 1479535918f1STrond Myklebust 14805584c306STrond Myklebust no_open_dput: 14811da177e4SLinus Torvalds dput(parent); 14825584c306STrond Myklebust no_open: 14831da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 14841da177e4SLinus Torvalds } 1485c0204fd2STrond Myklebust 1486c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, 1487c0204fd2STrond Myklebust struct nameidata *nd) 1488c0204fd2STrond Myklebust { 1489c0204fd2STrond Myklebust struct nfs_open_context *ctx = NULL; 1490c0204fd2STrond Myklebust struct iattr attr; 1491c0204fd2STrond Myklebust int error; 1492c0204fd2STrond Myklebust int open_flags = 0; 1493c0204fd2STrond Myklebust 1494c0204fd2STrond Myklebust dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 1495c0204fd2STrond Myklebust dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 1496c0204fd2STrond Myklebust 1497c0204fd2STrond Myklebust attr.ia_mode = mode; 1498c0204fd2STrond Myklebust attr.ia_valid = ATTR_MODE; 1499c0204fd2STrond Myklebust 1500c0204fd2STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) { 1501c0204fd2STrond Myklebust open_flags = nd->intent.open.flags; 1502c0204fd2STrond Myklebust 1503c0204fd2STrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1504c0204fd2STrond Myklebust error = PTR_ERR(ctx); 1505c0204fd2STrond Myklebust if (IS_ERR(ctx)) 1506898f635cSTrond Myklebust goto out_err_drop; 1507c0204fd2STrond Myklebust } 1508c0204fd2STrond Myklebust 1509c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx); 1510c0204fd2STrond Myklebust if (error != 0) 1511c0204fd2STrond Myklebust goto out_put_ctx; 1512898f635cSTrond Myklebust if (ctx != NULL) { 1513898f635cSTrond Myklebust error = nfs_intent_set_file(nd, ctx); 1514898f635cSTrond Myklebust if (error < 0) 1515898f635cSTrond Myklebust goto out_err; 1516898f635cSTrond Myklebust } 1517c0204fd2STrond Myklebust return 0; 1518c0204fd2STrond Myklebust out_put_ctx: 1519c0204fd2STrond Myklebust if (ctx != NULL) 1520c0204fd2STrond Myklebust put_nfs_open_context(ctx); 1521898f635cSTrond Myklebust out_err_drop: 1522c0204fd2STrond Myklebust d_drop(dentry); 1523898f635cSTrond Myklebust out_err: 1524c0204fd2STrond Myklebust return error; 1525c0204fd2STrond Myklebust } 1526c0204fd2STrond Myklebust 15271da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /* 15301da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 15311da177e4SLinus Torvalds */ 15321da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 15331da177e4SLinus Torvalds struct nfs_fattr *fattr) 15341da177e4SLinus Torvalds { 1535fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1536fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 15371da177e4SLinus Torvalds struct inode *inode; 15381da177e4SLinus Torvalds int error = -EACCES; 15391da177e4SLinus Torvalds 1540fab728e1STrond Myklebust d_drop(dentry); 1541fab728e1STrond Myklebust 15421da177e4SLinus Torvalds /* We may have been initialized further down */ 15431da177e4SLinus Torvalds if (dentry->d_inode) 1544fab728e1STrond Myklebust goto out; 15451da177e4SLinus Torvalds if (fhandle->size == 0) { 15461da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 15471da177e4SLinus Torvalds if (error) 1548fab728e1STrond Myklebust goto out_error; 15491da177e4SLinus Torvalds } 15505724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15511da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 15521da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 15538fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 15541da177e4SLinus Torvalds if (error < 0) 1555fab728e1STrond Myklebust goto out_error; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 155803f28e3aSTrond Myklebust error = PTR_ERR(inode); 155903f28e3aSTrond Myklebust if (IS_ERR(inode)) 1560fab728e1STrond Myklebust goto out_error; 1561fab728e1STrond Myklebust d_add(dentry, inode); 1562fab728e1STrond Myklebust out: 1563fab728e1STrond Myklebust dput(parent); 15641da177e4SLinus Torvalds return 0; 1565fab728e1STrond Myklebust out_error: 1566fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1567fab728e1STrond Myklebust dput(parent); 1568fab728e1STrond Myklebust return error; 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds /* 15721da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 15731da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 15741da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 15751da177e4SLinus Torvalds * reply path made it appear to have failed. 15761da177e4SLinus Torvalds */ 15771da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 15781da177e4SLinus Torvalds struct nameidata *nd) 15791da177e4SLinus Torvalds { 15801da177e4SLinus Torvalds struct iattr attr; 15811da177e4SLinus Torvalds int error; 15821da177e4SLinus Torvalds 15831e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 15841e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds attr.ia_mode = mode; 15871da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 15881da177e4SLinus Torvalds 1589c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL); 15901da177e4SLinus Torvalds if (error != 0) 15911da177e4SLinus Torvalds goto out_err; 15921da177e4SLinus Torvalds return 0; 15931da177e4SLinus Torvalds out_err: 15941da177e4SLinus Torvalds d_drop(dentry); 15951da177e4SLinus Torvalds return error; 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds /* 15991da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 16001da177e4SLinus Torvalds */ 16011da177e4SLinus Torvalds static int 16021da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 16031da177e4SLinus Torvalds { 16041da177e4SLinus Torvalds struct iattr attr; 16051da177e4SLinus Torvalds int status; 16061da177e4SLinus Torvalds 16071e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 16081e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 16111da177e4SLinus Torvalds return -EINVAL; 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds attr.ia_mode = mode; 16141da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 16171da177e4SLinus Torvalds if (status != 0) 16181da177e4SLinus Torvalds goto out_err; 16191da177e4SLinus Torvalds return 0; 16201da177e4SLinus Torvalds out_err: 16211da177e4SLinus Torvalds d_drop(dentry); 16221da177e4SLinus Torvalds return status; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds /* 16261da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 16271da177e4SLinus Torvalds */ 16281da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 16291da177e4SLinus Torvalds { 16301da177e4SLinus Torvalds struct iattr attr; 16311da177e4SLinus Torvalds int error; 16321da177e4SLinus Torvalds 16331e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 16341e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 16371da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 16401da177e4SLinus Torvalds if (error != 0) 16411da177e4SLinus Torvalds goto out_err; 16421da177e4SLinus Torvalds return 0; 16431da177e4SLinus Torvalds out_err: 16441da177e4SLinus Torvalds d_drop(dentry); 16451da177e4SLinus Torvalds return error; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 1648d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1649d45b9d8bSTrond Myklebust { 1650d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1651d45b9d8bSTrond Myklebust d_delete(dentry); 1652d45b9d8bSTrond Myklebust } 1653d45b9d8bSTrond Myklebust 16541da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 16551da177e4SLinus Torvalds { 16561da177e4SLinus Torvalds int error; 16571da177e4SLinus Torvalds 16581e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 16591e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 16621da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 16631da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1664ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1665d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1666d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds return error; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* 16721da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 16731da177e4SLinus Torvalds * and after checking that the file has only one user. 16741da177e4SLinus Torvalds * 16751da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 16761da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 16771da177e4SLinus Torvalds */ 16781da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 16791da177e4SLinus Torvalds { 16801da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 16811da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16821da177e4SLinus Torvalds int error = -EBUSY; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 16851da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 16881da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 16891da177e4SLinus Torvalds error = 0; 16901da177e4SLinus Torvalds goto out; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds if (inode != NULL) { 1694cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 16951da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 16961da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 16971da177e4SLinus Torvalds if (error == 0) 16981b83d707STrond Myklebust nfs_drop_nlink(inode); 16995ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 17001da177e4SLinus Torvalds } else 17011da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1702d45b9d8bSTrond Myklebust if (error == -ENOENT) 1703d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 17041da177e4SLinus Torvalds out: 17051da177e4SLinus Torvalds return error; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 17091da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 17101da177e4SLinus Torvalds * 17111da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 17121da177e4SLinus Torvalds */ 17131da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 17141da177e4SLinus Torvalds { 17151da177e4SLinus Torvalds int error; 17161da177e4SLinus Torvalds int need_rehash = 0; 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 17191da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds spin_lock(&dcache_lock); 17221da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 1723*b7ab39f6SNick Piggin if (dentry->d_count > 1) { 17241da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 17251da177e4SLinus Torvalds spin_unlock(&dcache_lock); 1726ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1727ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 17281da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 17291da177e4SLinus Torvalds return error; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 17321da177e4SLinus Torvalds __d_drop(dentry); 17331da177e4SLinus Torvalds need_rehash = 1; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 17361da177e4SLinus Torvalds spin_unlock(&dcache_lock); 17371da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1738d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 17391da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 17401da177e4SLinus Torvalds } else if (need_rehash) 17411da177e4SLinus Torvalds d_rehash(dentry); 17421da177e4SLinus Torvalds return error; 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 1745873101b3SChuck Lever /* 1746873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1747873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1748873101b3SChuck Lever * using prepare_write/commit_write. 1749873101b3SChuck Lever * 1750873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1751873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1752873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1753873101b3SChuck Lever * symlink request has completed on the server. 1754873101b3SChuck Lever * 1755873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1756873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1757873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1758873101b3SChuck Lever * and move the raw page into its mapping. 1759873101b3SChuck Lever */ 1760873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 17611da177e4SLinus Torvalds { 1762873101b3SChuck Lever struct pagevec lru_pvec; 1763873101b3SChuck Lever struct page *page; 1764873101b3SChuck Lever char *kaddr; 17651da177e4SLinus Torvalds struct iattr attr; 1766873101b3SChuck Lever unsigned int pathlen = strlen(symname); 17671da177e4SLinus Torvalds int error; 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 17701da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 17711da177e4SLinus Torvalds 1772873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1773873101b3SChuck Lever return -ENAMETOOLONG; 17741da177e4SLinus Torvalds 1775873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1776873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 17771da177e4SLinus Torvalds 177883d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 177976566991STrond Myklebust if (!page) 1780873101b3SChuck Lever return -ENOMEM; 1781873101b3SChuck Lever 1782873101b3SChuck Lever kaddr = kmap_atomic(page, KM_USER0); 1783873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1784873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1785873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1786873101b3SChuck Lever kunmap_atomic(kaddr, KM_USER0); 1787873101b3SChuck Lever 178894a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1789873101b3SChuck Lever if (error != 0) { 1790873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1791873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1792873101b3SChuck Lever dentry->d_name.name, symname, error); 17931da177e4SLinus Torvalds d_drop(dentry); 1794873101b3SChuck Lever __free_page(page); 17951da177e4SLinus Torvalds return error; 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 1798873101b3SChuck Lever /* 1799873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1800873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1801873101b3SChuck Lever */ 1802873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1803873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1804873101b3SChuck Lever GFP_KERNEL)) { 180539cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 18064f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1807873101b3SChuck Lever SetPageUptodate(page); 1808873101b3SChuck Lever unlock_page(page); 1809873101b3SChuck Lever } else 1810873101b3SChuck Lever __free_page(page); 1811873101b3SChuck Lever 1812873101b3SChuck Lever return 0; 1813873101b3SChuck Lever } 1814873101b3SChuck Lever 18151da177e4SLinus Torvalds static int 18161da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 18171da177e4SLinus Torvalds { 18181da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 18191da177e4SLinus Torvalds int error; 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 18221da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18231da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 18241da177e4SLinus Torvalds 18259a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 18269a3936aaSTrond Myklebust 18279697d234STrond Myklebust d_drop(dentry); 18281da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1829cf809556STrond Myklebust if (error == 0) { 18307de9c6eeSAl Viro ihold(inode); 18319697d234STrond Myklebust d_add(dentry, inode); 1832cf809556STrond Myklebust } 18331da177e4SLinus Torvalds return error; 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds /* 18371da177e4SLinus Torvalds * RENAME 18381da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 18391da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 18401da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 18411da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 18421da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 18431da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 18441da177e4SLinus Torvalds * 18451da177e4SLinus Torvalds * FIXED. 18461da177e4SLinus Torvalds * 18471da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 18481da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 18491da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 18501da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 18511da177e4SLinus Torvalds * using the inode layer 18521da177e4SLinus Torvalds * 18531da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 18541da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 18551da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 18561da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 18571da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 18581da177e4SLinus Torvalds * the rename. 18591da177e4SLinus Torvalds */ 18601da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 18611da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 18621da177e4SLinus Torvalds { 18631da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 18641da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 18651da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 18661da177e4SLinus Torvalds int error = -EBUSY; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 18691da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 18701da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 1871*b7ab39f6SNick Piggin new_dentry->d_count); 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* 187428f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 187528f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 187628f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 187728f79a1aSMiklos Szeredi * the new target. 18781da177e4SLinus Torvalds */ 187927226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 188027226104SMiklos Szeredi /* 188127226104SMiklos Szeredi * To prevent any new references to the target during the 188227226104SMiklos Szeredi * rename, we unhash the dentry in advance. 188327226104SMiklos Szeredi */ 188427226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 188527226104SMiklos Szeredi d_drop(new_dentry); 188627226104SMiklos Szeredi rehash = new_dentry; 188727226104SMiklos Szeredi } 188827226104SMiklos Szeredi 1889*b7ab39f6SNick Piggin if (new_dentry->d_count > 2) { 18901da177e4SLinus Torvalds int err; 189127226104SMiklos Szeredi 18921da177e4SLinus Torvalds /* copy the target dentry's name */ 18931da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 18941da177e4SLinus Torvalds &new_dentry->d_name); 18951da177e4SLinus Torvalds if (!dentry) 18961da177e4SLinus Torvalds goto out; 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds /* silly-rename the existing target ... */ 18991da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 190024e93025SMiklos Szeredi if (err) 19011da177e4SLinus Torvalds goto out; 190224e93025SMiklos Szeredi 190324e93025SMiklos Szeredi new_dentry = dentry; 190456335936SOGAWA Hirofumi rehash = NULL; 190524e93025SMiklos Szeredi new_inode = NULL; 1906b1e4adf4STrond Myklebust } 190727226104SMiklos Szeredi } 19081da177e4SLinus Torvalds 1909cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 1910b1e4adf4STrond Myklebust if (new_inode != NULL) 191124174119STrond Myklebust nfs_inode_return_delegation(new_inode); 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 19141da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 19155ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 19161da177e4SLinus Torvalds out: 19171da177e4SLinus Torvalds if (rehash) 19181da177e4SLinus Torvalds d_rehash(rehash); 19191da177e4SLinus Torvalds if (!error) { 1920b1e4adf4STrond Myklebust if (new_inode != NULL) 1921b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 19221da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 19238fb559f8SChuck Lever nfs_set_verifier(new_dentry, 19248fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1925d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 1926d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds /* new dentry created? */ 19291da177e4SLinus Torvalds if (dentry) 19301da177e4SLinus Torvalds dput(dentry); 19311da177e4SLinus Torvalds return error; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 1934cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 1935cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 1936cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 1937cfcea3e8STrond Myklebust 19381c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 19391c3c07e9STrond Myklebust { 19401c3c07e9STrond Myklebust put_rpccred(entry->cred); 19411c3c07e9STrond Myklebust kfree(entry); 1942cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 1943cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 1944cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 19451c3c07e9STrond Myklebust } 19461c3c07e9STrond Myklebust 19471a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 19481a81bb8aSTrond Myklebust { 19491a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 19501a81bb8aSTrond Myklebust 19511a81bb8aSTrond Myklebust while (!list_empty(head)) { 19521a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 19531a81bb8aSTrond Myklebust list_del(&cache->lru); 19541a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 19551a81bb8aSTrond Myklebust } 19561a81bb8aSTrond Myklebust } 19571a81bb8aSTrond Myklebust 19587f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) 1959979df72eSTrond Myklebust { 1960979df72eSTrond Myklebust LIST_HEAD(head); 1961aa510da5STrond Myklebust struct nfs_inode *nfsi, *next; 1962979df72eSTrond Myklebust struct nfs_access_entry *cache; 1963979df72eSTrond Myklebust 196461d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 196561d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 19669c7e7e23STrond Myklebust 1967a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 1968aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 1969979df72eSTrond Myklebust struct inode *inode; 1970979df72eSTrond Myklebust 1971979df72eSTrond Myklebust if (nr_to_scan-- == 0) 1972979df72eSTrond Myklebust break; 19739c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 1974979df72eSTrond Myklebust spin_lock(&inode->i_lock); 1975979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 1976979df72eSTrond Myklebust goto remove_lru_entry; 1977979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 1978979df72eSTrond Myklebust struct nfs_access_entry, lru); 1979979df72eSTrond Myklebust list_move(&cache->lru, &head); 1980979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1981979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 1982979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 1983979df72eSTrond Myklebust &nfs_access_lru_list); 1984979df72eSTrond Myklebust else { 1985979df72eSTrond Myklebust remove_lru_entry: 1986979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 19879c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 1988979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 19899c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 1990979df72eSTrond Myklebust } 199159844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 1992979df72eSTrond Myklebust } 1993979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 19941a81bb8aSTrond Myklebust nfs_access_free_list(&head); 1995979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 1996979df72eSTrond Myklebust } 1997979df72eSTrond Myklebust 19981a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 19991c3c07e9STrond Myklebust { 20001c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 20011a81bb8aSTrond Myklebust struct rb_node *n; 20021c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20031c3c07e9STrond Myklebust 20041c3c07e9STrond Myklebust /* Unhook entries from the cache */ 20051c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 20061c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20071c3c07e9STrond Myklebust rb_erase(n, root_node); 20081a81bb8aSTrond Myklebust list_move(&entry->lru, head); 20091c3c07e9STrond Myklebust } 20101c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 20111c3c07e9STrond Myklebust } 20121c3c07e9STrond Myklebust 20131c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 20141c3c07e9STrond Myklebust { 20151a81bb8aSTrond Myklebust LIST_HEAD(head); 20161a81bb8aSTrond Myklebust 20171a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 20181a81bb8aSTrond Myklebust return; 2019cfcea3e8STrond Myklebust /* Remove from global LRU init */ 2020cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 20211a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2022cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2023cfcea3e8STrond Myklebust 20241c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20251a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 20261a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 20271a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 20281a81bb8aSTrond Myklebust nfs_access_free_list(&head); 20291c3c07e9STrond Myklebust } 20301c3c07e9STrond Myklebust 20311c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 20321c3c07e9STrond Myklebust { 20331c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 20341c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20351c3c07e9STrond Myklebust 20361c3c07e9STrond Myklebust while (n != NULL) { 20371c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 20381c3c07e9STrond Myklebust 20391c3c07e9STrond Myklebust if (cred < entry->cred) 20401c3c07e9STrond Myklebust n = n->rb_left; 20411c3c07e9STrond Myklebust else if (cred > entry->cred) 20421c3c07e9STrond Myklebust n = n->rb_right; 20431c3c07e9STrond Myklebust else 20441c3c07e9STrond Myklebust return entry; 20451c3c07e9STrond Myklebust } 20461c3c07e9STrond Myklebust return NULL; 20471c3c07e9STrond Myklebust } 20481c3c07e9STrond Myklebust 2049af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 20501da177e4SLinus Torvalds { 205155296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 20521c3c07e9STrond Myklebust struct nfs_access_entry *cache; 20531c3c07e9STrond Myklebust int err = -ENOENT; 20541da177e4SLinus Torvalds 20551c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20561c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 20571c3c07e9STrond Myklebust goto out_zap; 20581c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 20591c3c07e9STrond Myklebust if (cache == NULL) 20601c3c07e9STrond Myklebust goto out; 2061b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 206264672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 20631c3c07e9STrond Myklebust goto out_stale; 20641c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 20651c3c07e9STrond Myklebust res->cred = cache->cred; 20661c3c07e9STrond Myklebust res->mask = cache->mask; 2067cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 20681c3c07e9STrond Myklebust err = 0; 20691c3c07e9STrond Myklebust out: 20701c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20711c3c07e9STrond Myklebust return err; 20721c3c07e9STrond Myklebust out_stale: 20731c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 2074cfcea3e8STrond Myklebust list_del(&cache->lru); 20751c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20761c3c07e9STrond Myklebust nfs_access_free_entry(cache); 20771da177e4SLinus Torvalds return -ENOENT; 20781c3c07e9STrond Myklebust out_zap: 20791a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 20801a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 20811c3c07e9STrond Myklebust return -ENOENT; 20821c3c07e9STrond Myklebust } 20831c3c07e9STrond Myklebust 20841c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 20851c3c07e9STrond Myklebust { 2086cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 2087cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 20881c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 20891c3c07e9STrond Myklebust struct rb_node *parent = NULL; 20901c3c07e9STrond Myklebust struct nfs_access_entry *entry; 20911c3c07e9STrond Myklebust 20921c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 20931c3c07e9STrond Myklebust while (*p != NULL) { 20941c3c07e9STrond Myklebust parent = *p; 20951c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 20961c3c07e9STrond Myklebust 20971c3c07e9STrond Myklebust if (set->cred < entry->cred) 20981c3c07e9STrond Myklebust p = &parent->rb_left; 20991c3c07e9STrond Myklebust else if (set->cred > entry->cred) 21001c3c07e9STrond Myklebust p = &parent->rb_right; 21011c3c07e9STrond Myklebust else 21021c3c07e9STrond Myklebust goto found; 21031c3c07e9STrond Myklebust } 21041c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 21051c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2106cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 21071c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21081c3c07e9STrond Myklebust return; 21091c3c07e9STrond Myklebust found: 21101c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2111cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2112cfcea3e8STrond Myklebust list_del(&entry->lru); 21131c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 21141c3c07e9STrond Myklebust nfs_access_free_entry(entry); 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds 2117af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 21181da177e4SLinus Torvalds { 21191c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 21201c3c07e9STrond Myklebust if (cache == NULL) 21211c3c07e9STrond Myklebust return; 21221c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 21231da177e4SLinus Torvalds cache->jiffies = set->jiffies; 21241c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 21251da177e4SLinus Torvalds cache->mask = set->mask; 21261c3c07e9STrond Myklebust 21271c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2128cfcea3e8STrond Myklebust 2129cfcea3e8STrond Myklebust /* Update accounting */ 2130cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 2131cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 2132cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 2133cfcea3e8STrond Myklebust 2134cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 21351a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2136cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 21371a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 21381a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 21391a81bb8aSTrond Myklebust &nfs_access_lru_list); 2140cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2141cfcea3e8STrond Myklebust } 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 21451da177e4SLinus Torvalds { 21461da177e4SLinus Torvalds struct nfs_access_entry cache; 21471da177e4SLinus Torvalds int status; 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 21501da177e4SLinus Torvalds if (status == 0) 21511da177e4SLinus Torvalds goto out; 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 21541da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 21551da177e4SLinus Torvalds cache.cred = cred; 21561da177e4SLinus Torvalds cache.jiffies = jiffies; 21571da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2158a71ee337SSuresh Jayaraman if (status != 0) { 2159a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2160a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2161a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2162a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2163a71ee337SSuresh Jayaraman } 21641da177e4SLinus Torvalds return status; 2165a71ee337SSuresh Jayaraman } 21661da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 21671da177e4SLinus Torvalds out: 2168e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 21691da177e4SLinus Torvalds return 0; 21701da177e4SLinus Torvalds return -EACCES; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 2173af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2174af22f94aSTrond Myklebust { 2175af22f94aSTrond Myklebust int mask = 0; 2176af22f94aSTrond Myklebust 2177af22f94aSTrond Myklebust if (openflags & FMODE_READ) 2178af22f94aSTrond Myklebust mask |= MAY_READ; 2179af22f94aSTrond Myklebust if (openflags & FMODE_WRITE) 2180af22f94aSTrond Myklebust mask |= MAY_WRITE; 2181af22f94aSTrond Myklebust if (openflags & FMODE_EXEC) 2182af22f94aSTrond Myklebust mask |= MAY_EXEC; 2183af22f94aSTrond Myklebust return mask; 2184af22f94aSTrond Myklebust } 2185af22f94aSTrond Myklebust 2186af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2187af22f94aSTrond Myklebust { 2188af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2189af22f94aSTrond Myklebust } 2190af22f94aSTrond Myklebust 2191e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask) 21921da177e4SLinus Torvalds { 21931da177e4SLinus Torvalds struct rpc_cred *cred; 21941da177e4SLinus Torvalds int res = 0; 21951da177e4SLinus Torvalds 219691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 219791d5b470SChuck Lever 2198e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 21991da177e4SLinus Torvalds goto out; 22001da177e4SLinus Torvalds /* Is this sys_access() ? */ 22019cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 22021da177e4SLinus Torvalds goto force_lookup; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 22051da177e4SLinus Torvalds case S_IFLNK: 22061da177e4SLinus Torvalds goto out; 22071da177e4SLinus Torvalds case S_IFREG: 22081da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 22091da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 22107ee2cb7fSFrank Filz && (mask & MAY_OPEN) 22117ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 22121da177e4SLinus Torvalds goto out; 22131da177e4SLinus Torvalds break; 22141da177e4SLinus Torvalds case S_IFDIR: 22151da177e4SLinus Torvalds /* 22161da177e4SLinus Torvalds * Optimize away all write operations, since the server 22171da177e4SLinus Torvalds * will check permissions when we perform the op. 22181da177e4SLinus Torvalds */ 22191da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 22201da177e4SLinus Torvalds goto out; 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds force_lookup: 22241da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 22251da177e4SLinus Torvalds goto out_notsup; 22261da177e4SLinus Torvalds 222798a8e323STrond Myklebust cred = rpc_lookup_cred(); 22281da177e4SLinus Torvalds if (!IS_ERR(cred)) { 22291da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 22301da177e4SLinus Torvalds put_rpccred(cred); 22311da177e4SLinus Torvalds } else 22321da177e4SLinus Torvalds res = PTR_ERR(cred); 22331da177e4SLinus Torvalds out: 2234f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2235f696a365SMiklos Szeredi res = -EACCES; 2236f696a365SMiklos Szeredi 22371e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 22381e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 22391da177e4SLinus Torvalds return res; 22401da177e4SLinus Torvalds out_notsup: 22411da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 22421da177e4SLinus Torvalds if (res == 0) 22431da177e4SLinus Torvalds res = generic_permission(inode, mask, NULL); 22441e7cb3dcSChuck Lever goto out; 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds /* 22481da177e4SLinus Torvalds * Local variables: 22491da177e4SLinus Torvalds * version-control: t 22501da177e4SLinus Torvalds * kept-new-versions: 5 22511da177e4SLinus Torvalds * End: 22521da177e4SLinus Torvalds */ 2253