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> 361da177e4SLinus Torvalds 374ce79717STrond Myklebust #include "nfs4_fs.h" 381da177e4SLinus Torvalds #include "delegation.h" 3991d5b470SChuck Lever #include "iostat.h" 404c30d56eSAdrian Bunk #include "internal.h" 41*cd9a1c0eSTrond Myklebust #include "fscache.h" 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 461da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 471da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 481da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 491da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 501da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 511da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 521da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 531da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 541da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 551da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 561da177e4SLinus Torvalds struct inode *, struct dentry *); 577ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int); 58f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 591da177e4SLinus Torvalds 604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 61f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 621da177e4SLinus Torvalds .read = generic_read_dir, 631da177e4SLinus Torvalds .readdir = nfs_readdir, 641da177e4SLinus Torvalds .open = nfs_opendir, 651da177e4SLinus Torvalds .release = nfs_release, 661da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 671da177e4SLinus Torvalds }; 681da177e4SLinus Torvalds 6992e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = { 701da177e4SLinus Torvalds .create = nfs_create, 711da177e4SLinus Torvalds .lookup = nfs_lookup, 721da177e4SLinus Torvalds .link = nfs_link, 731da177e4SLinus Torvalds .unlink = nfs_unlink, 741da177e4SLinus Torvalds .symlink = nfs_symlink, 751da177e4SLinus Torvalds .mkdir = nfs_mkdir, 761da177e4SLinus Torvalds .rmdir = nfs_rmdir, 771da177e4SLinus Torvalds .mknod = nfs_mknod, 781da177e4SLinus Torvalds .rename = nfs_rename, 791da177e4SLinus Torvalds .permission = nfs_permission, 801da177e4SLinus Torvalds .getattr = nfs_getattr, 811da177e4SLinus Torvalds .setattr = nfs_setattr, 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 84b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 8592e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = { 86b7fa0554SAndreas Gruenbacher .create = nfs_create, 87b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 88b7fa0554SAndreas Gruenbacher .link = nfs_link, 89b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 90b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 91b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 92b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 93b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 94b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 95b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 96b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 97b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 98b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 99b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 100b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 101b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 102b7fa0554SAndreas Gruenbacher }; 103b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 104b7fa0554SAndreas Gruenbacher 1051da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 10892e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = { 1091da177e4SLinus Torvalds .create = nfs_create, 1101da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1111da177e4SLinus Torvalds .link = nfs_link, 1121da177e4SLinus Torvalds .unlink = nfs_unlink, 1131da177e4SLinus Torvalds .symlink = nfs_symlink, 1141da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1151da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1161da177e4SLinus Torvalds .mknod = nfs_mknod, 1171da177e4SLinus Torvalds .rename = nfs_rename, 1181da177e4SLinus Torvalds .permission = nfs_permission, 1191da177e4SLinus Torvalds .getattr = nfs_getattr, 1201da177e4SLinus Torvalds .setattr = nfs_setattr, 1216b3b5496SJ. Bruce Fields .getxattr = nfs4_getxattr, 1226b3b5496SJ. Bruce Fields .setxattr = nfs4_setxattr, 1236b3b5496SJ. Bruce Fields .listxattr = nfs4_listxattr, 1241da177e4SLinus Torvalds }; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * Open file 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds static int 1321da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1331da177e4SLinus Torvalds { 1347451c4f0SCarsten Otte int res; 1351da177e4SLinus Torvalds 1366da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 137cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 138cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 139cc0dd2d1SChuck Lever 140cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1411e7cb3dcSChuck Lever 1421da177e4SLinus Torvalds /* Call generic open code in order to cache credentials */ 1431da177e4SLinus Torvalds res = nfs_open(inode, filp); 144f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 145f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 146f5a73672SNeil Brown * have been called, so we need to refresh the 147f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 148f5a73672SNeil Brown */ 149f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 150f5a73672SNeil Brown } 1511da177e4SLinus Torvalds return res; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1540dbb4c67SAl Viro typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int); 1551da177e4SLinus Torvalds typedef struct { 1561da177e4SLinus Torvalds struct file *file; 1571da177e4SLinus Torvalds struct page *page; 1581da177e4SLinus Torvalds unsigned long page_index; 1590dbb4c67SAl Viro __be32 *ptr; 160f0dd2136STrond Myklebust u64 *dir_cookie; 161f0dd2136STrond Myklebust loff_t current_index; 1621da177e4SLinus Torvalds struct nfs_entry *entry; 1631da177e4SLinus Torvalds decode_dirent_t decode; 1641da177e4SLinus Torvalds int plus; 1651f4eab7eSNeil Brown unsigned long timestamp; 1664704f0e2STrond Myklebust unsigned long gencount; 1671f4eab7eSNeil Brown int timestamp_valid; 1681da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent 1711da177e4SLinus Torvalds * data directly in the page cache. 1721da177e4SLinus Torvalds * 1731da177e4SLinus Torvalds * Inode invalidation due to refresh etc. takes care of 1741da177e4SLinus Torvalds * _everything_, no sloppy entry flushing logic, no extraneous 1751da177e4SLinus Torvalds * copying, network direct to page cache, the way it was meant 1761da177e4SLinus Torvalds * to be. 1771da177e4SLinus Torvalds * 1781da177e4SLinus Torvalds * NOTE: Dirent information verification is done always by the 1791da177e4SLinus Torvalds * page-in of the RPC reply, nowhere else, this simplies 1801da177e4SLinus Torvalds * things substantially. 1811da177e4SLinus Torvalds */ 1821da177e4SLinus Torvalds static 1831da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds struct file *file = desc->file; 18601cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 1871da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 1884704f0e2STrond Myklebust unsigned long timestamp, gencount; 1891da177e4SLinus Torvalds int error; 1901da177e4SLinus Torvalds 1911e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n", 1923110ff80SHarvey Harrison __func__, (long long)desc->entry->cookie, 1931e7cb3dcSChuck Lever page->index); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds again: 1961da177e4SLinus Torvalds timestamp = jiffies; 1974704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 19801cce933SJosef "Jeff" Sipek error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page, 1991da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 2001da177e4SLinus Torvalds if (error < 0) { 2011da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 2021da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 2031da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 2043a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 2051da177e4SLinus Torvalds desc->plus = 0; 2061da177e4SLinus Torvalds goto again; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds goto error; 2091da177e4SLinus Torvalds } 2101f4eab7eSNeil Brown desc->timestamp = timestamp; 2114704f0e2STrond Myklebust desc->gencount = gencount; 2121f4eab7eSNeil Brown desc->timestamp_valid = 1; 2131da177e4SLinus Torvalds SetPageUptodate(page); 2141da177e4SLinus Torvalds /* Ensure consistent page alignment of the data. 2151da177e4SLinus Torvalds * Note: assumes we have exclusive access to this mapping either 2161b1dcc1bSJes Sorensen * through inode->i_mutex or some other mechanism. 2171da177e4SLinus Torvalds */ 2182aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 219cd9ae2b6STrond Myklebust /* Should never happen */ 220cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 221cd9ae2b6STrond Myklebust } 2221da177e4SLinus Torvalds unlock_page(page); 2231da177e4SLinus Torvalds return 0; 2241da177e4SLinus Torvalds error: 2251da177e4SLinus Torvalds unlock_page(page); 2261da177e4SLinus Torvalds return -EIO; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static inline 2301da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc) 2311da177e4SLinus Torvalds { 2320dbb4c67SAl Viro __be32 *p = desc->ptr; 2331da177e4SLinus Torvalds p = desc->decode(p, desc->entry, desc->plus); 2341da177e4SLinus Torvalds if (IS_ERR(p)) 2351da177e4SLinus Torvalds return PTR_ERR(p); 2361da177e4SLinus Torvalds desc->ptr = p; 2374704f0e2STrond Myklebust if (desc->timestamp_valid) { 2381f4eab7eSNeil Brown desc->entry->fattr->time_start = desc->timestamp; 2394704f0e2STrond Myklebust desc->entry->fattr->gencount = desc->gencount; 2404704f0e2STrond Myklebust } else 2411f4eab7eSNeil Brown desc->entry->fattr->valid &= ~NFS_ATTR_FATTR; 2421da177e4SLinus Torvalds return 0; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds static inline 2461da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc) 2471da177e4SLinus Torvalds { 2481da177e4SLinus Torvalds kunmap(desc->page); 2491da177e4SLinus Torvalds page_cache_release(desc->page); 2501da177e4SLinus Torvalds desc->page = NULL; 2511da177e4SLinus Torvalds desc->ptr = NULL; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /* 2551da177e4SLinus Torvalds * Given a pointer to a buffer that has already been filled by a call 256f0dd2136STrond Myklebust * to readdir, find the next entry with cookie '*desc->dir_cookie'. 2571da177e4SLinus Torvalds * 2581da177e4SLinus Torvalds * If the end of the buffer has been reached, return -EAGAIN, if not, 2591da177e4SLinus Torvalds * return the offset within the buffer of the next entry to be 2601da177e4SLinus Torvalds * read. 2611da177e4SLinus Torvalds */ 2621da177e4SLinus Torvalds static inline 26300a92642SOlivier Galibert int find_dirent(nfs_readdir_descriptor_t *desc) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 2661da177e4SLinus Torvalds int loop_count = 0, 2671da177e4SLinus Torvalds status; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds while((status = dir_decode(desc)) == 0) { 2701e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n", 2713110ff80SHarvey Harrison __func__, (unsigned long long)entry->cookie); 272f0dd2136STrond Myklebust if (entry->prev_cookie == *desc->dir_cookie) 2731da177e4SLinus Torvalds break; 2741da177e4SLinus Torvalds if (loop_count++ > 200) { 2751da177e4SLinus Torvalds loop_count = 0; 2761da177e4SLinus Torvalds schedule(); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds return status; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* 28300a92642SOlivier Galibert * Given a pointer to a buffer that has already been filled by a call 284f0dd2136STrond Myklebust * to readdir, find the entry at offset 'desc->file->f_pos'. 28500a92642SOlivier Galibert * 28600a92642SOlivier Galibert * If the end of the buffer has been reached, return -EAGAIN, if not, 28700a92642SOlivier Galibert * return the offset within the buffer of the next entry to be 28800a92642SOlivier Galibert * read. 28900a92642SOlivier Galibert */ 29000a92642SOlivier Galibert static inline 29100a92642SOlivier Galibert int find_dirent_index(nfs_readdir_descriptor_t *desc) 29200a92642SOlivier Galibert { 29300a92642SOlivier Galibert struct nfs_entry *entry = desc->entry; 29400a92642SOlivier Galibert int loop_count = 0, 29500a92642SOlivier Galibert status; 29600a92642SOlivier Galibert 29700a92642SOlivier Galibert for(;;) { 29800a92642SOlivier Galibert status = dir_decode(desc); 29900a92642SOlivier Galibert if (status) 30000a92642SOlivier Galibert break; 30100a92642SOlivier Galibert 3021e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n", 3031e7cb3dcSChuck Lever (unsigned long long)entry->cookie, desc->current_index); 30400a92642SOlivier Galibert 305f0dd2136STrond Myklebust if (desc->file->f_pos == desc->current_index) { 306f0dd2136STrond Myklebust *desc->dir_cookie = entry->cookie; 30700a92642SOlivier Galibert break; 30800a92642SOlivier Galibert } 30900a92642SOlivier Galibert desc->current_index++; 31000a92642SOlivier Galibert if (loop_count++ > 200) { 31100a92642SOlivier Galibert loop_count = 0; 31200a92642SOlivier Galibert schedule(); 31300a92642SOlivier Galibert } 31400a92642SOlivier Galibert } 31500a92642SOlivier Galibert return status; 31600a92642SOlivier Galibert } 31700a92642SOlivier Galibert 31800a92642SOlivier Galibert /* 31900a92642SOlivier Galibert * Find the given page, and call find_dirent() or find_dirent_index in 32000a92642SOlivier Galibert * order to try to return the next entry. 3211da177e4SLinus Torvalds */ 3221da177e4SLinus Torvalds static inline 3231da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc) 3241da177e4SLinus Torvalds { 32501cce933SJosef "Jeff" Sipek struct inode *inode = desc->file->f_path.dentry->d_inode; 3261da177e4SLinus Torvalds struct page *page; 3271da177e4SLinus Torvalds int status; 3281da177e4SLinus Torvalds 3291e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n", 3303110ff80SHarvey Harrison __func__, desc->page_index, 3311e7cb3dcSChuck Lever (long long) *desc->dir_cookie); 3321da177e4SLinus Torvalds 3331f4eab7eSNeil Brown /* If we find the page in the page_cache, we cannot be sure 3341f4eab7eSNeil Brown * how fresh the data is, so we will ignore readdir_plus attributes. 3351f4eab7eSNeil Brown */ 3361f4eab7eSNeil Brown desc->timestamp_valid = 0; 3371da177e4SLinus Torvalds page = read_cache_page(inode->i_mapping, desc->page_index, 3381da177e4SLinus Torvalds (filler_t *)nfs_readdir_filler, desc); 3391da177e4SLinus Torvalds if (IS_ERR(page)) { 3401da177e4SLinus Torvalds status = PTR_ERR(page); 3411da177e4SLinus Torvalds goto out; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* NOTE: Someone else may have changed the READDIRPLUS flag */ 3451da177e4SLinus Torvalds desc->page = page; 3461da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 347f0dd2136STrond Myklebust if (*desc->dir_cookie != 0) 34800a92642SOlivier Galibert status = find_dirent(desc); 34900a92642SOlivier Galibert else 35000a92642SOlivier Galibert status = find_dirent_index(desc); 3511da177e4SLinus Torvalds if (status < 0) 3521da177e4SLinus Torvalds dir_page_release(desc); 3531da177e4SLinus Torvalds out: 3543110ff80SHarvey Harrison dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 3551da177e4SLinus Torvalds return status; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds /* 3591da177e4SLinus Torvalds * Recurse through the page cache pages, and return a 3601da177e4SLinus Torvalds * filled nfs_entry structure of the next directory entry if possible. 3611da177e4SLinus Torvalds * 362f0dd2136STrond Myklebust * The target for the search is '*desc->dir_cookie' if non-0, 363f0dd2136STrond Myklebust * 'desc->file->f_pos' otherwise 3641da177e4SLinus Torvalds */ 3651da177e4SLinus Torvalds static inline 3661da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds int loop_count = 0; 3691da177e4SLinus Torvalds int res; 3701da177e4SLinus Torvalds 37100a92642SOlivier Galibert /* Always search-by-index from the beginning of the cache */ 372f0dd2136STrond Myklebust if (*desc->dir_cookie == 0) { 3731e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n", 3741e7cb3dcSChuck Lever (long long)desc->file->f_pos); 37500a92642SOlivier Galibert desc->page_index = 0; 37600a92642SOlivier Galibert desc->entry->cookie = desc->entry->prev_cookie = 0; 37700a92642SOlivier Galibert desc->entry->eof = 0; 37800a92642SOlivier Galibert desc->current_index = 0; 379f0dd2136STrond Myklebust } else 3801e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", 3811e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 38200a92642SOlivier Galibert 3831da177e4SLinus Torvalds for (;;) { 3841da177e4SLinus Torvalds res = find_dirent_page(desc); 3851da177e4SLinus Torvalds if (res != -EAGAIN) 3861da177e4SLinus Torvalds break; 3871da177e4SLinus Torvalds /* Align to beginning of next page */ 3881da177e4SLinus Torvalds desc->page_index ++; 3891da177e4SLinus Torvalds if (loop_count++ > 200) { 3901da177e4SLinus Torvalds loop_count = 0; 3911da177e4SLinus Torvalds schedule(); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds } 3941e7cb3dcSChuck Lever 3953110ff80SHarvey Harrison dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res); 3961da177e4SLinus Torvalds return res; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 4071da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 4081da177e4SLinus Torvalds */ 4091da177e4SLinus Torvalds static 4101da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 4111da177e4SLinus Torvalds filldir_t filldir) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct file *file = desc->file; 4141da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 4151da177e4SLinus Torvalds struct dentry *dentry = NULL; 4164e769b93SPeter Staubach u64 fileid; 4171da177e4SLinus Torvalds int loop_count = 0, 4181da177e4SLinus Torvalds res; 4191da177e4SLinus Torvalds 4201e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", 4211e7cb3dcSChuck Lever (unsigned long long)entry->cookie); 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds for(;;) { 4241da177e4SLinus Torvalds unsigned d_type = DT_UNKNOWN; 4251da177e4SLinus Torvalds /* Note: entry->prev_cookie contains the cookie for 4261da177e4SLinus Torvalds * retrieving the current dirent on the server */ 4274e769b93SPeter Staubach fileid = entry->ino; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* Get a dentry if we have one */ 4301da177e4SLinus Torvalds if (dentry != NULL) 4311da177e4SLinus Torvalds dput(dentry); 4321da177e4SLinus Torvalds dentry = nfs_readdir_lookup(desc); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* Use readdirplus info */ 4351da177e4SLinus Torvalds if (dentry != NULL && dentry->d_inode != NULL) { 4361da177e4SLinus Torvalds d_type = dt_type(dentry->d_inode); 4374e769b93SPeter Staubach fileid = NFS_FILEID(dentry->d_inode); 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds res = filldir(dirent, entry->name, entry->len, 441f43bf0beSTrond Myklebust file->f_pos, nfs_compat_user_ino64(fileid), 442f43bf0beSTrond Myklebust d_type); 4431da177e4SLinus Torvalds if (res < 0) 4441da177e4SLinus Torvalds break; 44500a92642SOlivier Galibert file->f_pos++; 446f0dd2136STrond Myklebust *desc->dir_cookie = entry->cookie; 4471da177e4SLinus Torvalds if (dir_decode(desc) != 0) { 4481da177e4SLinus Torvalds desc->page_index ++; 4491da177e4SLinus Torvalds break; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds if (loop_count++ > 200) { 4521da177e4SLinus Torvalds loop_count = 0; 4531da177e4SLinus Torvalds schedule(); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds dir_page_release(desc); 4571da177e4SLinus Torvalds if (dentry != NULL) 4581da177e4SLinus Torvalds dput(dentry); 4591e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 4601e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 4611da177e4SLinus Torvalds return res; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 4661da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 4671da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 4681da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 4691da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 4701da177e4SLinus Torvalds * 4711da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 4721da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 4731da177e4SLinus Torvalds * we should already have a complete representation of the 4741da177e4SLinus Torvalds * directory in the page cache by the time we get here. 4751da177e4SLinus Torvalds */ 4761da177e4SLinus Torvalds static inline 4771da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 4781da177e4SLinus Torvalds filldir_t filldir) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds struct file *file = desc->file; 48101cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 4821da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 4831da177e4SLinus Torvalds struct page *page = NULL; 4841da177e4SLinus Torvalds int status; 4854704f0e2STrond Myklebust unsigned long timestamp, gencount; 4861da177e4SLinus Torvalds 4871e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 4881e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 4911da177e4SLinus Torvalds if (!page) { 4921da177e4SLinus Torvalds status = -ENOMEM; 4931da177e4SLinus Torvalds goto out; 4941da177e4SLinus Torvalds } 4951f4eab7eSNeil Brown timestamp = jiffies; 4964704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 49725606656SJeff Layton status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, 49825606656SJeff Layton *desc->dir_cookie, page, 4991da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, 5001da177e4SLinus Torvalds desc->plus); 5011da177e4SLinus Torvalds desc->page = page; 5021da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 50325606656SJeff Layton if (status >= 0) { 5041f4eab7eSNeil Brown desc->timestamp = timestamp; 5054704f0e2STrond Myklebust desc->gencount = gencount; 5061f4eab7eSNeil Brown desc->timestamp_valid = 1; 5071da177e4SLinus Torvalds if ((status = dir_decode(desc)) == 0) 508f0dd2136STrond Myklebust desc->entry->prev_cookie = *desc->dir_cookie; 5091da177e4SLinus Torvalds } else 5101da177e4SLinus Torvalds status = -EIO; 5111da177e4SLinus Torvalds if (status < 0) 5121da177e4SLinus Torvalds goto out_release; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* Reset read descriptor so it searches the page cache from 5171da177e4SLinus Torvalds * the start upon the next call to readdir_search_pagecache() */ 5181da177e4SLinus Torvalds desc->page_index = 0; 5191da177e4SLinus Torvalds desc->entry->cookie = desc->entry->prev_cookie = 0; 5201da177e4SLinus Torvalds desc->entry->eof = 0; 5211da177e4SLinus Torvalds out: 5221e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 5233110ff80SHarvey Harrison __func__, status); 5241da177e4SLinus Torvalds return status; 5251da177e4SLinus Torvalds out_release: 5261da177e4SLinus Torvalds dir_page_release(desc); 5271da177e4SLinus Torvalds goto out; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 53000a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 53100a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 53200a92642SOlivier Galibert whole directory. 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 5351da177e4SLinus Torvalds { 53601cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 5371da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5381da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 5391da177e4SLinus Torvalds *desc = &my_desc; 5401da177e4SLinus Torvalds struct nfs_entry my_entry; 541aa49b4cfSTrond Myklebust int res = -ENOMEM; 5421da177e4SLinus Torvalds 5436da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 5441e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 5451e7cb3dcSChuck Lever (long long)filp->f_pos); 54691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 54791d5b470SChuck Lever 5481da177e4SLinus Torvalds /* 54900a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 550f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 55100a92642SOlivier Galibert * to either find the entry with the appropriate number or 55200a92642SOlivier Galibert * revalidate the cookie. 5531da177e4SLinus Torvalds */ 5541da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds desc->file = filp; 557cd3758e3STrond Myklebust desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie; 5581da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 5591da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds my_entry.cookie = my_entry.prev_cookie = 0; 5621da177e4SLinus Torvalds my_entry.eof = 0; 563aa49b4cfSTrond Myklebust my_entry.fh = nfs_alloc_fhandle(); 564aa49b4cfSTrond Myklebust my_entry.fattr = nfs_alloc_fattr(); 565aa49b4cfSTrond Myklebust if (my_entry.fh == NULL || my_entry.fattr == NULL) 566aa49b4cfSTrond Myklebust goto out_alloc_failed; 567aa49b4cfSTrond Myklebust 5681da177e4SLinus Torvalds desc->entry = &my_entry; 5691da177e4SLinus Torvalds 570565277f6STrond Myklebust nfs_block_sillyrename(dentry); 5711cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 572fccca7fcSTrond Myklebust if (res < 0) 573fccca7fcSTrond Myklebust goto out; 574fccca7fcSTrond Myklebust 5751da177e4SLinus Torvalds while(!desc->entry->eof) { 5761da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 57700a92642SOlivier Galibert 5781da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 5791da177e4SLinus Torvalds /* This means either end of directory */ 580f0dd2136STrond Myklebust if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) { 5811da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 5821da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 5831da177e4SLinus Torvalds if (res >= 0) 5841da177e4SLinus Torvalds continue; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds res = 0; 5871da177e4SLinus Torvalds break; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 5903a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 5911da177e4SLinus Torvalds nfs_zap_caches(inode); 5921da177e4SLinus Torvalds desc->plus = 0; 5931da177e4SLinus Torvalds desc->entry->eof = 0; 5941da177e4SLinus Torvalds continue; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds if (res < 0) 5971da177e4SLinus Torvalds break; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 6001da177e4SLinus Torvalds if (res < 0) { 6011da177e4SLinus Torvalds res = 0; 6021da177e4SLinus Torvalds break; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds } 605fccca7fcSTrond Myklebust out: 606565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 6071e7cb3dcSChuck Lever if (res > 0) 6081e7cb3dcSChuck Lever res = 0; 609aa49b4cfSTrond Myklebust out_alloc_failed: 610aa49b4cfSTrond Myklebust nfs_free_fattr(my_entry.fattr); 611aa49b4cfSTrond Myklebust nfs_free_fhandle(my_entry.fh); 612aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 6131e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 6141e7cb3dcSChuck Lever res); 6151da177e4SLinus Torvalds return res; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 61810afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 619f0dd2136STrond Myklebust { 620b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 621b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 622b84e06c5SChuck Lever 6236da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 624b84e06c5SChuck Lever dentry->d_parent->d_name.name, 625b84e06c5SChuck Lever dentry->d_name.name, 626b84e06c5SChuck Lever offset, origin); 627b84e06c5SChuck Lever 628b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 629f0dd2136STrond Myklebust switch (origin) { 630f0dd2136STrond Myklebust case 1: 631f0dd2136STrond Myklebust offset += filp->f_pos; 632f0dd2136STrond Myklebust case 0: 633f0dd2136STrond Myklebust if (offset >= 0) 634f0dd2136STrond Myklebust break; 635f0dd2136STrond Myklebust default: 636f0dd2136STrond Myklebust offset = -EINVAL; 637f0dd2136STrond Myklebust goto out; 638f0dd2136STrond Myklebust } 639f0dd2136STrond Myklebust if (offset != filp->f_pos) { 640f0dd2136STrond Myklebust filp->f_pos = offset; 641cd3758e3STrond Myklebust nfs_file_open_context(filp)->dir_cookie = 0; 642f0dd2136STrond Myklebust } 643f0dd2136STrond Myklebust out: 644b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 645f0dd2136STrond Myklebust return offset; 646f0dd2136STrond Myklebust } 647f0dd2136STrond Myklebust 6481da177e4SLinus Torvalds /* 6491da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 6501da177e4SLinus Torvalds * is a dummy operation. 6511da177e4SLinus Torvalds */ 6527ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync) 6531da177e4SLinus Torvalds { 6547ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 6557ea80859SChristoph Hellwig 6566da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 6571e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 6581e7cb3dcSChuck Lever datasync); 6591e7cb3dcSChuck Lever 66054917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 6611da177e4SLinus Torvalds return 0; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 664bfc69a45STrond Myklebust /** 665bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 666bfc69a45STrond Myklebust * @dir - pointer to directory inode 667bfc69a45STrond Myklebust * 668bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 669bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 670bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 671bfc69a45STrond Myklebust * 672bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 673bfc69a45STrond Myklebust */ 674bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 675bfc69a45STrond Myklebust { 676011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 677bfc69a45STrond Myklebust } 678bfc69a45STrond Myklebust 6791da177e4SLinus Torvalds /* 6801da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 6811da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 6821da177e4SLinus Torvalds * and may need to be looked up again. 6831da177e4SLinus Torvalds */ 684c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds if (IS_ROOT(dentry)) 6871da177e4SLinus Torvalds return 1; 6884eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 6894eec952eSTrond Myklebust return 0; 690f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 6916ecc5e8fSTrond Myklebust return 0; 692f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 693f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 694f2c77f4eSTrond Myklebust return 0; 695f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 696f2c77f4eSTrond Myklebust return 0; 697f2c77f4eSTrond Myklebust return 1; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* 7011d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 7021d6757fbSTrond Myklebust * 7031d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 7041d6757fbSTrond Myklebust * component of the path. 7051d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 7061d6757fbSTrond Myklebust */ 7071d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) 7081d6757fbSTrond Myklebust { 7091d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 7101d6757fbSTrond Myklebust return 0; 7111d6757fbSTrond Myklebust return nd->flags & mask; 7121d6757fbSTrond Myklebust } 7131d6757fbSTrond Myklebust 7141d6757fbSTrond Myklebust /* 715a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 716a12802caSTrond Myklebust * an O_EXCL create using this path component. 717a12802caSTrond Myklebust */ 718a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 719a12802caSTrond Myklebust { 720a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 721a12802caSTrond Myklebust return 0; 7223516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 723a12802caSTrond Myklebust } 724a12802caSTrond Myklebust 725a12802caSTrond Myklebust /* 7261d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 7271d6757fbSTrond Myklebust * 7281d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 7291d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 7301d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 7311d6757fbSTrond Myklebust * 7321d6757fbSTrond Myklebust */ 7331da177e4SLinus Torvalds static inline 7341da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 7371da177e4SLinus Torvalds 7384e99a1ffSTrond Myklebust if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags)) 7394e99a1ffSTrond Myklebust return 0; 7401da177e4SLinus Torvalds if (nd != NULL) { 7411da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 7421d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 7431da177e4SLinus Torvalds goto out_force; 7441da177e4SLinus Torvalds /* This is an open(2) */ 7451d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 7464e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 7474e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 7484e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 7491da177e4SLinus Torvalds goto out_force; 7504f48af45STrond Myklebust return 0; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 7531da177e4SLinus Torvalds out_force: 7541da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* 7581da177e4SLinus Torvalds * We judge how long we want to trust negative 7591da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 7601da177e4SLinus Torvalds * 7611da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 7621da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds static inline 7651da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 7661da177e4SLinus Torvalds struct nameidata *nd) 7671da177e4SLinus Torvalds { 7681da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 7691d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 7701da177e4SLinus Torvalds return 0; 7714eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 7724eec952eSTrond Myklebust return 1; 7731da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds /* 7771da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 7781da177e4SLinus Torvalds * and we should check whether we can really trust that 7791da177e4SLinus Torvalds * lookup. 7801da177e4SLinus Torvalds * 7811da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 7821da177e4SLinus Torvalds * we have an inode! 7831da177e4SLinus Torvalds * 7841da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 7851da177e4SLinus Torvalds * cached dentry and do a new lookup. 7861da177e4SLinus Torvalds */ 7871da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) 7881da177e4SLinus Torvalds { 7891da177e4SLinus Torvalds struct inode *dir; 7901da177e4SLinus Torvalds struct inode *inode; 7911da177e4SLinus Torvalds struct dentry *parent; 792e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 793e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 7941da177e4SLinus Torvalds int error; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds parent = dget_parent(dentry); 7971da177e4SLinus Torvalds dir = parent->d_inode; 79891d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 7991da177e4SLinus Torvalds inode = dentry->d_inode; 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds if (!inode) { 8021da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 8031da177e4SLinus Torvalds goto out_bad; 8041da177e4SLinus Torvalds goto out_valid; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if (is_bad_inode(inode)) { 8081e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 8093110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8101e7cb3dcSChuck Lever dentry->d_name.name); 8111da177e4SLinus Torvalds goto out_bad; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 81415860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 81515860ab1STrond Myklebust goto out_set_verifier; 81615860ab1STrond Myklebust 8171da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 818a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 8191da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 8201da177e4SLinus Torvalds goto out_zap_parent; 8211da177e4SLinus Torvalds goto out_valid; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds if (NFS_STALE(inode)) 8251da177e4SLinus Torvalds goto out_bad; 8261da177e4SLinus Torvalds 827e1fb4d05STrond Myklebust error = -ENOMEM; 828e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 829e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 830e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 831e1fb4d05STrond Myklebust goto out_error; 832e1fb4d05STrond Myklebust 833e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 8341da177e4SLinus Torvalds if (error) 8351da177e4SLinus Torvalds goto out_bad; 836e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 8371da177e4SLinus Torvalds goto out_bad; 838e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 8391da177e4SLinus Torvalds goto out_bad; 8401da177e4SLinus Torvalds 841e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 842e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 84315860ab1STrond Myklebust out_set_verifier: 844cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 8451da177e4SLinus Torvalds out_valid: 8461da177e4SLinus Torvalds dput(parent); 8471e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 8483110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8491e7cb3dcSChuck Lever dentry->d_name.name); 8501da177e4SLinus Torvalds return 1; 8511da177e4SLinus Torvalds out_zap_parent: 8521da177e4SLinus Torvalds nfs_zap_caches(dir); 8531da177e4SLinus Torvalds out_bad: 854a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 8551da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 8561da177e4SLinus Torvalds /* Purge readdir caches. */ 8571da177e4SLinus Torvalds nfs_zap_caches(inode); 8581da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 8591da177e4SLinus Torvalds if (have_submounts(dentry)) 8601da177e4SLinus Torvalds goto out_valid; 861d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 862d9e80b7dSAl Viro goto out_valid; 8631da177e4SLinus Torvalds shrink_dcache_parent(dentry); 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds d_drop(dentry); 866e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 867e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 8681da177e4SLinus Torvalds dput(parent); 8691e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 8703110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8711e7cb3dcSChuck Lever dentry->d_name.name); 8721da177e4SLinus Torvalds return 0; 873e1fb4d05STrond Myklebust out_error: 874e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 875e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 876e1fb4d05STrond Myklebust dput(parent); 877e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 878e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 879e1fb4d05STrond Myklebust dentry->d_name.name, error); 880e1fb4d05STrond Myklebust return error; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* 8841da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry) 8871da177e4SLinus Torvalds { 8881da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 8891da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 8901da177e4SLinus Torvalds dentry->d_flags); 8911da177e4SLinus Torvalds 89277f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 89377f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 89477f11192STrond Myklebust return 1; 89577f11192STrond Myklebust 8961da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 8971da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 8981da177e4SLinus Torvalds return 1; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 9011da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 9021da177e4SLinus Torvalds * files will be cleaned up during umount */ 9031da177e4SLinus Torvalds return 1; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds return 0; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 9091b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 9101b83d707STrond Myklebust { 9111b83d707STrond Myklebust spin_lock(&inode->i_lock); 9121b83d707STrond Myklebust if (inode->i_nlink > 0) 9131b83d707STrond Myklebust drop_nlink(inode); 9141b83d707STrond Myklebust spin_unlock(&inode->i_lock); 9151b83d707STrond Myklebust } 9161b83d707STrond Myklebust 9171da177e4SLinus Torvalds /* 9181da177e4SLinus Torvalds * Called when the dentry loses inode. 9191da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 9201da177e4SLinus Torvalds */ 9211da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 9221da177e4SLinus Torvalds { 92383672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 92483672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 92583672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 92683672d39SNeil Brown 9271da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 9289a53c3a7SDave Hansen drop_nlink(inode); 929e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds iput(inode); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 934f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 9351da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 9361da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 9371da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 9381da177e4SLinus Torvalds }; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 9411da177e4SLinus Torvalds { 9421da177e4SLinus Torvalds struct dentry *res; 943565277f6STrond Myklebust struct dentry *parent; 9441da177e4SLinus Torvalds struct inode *inode = NULL; 945e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 946e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 9471da177e4SLinus Torvalds int error; 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 9501da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 95191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 9541da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 9551da177e4SLinus Torvalds goto out; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 9581da177e4SLinus Torvalds 959fd684071STrond Myklebust /* 960fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 961fd684071STrond Myklebust * but don't hash the dentry. 962fd684071STrond Myklebust */ 963fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 964fd684071STrond Myklebust d_instantiate(dentry, NULL); 965fd684071STrond Myklebust res = NULL; 966fc0f684cSTrond Myklebust goto out; 967fd684071STrond Myklebust } 9681da177e4SLinus Torvalds 969e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 970e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 971e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 972e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 973e1fb4d05STrond Myklebust goto out; 974e1fb4d05STrond Myklebust 975565277f6STrond Myklebust parent = dentry->d_parent; 976565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 977565277f6STrond Myklebust nfs_block_sillyrename(parent); 978e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 9791da177e4SLinus Torvalds if (error == -ENOENT) 9801da177e4SLinus Torvalds goto no_entry; 9811da177e4SLinus Torvalds if (error < 0) { 9821da177e4SLinus Torvalds res = ERR_PTR(error); 983565277f6STrond Myklebust goto out_unblock_sillyrename; 9841da177e4SLinus Torvalds } 985e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 98603f28e3aSTrond Myklebust res = (struct dentry *)inode; 98703f28e3aSTrond Myklebust if (IS_ERR(res)) 988565277f6STrond Myklebust goto out_unblock_sillyrename; 98954ceac45SDavid Howells 9901da177e4SLinus Torvalds no_entry: 99154ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 9929eaef27bSTrond Myklebust if (res != NULL) { 9939eaef27bSTrond Myklebust if (IS_ERR(res)) 994565277f6STrond Myklebust goto out_unblock_sillyrename; 9951da177e4SLinus Torvalds dentry = res; 9969eaef27bSTrond Myklebust } 9971da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 998565277f6STrond Myklebust out_unblock_sillyrename: 999565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 10001da177e4SLinus Torvalds out: 1001e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1002e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 10031da177e4SLinus Torvalds return res; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 10071da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 10081da177e4SLinus Torvalds 1009f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 10101da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 10111da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 10121da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 10131da177e4SLinus Torvalds }; 10141da177e4SLinus Torvalds 10151d6757fbSTrond Myklebust /* 10161d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 10171d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 10181d6757fbSTrond Myklebust */ 10195584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 10201da177e4SLinus Torvalds { 10211d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 10221da177e4SLinus Torvalds return 0; 10231da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 10241da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 10251da177e4SLinus Torvalds return 0; 10261da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 10272c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 10282c463e95SDave Hansen (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 10291da177e4SLinus Torvalds return 0; 10301da177e4SLinus Torvalds return 1; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 1033*cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd) 1034*cd9a1c0eSTrond Myklebust { 1035*cd9a1c0eSTrond Myklebust struct path path = { 1036*cd9a1c0eSTrond Myklebust .mnt = nd->path.mnt, 1037*cd9a1c0eSTrond Myklebust .dentry = dentry, 1038*cd9a1c0eSTrond Myklebust }; 1039*cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1040*cd9a1c0eSTrond Myklebust struct rpc_cred *cred; 1041*cd9a1c0eSTrond Myklebust fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); 1042*cd9a1c0eSTrond Myklebust 1043*cd9a1c0eSTrond Myklebust cred = rpc_lookup_cred(); 1044*cd9a1c0eSTrond Myklebust if (IS_ERR(cred)) 1045*cd9a1c0eSTrond Myklebust return ERR_CAST(cred); 1046*cd9a1c0eSTrond Myklebust ctx = alloc_nfs_open_context(&path, cred, fmode); 1047*cd9a1c0eSTrond Myklebust put_rpccred(cred); 1048*cd9a1c0eSTrond Myklebust if (ctx == NULL) 1049*cd9a1c0eSTrond Myklebust return ERR_PTR(-ENOMEM); 1050*cd9a1c0eSTrond Myklebust return ctx; 1051*cd9a1c0eSTrond Myklebust } 1052*cd9a1c0eSTrond Myklebust 1053*cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1054*cd9a1c0eSTrond Myklebust { 1055*cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1056*cd9a1c0eSTrond Myklebust return 0; 1057*cd9a1c0eSTrond Myklebust } 1058*cd9a1c0eSTrond Myklebust 1059*cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) 1060*cd9a1c0eSTrond Myklebust { 1061*cd9a1c0eSTrond Myklebust struct file *filp; 1062*cd9a1c0eSTrond Myklebust int ret = 0; 1063*cd9a1c0eSTrond Myklebust 1064*cd9a1c0eSTrond Myklebust /* If the open_intent is for execute, we have an extra check to make */ 1065*cd9a1c0eSTrond Myklebust if (ctx->mode & FMODE_EXEC) { 1066*cd9a1c0eSTrond Myklebust ret = nfs_may_open(ctx->path.dentry->d_inode, 1067*cd9a1c0eSTrond Myklebust ctx->cred, 1068*cd9a1c0eSTrond Myklebust nd->intent.open.flags); 1069*cd9a1c0eSTrond Myklebust if (ret < 0) 1070*cd9a1c0eSTrond Myklebust goto out; 1071*cd9a1c0eSTrond Myklebust } 1072*cd9a1c0eSTrond Myklebust filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open); 1073*cd9a1c0eSTrond Myklebust if (IS_ERR(filp)) 1074*cd9a1c0eSTrond Myklebust ret = PTR_ERR(filp); 1075*cd9a1c0eSTrond Myklebust else 1076*cd9a1c0eSTrond Myklebust nfs_file_set_open_context(filp, ctx); 1077*cd9a1c0eSTrond Myklebust out: 1078*cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1079*cd9a1c0eSTrond Myklebust return ret; 1080*cd9a1c0eSTrond Myklebust } 1081*cd9a1c0eSTrond Myklebust 10821da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 10831da177e4SLinus Torvalds { 1084*cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1085*cd9a1c0eSTrond Myklebust struct iattr attr; 10861da177e4SLinus Torvalds struct dentry *res = NULL; 1087*cd9a1c0eSTrond Myklebust int open_flags; 10881da177e4SLinus Torvalds int error; 10891da177e4SLinus Torvalds 10901e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 10911e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 10921e7cb3dcSChuck Lever 10931da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 10945584c306STrond Myklebust if (!is_atomic_open(nd)) 10951da177e4SLinus Torvalds goto no_open; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 10981da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 10991da177e4SLinus Torvalds goto out; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 11021da177e4SLinus Torvalds 1103d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1104d4d9cdcbSTrond Myklebust * the dentry. */ 11053516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1106d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 110702a913a7STrond Myklebust goto out; 110802a913a7STrond Myklebust } 11091da177e4SLinus Torvalds 1110*cd9a1c0eSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1111*cd9a1c0eSTrond Myklebust res = ERR_CAST(ctx); 1112*cd9a1c0eSTrond Myklebust if (IS_ERR(ctx)) 1113*cd9a1c0eSTrond Myklebust goto out; 1114*cd9a1c0eSTrond Myklebust 1115*cd9a1c0eSTrond Myklebust open_flags = nd->intent.open.flags; 1116*cd9a1c0eSTrond Myklebust if (nd->flags & LOOKUP_CREATE) { 1117*cd9a1c0eSTrond Myklebust attr.ia_mode = nd->intent.open.create_mode; 1118*cd9a1c0eSTrond Myklebust attr.ia_valid = ATTR_MODE; 1119*cd9a1c0eSTrond Myklebust if (!IS_POSIXACL(dir)) 1120*cd9a1c0eSTrond Myklebust attr.ia_mode &= ~current_umask(); 1121*cd9a1c0eSTrond Myklebust } else { 1122*cd9a1c0eSTrond Myklebust open_flags &= ~O_EXCL; 1123*cd9a1c0eSTrond Myklebust attr.ia_valid = 0; 1124*cd9a1c0eSTrond Myklebust BUG_ON(open_flags & O_CREAT); 1125*cd9a1c0eSTrond Myklebust } 1126*cd9a1c0eSTrond Myklebust 11271da177e4SLinus Torvalds /* Open the file on the server */ 1128*cd9a1c0eSTrond Myklebust res = nfs4_atomic_open(dir, ctx, open_flags, &attr); 112902a913a7STrond Myklebust if (IS_ERR(res)) { 1130*cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 113102a913a7STrond Myklebust error = PTR_ERR(res); 11321da177e4SLinus Torvalds switch (error) { 11331da177e4SLinus Torvalds /* Make a negative dentry */ 11341da177e4SLinus Torvalds case -ENOENT: 113502a913a7STrond Myklebust res = NULL; 113602a913a7STrond Myklebust goto out; 11371da177e4SLinus Torvalds /* This turned out not to be a regular file */ 113880e60639STrond Myklebust case -EISDIR: 11396f926b5bSTrond Myklebust case -ENOTDIR: 11406f926b5bSTrond Myklebust goto no_open; 11411da177e4SLinus Torvalds case -ELOOP: 11421da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 11431da177e4SLinus Torvalds goto no_open; 11441da177e4SLinus Torvalds /* case -EINVAL: */ 11451da177e4SLinus Torvalds default: 11461da177e4SLinus Torvalds goto out; 11471da177e4SLinus Torvalds } 1148*cd9a1c0eSTrond Myklebust } 1149*cd9a1c0eSTrond Myklebust if (res != NULL) 11501da177e4SLinus Torvalds dentry = res; 1151*cd9a1c0eSTrond Myklebust nfs_intent_set_file(nd, ctx); 11521da177e4SLinus Torvalds out: 11531da177e4SLinus Torvalds return res; 11541da177e4SLinus Torvalds no_open: 11551da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 11591da177e4SLinus Torvalds { 11601da177e4SLinus Torvalds struct dentry *parent = NULL; 11611da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11621da177e4SLinus Torvalds struct inode *dir; 11631da177e4SLinus Torvalds int openflags, ret = 0; 11641da177e4SLinus Torvalds 11651f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 11665584c306STrond Myklebust goto no_open; 11671da177e4SLinus Torvalds parent = dget_parent(dentry); 11681da177e4SLinus Torvalds dir = parent->d_inode; 11691da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 11701da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 11711da177e4SLinus Torvalds */ 1172216d5d06STrond Myklebust if (inode == NULL) { 1173216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1174216d5d06STrond Myklebust ret = 1; 11751da177e4SLinus Torvalds goto out; 1176216d5d06STrond Myklebust } 1177216d5d06STrond Myklebust 11781da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 11791da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 11805584c306STrond Myklebust goto no_open_dput; 11811da177e4SLinus Torvalds openflags = nd->intent.open.flags; 11821da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 11831da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 11845584c306STrond Myklebust goto no_open_dput; 11851da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 11860a377cffSTrond Myklebust openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds /* 11891b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 11901da177e4SLinus Torvalds * operations that change the directory. We therefore save the 11911da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 11921da177e4SLinus Torvalds */ 119302a913a7STrond Myklebust ret = nfs4_open_revalidate(dir, dentry, openflags, nd); 11941da177e4SLinus Torvalds out: 11951da177e4SLinus Torvalds dput(parent); 11961da177e4SLinus Torvalds if (!ret) 11971da177e4SLinus Torvalds d_drop(dentry); 11981da177e4SLinus Torvalds return ret; 11995584c306STrond Myklebust no_open_dput: 12001da177e4SLinus Torvalds dput(parent); 12015584c306STrond Myklebust no_open: 12021da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc) 12071da177e4SLinus Torvalds { 120801cce933SJosef "Jeff" Sipek struct dentry *parent = desc->file->f_path.dentry; 12091da177e4SLinus Torvalds struct inode *dir = parent->d_inode; 12101da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 12111da177e4SLinus Torvalds struct dentry *dentry, *alias; 12121da177e4SLinus Torvalds struct qstr name = { 12131da177e4SLinus Torvalds .name = entry->name, 12141da177e4SLinus Torvalds .len = entry->len, 12151da177e4SLinus Torvalds }; 12161da177e4SLinus Torvalds struct inode *inode; 121757fa76f2STrond Myklebust unsigned long verf = nfs_save_change_attribute(dir); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds switch (name.len) { 12201da177e4SLinus Torvalds case 2: 12211da177e4SLinus Torvalds if (name.name[0] == '.' && name.name[1] == '.') 12221da177e4SLinus Torvalds return dget_parent(parent); 12231da177e4SLinus Torvalds break; 12241da177e4SLinus Torvalds case 1: 12251da177e4SLinus Torvalds if (name.name[0] == '.') 12261da177e4SLinus Torvalds return dget(parent); 12271da177e4SLinus Torvalds } 122857fa76f2STrond Myklebust 122957fa76f2STrond Myklebust spin_lock(&dir->i_lock); 123057fa76f2STrond Myklebust if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) { 123157fa76f2STrond Myklebust spin_unlock(&dir->i_lock); 123257fa76f2STrond Myklebust return NULL; 123357fa76f2STrond Myklebust } 123457fa76f2STrond Myklebust spin_unlock(&dir->i_lock); 123557fa76f2STrond Myklebust 12361da177e4SLinus Torvalds name.hash = full_name_hash(name.name, name.len); 12371da177e4SLinus Torvalds dentry = d_lookup(parent, &name); 1238df1d5d23STrond Myklebust if (dentry != NULL) { 1239ef75c797STrond Myklebust /* Is this a positive dentry that matches the readdir info? */ 1240ef75c797STrond Myklebust if (dentry->d_inode != NULL && 1241ef75c797STrond Myklebust (NFS_FILEID(dentry->d_inode) == entry->ino || 1242ef75c797STrond Myklebust d_mountpoint(dentry))) { 1243ef75c797STrond Myklebust if (!desc->plus || entry->fh->size == 0) 12441da177e4SLinus Torvalds return dentry; 1245ef75c797STrond Myklebust if (nfs_compare_fh(NFS_FH(dentry->d_inode), 1246ef75c797STrond Myklebust entry->fh) == 0) 1247ef75c797STrond Myklebust goto out_renew; 1248ef75c797STrond Myklebust } 1249df1d5d23STrond Myklebust /* No, so d_drop to allow one to be created */ 1250df1d5d23STrond Myklebust d_drop(dentry); 1251df1d5d23STrond Myklebust dput(dentry); 1252df1d5d23STrond Myklebust } 12531da177e4SLinus Torvalds if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR)) 12541da177e4SLinus Torvalds return NULL; 125554af3bb5STrond Myklebust if (name.len > NFS_SERVER(dir)->namelen) 125654af3bb5STrond Myklebust return NULL; 12571b1dcc1bSJes Sorensen /* Note: caller is already holding the dir->i_mutex! */ 12581da177e4SLinus Torvalds dentry = d_alloc(parent, &name); 12591da177e4SLinus Torvalds if (dentry == NULL) 12601da177e4SLinus Torvalds return NULL; 12611da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 12621da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 126303f28e3aSTrond Myklebust if (IS_ERR(inode)) { 12641da177e4SLinus Torvalds dput(dentry); 12651da177e4SLinus Torvalds return NULL; 12661da177e4SLinus Torvalds } 126754ceac45SDavid Howells 126854ceac45SDavid Howells alias = d_materialise_unique(dentry, inode); 12691da177e4SLinus Torvalds if (alias != NULL) { 12701da177e4SLinus Torvalds dput(dentry); 12719eaef27bSTrond Myklebust if (IS_ERR(alias)) 12729eaef27bSTrond Myklebust return NULL; 12731da177e4SLinus Torvalds dentry = alias; 12741da177e4SLinus Torvalds } 127554ceac45SDavid Howells 1276c79ba787STrond Myklebust out_renew: 127757fa76f2STrond Myklebust nfs_set_verifier(dentry, verf); 1278c79ba787STrond Myklebust return dentry; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds /* 12821da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 12831da177e4SLinus Torvalds */ 12841da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 12851da177e4SLinus Torvalds struct nfs_fattr *fattr) 12861da177e4SLinus Torvalds { 1287fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1288fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 12891da177e4SLinus Torvalds struct inode *inode; 12901da177e4SLinus Torvalds int error = -EACCES; 12911da177e4SLinus Torvalds 1292fab728e1STrond Myklebust d_drop(dentry); 1293fab728e1STrond Myklebust 12941da177e4SLinus Torvalds /* We may have been initialized further down */ 12951da177e4SLinus Torvalds if (dentry->d_inode) 1296fab728e1STrond Myklebust goto out; 12971da177e4SLinus Torvalds if (fhandle->size == 0) { 12981da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 12991da177e4SLinus Torvalds if (error) 1300fab728e1STrond Myklebust goto out_error; 13011da177e4SLinus Torvalds } 13025724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 13031da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 13041da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 13058fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 13061da177e4SLinus Torvalds if (error < 0) 1307fab728e1STrond Myklebust goto out_error; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 131003f28e3aSTrond Myklebust error = PTR_ERR(inode); 131103f28e3aSTrond Myklebust if (IS_ERR(inode)) 1312fab728e1STrond Myklebust goto out_error; 1313fab728e1STrond Myklebust d_add(dentry, inode); 1314fab728e1STrond Myklebust out: 1315fab728e1STrond Myklebust dput(parent); 13161da177e4SLinus Torvalds return 0; 1317fab728e1STrond Myklebust out_error: 1318fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1319fab728e1STrond Myklebust dput(parent); 1320fab728e1STrond Myklebust return error; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds /* 13241da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 13251da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 13261da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 13271da177e4SLinus Torvalds * reply path made it appear to have failed. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 13301da177e4SLinus Torvalds struct nameidata *nd) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds struct iattr attr; 13331da177e4SLinus Torvalds int error; 13341da177e4SLinus Torvalds int open_flags = 0; 13351da177e4SLinus Torvalds 13361e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 13371e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds attr.ia_mode = mode; 13401da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 13411da177e4SLinus Torvalds 1342ad389da7STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) 13431da177e4SLinus Torvalds open_flags = nd->intent.open.flags; 13441da177e4SLinus Torvalds 134502a913a7STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd); 13461da177e4SLinus Torvalds if (error != 0) 13471da177e4SLinus Torvalds goto out_err; 13481da177e4SLinus Torvalds return 0; 13491da177e4SLinus Torvalds out_err: 13501da177e4SLinus Torvalds d_drop(dentry); 13511da177e4SLinus Torvalds return error; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* 13551da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 13561da177e4SLinus Torvalds */ 13571da177e4SLinus Torvalds static int 13581da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 13591da177e4SLinus Torvalds { 13601da177e4SLinus Torvalds struct iattr attr; 13611da177e4SLinus Torvalds int status; 13621da177e4SLinus Torvalds 13631e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 13641e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 13671da177e4SLinus Torvalds return -EINVAL; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds attr.ia_mode = mode; 13701da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 13731da177e4SLinus Torvalds if (status != 0) 13741da177e4SLinus Torvalds goto out_err; 13751da177e4SLinus Torvalds return 0; 13761da177e4SLinus Torvalds out_err: 13771da177e4SLinus Torvalds d_drop(dentry); 13781da177e4SLinus Torvalds return status; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /* 13821da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 13831da177e4SLinus Torvalds */ 13841da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 13851da177e4SLinus Torvalds { 13861da177e4SLinus Torvalds struct iattr attr; 13871da177e4SLinus Torvalds int error; 13881da177e4SLinus Torvalds 13891e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 13901e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 13931da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 13961da177e4SLinus Torvalds if (error != 0) 13971da177e4SLinus Torvalds goto out_err; 13981da177e4SLinus Torvalds return 0; 13991da177e4SLinus Torvalds out_err: 14001da177e4SLinus Torvalds d_drop(dentry); 14011da177e4SLinus Torvalds return error; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 1404d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1405d45b9d8bSTrond Myklebust { 1406d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1407d45b9d8bSTrond Myklebust d_delete(dentry); 1408d45b9d8bSTrond Myklebust } 1409d45b9d8bSTrond Myklebust 14101da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds int error; 14131da177e4SLinus Torvalds 14141e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 14151e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 14181da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 14191da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1420ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1421d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1422d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds return error; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds static unsigned int sillycounter; 14304e769b93SPeter Staubach const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2; 14311da177e4SLinus Torvalds const int countersize = sizeof(sillycounter)*2; 14324e769b93SPeter Staubach const int slen = sizeof(".nfs")+fileidsize+countersize-1; 14331da177e4SLinus Torvalds char silly[slen+1]; 14341da177e4SLinus Torvalds struct qstr qsilly; 14351da177e4SLinus Torvalds struct dentry *sdentry; 14361da177e4SLinus Torvalds int error = -EIO; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n", 14391da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 14401da177e4SLinus Torvalds atomic_read(&dentry->d_count)); 144191d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_SILLYRENAME); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* 14441da177e4SLinus Torvalds * We don't allow a dentry to be silly-renamed twice. 14451da177e4SLinus Torvalds */ 14461da177e4SLinus Torvalds error = -EBUSY; 14471da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 14481da177e4SLinus Torvalds goto out; 14491da177e4SLinus Torvalds 14504e769b93SPeter Staubach sprintf(silly, ".nfs%*.*Lx", 14514e769b93SPeter Staubach fileidsize, fileidsize, 14524e769b93SPeter Staubach (unsigned long long)NFS_FILEID(dentry->d_inode)); 14531da177e4SLinus Torvalds 145434ea8188STrond Myklebust /* Return delegation in anticipation of the rename */ 145534ea8188STrond Myklebust nfs_inode_return_delegation(dentry->d_inode); 145634ea8188STrond Myklebust 14571da177e4SLinus Torvalds sdentry = NULL; 14581da177e4SLinus Torvalds do { 14591da177e4SLinus Torvalds char *suffix = silly + slen - countersize; 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds dput(sdentry); 14621da177e4SLinus Torvalds sillycounter++; 14631da177e4SLinus Torvalds sprintf(suffix, "%*.*x", countersize, countersize, sillycounter); 14641da177e4SLinus Torvalds 14651e7cb3dcSChuck Lever dfprintk(VFS, "NFS: trying to rename %s to %s\n", 14661da177e4SLinus Torvalds dentry->d_name.name, silly); 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds sdentry = lookup_one_len(silly, dentry->d_parent, slen); 14691da177e4SLinus Torvalds /* 14701da177e4SLinus Torvalds * N.B. Better to return EBUSY here ... it could be 14711da177e4SLinus Torvalds * dangerous to delete the file while it's in use. 14721da177e4SLinus Torvalds */ 14731da177e4SLinus Torvalds if (IS_ERR(sdentry)) 14741da177e4SLinus Torvalds goto out; 14751da177e4SLinus Torvalds } while(sdentry->d_inode != NULL); /* need negative lookup */ 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds qsilly.name = silly; 14781da177e4SLinus Torvalds qsilly.len = strlen(silly); 14791da177e4SLinus Torvalds if (dentry->d_inode) { 14801da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 14811da177e4SLinus Torvalds dir, &qsilly); 14825ba7cc48STrond Myklebust nfs_mark_for_revalidate(dentry->d_inode); 14831da177e4SLinus Torvalds } else 14841da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 14851da177e4SLinus Torvalds dir, &qsilly); 14861da177e4SLinus Torvalds if (!error) { 14871da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14881da177e4SLinus Torvalds d_move(dentry, sdentry); 1489e4eff1a6STrond Myklebust error = nfs_async_unlink(dir, dentry); 14901da177e4SLinus Torvalds /* If we return 0 we don't unlink */ 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds dput(sdentry); 14931da177e4SLinus Torvalds out: 14941da177e4SLinus Torvalds return error; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds /* 14981da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 14991da177e4SLinus Torvalds * and after checking that the file has only one user. 15001da177e4SLinus Torvalds * 15011da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 15021da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 15031da177e4SLinus Torvalds */ 15041da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 15051da177e4SLinus Torvalds { 15061da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 15071da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15081da177e4SLinus Torvalds int error = -EBUSY; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 15111da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 15141da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 15151da177e4SLinus Torvalds error = 0; 15161da177e4SLinus Torvalds goto out; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds if (inode != NULL) { 1520cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 15211da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 15221da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 15231da177e4SLinus Torvalds if (error == 0) 15241b83d707STrond Myklebust nfs_drop_nlink(inode); 15255ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 15261da177e4SLinus Torvalds } else 15271da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1528d45b9d8bSTrond Myklebust if (error == -ENOENT) 1529d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 15301da177e4SLinus Torvalds out: 15311da177e4SLinus Torvalds return error; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 15351da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 15361da177e4SLinus Torvalds * 15371da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 15381da177e4SLinus Torvalds */ 15391da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 15401da177e4SLinus Torvalds { 15411da177e4SLinus Torvalds int error; 15421da177e4SLinus Torvalds int need_rehash = 0; 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 15451da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds spin_lock(&dcache_lock); 15481da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 15491da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) > 1) { 15501da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 15511da177e4SLinus Torvalds spin_unlock(&dcache_lock); 1552ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1553ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 15541da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 15551da177e4SLinus Torvalds return error; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 15581da177e4SLinus Torvalds __d_drop(dentry); 15591da177e4SLinus Torvalds need_rehash = 1; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 15621da177e4SLinus Torvalds spin_unlock(&dcache_lock); 15631da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1564d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 15651da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15661da177e4SLinus Torvalds } else if (need_rehash) 15671da177e4SLinus Torvalds d_rehash(dentry); 15681da177e4SLinus Torvalds return error; 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 1571873101b3SChuck Lever /* 1572873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1573873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1574873101b3SChuck Lever * using prepare_write/commit_write. 1575873101b3SChuck Lever * 1576873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1577873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1578873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1579873101b3SChuck Lever * symlink request has completed on the server. 1580873101b3SChuck Lever * 1581873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1582873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1583873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1584873101b3SChuck Lever * and move the raw page into its mapping. 1585873101b3SChuck Lever */ 1586873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 15871da177e4SLinus Torvalds { 1588873101b3SChuck Lever struct pagevec lru_pvec; 1589873101b3SChuck Lever struct page *page; 1590873101b3SChuck Lever char *kaddr; 15911da177e4SLinus Torvalds struct iattr attr; 1592873101b3SChuck Lever unsigned int pathlen = strlen(symname); 15931da177e4SLinus Torvalds int error; 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 15961da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 15971da177e4SLinus Torvalds 1598873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1599873101b3SChuck Lever return -ENAMETOOLONG; 16001da177e4SLinus Torvalds 1601873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1602873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 16031da177e4SLinus Torvalds 160483d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 160576566991STrond Myklebust if (!page) 1606873101b3SChuck Lever return -ENOMEM; 1607873101b3SChuck Lever 1608873101b3SChuck Lever kaddr = kmap_atomic(page, KM_USER0); 1609873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1610873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1611873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1612873101b3SChuck Lever kunmap_atomic(kaddr, KM_USER0); 1613873101b3SChuck Lever 161494a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1615873101b3SChuck Lever if (error != 0) { 1616873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1617873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1618873101b3SChuck Lever dentry->d_name.name, symname, error); 16191da177e4SLinus Torvalds d_drop(dentry); 1620873101b3SChuck Lever __free_page(page); 16211da177e4SLinus Torvalds return error; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 1624873101b3SChuck Lever /* 1625873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1626873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1627873101b3SChuck Lever */ 1628873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1629873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1630873101b3SChuck Lever GFP_KERNEL)) { 163139cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 16324f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1633873101b3SChuck Lever SetPageUptodate(page); 1634873101b3SChuck Lever unlock_page(page); 1635873101b3SChuck Lever } else 1636873101b3SChuck Lever __free_page(page); 1637873101b3SChuck Lever 1638873101b3SChuck Lever return 0; 1639873101b3SChuck Lever } 1640873101b3SChuck Lever 16411da177e4SLinus Torvalds static int 16421da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 16431da177e4SLinus Torvalds { 16441da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 16451da177e4SLinus Torvalds int error; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 16481da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 16491da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 16501da177e4SLinus Torvalds 16519a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 16529a3936aaSTrond Myklebust 16539697d234STrond Myklebust d_drop(dentry); 16541da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1655cf809556STrond Myklebust if (error == 0) { 1656cf809556STrond Myklebust atomic_inc(&inode->i_count); 16579697d234STrond Myklebust d_add(dentry, inode); 1658cf809556STrond Myklebust } 16591da177e4SLinus Torvalds return error; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds /* 16631da177e4SLinus Torvalds * RENAME 16641da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 16651da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 16661da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 16671da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 16681da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 16691da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 16701da177e4SLinus Torvalds * 16711da177e4SLinus Torvalds * FIXED. 16721da177e4SLinus Torvalds * 16731da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 16741da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 16751da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 16761da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 16771da177e4SLinus Torvalds * using the inode layer 16781da177e4SLinus Torvalds * 16791da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 16801da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 16811da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 16821da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 16831da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 16841da177e4SLinus Torvalds * the rename. 16851da177e4SLinus Torvalds */ 16861da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 16871da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 16881da177e4SLinus Torvalds { 16891da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 16901da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 16911da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 16921da177e4SLinus Torvalds int error = -EBUSY; 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 16951da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 16961da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 16971da177e4SLinus Torvalds atomic_read(&new_dentry->d_count)); 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds /* 170028f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 170128f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 170228f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 170328f79a1aSMiklos Szeredi * the new target. 17041da177e4SLinus Torvalds */ 170527226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 170627226104SMiklos Szeredi /* 170727226104SMiklos Szeredi * To prevent any new references to the target during the 170827226104SMiklos Szeredi * rename, we unhash the dentry in advance. 170927226104SMiklos Szeredi */ 171027226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 171127226104SMiklos Szeredi d_drop(new_dentry); 171227226104SMiklos Szeredi rehash = new_dentry; 171327226104SMiklos Szeredi } 171427226104SMiklos Szeredi 171527226104SMiklos Szeredi if (atomic_read(&new_dentry->d_count) > 2) { 17161da177e4SLinus Torvalds int err; 171727226104SMiklos Szeredi 17181da177e4SLinus Torvalds /* copy the target dentry's name */ 17191da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 17201da177e4SLinus Torvalds &new_dentry->d_name); 17211da177e4SLinus Torvalds if (!dentry) 17221da177e4SLinus Torvalds goto out; 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds /* silly-rename the existing target ... */ 17251da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 172624e93025SMiklos Szeredi if (err) 17271da177e4SLinus Torvalds goto out; 172824e93025SMiklos Szeredi 172924e93025SMiklos Szeredi new_dentry = dentry; 173056335936SOGAWA Hirofumi rehash = NULL; 173124e93025SMiklos Szeredi new_inode = NULL; 1732b1e4adf4STrond Myklebust } 173327226104SMiklos Szeredi } 17341da177e4SLinus Torvalds 1735cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 1736b1e4adf4STrond Myklebust if (new_inode != NULL) 173724174119STrond Myklebust nfs_inode_return_delegation(new_inode); 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 17401da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 17415ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 17421da177e4SLinus Torvalds out: 17431da177e4SLinus Torvalds if (rehash) 17441da177e4SLinus Torvalds d_rehash(rehash); 17451da177e4SLinus Torvalds if (!error) { 1746b1e4adf4STrond Myklebust if (new_inode != NULL) 1747b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 17481da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 17498fb559f8SChuck Lever nfs_set_verifier(new_dentry, 17508fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1751d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 1752d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* new dentry created? */ 17551da177e4SLinus Torvalds if (dentry) 17561da177e4SLinus Torvalds dput(dentry); 17571da177e4SLinus Torvalds return error; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 1760cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 1761cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 1762cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 1763cfcea3e8STrond Myklebust 17641c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 17651c3c07e9STrond Myklebust { 17661c3c07e9STrond Myklebust put_rpccred(entry->cred); 17671c3c07e9STrond Myklebust kfree(entry); 1768cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 1769cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 1770cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 17711c3c07e9STrond Myklebust } 17721c3c07e9STrond Myklebust 17731a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 17741a81bb8aSTrond Myklebust { 17751a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 17761a81bb8aSTrond Myklebust 17771a81bb8aSTrond Myklebust while (!list_empty(head)) { 17781a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 17791a81bb8aSTrond Myklebust list_del(&cache->lru); 17801a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 17811a81bb8aSTrond Myklebust } 17821a81bb8aSTrond Myklebust } 17831a81bb8aSTrond Myklebust 17847f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) 1785979df72eSTrond Myklebust { 1786979df72eSTrond Myklebust LIST_HEAD(head); 1787979df72eSTrond Myklebust struct nfs_inode *nfsi; 1788979df72eSTrond Myklebust struct nfs_access_entry *cache; 1789979df72eSTrond Myklebust 179061d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 179161d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 17929c7e7e23STrond Myklebust 1793a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 1794979df72eSTrond Myklebust list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) { 1795979df72eSTrond Myklebust struct inode *inode; 1796979df72eSTrond Myklebust 1797979df72eSTrond Myklebust if (nr_to_scan-- == 0) 1798979df72eSTrond Myklebust break; 17999c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 1800979df72eSTrond Myklebust spin_lock(&inode->i_lock); 1801979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 1802979df72eSTrond Myklebust goto remove_lru_entry; 1803979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 1804979df72eSTrond Myklebust struct nfs_access_entry, lru); 1805979df72eSTrond Myklebust list_move(&cache->lru, &head); 1806979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1807979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 1808979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 1809979df72eSTrond Myklebust &nfs_access_lru_list); 1810979df72eSTrond Myklebust else { 1811979df72eSTrond Myklebust remove_lru_entry: 1812979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 18139c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 1814979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 18159c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 1816979df72eSTrond Myklebust } 181759844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 1818979df72eSTrond Myklebust } 1819979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 18201a81bb8aSTrond Myklebust nfs_access_free_list(&head); 1821979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 1822979df72eSTrond Myklebust } 1823979df72eSTrond Myklebust 18241a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 18251c3c07e9STrond Myklebust { 18261c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 18271a81bb8aSTrond Myklebust struct rb_node *n; 18281c3c07e9STrond Myklebust struct nfs_access_entry *entry; 18291c3c07e9STrond Myklebust 18301c3c07e9STrond Myklebust /* Unhook entries from the cache */ 18311c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 18321c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 18331c3c07e9STrond Myklebust rb_erase(n, root_node); 18341a81bb8aSTrond Myklebust list_move(&entry->lru, head); 18351c3c07e9STrond Myklebust } 18361c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 18371c3c07e9STrond Myklebust } 18381c3c07e9STrond Myklebust 18391c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 18401c3c07e9STrond Myklebust { 18411a81bb8aSTrond Myklebust LIST_HEAD(head); 18421a81bb8aSTrond Myklebust 18431a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 18441a81bb8aSTrond Myklebust return; 1845cfcea3e8STrond Myklebust /* Remove from global LRU init */ 1846cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 18471a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 1848cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 1849cfcea3e8STrond Myklebust 18501c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 18511a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 18521a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 18531a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 18541a81bb8aSTrond Myklebust nfs_access_free_list(&head); 18551c3c07e9STrond Myklebust } 18561c3c07e9STrond Myklebust 18571c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 18581c3c07e9STrond Myklebust { 18591c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 18601c3c07e9STrond Myklebust struct nfs_access_entry *entry; 18611c3c07e9STrond Myklebust 18621c3c07e9STrond Myklebust while (n != NULL) { 18631c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 18641c3c07e9STrond Myklebust 18651c3c07e9STrond Myklebust if (cred < entry->cred) 18661c3c07e9STrond Myklebust n = n->rb_left; 18671c3c07e9STrond Myklebust else if (cred > entry->cred) 18681c3c07e9STrond Myklebust n = n->rb_right; 18691c3c07e9STrond Myklebust else 18701c3c07e9STrond Myklebust return entry; 18711c3c07e9STrond Myklebust } 18721c3c07e9STrond Myklebust return NULL; 18731c3c07e9STrond Myklebust } 18741c3c07e9STrond Myklebust 1875af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 18761da177e4SLinus Torvalds { 187755296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 18781c3c07e9STrond Myklebust struct nfs_access_entry *cache; 18791c3c07e9STrond Myklebust int err = -ENOENT; 18801da177e4SLinus Torvalds 18811c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 18821c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 18831c3c07e9STrond Myklebust goto out_zap; 18841c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 18851c3c07e9STrond Myklebust if (cache == NULL) 18861c3c07e9STrond Myklebust goto out; 1887b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 188864672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 18891c3c07e9STrond Myklebust goto out_stale; 18901c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 18911c3c07e9STrond Myklebust res->cred = cache->cred; 18921c3c07e9STrond Myklebust res->mask = cache->mask; 1893cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 18941c3c07e9STrond Myklebust err = 0; 18951c3c07e9STrond Myklebust out: 18961c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 18971c3c07e9STrond Myklebust return err; 18981c3c07e9STrond Myklebust out_stale: 18991c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1900cfcea3e8STrond Myklebust list_del(&cache->lru); 19011c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 19021c3c07e9STrond Myklebust nfs_access_free_entry(cache); 19031da177e4SLinus Torvalds return -ENOENT; 19041c3c07e9STrond Myklebust out_zap: 19051a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 19061a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 19071c3c07e9STrond Myklebust return -ENOENT; 19081c3c07e9STrond Myklebust } 19091c3c07e9STrond Myklebust 19101c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 19111c3c07e9STrond Myklebust { 1912cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1913cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 19141c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 19151c3c07e9STrond Myklebust struct rb_node *parent = NULL; 19161c3c07e9STrond Myklebust struct nfs_access_entry *entry; 19171c3c07e9STrond Myklebust 19181c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 19191c3c07e9STrond Myklebust while (*p != NULL) { 19201c3c07e9STrond Myklebust parent = *p; 19211c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 19221c3c07e9STrond Myklebust 19231c3c07e9STrond Myklebust if (set->cred < entry->cred) 19241c3c07e9STrond Myklebust p = &parent->rb_left; 19251c3c07e9STrond Myklebust else if (set->cred > entry->cred) 19261c3c07e9STrond Myklebust p = &parent->rb_right; 19271c3c07e9STrond Myklebust else 19281c3c07e9STrond Myklebust goto found; 19291c3c07e9STrond Myklebust } 19301c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 19311c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 1932cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 19331c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 19341c3c07e9STrond Myklebust return; 19351c3c07e9STrond Myklebust found: 19361c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 1937cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 1938cfcea3e8STrond Myklebust list_del(&entry->lru); 19391c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 19401c3c07e9STrond Myklebust nfs_access_free_entry(entry); 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 1943af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 19441da177e4SLinus Torvalds { 19451c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 19461c3c07e9STrond Myklebust if (cache == NULL) 19471c3c07e9STrond Myklebust return; 19481c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 19491da177e4SLinus Torvalds cache->jiffies = set->jiffies; 19501c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 19511da177e4SLinus Torvalds cache->mask = set->mask; 19521c3c07e9STrond Myklebust 19531c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 1954cfcea3e8STrond Myklebust 1955cfcea3e8STrond Myklebust /* Update accounting */ 1956cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 1957cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 1958cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 1959cfcea3e8STrond Myklebust 1960cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 19611a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 1962cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 19631a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 19641a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 19651a81bb8aSTrond Myklebust &nfs_access_lru_list); 1966cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 1967cfcea3e8STrond Myklebust } 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 19711da177e4SLinus Torvalds { 19721da177e4SLinus Torvalds struct nfs_access_entry cache; 19731da177e4SLinus Torvalds int status; 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 19761da177e4SLinus Torvalds if (status == 0) 19771da177e4SLinus Torvalds goto out; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 19801da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 19811da177e4SLinus Torvalds cache.cred = cred; 19821da177e4SLinus Torvalds cache.jiffies = jiffies; 19831da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 1984a71ee337SSuresh Jayaraman if (status != 0) { 1985a71ee337SSuresh Jayaraman if (status == -ESTALE) { 1986a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 1987a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 1988a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 1989a71ee337SSuresh Jayaraman } 19901da177e4SLinus Torvalds return status; 1991a71ee337SSuresh Jayaraman } 19921da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 19931da177e4SLinus Torvalds out: 1994e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 19951da177e4SLinus Torvalds return 0; 19961da177e4SLinus Torvalds return -EACCES; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 1999af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2000af22f94aSTrond Myklebust { 2001af22f94aSTrond Myklebust int mask = 0; 2002af22f94aSTrond Myklebust 2003af22f94aSTrond Myklebust if (openflags & FMODE_READ) 2004af22f94aSTrond Myklebust mask |= MAY_READ; 2005af22f94aSTrond Myklebust if (openflags & FMODE_WRITE) 2006af22f94aSTrond Myklebust mask |= MAY_WRITE; 2007af22f94aSTrond Myklebust if (openflags & FMODE_EXEC) 2008af22f94aSTrond Myklebust mask |= MAY_EXEC; 2009af22f94aSTrond Myklebust return mask; 2010af22f94aSTrond Myklebust } 2011af22f94aSTrond Myklebust 2012af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2013af22f94aSTrond Myklebust { 2014af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2015af22f94aSTrond Myklebust } 2016af22f94aSTrond Myklebust 2017e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask) 20181da177e4SLinus Torvalds { 20191da177e4SLinus Torvalds struct rpc_cred *cred; 20201da177e4SLinus Torvalds int res = 0; 20211da177e4SLinus Torvalds 202291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 202391d5b470SChuck Lever 2024e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 20251da177e4SLinus Torvalds goto out; 20261da177e4SLinus Torvalds /* Is this sys_access() ? */ 20279cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 20281da177e4SLinus Torvalds goto force_lookup; 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 20311da177e4SLinus Torvalds case S_IFLNK: 20321da177e4SLinus Torvalds goto out; 20331da177e4SLinus Torvalds case S_IFREG: 20341da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 20351da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 20367ee2cb7fSFrank Filz && (mask & MAY_OPEN) 20377ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 20381da177e4SLinus Torvalds goto out; 20391da177e4SLinus Torvalds break; 20401da177e4SLinus Torvalds case S_IFDIR: 20411da177e4SLinus Torvalds /* 20421da177e4SLinus Torvalds * Optimize away all write operations, since the server 20431da177e4SLinus Torvalds * will check permissions when we perform the op. 20441da177e4SLinus Torvalds */ 20451da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 20461da177e4SLinus Torvalds goto out; 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds force_lookup: 20501da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 20511da177e4SLinus Torvalds goto out_notsup; 20521da177e4SLinus Torvalds 205398a8e323STrond Myklebust cred = rpc_lookup_cred(); 20541da177e4SLinus Torvalds if (!IS_ERR(cred)) { 20551da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 20561da177e4SLinus Torvalds put_rpccred(cred); 20571da177e4SLinus Torvalds } else 20581da177e4SLinus Torvalds res = PTR_ERR(cred); 20591da177e4SLinus Torvalds out: 2060f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2061f696a365SMiklos Szeredi res = -EACCES; 2062f696a365SMiklos Szeredi 20631e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 20641e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 20651da177e4SLinus Torvalds return res; 20661da177e4SLinus Torvalds out_notsup: 20671da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 20681da177e4SLinus Torvalds if (res == 0) 20691da177e4SLinus Torvalds res = generic_permission(inode, mask, NULL); 20701e7cb3dcSChuck Lever goto out; 20711da177e4SLinus Torvalds } 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds /* 20741da177e4SLinus Torvalds * Local variables: 20751da177e4SLinus Torvalds * version-control: t 20761da177e4SLinus Torvalds * kept-new-versions: 5 20771da177e4SLinus Torvalds * End: 20781da177e4SLinus Torvalds */ 2079