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" 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 451da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 461da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 471da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 481da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 491da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 501da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 511da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 521da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 531da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 541da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 551da177e4SLinus Torvalds struct inode *, struct dentry *); 567ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int); 57f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 581da177e4SLinus Torvalds 594b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 60f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 611da177e4SLinus Torvalds .read = generic_read_dir, 621da177e4SLinus Torvalds .readdir = nfs_readdir, 631da177e4SLinus Torvalds .open = nfs_opendir, 641da177e4SLinus Torvalds .release = nfs_release, 651da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 661da177e4SLinus Torvalds }; 671da177e4SLinus Torvalds 6892e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = { 691da177e4SLinus Torvalds .create = nfs_create, 701da177e4SLinus Torvalds .lookup = nfs_lookup, 711da177e4SLinus Torvalds .link = nfs_link, 721da177e4SLinus Torvalds .unlink = nfs_unlink, 731da177e4SLinus Torvalds .symlink = nfs_symlink, 741da177e4SLinus Torvalds .mkdir = nfs_mkdir, 751da177e4SLinus Torvalds .rmdir = nfs_rmdir, 761da177e4SLinus Torvalds .mknod = nfs_mknod, 771da177e4SLinus Torvalds .rename = nfs_rename, 781da177e4SLinus Torvalds .permission = nfs_permission, 791da177e4SLinus Torvalds .getattr = nfs_getattr, 801da177e4SLinus Torvalds .setattr = nfs_setattr, 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 83b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 8492e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = { 85b7fa0554SAndreas Gruenbacher .create = nfs_create, 86b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 87b7fa0554SAndreas Gruenbacher .link = nfs_link, 88b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 89b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 90b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 91b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 92b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 93b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 94b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 95b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 96b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 97b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 98b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 99b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 100b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 101b7fa0554SAndreas Gruenbacher }; 102b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 103b7fa0554SAndreas Gruenbacher 1041da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 10792e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = { 1081da177e4SLinus Torvalds .create = nfs_create, 1091da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1101da177e4SLinus Torvalds .link = nfs_link, 1111da177e4SLinus Torvalds .unlink = nfs_unlink, 1121da177e4SLinus Torvalds .symlink = nfs_symlink, 1131da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1141da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1151da177e4SLinus Torvalds .mknod = nfs_mknod, 1161da177e4SLinus Torvalds .rename = nfs_rename, 1171da177e4SLinus Torvalds .permission = nfs_permission, 1181da177e4SLinus Torvalds .getattr = nfs_getattr, 1191da177e4SLinus Torvalds .setattr = nfs_setattr, 1206b3b5496SJ. Bruce Fields .getxattr = nfs4_getxattr, 1216b3b5496SJ. Bruce Fields .setxattr = nfs4_setxattr, 1226b3b5496SJ. Bruce Fields .listxattr = nfs4_listxattr, 1231da177e4SLinus Torvalds }; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* 1281da177e4SLinus Torvalds * Open file 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds static int 1311da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1321da177e4SLinus Torvalds { 1337451c4f0SCarsten Otte int res; 1341da177e4SLinus Torvalds 1356da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 136cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 137cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 138cc0dd2d1SChuck Lever 139cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1401e7cb3dcSChuck Lever 1411da177e4SLinus Torvalds /* Call generic open code in order to cache credentials */ 1421da177e4SLinus Torvalds res = nfs_open(inode, filp); 143f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 144f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 145f5a73672SNeil Brown * have been called, so we need to refresh the 146f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 147f5a73672SNeil Brown */ 148f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 149f5a73672SNeil Brown } 1501da177e4SLinus Torvalds return res; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1530dbb4c67SAl Viro typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int); 1541da177e4SLinus Torvalds typedef struct { 1551da177e4SLinus Torvalds struct file *file; 1561da177e4SLinus Torvalds struct page *page; 1571da177e4SLinus Torvalds unsigned long page_index; 1580dbb4c67SAl Viro __be32 *ptr; 159f0dd2136STrond Myklebust u64 *dir_cookie; 160f0dd2136STrond Myklebust loff_t current_index; 1611da177e4SLinus Torvalds struct nfs_entry *entry; 1621da177e4SLinus Torvalds decode_dirent_t decode; 1631da177e4SLinus Torvalds int plus; 1641f4eab7eSNeil Brown unsigned long timestamp; 1654704f0e2STrond Myklebust unsigned long gencount; 1661f4eab7eSNeil Brown int timestamp_valid; 1671da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent 1701da177e4SLinus Torvalds * data directly in the page cache. 1711da177e4SLinus Torvalds * 1721da177e4SLinus Torvalds * Inode invalidation due to refresh etc. takes care of 1731da177e4SLinus Torvalds * _everything_, no sloppy entry flushing logic, no extraneous 1741da177e4SLinus Torvalds * copying, network direct to page cache, the way it was meant 1751da177e4SLinus Torvalds * to be. 1761da177e4SLinus Torvalds * 1771da177e4SLinus Torvalds * NOTE: Dirent information verification is done always by the 1781da177e4SLinus Torvalds * page-in of the RPC reply, nowhere else, this simplies 1791da177e4SLinus Torvalds * things substantially. 1801da177e4SLinus Torvalds */ 1811da177e4SLinus Torvalds static 1821da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds struct file *file = desc->file; 18501cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 1861da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 1874704f0e2STrond Myklebust unsigned long timestamp, gencount; 1881da177e4SLinus Torvalds int error; 1891da177e4SLinus Torvalds 1901e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n", 1913110ff80SHarvey Harrison __func__, (long long)desc->entry->cookie, 1921e7cb3dcSChuck Lever page->index); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds again: 1951da177e4SLinus Torvalds timestamp = jiffies; 1964704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 19701cce933SJosef "Jeff" Sipek error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page, 1981da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 1991da177e4SLinus Torvalds if (error < 0) { 2001da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 2011da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 2021da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 2033a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 2041da177e4SLinus Torvalds desc->plus = 0; 2051da177e4SLinus Torvalds goto again; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds goto error; 2081da177e4SLinus Torvalds } 2091f4eab7eSNeil Brown desc->timestamp = timestamp; 2104704f0e2STrond Myklebust desc->gencount = gencount; 2111f4eab7eSNeil Brown desc->timestamp_valid = 1; 2121da177e4SLinus Torvalds SetPageUptodate(page); 2131da177e4SLinus Torvalds /* Ensure consistent page alignment of the data. 2141da177e4SLinus Torvalds * Note: assumes we have exclusive access to this mapping either 2151b1dcc1bSJes Sorensen * through inode->i_mutex or some other mechanism. 2161da177e4SLinus Torvalds */ 2172aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 218cd9ae2b6STrond Myklebust /* Should never happen */ 219cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 220cd9ae2b6STrond Myklebust } 2211da177e4SLinus Torvalds unlock_page(page); 2221da177e4SLinus Torvalds return 0; 2231da177e4SLinus Torvalds error: 2241da177e4SLinus Torvalds unlock_page(page); 2251da177e4SLinus Torvalds return -EIO; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static inline 2291da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc) 2301da177e4SLinus Torvalds { 2310dbb4c67SAl Viro __be32 *p = desc->ptr; 2321da177e4SLinus Torvalds p = desc->decode(p, desc->entry, desc->plus); 2331da177e4SLinus Torvalds if (IS_ERR(p)) 2341da177e4SLinus Torvalds return PTR_ERR(p); 2351da177e4SLinus Torvalds desc->ptr = p; 2364704f0e2STrond Myklebust if (desc->timestamp_valid) { 2371f4eab7eSNeil Brown desc->entry->fattr->time_start = desc->timestamp; 2384704f0e2STrond Myklebust desc->entry->fattr->gencount = desc->gencount; 2394704f0e2STrond Myklebust } else 2401f4eab7eSNeil Brown desc->entry->fattr->valid &= ~NFS_ATTR_FATTR; 2411da177e4SLinus Torvalds return 0; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static inline 2451da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds kunmap(desc->page); 2481da177e4SLinus Torvalds page_cache_release(desc->page); 2491da177e4SLinus Torvalds desc->page = NULL; 2501da177e4SLinus Torvalds desc->ptr = NULL; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * Given a pointer to a buffer that has already been filled by a call 255f0dd2136STrond Myklebust * to readdir, find the next entry with cookie '*desc->dir_cookie'. 2561da177e4SLinus Torvalds * 2571da177e4SLinus Torvalds * If the end of the buffer has been reached, return -EAGAIN, if not, 2581da177e4SLinus Torvalds * return the offset within the buffer of the next entry to be 2591da177e4SLinus Torvalds * read. 2601da177e4SLinus Torvalds */ 2611da177e4SLinus Torvalds static inline 26200a92642SOlivier Galibert int find_dirent(nfs_readdir_descriptor_t *desc) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 2651da177e4SLinus Torvalds int loop_count = 0, 2661da177e4SLinus Torvalds status; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds while((status = dir_decode(desc)) == 0) { 2691e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n", 2703110ff80SHarvey Harrison __func__, (unsigned long long)entry->cookie); 271f0dd2136STrond Myklebust if (entry->prev_cookie == *desc->dir_cookie) 2721da177e4SLinus Torvalds break; 2731da177e4SLinus Torvalds if (loop_count++ > 200) { 2741da177e4SLinus Torvalds loop_count = 0; 2751da177e4SLinus Torvalds schedule(); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds return status; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds /* 28200a92642SOlivier Galibert * Given a pointer to a buffer that has already been filled by a call 283f0dd2136STrond Myklebust * to readdir, find the entry at offset 'desc->file->f_pos'. 28400a92642SOlivier Galibert * 28500a92642SOlivier Galibert * If the end of the buffer has been reached, return -EAGAIN, if not, 28600a92642SOlivier Galibert * return the offset within the buffer of the next entry to be 28700a92642SOlivier Galibert * read. 28800a92642SOlivier Galibert */ 28900a92642SOlivier Galibert static inline 29000a92642SOlivier Galibert int find_dirent_index(nfs_readdir_descriptor_t *desc) 29100a92642SOlivier Galibert { 29200a92642SOlivier Galibert struct nfs_entry *entry = desc->entry; 29300a92642SOlivier Galibert int loop_count = 0, 29400a92642SOlivier Galibert status; 29500a92642SOlivier Galibert 29600a92642SOlivier Galibert for(;;) { 29700a92642SOlivier Galibert status = dir_decode(desc); 29800a92642SOlivier Galibert if (status) 29900a92642SOlivier Galibert break; 30000a92642SOlivier Galibert 3011e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n", 3021e7cb3dcSChuck Lever (unsigned long long)entry->cookie, desc->current_index); 30300a92642SOlivier Galibert 304f0dd2136STrond Myklebust if (desc->file->f_pos == desc->current_index) { 305f0dd2136STrond Myklebust *desc->dir_cookie = entry->cookie; 30600a92642SOlivier Galibert break; 30700a92642SOlivier Galibert } 30800a92642SOlivier Galibert desc->current_index++; 30900a92642SOlivier Galibert if (loop_count++ > 200) { 31000a92642SOlivier Galibert loop_count = 0; 31100a92642SOlivier Galibert schedule(); 31200a92642SOlivier Galibert } 31300a92642SOlivier Galibert } 31400a92642SOlivier Galibert return status; 31500a92642SOlivier Galibert } 31600a92642SOlivier Galibert 31700a92642SOlivier Galibert /* 31800a92642SOlivier Galibert * Find the given page, and call find_dirent() or find_dirent_index in 31900a92642SOlivier Galibert * order to try to return the next entry. 3201da177e4SLinus Torvalds */ 3211da177e4SLinus Torvalds static inline 3221da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc) 3231da177e4SLinus Torvalds { 32401cce933SJosef "Jeff" Sipek struct inode *inode = desc->file->f_path.dentry->d_inode; 3251da177e4SLinus Torvalds struct page *page; 3261da177e4SLinus Torvalds int status; 3271da177e4SLinus Torvalds 3281e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n", 3293110ff80SHarvey Harrison __func__, desc->page_index, 3301e7cb3dcSChuck Lever (long long) *desc->dir_cookie); 3311da177e4SLinus Torvalds 3321f4eab7eSNeil Brown /* If we find the page in the page_cache, we cannot be sure 3331f4eab7eSNeil Brown * how fresh the data is, so we will ignore readdir_plus attributes. 3341f4eab7eSNeil Brown */ 3351f4eab7eSNeil Brown desc->timestamp_valid = 0; 3361da177e4SLinus Torvalds page = read_cache_page(inode->i_mapping, desc->page_index, 3371da177e4SLinus Torvalds (filler_t *)nfs_readdir_filler, desc); 3381da177e4SLinus Torvalds if (IS_ERR(page)) { 3391da177e4SLinus Torvalds status = PTR_ERR(page); 3401da177e4SLinus Torvalds goto out; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /* NOTE: Someone else may have changed the READDIRPLUS flag */ 3441da177e4SLinus Torvalds desc->page = page; 3451da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 346f0dd2136STrond Myklebust if (*desc->dir_cookie != 0) 34700a92642SOlivier Galibert status = find_dirent(desc); 34800a92642SOlivier Galibert else 34900a92642SOlivier Galibert status = find_dirent_index(desc); 3501da177e4SLinus Torvalds if (status < 0) 3511da177e4SLinus Torvalds dir_page_release(desc); 3521da177e4SLinus Torvalds out: 3533110ff80SHarvey Harrison dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 3541da177e4SLinus Torvalds return status; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * Recurse through the page cache pages, and return a 3591da177e4SLinus Torvalds * filled nfs_entry structure of the next directory entry if possible. 3601da177e4SLinus Torvalds * 361f0dd2136STrond Myklebust * The target for the search is '*desc->dir_cookie' if non-0, 362f0dd2136STrond Myklebust * 'desc->file->f_pos' otherwise 3631da177e4SLinus Torvalds */ 3641da177e4SLinus Torvalds static inline 3651da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds int loop_count = 0; 3681da177e4SLinus Torvalds int res; 3691da177e4SLinus Torvalds 37000a92642SOlivier Galibert /* Always search-by-index from the beginning of the cache */ 371f0dd2136STrond Myklebust if (*desc->dir_cookie == 0) { 3721e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n", 3731e7cb3dcSChuck Lever (long long)desc->file->f_pos); 37400a92642SOlivier Galibert desc->page_index = 0; 37500a92642SOlivier Galibert desc->entry->cookie = desc->entry->prev_cookie = 0; 37600a92642SOlivier Galibert desc->entry->eof = 0; 37700a92642SOlivier Galibert desc->current_index = 0; 378f0dd2136STrond Myklebust } else 3791e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", 3801e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 38100a92642SOlivier Galibert 3821da177e4SLinus Torvalds for (;;) { 3831da177e4SLinus Torvalds res = find_dirent_page(desc); 3841da177e4SLinus Torvalds if (res != -EAGAIN) 3851da177e4SLinus Torvalds break; 3861da177e4SLinus Torvalds /* Align to beginning of next page */ 3871da177e4SLinus Torvalds desc->page_index ++; 3881da177e4SLinus Torvalds if (loop_count++ > 200) { 3891da177e4SLinus Torvalds loop_count = 0; 3901da177e4SLinus Torvalds schedule(); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds } 3931e7cb3dcSChuck Lever 3943110ff80SHarvey Harrison dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res); 3951da177e4SLinus Torvalds return res; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc); 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds static 4091da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 4101da177e4SLinus Torvalds filldir_t filldir) 4111da177e4SLinus Torvalds { 4121da177e4SLinus Torvalds struct file *file = desc->file; 4131da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 4141da177e4SLinus Torvalds struct dentry *dentry = NULL; 4154e769b93SPeter Staubach u64 fileid; 4161da177e4SLinus Torvalds int loop_count = 0, 4171da177e4SLinus Torvalds res; 4181da177e4SLinus Torvalds 4191e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", 4201e7cb3dcSChuck Lever (unsigned long long)entry->cookie); 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds for(;;) { 4231da177e4SLinus Torvalds unsigned d_type = DT_UNKNOWN; 4241da177e4SLinus Torvalds /* Note: entry->prev_cookie contains the cookie for 4251da177e4SLinus Torvalds * retrieving the current dirent on the server */ 4264e769b93SPeter Staubach fileid = entry->ino; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* Get a dentry if we have one */ 4291da177e4SLinus Torvalds if (dentry != NULL) 4301da177e4SLinus Torvalds dput(dentry); 4311da177e4SLinus Torvalds dentry = nfs_readdir_lookup(desc); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds /* Use readdirplus info */ 4341da177e4SLinus Torvalds if (dentry != NULL && dentry->d_inode != NULL) { 4351da177e4SLinus Torvalds d_type = dt_type(dentry->d_inode); 4364e769b93SPeter Staubach fileid = NFS_FILEID(dentry->d_inode); 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds res = filldir(dirent, entry->name, entry->len, 440f43bf0beSTrond Myklebust file->f_pos, nfs_compat_user_ino64(fileid), 441f43bf0beSTrond Myklebust d_type); 4421da177e4SLinus Torvalds if (res < 0) 4431da177e4SLinus Torvalds break; 44400a92642SOlivier Galibert file->f_pos++; 445f0dd2136STrond Myklebust *desc->dir_cookie = entry->cookie; 4461da177e4SLinus Torvalds if (dir_decode(desc) != 0) { 4471da177e4SLinus Torvalds desc->page_index ++; 4481da177e4SLinus Torvalds break; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds if (loop_count++ > 200) { 4511da177e4SLinus Torvalds loop_count = 0; 4521da177e4SLinus Torvalds schedule(); 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds dir_page_release(desc); 4561da177e4SLinus Torvalds if (dentry != NULL) 4571da177e4SLinus Torvalds dput(dentry); 4581e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 4591e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 4601da177e4SLinus Torvalds return res; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* 4641da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 4651da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 4661da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 4671da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 4681da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 4691da177e4SLinus Torvalds * 4701da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 4711da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 4721da177e4SLinus Torvalds * we should already have a complete representation of the 4731da177e4SLinus Torvalds * directory in the page cache by the time we get here. 4741da177e4SLinus Torvalds */ 4751da177e4SLinus Torvalds static inline 4761da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 4771da177e4SLinus Torvalds filldir_t filldir) 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds struct file *file = desc->file; 48001cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 4811da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 4821da177e4SLinus Torvalds struct page *page = NULL; 4831da177e4SLinus Torvalds int status; 4844704f0e2STrond Myklebust unsigned long timestamp, gencount; 4851da177e4SLinus Torvalds 4861e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 4871e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 4901da177e4SLinus Torvalds if (!page) { 4911da177e4SLinus Torvalds status = -ENOMEM; 4921da177e4SLinus Torvalds goto out; 4931da177e4SLinus Torvalds } 4941f4eab7eSNeil Brown timestamp = jiffies; 4954704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 49625606656SJeff Layton status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, 49725606656SJeff Layton *desc->dir_cookie, page, 4981da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, 4991da177e4SLinus Torvalds desc->plus); 5001da177e4SLinus Torvalds desc->page = page; 5011da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 50225606656SJeff Layton if (status >= 0) { 5031f4eab7eSNeil Brown desc->timestamp = timestamp; 5044704f0e2STrond Myklebust desc->gencount = gencount; 5051f4eab7eSNeil Brown desc->timestamp_valid = 1; 5061da177e4SLinus Torvalds if ((status = dir_decode(desc)) == 0) 507f0dd2136STrond Myklebust desc->entry->prev_cookie = *desc->dir_cookie; 5081da177e4SLinus Torvalds } else 5091da177e4SLinus Torvalds status = -EIO; 5101da177e4SLinus Torvalds if (status < 0) 5111da177e4SLinus Torvalds goto out_release; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* Reset read descriptor so it searches the page cache from 5161da177e4SLinus Torvalds * the start upon the next call to readdir_search_pagecache() */ 5171da177e4SLinus Torvalds desc->page_index = 0; 5181da177e4SLinus Torvalds desc->entry->cookie = desc->entry->prev_cookie = 0; 5191da177e4SLinus Torvalds desc->entry->eof = 0; 5201da177e4SLinus Torvalds out: 5211e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 5223110ff80SHarvey Harrison __func__, status); 5231da177e4SLinus Torvalds return status; 5241da177e4SLinus Torvalds out_release: 5251da177e4SLinus Torvalds dir_page_release(desc); 5261da177e4SLinus Torvalds goto out; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 52900a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 53000a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 53100a92642SOlivier Galibert whole directory. 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 5341da177e4SLinus Torvalds { 53501cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 5361da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5371da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 5381da177e4SLinus Torvalds *desc = &my_desc; 5391da177e4SLinus Torvalds struct nfs_entry my_entry; 540aa49b4cfSTrond Myklebust int res = -ENOMEM; 5411da177e4SLinus Torvalds 5426da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 5431e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 5441e7cb3dcSChuck Lever (long long)filp->f_pos); 54591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 54691d5b470SChuck Lever 5471da177e4SLinus Torvalds /* 54800a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 549f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 55000a92642SOlivier Galibert * to either find the entry with the appropriate number or 55100a92642SOlivier Galibert * revalidate the cookie. 5521da177e4SLinus Torvalds */ 5531da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds desc->file = filp; 556cd3758e3STrond Myklebust desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie; 5571da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 5581da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds my_entry.cookie = my_entry.prev_cookie = 0; 5611da177e4SLinus Torvalds my_entry.eof = 0; 562aa49b4cfSTrond Myklebust my_entry.fh = nfs_alloc_fhandle(); 563aa49b4cfSTrond Myklebust my_entry.fattr = nfs_alloc_fattr(); 564aa49b4cfSTrond Myklebust if (my_entry.fh == NULL || my_entry.fattr == NULL) 565aa49b4cfSTrond Myklebust goto out_alloc_failed; 566aa49b4cfSTrond Myklebust 5671da177e4SLinus Torvalds desc->entry = &my_entry; 5681da177e4SLinus Torvalds 569565277f6STrond Myklebust nfs_block_sillyrename(dentry); 5701cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 571fccca7fcSTrond Myklebust if (res < 0) 572fccca7fcSTrond Myklebust goto out; 573fccca7fcSTrond Myklebust 5741da177e4SLinus Torvalds while(!desc->entry->eof) { 5751da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 57600a92642SOlivier Galibert 5771da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 5781da177e4SLinus Torvalds /* This means either end of directory */ 579f0dd2136STrond Myklebust if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) { 5801da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 5811da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 5821da177e4SLinus Torvalds if (res >= 0) 5831da177e4SLinus Torvalds continue; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds res = 0; 5861da177e4SLinus Torvalds break; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 5893a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 5901da177e4SLinus Torvalds nfs_zap_caches(inode); 5911da177e4SLinus Torvalds desc->plus = 0; 5921da177e4SLinus Torvalds desc->entry->eof = 0; 5931da177e4SLinus Torvalds continue; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds if (res < 0) 5961da177e4SLinus Torvalds break; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 5991da177e4SLinus Torvalds if (res < 0) { 6001da177e4SLinus Torvalds res = 0; 6011da177e4SLinus Torvalds break; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds } 604fccca7fcSTrond Myklebust out: 605565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 6061e7cb3dcSChuck Lever if (res > 0) 6071e7cb3dcSChuck Lever res = 0; 608aa49b4cfSTrond Myklebust out_alloc_failed: 609aa49b4cfSTrond Myklebust nfs_free_fattr(my_entry.fattr); 610aa49b4cfSTrond Myklebust nfs_free_fhandle(my_entry.fh); 611aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 6121e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 6131e7cb3dcSChuck Lever res); 6141da177e4SLinus Torvalds return res; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 61710afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 618f0dd2136STrond Myklebust { 619b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 620b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 621b84e06c5SChuck Lever 6226da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 623b84e06c5SChuck Lever dentry->d_parent->d_name.name, 624b84e06c5SChuck Lever dentry->d_name.name, 625b84e06c5SChuck Lever offset, origin); 626b84e06c5SChuck Lever 627b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 628f0dd2136STrond Myklebust switch (origin) { 629f0dd2136STrond Myklebust case 1: 630f0dd2136STrond Myklebust offset += filp->f_pos; 631f0dd2136STrond Myklebust case 0: 632f0dd2136STrond Myklebust if (offset >= 0) 633f0dd2136STrond Myklebust break; 634f0dd2136STrond Myklebust default: 635f0dd2136STrond Myklebust offset = -EINVAL; 636f0dd2136STrond Myklebust goto out; 637f0dd2136STrond Myklebust } 638f0dd2136STrond Myklebust if (offset != filp->f_pos) { 639f0dd2136STrond Myklebust filp->f_pos = offset; 640cd3758e3STrond Myklebust nfs_file_open_context(filp)->dir_cookie = 0; 641f0dd2136STrond Myklebust } 642f0dd2136STrond Myklebust out: 643b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 644f0dd2136STrond Myklebust return offset; 645f0dd2136STrond Myklebust } 646f0dd2136STrond Myklebust 6471da177e4SLinus Torvalds /* 6481da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 6491da177e4SLinus Torvalds * is a dummy operation. 6501da177e4SLinus Torvalds */ 6517ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync) 6521da177e4SLinus Torvalds { 6537ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 6547ea80859SChristoph Hellwig 6556da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 6561e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 6571e7cb3dcSChuck Lever datasync); 6581e7cb3dcSChuck Lever 65954917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 663bfc69a45STrond Myklebust /** 664bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 665bfc69a45STrond Myklebust * @dir - pointer to directory inode 666bfc69a45STrond Myklebust * 667bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 668bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 669bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 670bfc69a45STrond Myklebust * 671bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 672bfc69a45STrond Myklebust */ 673bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 674bfc69a45STrond Myklebust { 675011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 676bfc69a45STrond Myklebust } 677bfc69a45STrond Myklebust 6781da177e4SLinus Torvalds /* 6791da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 6801da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 6811da177e4SLinus Torvalds * and may need to be looked up again. 6821da177e4SLinus Torvalds */ 683c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds if (IS_ROOT(dentry)) 6861da177e4SLinus Torvalds return 1; 6874eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 6884eec952eSTrond Myklebust return 0; 689f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 6906ecc5e8fSTrond Myklebust return 0; 691f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 692f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 693f2c77f4eSTrond Myklebust return 0; 694f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 695f2c77f4eSTrond Myklebust return 0; 696f2c77f4eSTrond Myklebust return 1; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds /* 7001d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 7011d6757fbSTrond Myklebust * 7021d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 7031d6757fbSTrond Myklebust * component of the path. 7041d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 7051d6757fbSTrond Myklebust */ 7061d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) 7071d6757fbSTrond Myklebust { 7081d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 7091d6757fbSTrond Myklebust return 0; 7101d6757fbSTrond Myklebust return nd->flags & mask; 7111d6757fbSTrond Myklebust } 7121d6757fbSTrond Myklebust 7131d6757fbSTrond Myklebust /* 714a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 715a12802caSTrond Myklebust * an O_EXCL create using this path component. 716a12802caSTrond Myklebust */ 717a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 718a12802caSTrond Myklebust { 719a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 720a12802caSTrond Myklebust return 0; 7213516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 722a12802caSTrond Myklebust } 723a12802caSTrond Myklebust 724a12802caSTrond Myklebust /* 7251d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 7261d6757fbSTrond Myklebust * 7271d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 7281d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 7291d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 7301d6757fbSTrond Myklebust * 7311d6757fbSTrond Myklebust */ 7321da177e4SLinus Torvalds static inline 7331da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 7341da177e4SLinus Torvalds { 7351da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 7361da177e4SLinus Torvalds 7374e99a1ffSTrond Myklebust if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags)) 7384e99a1ffSTrond Myklebust return 0; 7391da177e4SLinus Torvalds if (nd != NULL) { 7401da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 7411d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 7421da177e4SLinus Torvalds goto out_force; 7431da177e4SLinus Torvalds /* This is an open(2) */ 7441d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 7454e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 7464e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 7474e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 7481da177e4SLinus Torvalds goto out_force; 7494f48af45STrond Myklebust return 0; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 7521da177e4SLinus Torvalds out_force: 7531da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds /* 7571da177e4SLinus Torvalds * We judge how long we want to trust negative 7581da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 7591da177e4SLinus Torvalds * 7601da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 7611da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 7621da177e4SLinus Torvalds */ 7631da177e4SLinus Torvalds static inline 7641da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 7651da177e4SLinus Torvalds struct nameidata *nd) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 7681d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 7691da177e4SLinus Torvalds return 0; 7704eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 7714eec952eSTrond Myklebust return 1; 7721da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 7771da177e4SLinus Torvalds * and we should check whether we can really trust that 7781da177e4SLinus Torvalds * lookup. 7791da177e4SLinus Torvalds * 7801da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 7811da177e4SLinus Torvalds * we have an inode! 7821da177e4SLinus Torvalds * 7831da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 7841da177e4SLinus Torvalds * cached dentry and do a new lookup. 7851da177e4SLinus Torvalds */ 7861da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) 7871da177e4SLinus Torvalds { 7881da177e4SLinus Torvalds struct inode *dir; 7891da177e4SLinus Torvalds struct inode *inode; 7901da177e4SLinus Torvalds struct dentry *parent; 791e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 792e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 7931da177e4SLinus Torvalds int error; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds parent = dget_parent(dentry); 7961da177e4SLinus Torvalds dir = parent->d_inode; 79791d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 7981da177e4SLinus Torvalds inode = dentry->d_inode; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (!inode) { 8011da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 8021da177e4SLinus Torvalds goto out_bad; 8031da177e4SLinus Torvalds goto out_valid; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds if (is_bad_inode(inode)) { 8071e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 8083110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8091e7cb3dcSChuck Lever dentry->d_name.name); 8101da177e4SLinus Torvalds goto out_bad; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 81315860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 81415860ab1STrond Myklebust goto out_set_verifier; 81515860ab1STrond Myklebust 8161da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 817a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 8181da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 8191da177e4SLinus Torvalds goto out_zap_parent; 8201da177e4SLinus Torvalds goto out_valid; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds if (NFS_STALE(inode)) 8241da177e4SLinus Torvalds goto out_bad; 8251da177e4SLinus Torvalds 826e1fb4d05STrond Myklebust error = -ENOMEM; 827e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 828e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 829e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 830e1fb4d05STrond Myklebust goto out_error; 831e1fb4d05STrond Myklebust 832e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 8331da177e4SLinus Torvalds if (error) 8341da177e4SLinus Torvalds goto out_bad; 835e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 8361da177e4SLinus Torvalds goto out_bad; 837e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 8381da177e4SLinus Torvalds goto out_bad; 8391da177e4SLinus Torvalds 840e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 841e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 84215860ab1STrond Myklebust out_set_verifier: 843cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 8441da177e4SLinus Torvalds out_valid: 8451da177e4SLinus Torvalds dput(parent); 8461e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 8473110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8481e7cb3dcSChuck Lever dentry->d_name.name); 8491da177e4SLinus Torvalds return 1; 8501da177e4SLinus Torvalds out_zap_parent: 8511da177e4SLinus Torvalds nfs_zap_caches(dir); 8521da177e4SLinus Torvalds out_bad: 853a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 8541da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 8551da177e4SLinus Torvalds /* Purge readdir caches. */ 8561da177e4SLinus Torvalds nfs_zap_caches(inode); 8571da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 8581da177e4SLinus Torvalds if (have_submounts(dentry)) 8591da177e4SLinus Torvalds goto out_valid; 860d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 861d9e80b7dSAl Viro goto out_valid; 8621da177e4SLinus Torvalds shrink_dcache_parent(dentry); 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds d_drop(dentry); 865e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 866e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 8671da177e4SLinus Torvalds dput(parent); 8681e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 8693110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8701e7cb3dcSChuck Lever dentry->d_name.name); 8711da177e4SLinus Torvalds return 0; 872e1fb4d05STrond Myklebust out_error: 873e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 874e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 875e1fb4d05STrond Myklebust dput(parent); 876e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 877e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 878e1fb4d05STrond Myklebust dentry->d_name.name, error); 879e1fb4d05STrond Myklebust return error; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 8841da177e4SLinus Torvalds */ 8851da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry) 8861da177e4SLinus Torvalds { 8871da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 8881da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 8891da177e4SLinus Torvalds dentry->d_flags); 8901da177e4SLinus Torvalds 89177f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 89277f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 89377f11192STrond Myklebust return 1; 89477f11192STrond Myklebust 8951da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 8961da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 8971da177e4SLinus Torvalds return 1; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 9001da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 9011da177e4SLinus Torvalds * files will be cleaned up during umount */ 9021da177e4SLinus Torvalds return 1; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds return 0; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 9081b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 9091b83d707STrond Myklebust { 9101b83d707STrond Myklebust spin_lock(&inode->i_lock); 9111b83d707STrond Myklebust if (inode->i_nlink > 0) 9121b83d707STrond Myklebust drop_nlink(inode); 9131b83d707STrond Myklebust spin_unlock(&inode->i_lock); 9141b83d707STrond Myklebust } 9151b83d707STrond Myklebust 9161da177e4SLinus Torvalds /* 9171da177e4SLinus Torvalds * Called when the dentry loses inode. 9181da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 9191da177e4SLinus Torvalds */ 9201da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 9211da177e4SLinus Torvalds { 92283672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 92383672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 92483672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 92583672d39SNeil Brown 9261da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 9279a53c3a7SDave Hansen drop_nlink(inode); 928e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds iput(inode); 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 933f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 9341da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 9351da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 9361da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 9371da177e4SLinus Torvalds }; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 9401da177e4SLinus Torvalds { 9411da177e4SLinus Torvalds struct dentry *res; 942565277f6STrond Myklebust struct dentry *parent; 9431da177e4SLinus Torvalds struct inode *inode = NULL; 944e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 945e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 9461da177e4SLinus Torvalds int error; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 9491da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 95091d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 9531da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 9541da177e4SLinus Torvalds goto out; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 9571da177e4SLinus Torvalds 958fd684071STrond Myklebust /* 959fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 960fd684071STrond Myklebust * but don't hash the dentry. 961fd684071STrond Myklebust */ 962fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 963fd684071STrond Myklebust d_instantiate(dentry, NULL); 964fd684071STrond Myklebust res = NULL; 965fc0f684cSTrond Myklebust goto out; 966fd684071STrond Myklebust } 9671da177e4SLinus Torvalds 968e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 969e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 970e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 971e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 972e1fb4d05STrond Myklebust goto out; 973e1fb4d05STrond Myklebust 974565277f6STrond Myklebust parent = dentry->d_parent; 975565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 976565277f6STrond Myklebust nfs_block_sillyrename(parent); 977e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 9781da177e4SLinus Torvalds if (error == -ENOENT) 9791da177e4SLinus Torvalds goto no_entry; 9801da177e4SLinus Torvalds if (error < 0) { 9811da177e4SLinus Torvalds res = ERR_PTR(error); 982565277f6STrond Myklebust goto out_unblock_sillyrename; 9831da177e4SLinus Torvalds } 984e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 98503f28e3aSTrond Myklebust res = (struct dentry *)inode; 98603f28e3aSTrond Myklebust if (IS_ERR(res)) 987565277f6STrond Myklebust goto out_unblock_sillyrename; 98854ceac45SDavid Howells 9891da177e4SLinus Torvalds no_entry: 99054ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 9919eaef27bSTrond Myklebust if (res != NULL) { 9929eaef27bSTrond Myklebust if (IS_ERR(res)) 993565277f6STrond Myklebust goto out_unblock_sillyrename; 9941da177e4SLinus Torvalds dentry = res; 9959eaef27bSTrond Myklebust } 9961da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 997565277f6STrond Myklebust out_unblock_sillyrename: 998565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 9991da177e4SLinus Torvalds out: 1000e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1001e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 10021da177e4SLinus Torvalds return res; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 10061da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 10071da177e4SLinus Torvalds 1008f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 10091da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 10101da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 10111da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 10121da177e4SLinus Torvalds }; 10131da177e4SLinus Torvalds 10141d6757fbSTrond Myklebust /* 10151d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 10161d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 10171d6757fbSTrond Myklebust */ 10185584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 10191da177e4SLinus Torvalds { 10201d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 10211da177e4SLinus Torvalds return 0; 10221da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 10231da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 10241da177e4SLinus Torvalds return 0; 10251da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 10262c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 10272c463e95SDave Hansen (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 10281da177e4SLinus Torvalds return 0; 10291da177e4SLinus Torvalds return 1; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 10331da177e4SLinus Torvalds { 10341da177e4SLinus Torvalds struct dentry *res = NULL; 10351da177e4SLinus Torvalds int error; 10361da177e4SLinus Torvalds 10371e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 10381e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 10391e7cb3dcSChuck Lever 10401da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 10415584c306STrond Myklebust if (!is_atomic_open(nd)) 10421da177e4SLinus Torvalds goto no_open; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 10451da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 10461da177e4SLinus Torvalds goto out; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 10491da177e4SLinus Torvalds 1050d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1051d4d9cdcbSTrond Myklebust * the dentry. */ 10523516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1053d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 105402a913a7STrond Myklebust goto out; 105502a913a7STrond Myklebust } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* Open the file on the server */ 105802a913a7STrond Myklebust res = nfs4_atomic_open(dir, dentry, nd); 105902a913a7STrond Myklebust if (IS_ERR(res)) { 106002a913a7STrond Myklebust error = PTR_ERR(res); 10611da177e4SLinus Torvalds switch (error) { 10621da177e4SLinus Torvalds /* Make a negative dentry */ 10631da177e4SLinus Torvalds case -ENOENT: 106402a913a7STrond Myklebust res = NULL; 106502a913a7STrond Myklebust goto out; 10661da177e4SLinus Torvalds /* This turned out not to be a regular file */ 106780e60639STrond Myklebust case -EISDIR: 10686f926b5bSTrond Myklebust case -ENOTDIR: 10696f926b5bSTrond Myklebust goto no_open; 10701da177e4SLinus Torvalds case -ELOOP: 10711da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 10721da177e4SLinus Torvalds goto no_open; 10731da177e4SLinus Torvalds /* case -EINVAL: */ 10741da177e4SLinus Torvalds default: 10751da177e4SLinus Torvalds goto out; 10761da177e4SLinus Torvalds } 107702a913a7STrond Myklebust } else if (res != NULL) 10781da177e4SLinus Torvalds dentry = res; 10791da177e4SLinus Torvalds out: 10801da177e4SLinus Torvalds return res; 10811da177e4SLinus Torvalds no_open: 10821da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 10861da177e4SLinus Torvalds { 10871da177e4SLinus Torvalds struct dentry *parent = NULL; 10881da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10891da177e4SLinus Torvalds struct inode *dir; 10901da177e4SLinus Torvalds int openflags, ret = 0; 10911da177e4SLinus Torvalds 10921f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 10935584c306STrond Myklebust goto no_open; 10941da177e4SLinus Torvalds parent = dget_parent(dentry); 10951da177e4SLinus Torvalds dir = parent->d_inode; 10961da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 10971da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 10981da177e4SLinus Torvalds */ 1099216d5d06STrond Myklebust if (inode == NULL) { 1100216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1101216d5d06STrond Myklebust ret = 1; 11021da177e4SLinus Torvalds goto out; 1103216d5d06STrond Myklebust } 1104216d5d06STrond Myklebust 11051da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 11061da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 11075584c306STrond Myklebust goto no_open_dput; 11081da177e4SLinus Torvalds openflags = nd->intent.open.flags; 11091da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 11101da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 11115584c306STrond Myklebust goto no_open_dput; 11121da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 11130a377cffSTrond Myklebust openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds /* 11161b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 11171da177e4SLinus Torvalds * operations that change the directory. We therefore save the 11181da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 11191da177e4SLinus Torvalds */ 112002a913a7STrond Myklebust ret = nfs4_open_revalidate(dir, dentry, openflags, nd); 11211da177e4SLinus Torvalds out: 11221da177e4SLinus Torvalds dput(parent); 11231da177e4SLinus Torvalds if (!ret) 11241da177e4SLinus Torvalds d_drop(dentry); 11251da177e4SLinus Torvalds return ret; 11265584c306STrond Myklebust no_open_dput: 11271da177e4SLinus Torvalds dput(parent); 11285584c306STrond Myklebust no_open: 11291da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc) 11341da177e4SLinus Torvalds { 113501cce933SJosef "Jeff" Sipek struct dentry *parent = desc->file->f_path.dentry; 11361da177e4SLinus Torvalds struct inode *dir = parent->d_inode; 11371da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 11381da177e4SLinus Torvalds struct dentry *dentry, *alias; 11391da177e4SLinus Torvalds struct qstr name = { 11401da177e4SLinus Torvalds .name = entry->name, 11411da177e4SLinus Torvalds .len = entry->len, 11421da177e4SLinus Torvalds }; 11431da177e4SLinus Torvalds struct inode *inode; 114457fa76f2STrond Myklebust unsigned long verf = nfs_save_change_attribute(dir); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds switch (name.len) { 11471da177e4SLinus Torvalds case 2: 11481da177e4SLinus Torvalds if (name.name[0] == '.' && name.name[1] == '.') 11491da177e4SLinus Torvalds return dget_parent(parent); 11501da177e4SLinus Torvalds break; 11511da177e4SLinus Torvalds case 1: 11521da177e4SLinus Torvalds if (name.name[0] == '.') 11531da177e4SLinus Torvalds return dget(parent); 11541da177e4SLinus Torvalds } 115557fa76f2STrond Myklebust 115657fa76f2STrond Myklebust spin_lock(&dir->i_lock); 115757fa76f2STrond Myklebust if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) { 115857fa76f2STrond Myklebust spin_unlock(&dir->i_lock); 115957fa76f2STrond Myklebust return NULL; 116057fa76f2STrond Myklebust } 116157fa76f2STrond Myklebust spin_unlock(&dir->i_lock); 116257fa76f2STrond Myklebust 11631da177e4SLinus Torvalds name.hash = full_name_hash(name.name, name.len); 11641da177e4SLinus Torvalds dentry = d_lookup(parent, &name); 1165df1d5d23STrond Myklebust if (dentry != NULL) { 1166ef75c797STrond Myklebust /* Is this a positive dentry that matches the readdir info? */ 1167ef75c797STrond Myklebust if (dentry->d_inode != NULL && 1168ef75c797STrond Myklebust (NFS_FILEID(dentry->d_inode) == entry->ino || 1169ef75c797STrond Myklebust d_mountpoint(dentry))) { 1170ef75c797STrond Myklebust if (!desc->plus || entry->fh->size == 0) 11711da177e4SLinus Torvalds return dentry; 1172ef75c797STrond Myklebust if (nfs_compare_fh(NFS_FH(dentry->d_inode), 1173ef75c797STrond Myklebust entry->fh) == 0) 1174ef75c797STrond Myklebust goto out_renew; 1175ef75c797STrond Myklebust } 1176df1d5d23STrond Myklebust /* No, so d_drop to allow one to be created */ 1177df1d5d23STrond Myklebust d_drop(dentry); 1178df1d5d23STrond Myklebust dput(dentry); 1179df1d5d23STrond Myklebust } 11801da177e4SLinus Torvalds if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR)) 11811da177e4SLinus Torvalds return NULL; 118254af3bb5STrond Myklebust if (name.len > NFS_SERVER(dir)->namelen) 118354af3bb5STrond Myklebust return NULL; 11841b1dcc1bSJes Sorensen /* Note: caller is already holding the dir->i_mutex! */ 11851da177e4SLinus Torvalds dentry = d_alloc(parent, &name); 11861da177e4SLinus Torvalds if (dentry == NULL) 11871da177e4SLinus Torvalds return NULL; 11881da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 11891da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 119003f28e3aSTrond Myklebust if (IS_ERR(inode)) { 11911da177e4SLinus Torvalds dput(dentry); 11921da177e4SLinus Torvalds return NULL; 11931da177e4SLinus Torvalds } 119454ceac45SDavid Howells 119554ceac45SDavid Howells alias = d_materialise_unique(dentry, inode); 11961da177e4SLinus Torvalds if (alias != NULL) { 11971da177e4SLinus Torvalds dput(dentry); 11989eaef27bSTrond Myklebust if (IS_ERR(alias)) 11999eaef27bSTrond Myklebust return NULL; 12001da177e4SLinus Torvalds dentry = alias; 12011da177e4SLinus Torvalds } 120254ceac45SDavid Howells 1203c79ba787STrond Myklebust out_renew: 120457fa76f2STrond Myklebust nfs_set_verifier(dentry, verf); 1205c79ba787STrond Myklebust return dentry; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* 12091da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 12101da177e4SLinus Torvalds */ 12111da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 12121da177e4SLinus Torvalds struct nfs_fattr *fattr) 12131da177e4SLinus Torvalds { 1214fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1215fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 12161da177e4SLinus Torvalds struct inode *inode; 12171da177e4SLinus Torvalds int error = -EACCES; 12181da177e4SLinus Torvalds 1219fab728e1STrond Myklebust d_drop(dentry); 1220fab728e1STrond Myklebust 12211da177e4SLinus Torvalds /* We may have been initialized further down */ 12221da177e4SLinus Torvalds if (dentry->d_inode) 1223fab728e1STrond Myklebust goto out; 12241da177e4SLinus Torvalds if (fhandle->size == 0) { 12251da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 12261da177e4SLinus Torvalds if (error) 1227fab728e1STrond Myklebust goto out_error; 12281da177e4SLinus Torvalds } 12295724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12301da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 12311da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 12328fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 12331da177e4SLinus Torvalds if (error < 0) 1234fab728e1STrond Myklebust goto out_error; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 123703f28e3aSTrond Myklebust error = PTR_ERR(inode); 123803f28e3aSTrond Myklebust if (IS_ERR(inode)) 1239fab728e1STrond Myklebust goto out_error; 1240fab728e1STrond Myklebust d_add(dentry, inode); 1241fab728e1STrond Myklebust out: 1242fab728e1STrond Myklebust dput(parent); 12431da177e4SLinus Torvalds return 0; 1244fab728e1STrond Myklebust out_error: 1245fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1246fab728e1STrond Myklebust dput(parent); 1247fab728e1STrond Myklebust return error; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds /* 12511da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 12521da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 12531da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 12541da177e4SLinus Torvalds * reply path made it appear to have failed. 12551da177e4SLinus Torvalds */ 12561da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 12571da177e4SLinus Torvalds struct nameidata *nd) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct iattr attr; 12601da177e4SLinus Torvalds int error; 12611da177e4SLinus Torvalds int open_flags = 0; 12621da177e4SLinus Torvalds 12631e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 12641e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds attr.ia_mode = mode; 12671da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 12681da177e4SLinus Torvalds 1269ad389da7STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) 12701da177e4SLinus Torvalds open_flags = nd->intent.open.flags; 12711da177e4SLinus Torvalds 127202a913a7STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd); 12731da177e4SLinus Torvalds if (error != 0) 12741da177e4SLinus Torvalds goto out_err; 12751da177e4SLinus Torvalds return 0; 12761da177e4SLinus Torvalds out_err: 12771da177e4SLinus Torvalds d_drop(dentry); 12781da177e4SLinus Torvalds return error; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds /* 12821da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 12831da177e4SLinus Torvalds */ 12841da177e4SLinus Torvalds static int 12851da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 12861da177e4SLinus Torvalds { 12871da177e4SLinus Torvalds struct iattr attr; 12881da177e4SLinus Torvalds int status; 12891da177e4SLinus Torvalds 12901e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 12911e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 12941da177e4SLinus Torvalds return -EINVAL; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds attr.ia_mode = mode; 12971da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 13001da177e4SLinus Torvalds if (status != 0) 13011da177e4SLinus Torvalds goto out_err; 13021da177e4SLinus Torvalds return 0; 13031da177e4SLinus Torvalds out_err: 13041da177e4SLinus Torvalds d_drop(dentry); 13051da177e4SLinus Torvalds return status; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds /* 13091da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 13121da177e4SLinus Torvalds { 13131da177e4SLinus Torvalds struct iattr attr; 13141da177e4SLinus Torvalds int error; 13151da177e4SLinus Torvalds 13161e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 13171e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 13201da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 13231da177e4SLinus Torvalds if (error != 0) 13241da177e4SLinus Torvalds goto out_err; 13251da177e4SLinus Torvalds return 0; 13261da177e4SLinus Torvalds out_err: 13271da177e4SLinus Torvalds d_drop(dentry); 13281da177e4SLinus Torvalds return error; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 1331d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1332d45b9d8bSTrond Myklebust { 1333d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1334d45b9d8bSTrond Myklebust d_delete(dentry); 1335d45b9d8bSTrond Myklebust } 1336d45b9d8bSTrond Myklebust 13371da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 13381da177e4SLinus Torvalds { 13391da177e4SLinus Torvalds int error; 13401da177e4SLinus Torvalds 13411e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 13421e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 13451da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 13461da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1347ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1348d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1349d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds return error; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds static unsigned int sillycounter; 13574e769b93SPeter Staubach const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2; 13581da177e4SLinus Torvalds const int countersize = sizeof(sillycounter)*2; 13594e769b93SPeter Staubach const int slen = sizeof(".nfs")+fileidsize+countersize-1; 13601da177e4SLinus Torvalds char silly[slen+1]; 13611da177e4SLinus Torvalds struct qstr qsilly; 13621da177e4SLinus Torvalds struct dentry *sdentry; 13631da177e4SLinus Torvalds int error = -EIO; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n", 13661da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 13671da177e4SLinus Torvalds atomic_read(&dentry->d_count)); 136891d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_SILLYRENAME); 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /* 13711da177e4SLinus Torvalds * We don't allow a dentry to be silly-renamed twice. 13721da177e4SLinus Torvalds */ 13731da177e4SLinus Torvalds error = -EBUSY; 13741da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 13751da177e4SLinus Torvalds goto out; 13761da177e4SLinus Torvalds 13774e769b93SPeter Staubach sprintf(silly, ".nfs%*.*Lx", 13784e769b93SPeter Staubach fileidsize, fileidsize, 13794e769b93SPeter Staubach (unsigned long long)NFS_FILEID(dentry->d_inode)); 13801da177e4SLinus Torvalds 138134ea8188STrond Myklebust /* Return delegation in anticipation of the rename */ 138234ea8188STrond Myklebust nfs_inode_return_delegation(dentry->d_inode); 138334ea8188STrond Myklebust 13841da177e4SLinus Torvalds sdentry = NULL; 13851da177e4SLinus Torvalds do { 13861da177e4SLinus Torvalds char *suffix = silly + slen - countersize; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds dput(sdentry); 13891da177e4SLinus Torvalds sillycounter++; 13901da177e4SLinus Torvalds sprintf(suffix, "%*.*x", countersize, countersize, sillycounter); 13911da177e4SLinus Torvalds 13921e7cb3dcSChuck Lever dfprintk(VFS, "NFS: trying to rename %s to %s\n", 13931da177e4SLinus Torvalds dentry->d_name.name, silly); 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds sdentry = lookup_one_len(silly, dentry->d_parent, slen); 13961da177e4SLinus Torvalds /* 13971da177e4SLinus Torvalds * N.B. Better to return EBUSY here ... it could be 13981da177e4SLinus Torvalds * dangerous to delete the file while it's in use. 13991da177e4SLinus Torvalds */ 14001da177e4SLinus Torvalds if (IS_ERR(sdentry)) 14011da177e4SLinus Torvalds goto out; 14021da177e4SLinus Torvalds } while(sdentry->d_inode != NULL); /* need negative lookup */ 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds qsilly.name = silly; 14051da177e4SLinus Torvalds qsilly.len = strlen(silly); 14061da177e4SLinus Torvalds if (dentry->d_inode) { 14071da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 14081da177e4SLinus Torvalds dir, &qsilly); 14095ba7cc48STrond Myklebust nfs_mark_for_revalidate(dentry->d_inode); 14101da177e4SLinus Torvalds } else 14111da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 14121da177e4SLinus Torvalds dir, &qsilly); 14131da177e4SLinus Torvalds if (!error) { 14141da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14151da177e4SLinus Torvalds d_move(dentry, sdentry); 1416e4eff1a6STrond Myklebust error = nfs_async_unlink(dir, dentry); 14171da177e4SLinus Torvalds /* If we return 0 we don't unlink */ 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds dput(sdentry); 14201da177e4SLinus Torvalds out: 14211da177e4SLinus Torvalds return error; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds /* 14251da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 14261da177e4SLinus Torvalds * and after checking that the file has only one user. 14271da177e4SLinus Torvalds * 14281da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 14291da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 14301da177e4SLinus Torvalds */ 14311da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 14341da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14351da177e4SLinus Torvalds int error = -EBUSY; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 14381da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 14411da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 14421da177e4SLinus Torvalds error = 0; 14431da177e4SLinus Torvalds goto out; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds if (inode != NULL) { 1447cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 14481da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 14491da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 14501da177e4SLinus Torvalds if (error == 0) 14511b83d707STrond Myklebust nfs_drop_nlink(inode); 14525ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 14531da177e4SLinus Torvalds } else 14541da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1455d45b9d8bSTrond Myklebust if (error == -ENOENT) 1456d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 14571da177e4SLinus Torvalds out: 14581da177e4SLinus Torvalds return error; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 14621da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 14631da177e4SLinus Torvalds * 14641da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 14651da177e4SLinus Torvalds */ 14661da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 14671da177e4SLinus Torvalds { 14681da177e4SLinus Torvalds int error; 14691da177e4SLinus Torvalds int need_rehash = 0; 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 14721da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds spin_lock(&dcache_lock); 14751da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 14761da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) > 1) { 14771da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 14781da177e4SLinus Torvalds spin_unlock(&dcache_lock); 1479ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1480ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 14811da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 14821da177e4SLinus Torvalds return error; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 14851da177e4SLinus Torvalds __d_drop(dentry); 14861da177e4SLinus Torvalds need_rehash = 1; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 14891da177e4SLinus Torvalds spin_unlock(&dcache_lock); 14901da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1491d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 14921da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 14931da177e4SLinus Torvalds } else if (need_rehash) 14941da177e4SLinus Torvalds d_rehash(dentry); 14951da177e4SLinus Torvalds return error; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds 1498873101b3SChuck Lever /* 1499873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1500873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1501873101b3SChuck Lever * using prepare_write/commit_write. 1502873101b3SChuck Lever * 1503873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1504873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1505873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1506873101b3SChuck Lever * symlink request has completed on the server. 1507873101b3SChuck Lever * 1508873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1509873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1510873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1511873101b3SChuck Lever * and move the raw page into its mapping. 1512873101b3SChuck Lever */ 1513873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 15141da177e4SLinus Torvalds { 1515873101b3SChuck Lever struct pagevec lru_pvec; 1516873101b3SChuck Lever struct page *page; 1517873101b3SChuck Lever char *kaddr; 15181da177e4SLinus Torvalds struct iattr attr; 1519873101b3SChuck Lever unsigned int pathlen = strlen(symname); 15201da177e4SLinus Torvalds int error; 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 15231da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 15241da177e4SLinus Torvalds 1525873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1526873101b3SChuck Lever return -ENAMETOOLONG; 15271da177e4SLinus Torvalds 1528873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1529873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 15301da177e4SLinus Torvalds 153183d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 153276566991STrond Myklebust if (!page) 1533873101b3SChuck Lever return -ENOMEM; 1534873101b3SChuck Lever 1535873101b3SChuck Lever kaddr = kmap_atomic(page, KM_USER0); 1536873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1537873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1538873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1539873101b3SChuck Lever kunmap_atomic(kaddr, KM_USER0); 1540873101b3SChuck Lever 154194a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1542873101b3SChuck Lever if (error != 0) { 1543873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1544873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1545873101b3SChuck Lever dentry->d_name.name, symname, error); 15461da177e4SLinus Torvalds d_drop(dentry); 1547873101b3SChuck Lever __free_page(page); 15481da177e4SLinus Torvalds return error; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 1551873101b3SChuck Lever /* 1552873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1553873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1554873101b3SChuck Lever */ 1555873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1556873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1557873101b3SChuck Lever GFP_KERNEL)) { 155839cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 15594f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1560873101b3SChuck Lever SetPageUptodate(page); 1561873101b3SChuck Lever unlock_page(page); 1562873101b3SChuck Lever } else 1563873101b3SChuck Lever __free_page(page); 1564873101b3SChuck Lever 1565873101b3SChuck Lever return 0; 1566873101b3SChuck Lever } 1567873101b3SChuck Lever 15681da177e4SLinus Torvalds static int 15691da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 15701da177e4SLinus Torvalds { 15711da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 15721da177e4SLinus Torvalds int error; 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 15751da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 15761da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 15771da177e4SLinus Torvalds 15789a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 15799a3936aaSTrond Myklebust 15809697d234STrond Myklebust d_drop(dentry); 15811da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1582cf809556STrond Myklebust if (error == 0) { 1583*7de9c6eeSAl Viro ihold(inode); 15849697d234STrond Myklebust d_add(dentry, inode); 1585cf809556STrond Myklebust } 15861da177e4SLinus Torvalds return error; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* 15901da177e4SLinus Torvalds * RENAME 15911da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 15921da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 15931da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 15941da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 15951da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 15961da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 15971da177e4SLinus Torvalds * 15981da177e4SLinus Torvalds * FIXED. 15991da177e4SLinus Torvalds * 16001da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 16011da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 16021da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 16031da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 16041da177e4SLinus Torvalds * using the inode layer 16051da177e4SLinus Torvalds * 16061da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 16071da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 16081da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 16091da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 16101da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 16111da177e4SLinus Torvalds * the rename. 16121da177e4SLinus Torvalds */ 16131da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 16141da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 16171da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 16181da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 16191da177e4SLinus Torvalds int error = -EBUSY; 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 16221da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 16231da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 16241da177e4SLinus Torvalds atomic_read(&new_dentry->d_count)); 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds /* 162728f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 162828f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 162928f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 163028f79a1aSMiklos Szeredi * the new target. 16311da177e4SLinus Torvalds */ 163227226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 163327226104SMiklos Szeredi /* 163427226104SMiklos Szeredi * To prevent any new references to the target during the 163527226104SMiklos Szeredi * rename, we unhash the dentry in advance. 163627226104SMiklos Szeredi */ 163727226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 163827226104SMiklos Szeredi d_drop(new_dentry); 163927226104SMiklos Szeredi rehash = new_dentry; 164027226104SMiklos Szeredi } 164127226104SMiklos Szeredi 164227226104SMiklos Szeredi if (atomic_read(&new_dentry->d_count) > 2) { 16431da177e4SLinus Torvalds int err; 164427226104SMiklos Szeredi 16451da177e4SLinus Torvalds /* copy the target dentry's name */ 16461da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 16471da177e4SLinus Torvalds &new_dentry->d_name); 16481da177e4SLinus Torvalds if (!dentry) 16491da177e4SLinus Torvalds goto out; 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds /* silly-rename the existing target ... */ 16521da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 165324e93025SMiklos Szeredi if (err) 16541da177e4SLinus Torvalds goto out; 165524e93025SMiklos Szeredi 165624e93025SMiklos Szeredi new_dentry = dentry; 165756335936SOGAWA Hirofumi rehash = NULL; 165824e93025SMiklos Szeredi new_inode = NULL; 1659b1e4adf4STrond Myklebust } 166027226104SMiklos Szeredi } 16611da177e4SLinus Torvalds 1662cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 1663b1e4adf4STrond Myklebust if (new_inode != NULL) 166424174119STrond Myklebust nfs_inode_return_delegation(new_inode); 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 16671da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 16685ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 16691da177e4SLinus Torvalds out: 16701da177e4SLinus Torvalds if (rehash) 16711da177e4SLinus Torvalds d_rehash(rehash); 16721da177e4SLinus Torvalds if (!error) { 1673b1e4adf4STrond Myklebust if (new_inode != NULL) 1674b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 16751da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 16768fb559f8SChuck Lever nfs_set_verifier(new_dentry, 16778fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1678d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 1679d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds /* new dentry created? */ 16821da177e4SLinus Torvalds if (dentry) 16831da177e4SLinus Torvalds dput(dentry); 16841da177e4SLinus Torvalds return error; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 1687cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 1688cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 1689cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 1690cfcea3e8STrond Myklebust 16911c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 16921c3c07e9STrond Myklebust { 16931c3c07e9STrond Myklebust put_rpccred(entry->cred); 16941c3c07e9STrond Myklebust kfree(entry); 1695cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 1696cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 1697cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 16981c3c07e9STrond Myklebust } 16991c3c07e9STrond Myklebust 17001a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 17011a81bb8aSTrond Myklebust { 17021a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 17031a81bb8aSTrond Myklebust 17041a81bb8aSTrond Myklebust while (!list_empty(head)) { 17051a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 17061a81bb8aSTrond Myklebust list_del(&cache->lru); 17071a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 17081a81bb8aSTrond Myklebust } 17091a81bb8aSTrond Myklebust } 17101a81bb8aSTrond Myklebust 17117f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) 1712979df72eSTrond Myklebust { 1713979df72eSTrond Myklebust LIST_HEAD(head); 1714979df72eSTrond Myklebust struct nfs_inode *nfsi; 1715979df72eSTrond Myklebust struct nfs_access_entry *cache; 1716979df72eSTrond Myklebust 171761d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 171861d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 17199c7e7e23STrond Myklebust 1720a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 1721979df72eSTrond Myklebust list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) { 1722979df72eSTrond Myklebust struct inode *inode; 1723979df72eSTrond Myklebust 1724979df72eSTrond Myklebust if (nr_to_scan-- == 0) 1725979df72eSTrond Myklebust break; 17269c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 1727979df72eSTrond Myklebust spin_lock(&inode->i_lock); 1728979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 1729979df72eSTrond Myklebust goto remove_lru_entry; 1730979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 1731979df72eSTrond Myklebust struct nfs_access_entry, lru); 1732979df72eSTrond Myklebust list_move(&cache->lru, &head); 1733979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1734979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 1735979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 1736979df72eSTrond Myklebust &nfs_access_lru_list); 1737979df72eSTrond Myklebust else { 1738979df72eSTrond Myklebust remove_lru_entry: 1739979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 17409c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 1741979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 17429c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 1743979df72eSTrond Myklebust } 174459844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 1745979df72eSTrond Myklebust } 1746979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 17471a81bb8aSTrond Myklebust nfs_access_free_list(&head); 1748979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 1749979df72eSTrond Myklebust } 1750979df72eSTrond Myklebust 17511a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 17521c3c07e9STrond Myklebust { 17531c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 17541a81bb8aSTrond Myklebust struct rb_node *n; 17551c3c07e9STrond Myklebust struct nfs_access_entry *entry; 17561c3c07e9STrond Myklebust 17571c3c07e9STrond Myklebust /* Unhook entries from the cache */ 17581c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 17591c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 17601c3c07e9STrond Myklebust rb_erase(n, root_node); 17611a81bb8aSTrond Myklebust list_move(&entry->lru, head); 17621c3c07e9STrond Myklebust } 17631c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 17641c3c07e9STrond Myklebust } 17651c3c07e9STrond Myklebust 17661c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 17671c3c07e9STrond Myklebust { 17681a81bb8aSTrond Myklebust LIST_HEAD(head); 17691a81bb8aSTrond Myklebust 17701a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 17711a81bb8aSTrond Myklebust return; 1772cfcea3e8STrond Myklebust /* Remove from global LRU init */ 1773cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 17741a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 1775cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 1776cfcea3e8STrond Myklebust 17771c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 17781a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 17791a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 17801a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 17811a81bb8aSTrond Myklebust nfs_access_free_list(&head); 17821c3c07e9STrond Myklebust } 17831c3c07e9STrond Myklebust 17841c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 17851c3c07e9STrond Myklebust { 17861c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 17871c3c07e9STrond Myklebust struct nfs_access_entry *entry; 17881c3c07e9STrond Myklebust 17891c3c07e9STrond Myklebust while (n != NULL) { 17901c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 17911c3c07e9STrond Myklebust 17921c3c07e9STrond Myklebust if (cred < entry->cred) 17931c3c07e9STrond Myklebust n = n->rb_left; 17941c3c07e9STrond Myklebust else if (cred > entry->cred) 17951c3c07e9STrond Myklebust n = n->rb_right; 17961c3c07e9STrond Myklebust else 17971c3c07e9STrond Myklebust return entry; 17981c3c07e9STrond Myklebust } 17991c3c07e9STrond Myklebust return NULL; 18001c3c07e9STrond Myklebust } 18011c3c07e9STrond Myklebust 1802af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 18031da177e4SLinus Torvalds { 180455296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 18051c3c07e9STrond Myklebust struct nfs_access_entry *cache; 18061c3c07e9STrond Myklebust int err = -ENOENT; 18071da177e4SLinus Torvalds 18081c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 18091c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 18101c3c07e9STrond Myklebust goto out_zap; 18111c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 18121c3c07e9STrond Myklebust if (cache == NULL) 18131c3c07e9STrond Myklebust goto out; 1814b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 181564672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 18161c3c07e9STrond Myklebust goto out_stale; 18171c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 18181c3c07e9STrond Myklebust res->cred = cache->cred; 18191c3c07e9STrond Myklebust res->mask = cache->mask; 1820cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 18211c3c07e9STrond Myklebust err = 0; 18221c3c07e9STrond Myklebust out: 18231c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 18241c3c07e9STrond Myklebust return err; 18251c3c07e9STrond Myklebust out_stale: 18261c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1827cfcea3e8STrond Myklebust list_del(&cache->lru); 18281c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 18291c3c07e9STrond Myklebust nfs_access_free_entry(cache); 18301da177e4SLinus Torvalds return -ENOENT; 18311c3c07e9STrond Myklebust out_zap: 18321a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 18331a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 18341c3c07e9STrond Myklebust return -ENOENT; 18351c3c07e9STrond Myklebust } 18361c3c07e9STrond Myklebust 18371c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 18381c3c07e9STrond Myklebust { 1839cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1840cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 18411c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 18421c3c07e9STrond Myklebust struct rb_node *parent = NULL; 18431c3c07e9STrond Myklebust struct nfs_access_entry *entry; 18441c3c07e9STrond Myklebust 18451c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 18461c3c07e9STrond Myklebust while (*p != NULL) { 18471c3c07e9STrond Myklebust parent = *p; 18481c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 18491c3c07e9STrond Myklebust 18501c3c07e9STrond Myklebust if (set->cred < entry->cred) 18511c3c07e9STrond Myklebust p = &parent->rb_left; 18521c3c07e9STrond Myklebust else if (set->cred > entry->cred) 18531c3c07e9STrond Myklebust p = &parent->rb_right; 18541c3c07e9STrond Myklebust else 18551c3c07e9STrond Myklebust goto found; 18561c3c07e9STrond Myklebust } 18571c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 18581c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 1859cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 18601c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 18611c3c07e9STrond Myklebust return; 18621c3c07e9STrond Myklebust found: 18631c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 1864cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 1865cfcea3e8STrond Myklebust list_del(&entry->lru); 18661c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 18671c3c07e9STrond Myklebust nfs_access_free_entry(entry); 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 1870af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 18711da177e4SLinus Torvalds { 18721c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 18731c3c07e9STrond Myklebust if (cache == NULL) 18741c3c07e9STrond Myklebust return; 18751c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 18761da177e4SLinus Torvalds cache->jiffies = set->jiffies; 18771c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 18781da177e4SLinus Torvalds cache->mask = set->mask; 18791c3c07e9STrond Myklebust 18801c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 1881cfcea3e8STrond Myklebust 1882cfcea3e8STrond Myklebust /* Update accounting */ 1883cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 1884cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 1885cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 1886cfcea3e8STrond Myklebust 1887cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 18881a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 1889cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 18901a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 18911a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 18921a81bb8aSTrond Myklebust &nfs_access_lru_list); 1893cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 1894cfcea3e8STrond Myklebust } 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 18981da177e4SLinus Torvalds { 18991da177e4SLinus Torvalds struct nfs_access_entry cache; 19001da177e4SLinus Torvalds int status; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 19031da177e4SLinus Torvalds if (status == 0) 19041da177e4SLinus Torvalds goto out; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 19071da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 19081da177e4SLinus Torvalds cache.cred = cred; 19091da177e4SLinus Torvalds cache.jiffies = jiffies; 19101da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 1911a71ee337SSuresh Jayaraman if (status != 0) { 1912a71ee337SSuresh Jayaraman if (status == -ESTALE) { 1913a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 1914a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 1915a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 1916a71ee337SSuresh Jayaraman } 19171da177e4SLinus Torvalds return status; 1918a71ee337SSuresh Jayaraman } 19191da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 19201da177e4SLinus Torvalds out: 1921e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 19221da177e4SLinus Torvalds return 0; 19231da177e4SLinus Torvalds return -EACCES; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 1926af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 1927af22f94aSTrond Myklebust { 1928af22f94aSTrond Myklebust int mask = 0; 1929af22f94aSTrond Myklebust 1930af22f94aSTrond Myklebust if (openflags & FMODE_READ) 1931af22f94aSTrond Myklebust mask |= MAY_READ; 1932af22f94aSTrond Myklebust if (openflags & FMODE_WRITE) 1933af22f94aSTrond Myklebust mask |= MAY_WRITE; 1934af22f94aSTrond Myklebust if (openflags & FMODE_EXEC) 1935af22f94aSTrond Myklebust mask |= MAY_EXEC; 1936af22f94aSTrond Myklebust return mask; 1937af22f94aSTrond Myklebust } 1938af22f94aSTrond Myklebust 1939af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 1940af22f94aSTrond Myklebust { 1941af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 1942af22f94aSTrond Myklebust } 1943af22f94aSTrond Myklebust 1944e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask) 19451da177e4SLinus Torvalds { 19461da177e4SLinus Torvalds struct rpc_cred *cred; 19471da177e4SLinus Torvalds int res = 0; 19481da177e4SLinus Torvalds 194991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 195091d5b470SChuck Lever 1951e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 19521da177e4SLinus Torvalds goto out; 19531da177e4SLinus Torvalds /* Is this sys_access() ? */ 19549cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 19551da177e4SLinus Torvalds goto force_lookup; 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 19581da177e4SLinus Torvalds case S_IFLNK: 19591da177e4SLinus Torvalds goto out; 19601da177e4SLinus Torvalds case S_IFREG: 19611da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 19621da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 19637ee2cb7fSFrank Filz && (mask & MAY_OPEN) 19647ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 19651da177e4SLinus Torvalds goto out; 19661da177e4SLinus Torvalds break; 19671da177e4SLinus Torvalds case S_IFDIR: 19681da177e4SLinus Torvalds /* 19691da177e4SLinus Torvalds * Optimize away all write operations, since the server 19701da177e4SLinus Torvalds * will check permissions when we perform the op. 19711da177e4SLinus Torvalds */ 19721da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 19731da177e4SLinus Torvalds goto out; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds force_lookup: 19771da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 19781da177e4SLinus Torvalds goto out_notsup; 19791da177e4SLinus Torvalds 198098a8e323STrond Myklebust cred = rpc_lookup_cred(); 19811da177e4SLinus Torvalds if (!IS_ERR(cred)) { 19821da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 19831da177e4SLinus Torvalds put_rpccred(cred); 19841da177e4SLinus Torvalds } else 19851da177e4SLinus Torvalds res = PTR_ERR(cred); 19861da177e4SLinus Torvalds out: 1987f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 1988f696a365SMiklos Szeredi res = -EACCES; 1989f696a365SMiklos Szeredi 19901e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 19911e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 19921da177e4SLinus Torvalds return res; 19931da177e4SLinus Torvalds out_notsup: 19941da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 19951da177e4SLinus Torvalds if (res == 0) 19961da177e4SLinus Torvalds res = generic_permission(inode, mask, NULL); 19971e7cb3dcSChuck Lever goto out; 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds /* 20011da177e4SLinus Torvalds * Local variables: 20021da177e4SLinus Torvalds * version-control: t 20031da177e4SLinus Torvalds * kept-new-versions: 5 20041da177e4SLinus Torvalds * End: 20051da177e4SLinus Torvalds */ 2006