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" 41cd9a1c0eSTrond Myklebust #include "fscache.h" 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 461da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 471da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 481da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 491da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 501da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 511da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 521da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 531da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 541da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 551da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 561da177e4SLinus Torvalds struct inode *, struct dentry *); 577ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int); 58f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int); 591da177e4SLinus Torvalds 604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = { 61f0dd2136STrond Myklebust .llseek = nfs_llseek_dir, 621da177e4SLinus Torvalds .read = generic_read_dir, 631da177e4SLinus Torvalds .readdir = nfs_readdir, 641da177e4SLinus Torvalds .open = nfs_opendir, 651da177e4SLinus Torvalds .release = nfs_release, 661da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 671da177e4SLinus Torvalds }; 681da177e4SLinus Torvalds 6992e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = { 701da177e4SLinus Torvalds .create = nfs_create, 711da177e4SLinus Torvalds .lookup = nfs_lookup, 721da177e4SLinus Torvalds .link = nfs_link, 731da177e4SLinus Torvalds .unlink = nfs_unlink, 741da177e4SLinus Torvalds .symlink = nfs_symlink, 751da177e4SLinus Torvalds .mkdir = nfs_mkdir, 761da177e4SLinus Torvalds .rmdir = nfs_rmdir, 771da177e4SLinus Torvalds .mknod = nfs_mknod, 781da177e4SLinus Torvalds .rename = nfs_rename, 791da177e4SLinus Torvalds .permission = nfs_permission, 801da177e4SLinus Torvalds .getattr = nfs_getattr, 811da177e4SLinus Torvalds .setattr = nfs_setattr, 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 84b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 8592e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = { 86b7fa0554SAndreas Gruenbacher .create = nfs_create, 87b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 88b7fa0554SAndreas Gruenbacher .link = nfs_link, 89b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 90b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 91b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 92b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 93b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 94b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 95b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 96b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 97b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 98b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 99b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 100b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 101b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 102b7fa0554SAndreas Gruenbacher }; 103b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 104b7fa0554SAndreas Gruenbacher 1051da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 108*c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd); 10992e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = { 110*c0204fd2STrond Myklebust .create = nfs_open_create, 1111da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1121da177e4SLinus Torvalds .link = nfs_link, 1131da177e4SLinus Torvalds .unlink = nfs_unlink, 1141da177e4SLinus Torvalds .symlink = nfs_symlink, 1151da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1161da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1171da177e4SLinus Torvalds .mknod = nfs_mknod, 1181da177e4SLinus Torvalds .rename = nfs_rename, 1191da177e4SLinus Torvalds .permission = nfs_permission, 1201da177e4SLinus Torvalds .getattr = nfs_getattr, 1211da177e4SLinus Torvalds .setattr = nfs_setattr, 1226b3b5496SJ. Bruce Fields .getxattr = nfs4_getxattr, 1236b3b5496SJ. Bruce Fields .setxattr = nfs4_setxattr, 1246b3b5496SJ. Bruce Fields .listxattr = nfs4_listxattr, 1251da177e4SLinus Torvalds }; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * Open file 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds static int 1331da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1341da177e4SLinus Torvalds { 1357451c4f0SCarsten Otte int res; 1361da177e4SLinus Torvalds 1376da24bc9SChuck Lever dfprintk(FILE, "NFS: open dir(%s/%s)\n", 138cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 139cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 140cc0dd2d1SChuck Lever 141cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1421e7cb3dcSChuck Lever 1431da177e4SLinus Torvalds /* Call generic open code in order to cache credentials */ 1441da177e4SLinus Torvalds res = nfs_open(inode, filp); 145f5a73672SNeil Brown if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { 146f5a73672SNeil Brown /* This is a mountpoint, so d_revalidate will never 147f5a73672SNeil Brown * have been called, so we need to refresh the 148f5a73672SNeil Brown * inode (for close-open consistency) ourselves. 149f5a73672SNeil Brown */ 150f5a73672SNeil Brown __nfs_revalidate_inode(NFS_SERVER(inode), inode); 151f5a73672SNeil Brown } 1521da177e4SLinus Torvalds return res; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1550dbb4c67SAl Viro typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int); 1561da177e4SLinus Torvalds typedef struct { 1571da177e4SLinus Torvalds struct file *file; 1581da177e4SLinus Torvalds struct page *page; 1591da177e4SLinus Torvalds unsigned long page_index; 1600dbb4c67SAl Viro __be32 *ptr; 161f0dd2136STrond Myklebust u64 *dir_cookie; 162f0dd2136STrond Myklebust loff_t current_index; 1631da177e4SLinus Torvalds struct nfs_entry *entry; 1641da177e4SLinus Torvalds decode_dirent_t decode; 1651da177e4SLinus Torvalds int plus; 1661f4eab7eSNeil Brown unsigned long timestamp; 1674704f0e2STrond Myklebust unsigned long gencount; 1681f4eab7eSNeil Brown int timestamp_valid; 1691da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent 1721da177e4SLinus Torvalds * data directly in the page cache. 1731da177e4SLinus Torvalds * 1741da177e4SLinus Torvalds * Inode invalidation due to refresh etc. takes care of 1751da177e4SLinus Torvalds * _everything_, no sloppy entry flushing logic, no extraneous 1761da177e4SLinus Torvalds * copying, network direct to page cache, the way it was meant 1771da177e4SLinus Torvalds * to be. 1781da177e4SLinus Torvalds * 1791da177e4SLinus Torvalds * NOTE: Dirent information verification is done always by the 1801da177e4SLinus Torvalds * page-in of the RPC reply, nowhere else, this simplies 1811da177e4SLinus Torvalds * things substantially. 1821da177e4SLinus Torvalds */ 1831da177e4SLinus Torvalds static 1841da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page) 1851da177e4SLinus Torvalds { 1861da177e4SLinus Torvalds struct file *file = desc->file; 18701cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 1881da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 1894704f0e2STrond Myklebust unsigned long timestamp, gencount; 1901da177e4SLinus Torvalds int error; 1911da177e4SLinus Torvalds 1921e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n", 1933110ff80SHarvey Harrison __func__, (long long)desc->entry->cookie, 1941e7cb3dcSChuck Lever page->index); 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds again: 1971da177e4SLinus Torvalds timestamp = jiffies; 1984704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 19901cce933SJosef "Jeff" Sipek error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page, 2001da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 2011da177e4SLinus Torvalds if (error < 0) { 2021da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 2031da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 2041da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 2053a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 2061da177e4SLinus Torvalds desc->plus = 0; 2071da177e4SLinus Torvalds goto again; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds goto error; 2101da177e4SLinus Torvalds } 2111f4eab7eSNeil Brown desc->timestamp = timestamp; 2124704f0e2STrond Myklebust desc->gencount = gencount; 2131f4eab7eSNeil Brown desc->timestamp_valid = 1; 2141da177e4SLinus Torvalds SetPageUptodate(page); 2151da177e4SLinus Torvalds /* Ensure consistent page alignment of the data. 2161da177e4SLinus Torvalds * Note: assumes we have exclusive access to this mapping either 2171b1dcc1bSJes Sorensen * through inode->i_mutex or some other mechanism. 2181da177e4SLinus Torvalds */ 2192aac05a9STrond Myklebust if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { 220cd9ae2b6STrond Myklebust /* Should never happen */ 221cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 222cd9ae2b6STrond Myklebust } 2231da177e4SLinus Torvalds unlock_page(page); 2241da177e4SLinus Torvalds return 0; 2251da177e4SLinus Torvalds error: 2261da177e4SLinus Torvalds unlock_page(page); 2271da177e4SLinus Torvalds return -EIO; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static inline 2311da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc) 2321da177e4SLinus Torvalds { 2330dbb4c67SAl Viro __be32 *p = desc->ptr; 2341da177e4SLinus Torvalds p = desc->decode(p, desc->entry, desc->plus); 2351da177e4SLinus Torvalds if (IS_ERR(p)) 2361da177e4SLinus Torvalds return PTR_ERR(p); 2371da177e4SLinus Torvalds desc->ptr = p; 2384704f0e2STrond Myklebust if (desc->timestamp_valid) { 2391f4eab7eSNeil Brown desc->entry->fattr->time_start = desc->timestamp; 2404704f0e2STrond Myklebust desc->entry->fattr->gencount = desc->gencount; 2414704f0e2STrond Myklebust } else 2421f4eab7eSNeil Brown desc->entry->fattr->valid &= ~NFS_ATTR_FATTR; 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static inline 2471da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds kunmap(desc->page); 2501da177e4SLinus Torvalds page_cache_release(desc->page); 2511da177e4SLinus Torvalds desc->page = NULL; 2521da177e4SLinus Torvalds desc->ptr = NULL; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* 2561da177e4SLinus Torvalds * Given a pointer to a buffer that has already been filled by a call 257f0dd2136STrond Myklebust * to readdir, find the next entry with cookie '*desc->dir_cookie'. 2581da177e4SLinus Torvalds * 2591da177e4SLinus Torvalds * If the end of the buffer has been reached, return -EAGAIN, if not, 2601da177e4SLinus Torvalds * return the offset within the buffer of the next entry to be 2611da177e4SLinus Torvalds * read. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds static inline 26400a92642SOlivier Galibert int find_dirent(nfs_readdir_descriptor_t *desc) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 2671da177e4SLinus Torvalds int loop_count = 0, 2681da177e4SLinus Torvalds status; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds while((status = dir_decode(desc)) == 0) { 2711e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n", 2723110ff80SHarvey Harrison __func__, (unsigned long long)entry->cookie); 273f0dd2136STrond Myklebust if (entry->prev_cookie == *desc->dir_cookie) 2741da177e4SLinus Torvalds break; 2751da177e4SLinus Torvalds if (loop_count++ > 200) { 2761da177e4SLinus Torvalds loop_count = 0; 2771da177e4SLinus Torvalds schedule(); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds return status; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /* 28400a92642SOlivier Galibert * Given a pointer to a buffer that has already been filled by a call 285f0dd2136STrond Myklebust * to readdir, find the entry at offset 'desc->file->f_pos'. 28600a92642SOlivier Galibert * 28700a92642SOlivier Galibert * If the end of the buffer has been reached, return -EAGAIN, if not, 28800a92642SOlivier Galibert * return the offset within the buffer of the next entry to be 28900a92642SOlivier Galibert * read. 29000a92642SOlivier Galibert */ 29100a92642SOlivier Galibert static inline 29200a92642SOlivier Galibert int find_dirent_index(nfs_readdir_descriptor_t *desc) 29300a92642SOlivier Galibert { 29400a92642SOlivier Galibert struct nfs_entry *entry = desc->entry; 29500a92642SOlivier Galibert int loop_count = 0, 29600a92642SOlivier Galibert status; 29700a92642SOlivier Galibert 29800a92642SOlivier Galibert for(;;) { 29900a92642SOlivier Galibert status = dir_decode(desc); 30000a92642SOlivier Galibert if (status) 30100a92642SOlivier Galibert break; 30200a92642SOlivier Galibert 3031e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n", 3041e7cb3dcSChuck Lever (unsigned long long)entry->cookie, desc->current_index); 30500a92642SOlivier Galibert 306f0dd2136STrond Myklebust if (desc->file->f_pos == desc->current_index) { 307f0dd2136STrond Myklebust *desc->dir_cookie = entry->cookie; 30800a92642SOlivier Galibert break; 30900a92642SOlivier Galibert } 31000a92642SOlivier Galibert desc->current_index++; 31100a92642SOlivier Galibert if (loop_count++ > 200) { 31200a92642SOlivier Galibert loop_count = 0; 31300a92642SOlivier Galibert schedule(); 31400a92642SOlivier Galibert } 31500a92642SOlivier Galibert } 31600a92642SOlivier Galibert return status; 31700a92642SOlivier Galibert } 31800a92642SOlivier Galibert 31900a92642SOlivier Galibert /* 32000a92642SOlivier Galibert * Find the given page, and call find_dirent() or find_dirent_index in 32100a92642SOlivier Galibert * order to try to return the next entry. 3221da177e4SLinus Torvalds */ 3231da177e4SLinus Torvalds static inline 3241da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc) 3251da177e4SLinus Torvalds { 32601cce933SJosef "Jeff" Sipek struct inode *inode = desc->file->f_path.dentry->d_inode; 3271da177e4SLinus Torvalds struct page *page; 3281da177e4SLinus Torvalds int status; 3291da177e4SLinus Torvalds 3301e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n", 3313110ff80SHarvey Harrison __func__, desc->page_index, 3321e7cb3dcSChuck Lever (long long) *desc->dir_cookie); 3331da177e4SLinus Torvalds 3341f4eab7eSNeil Brown /* If we find the page in the page_cache, we cannot be sure 3351f4eab7eSNeil Brown * how fresh the data is, so we will ignore readdir_plus attributes. 3361f4eab7eSNeil Brown */ 3371f4eab7eSNeil Brown desc->timestamp_valid = 0; 3381da177e4SLinus Torvalds page = read_cache_page(inode->i_mapping, desc->page_index, 3391da177e4SLinus Torvalds (filler_t *)nfs_readdir_filler, desc); 3401da177e4SLinus Torvalds if (IS_ERR(page)) { 3411da177e4SLinus Torvalds status = PTR_ERR(page); 3421da177e4SLinus Torvalds goto out; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* NOTE: Someone else may have changed the READDIRPLUS flag */ 3461da177e4SLinus Torvalds desc->page = page; 3471da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 348f0dd2136STrond Myklebust if (*desc->dir_cookie != 0) 34900a92642SOlivier Galibert status = find_dirent(desc); 35000a92642SOlivier Galibert else 35100a92642SOlivier Galibert status = find_dirent_index(desc); 3521da177e4SLinus Torvalds if (status < 0) 3531da177e4SLinus Torvalds dir_page_release(desc); 3541da177e4SLinus Torvalds out: 3553110ff80SHarvey Harrison dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 3561da177e4SLinus Torvalds return status; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* 3601da177e4SLinus Torvalds * Recurse through the page cache pages, and return a 3611da177e4SLinus Torvalds * filled nfs_entry structure of the next directory entry if possible. 3621da177e4SLinus Torvalds * 363f0dd2136STrond Myklebust * The target for the search is '*desc->dir_cookie' if non-0, 364f0dd2136STrond Myklebust * 'desc->file->f_pos' otherwise 3651da177e4SLinus Torvalds */ 3661da177e4SLinus Torvalds static inline 3671da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds int loop_count = 0; 3701da177e4SLinus Torvalds int res; 3711da177e4SLinus Torvalds 37200a92642SOlivier Galibert /* Always search-by-index from the beginning of the cache */ 373f0dd2136STrond Myklebust if (*desc->dir_cookie == 0) { 3741e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n", 3751e7cb3dcSChuck Lever (long long)desc->file->f_pos); 37600a92642SOlivier Galibert desc->page_index = 0; 37700a92642SOlivier Galibert desc->entry->cookie = desc->entry->prev_cookie = 0; 37800a92642SOlivier Galibert desc->entry->eof = 0; 37900a92642SOlivier Galibert desc->current_index = 0; 380f0dd2136STrond Myklebust } else 3811e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", 3821e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 38300a92642SOlivier Galibert 3841da177e4SLinus Torvalds for (;;) { 3851da177e4SLinus Torvalds res = find_dirent_page(desc); 3861da177e4SLinus Torvalds if (res != -EAGAIN) 3871da177e4SLinus Torvalds break; 3881da177e4SLinus Torvalds /* Align to beginning of next page */ 3891da177e4SLinus Torvalds desc->page_index ++; 3901da177e4SLinus Torvalds if (loop_count++ > 200) { 3911da177e4SLinus Torvalds loop_count = 0; 3921da177e4SLinus Torvalds schedule(); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds } 3951e7cb3dcSChuck Lever 3963110ff80SHarvey Harrison dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res); 3971da177e4SLinus Torvalds return res; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc); 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /* 4081da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds static 4111da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 4121da177e4SLinus Torvalds filldir_t filldir) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds struct file *file = desc->file; 4151da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 4161da177e4SLinus Torvalds struct dentry *dentry = NULL; 4174e769b93SPeter Staubach u64 fileid; 4181da177e4SLinus Torvalds int loop_count = 0, 4191da177e4SLinus Torvalds res; 4201da177e4SLinus Torvalds 4211e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", 4221e7cb3dcSChuck Lever (unsigned long long)entry->cookie); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds for(;;) { 4251da177e4SLinus Torvalds unsigned d_type = DT_UNKNOWN; 4261da177e4SLinus Torvalds /* Note: entry->prev_cookie contains the cookie for 4271da177e4SLinus Torvalds * retrieving the current dirent on the server */ 4284e769b93SPeter Staubach fileid = entry->ino; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds /* Get a dentry if we have one */ 4311da177e4SLinus Torvalds if (dentry != NULL) 4321da177e4SLinus Torvalds dput(dentry); 4331da177e4SLinus Torvalds dentry = nfs_readdir_lookup(desc); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds /* Use readdirplus info */ 4361da177e4SLinus Torvalds if (dentry != NULL && dentry->d_inode != NULL) { 4371da177e4SLinus Torvalds d_type = dt_type(dentry->d_inode); 4384e769b93SPeter Staubach fileid = NFS_FILEID(dentry->d_inode); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds res = filldir(dirent, entry->name, entry->len, 442f43bf0beSTrond Myklebust file->f_pos, nfs_compat_user_ino64(fileid), 443f43bf0beSTrond Myklebust d_type); 4441da177e4SLinus Torvalds if (res < 0) 4451da177e4SLinus Torvalds break; 44600a92642SOlivier Galibert file->f_pos++; 447f0dd2136STrond Myklebust *desc->dir_cookie = entry->cookie; 4481da177e4SLinus Torvalds if (dir_decode(desc) != 0) { 4491da177e4SLinus Torvalds desc->page_index ++; 4501da177e4SLinus Torvalds break; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds if (loop_count++ > 200) { 4531da177e4SLinus Torvalds loop_count = 0; 4541da177e4SLinus Torvalds schedule(); 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds dir_page_release(desc); 4581da177e4SLinus Torvalds if (dentry != NULL) 4591da177e4SLinus Torvalds dput(dentry); 4601e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", 4611e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie, res); 4621da177e4SLinus Torvalds return res; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* 4661da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 4671da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 4681da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 4691da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 4701da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 4711da177e4SLinus Torvalds * 4721da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 4731da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 4741da177e4SLinus Torvalds * we should already have a complete representation of the 4751da177e4SLinus Torvalds * directory in the page cache by the time we get here. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds static inline 4781da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 4791da177e4SLinus Torvalds filldir_t filldir) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds struct file *file = desc->file; 48201cce933SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 4831da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 4841da177e4SLinus Torvalds struct page *page = NULL; 4851da177e4SLinus Torvalds int status; 4864704f0e2STrond Myklebust unsigned long timestamp, gencount; 4871da177e4SLinus Torvalds 4881e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", 4891e7cb3dcSChuck Lever (unsigned long long)*desc->dir_cookie); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 4921da177e4SLinus Torvalds if (!page) { 4931da177e4SLinus Torvalds status = -ENOMEM; 4941da177e4SLinus Torvalds goto out; 4951da177e4SLinus Torvalds } 4961f4eab7eSNeil Brown timestamp = jiffies; 4974704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter(); 49825606656SJeff Layton status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, 49925606656SJeff Layton *desc->dir_cookie, page, 5001da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, 5011da177e4SLinus Torvalds desc->plus); 5021da177e4SLinus Torvalds desc->page = page; 5031da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 50425606656SJeff Layton if (status >= 0) { 5051f4eab7eSNeil Brown desc->timestamp = timestamp; 5064704f0e2STrond Myklebust desc->gencount = gencount; 5071f4eab7eSNeil Brown desc->timestamp_valid = 1; 5081da177e4SLinus Torvalds if ((status = dir_decode(desc)) == 0) 509f0dd2136STrond Myklebust desc->entry->prev_cookie = *desc->dir_cookie; 5101da177e4SLinus Torvalds } else 5111da177e4SLinus Torvalds status = -EIO; 5121da177e4SLinus Torvalds if (status < 0) 5131da177e4SLinus Torvalds goto out_release; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* Reset read descriptor so it searches the page cache from 5181da177e4SLinus Torvalds * the start upon the next call to readdir_search_pagecache() */ 5191da177e4SLinus Torvalds desc->page_index = 0; 5201da177e4SLinus Torvalds desc->entry->cookie = desc->entry->prev_cookie = 0; 5211da177e4SLinus Torvalds desc->entry->eof = 0; 5221da177e4SLinus Torvalds out: 5231e7cb3dcSChuck Lever dfprintk(DIRCACHE, "NFS: %s: returns %d\n", 5243110ff80SHarvey Harrison __func__, status); 5251da177e4SLinus Torvalds return status; 5261da177e4SLinus Torvalds out_release: 5271da177e4SLinus Torvalds dir_page_release(desc); 5281da177e4SLinus Torvalds goto out; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 53100a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A 53200a92642SOlivier Galibert last cookie cache takes care of the common case of reading the 53300a92642SOlivier Galibert whole directory. 5341da177e4SLinus Torvalds */ 5351da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 5361da177e4SLinus Torvalds { 53701cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 5381da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5391da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 5401da177e4SLinus Torvalds *desc = &my_desc; 5411da177e4SLinus Torvalds struct nfs_entry my_entry; 542aa49b4cfSTrond Myklebust int res = -ENOMEM; 5431da177e4SLinus Torvalds 5446da24bc9SChuck Lever dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", 5451e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 5461e7cb3dcSChuck Lever (long long)filp->f_pos); 54791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 54891d5b470SChuck Lever 5491da177e4SLinus Torvalds /* 55000a92642SOlivier Galibert * filp->f_pos points to the dirent entry number. 551f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have 55200a92642SOlivier Galibert * to either find the entry with the appropriate number or 55300a92642SOlivier Galibert * revalidate the cookie. 5541da177e4SLinus Torvalds */ 5551da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds desc->file = filp; 558cd3758e3STrond Myklebust desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie; 5591da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 5601da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds my_entry.cookie = my_entry.prev_cookie = 0; 5631da177e4SLinus Torvalds my_entry.eof = 0; 564aa49b4cfSTrond Myklebust my_entry.fh = nfs_alloc_fhandle(); 565aa49b4cfSTrond Myklebust my_entry.fattr = nfs_alloc_fattr(); 566aa49b4cfSTrond Myklebust if (my_entry.fh == NULL || my_entry.fattr == NULL) 567aa49b4cfSTrond Myklebust goto out_alloc_failed; 568aa49b4cfSTrond Myklebust 5691da177e4SLinus Torvalds desc->entry = &my_entry; 5701da177e4SLinus Torvalds 571565277f6STrond Myklebust nfs_block_sillyrename(dentry); 5721cda707dSTrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 573fccca7fcSTrond Myklebust if (res < 0) 574fccca7fcSTrond Myklebust goto out; 575fccca7fcSTrond Myklebust 5761da177e4SLinus Torvalds while(!desc->entry->eof) { 5771da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 57800a92642SOlivier Galibert 5791da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 5801da177e4SLinus Torvalds /* This means either end of directory */ 581f0dd2136STrond Myklebust if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) { 5821da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 5831da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 5841da177e4SLinus Torvalds if (res >= 0) 5851da177e4SLinus Torvalds continue; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds res = 0; 5881da177e4SLinus Torvalds break; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 5913a10c30aSBenny Halevy clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 5921da177e4SLinus Torvalds nfs_zap_caches(inode); 5931da177e4SLinus Torvalds desc->plus = 0; 5941da177e4SLinus Torvalds desc->entry->eof = 0; 5951da177e4SLinus Torvalds continue; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds if (res < 0) 5981da177e4SLinus Torvalds break; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 6011da177e4SLinus Torvalds if (res < 0) { 6021da177e4SLinus Torvalds res = 0; 6031da177e4SLinus Torvalds break; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds } 606fccca7fcSTrond Myklebust out: 607565277f6STrond Myklebust nfs_unblock_sillyrename(dentry); 6081e7cb3dcSChuck Lever if (res > 0) 6091e7cb3dcSChuck Lever res = 0; 610aa49b4cfSTrond Myklebust out_alloc_failed: 611aa49b4cfSTrond Myklebust nfs_free_fattr(my_entry.fattr); 612aa49b4cfSTrond Myklebust nfs_free_fhandle(my_entry.fh); 613aa49b4cfSTrond Myklebust dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", 6141e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 6151e7cb3dcSChuck Lever res); 6161da177e4SLinus Torvalds return res; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 61910afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 620f0dd2136STrond Myklebust { 621b84e06c5SChuck Lever struct dentry *dentry = filp->f_path.dentry; 622b84e06c5SChuck Lever struct inode *inode = dentry->d_inode; 623b84e06c5SChuck Lever 6246da24bc9SChuck Lever dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", 625b84e06c5SChuck Lever dentry->d_parent->d_name.name, 626b84e06c5SChuck Lever dentry->d_name.name, 627b84e06c5SChuck Lever offset, origin); 628b84e06c5SChuck Lever 629b84e06c5SChuck Lever mutex_lock(&inode->i_mutex); 630f0dd2136STrond Myklebust switch (origin) { 631f0dd2136STrond Myklebust case 1: 632f0dd2136STrond Myklebust offset += filp->f_pos; 633f0dd2136STrond Myklebust case 0: 634f0dd2136STrond Myklebust if (offset >= 0) 635f0dd2136STrond Myklebust break; 636f0dd2136STrond Myklebust default: 637f0dd2136STrond Myklebust offset = -EINVAL; 638f0dd2136STrond Myklebust goto out; 639f0dd2136STrond Myklebust } 640f0dd2136STrond Myklebust if (offset != filp->f_pos) { 641f0dd2136STrond Myklebust filp->f_pos = offset; 642cd3758e3STrond Myklebust nfs_file_open_context(filp)->dir_cookie = 0; 643f0dd2136STrond Myklebust } 644f0dd2136STrond Myklebust out: 645b84e06c5SChuck Lever mutex_unlock(&inode->i_mutex); 646f0dd2136STrond Myklebust return offset; 647f0dd2136STrond Myklebust } 648f0dd2136STrond Myklebust 6491da177e4SLinus Torvalds /* 6501da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 6511da177e4SLinus Torvalds * is a dummy operation. 6521da177e4SLinus Torvalds */ 6537ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync) 6541da177e4SLinus Torvalds { 6557ea80859SChristoph Hellwig struct dentry *dentry = filp->f_path.dentry; 6567ea80859SChristoph Hellwig 6576da24bc9SChuck Lever dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", 6581e7cb3dcSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 6591e7cb3dcSChuck Lever datasync); 6601e7cb3dcSChuck Lever 66154917786SChuck Lever nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); 6621da177e4SLinus Torvalds return 0; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 665bfc69a45STrond Myklebust /** 666bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed 667bfc69a45STrond Myklebust * @dir - pointer to directory inode 668bfc69a45STrond Myklebust * 669bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a 670bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs 671bfc69a45STrond Myklebust * on the server that might have invalidated our dcache. 672bfc69a45STrond Myklebust * 673bfc69a45STrond Myklebust * The caller should be holding dir->i_lock 674bfc69a45STrond Myklebust */ 675bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir) 676bfc69a45STrond Myklebust { 677011935a0STrond Myklebust NFS_I(dir)->cache_change_attribute++; 678bfc69a45STrond Myklebust } 679bfc69a45STrond Myklebust 6801da177e4SLinus Torvalds /* 6811da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 6821da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 6831da177e4SLinus Torvalds * and may need to be looked up again. 6841da177e4SLinus Torvalds */ 685c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 6861da177e4SLinus Torvalds { 6871da177e4SLinus Torvalds if (IS_ROOT(dentry)) 6881da177e4SLinus Torvalds return 1; 6894eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 6904eec952eSTrond Myklebust return 0; 691f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 6926ecc5e8fSTrond Myklebust return 0; 693f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */ 694f2c77f4eSTrond Myklebust if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 695f2c77f4eSTrond Myklebust return 0; 696f2c77f4eSTrond Myklebust if (!nfs_verify_change_attribute(dir, dentry->d_time)) 697f2c77f4eSTrond Myklebust return 0; 698f2c77f4eSTrond Myklebust return 1; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* 7021d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 7031d6757fbSTrond Myklebust * 7041d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 7051d6757fbSTrond Myklebust * component of the path. 7061d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 7071d6757fbSTrond Myklebust */ 7081d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) 7091d6757fbSTrond Myklebust { 7101d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 7111d6757fbSTrond Myklebust return 0; 7121d6757fbSTrond Myklebust return nd->flags & mask; 7131d6757fbSTrond Myklebust } 7141d6757fbSTrond Myklebust 7151d6757fbSTrond Myklebust /* 716a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do 717a12802caSTrond Myklebust * an O_EXCL create using this path component. 718a12802caSTrond Myklebust */ 719a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 720a12802caSTrond Myklebust { 721a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2) 722a12802caSTrond Myklebust return 0; 7233516586aSAl Viro return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); 724a12802caSTrond Myklebust } 725a12802caSTrond Myklebust 726a12802caSTrond Myklebust /* 7271d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 7281d6757fbSTrond Myklebust * 7291d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 7301d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 7311d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 7321d6757fbSTrond Myklebust * 7331d6757fbSTrond Myklebust */ 7341da177e4SLinus Torvalds static inline 7351da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 7361da177e4SLinus Torvalds { 7371da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 7381da177e4SLinus Torvalds 7394e99a1ffSTrond Myklebust if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags)) 7404e99a1ffSTrond Myklebust return 0; 7411da177e4SLinus Torvalds if (nd != NULL) { 7421da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 7431d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 7441da177e4SLinus Torvalds goto out_force; 7451da177e4SLinus Torvalds /* This is an open(2) */ 7461d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 7474e0641a7STrond Myklebust !(server->flags & NFS_MOUNT_NOCTO) && 7484e0641a7STrond Myklebust (S_ISREG(inode->i_mode) || 7494e0641a7STrond Myklebust S_ISDIR(inode->i_mode))) 7501da177e4SLinus Torvalds goto out_force; 7514f48af45STrond Myklebust return 0; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 7541da177e4SLinus Torvalds out_force: 7551da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* 7591da177e4SLinus Torvalds * We judge how long we want to trust negative 7601da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 7611da177e4SLinus Torvalds * 7621da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 7631da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds static inline 7661da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 7671da177e4SLinus Torvalds struct nameidata *nd) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 7701d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 7711da177e4SLinus Torvalds return 0; 7724eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 7734eec952eSTrond Myklebust return 1; 7741da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* 7781da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 7791da177e4SLinus Torvalds * and we should check whether we can really trust that 7801da177e4SLinus Torvalds * lookup. 7811da177e4SLinus Torvalds * 7821da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 7831da177e4SLinus Torvalds * we have an inode! 7841da177e4SLinus Torvalds * 7851da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 7861da177e4SLinus Torvalds * cached dentry and do a new lookup. 7871da177e4SLinus Torvalds */ 7881da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) 7891da177e4SLinus Torvalds { 7901da177e4SLinus Torvalds struct inode *dir; 7911da177e4SLinus Torvalds struct inode *inode; 7921da177e4SLinus Torvalds struct dentry *parent; 793e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 794e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 7951da177e4SLinus Torvalds int error; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds parent = dget_parent(dentry); 7981da177e4SLinus Torvalds dir = parent->d_inode; 79991d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 8001da177e4SLinus Torvalds inode = dentry->d_inode; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (!inode) { 8031da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 8041da177e4SLinus Torvalds goto out_bad; 8051da177e4SLinus Torvalds goto out_valid; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds if (is_bad_inode(inode)) { 8091e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", 8103110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8111e7cb3dcSChuck Lever dentry->d_name.name); 8121da177e4SLinus Torvalds goto out_bad; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 81515860ab1STrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 81615860ab1STrond Myklebust goto out_set_verifier; 81715860ab1STrond Myklebust 8181da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 819a12802caSTrond Myklebust if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { 8201da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 8211da177e4SLinus Torvalds goto out_zap_parent; 8221da177e4SLinus Torvalds goto out_valid; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds if (NFS_STALE(inode)) 8261da177e4SLinus Torvalds goto out_bad; 8271da177e4SLinus Torvalds 828e1fb4d05STrond Myklebust error = -ENOMEM; 829e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 830e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 831e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 832e1fb4d05STrond Myklebust goto out_error; 833e1fb4d05STrond Myklebust 834e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 8351da177e4SLinus Torvalds if (error) 8361da177e4SLinus Torvalds goto out_bad; 837e1fb4d05STrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle)) 8381da177e4SLinus Torvalds goto out_bad; 839e1fb4d05STrond Myklebust if ((error = nfs_refresh_inode(inode, fattr)) != 0) 8401da177e4SLinus Torvalds goto out_bad; 8411da177e4SLinus Torvalds 842e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 843e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 84415860ab1STrond Myklebust out_set_verifier: 845cf8ba45eSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 8461da177e4SLinus Torvalds out_valid: 8471da177e4SLinus Torvalds dput(parent); 8481e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 8493110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8501e7cb3dcSChuck Lever dentry->d_name.name); 8511da177e4SLinus Torvalds return 1; 8521da177e4SLinus Torvalds out_zap_parent: 8531da177e4SLinus Torvalds nfs_zap_caches(dir); 8541da177e4SLinus Torvalds out_bad: 855a1643a92STrond Myklebust nfs_mark_for_revalidate(dir); 8561da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 8571da177e4SLinus Torvalds /* Purge readdir caches. */ 8581da177e4SLinus Torvalds nfs_zap_caches(inode); 8591da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 8601da177e4SLinus Torvalds if (have_submounts(dentry)) 8611da177e4SLinus Torvalds goto out_valid; 862d9e80b7dSAl Viro if (dentry->d_flags & DCACHE_DISCONNECTED) 863d9e80b7dSAl Viro goto out_valid; 8641da177e4SLinus Torvalds shrink_dcache_parent(dentry); 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds d_drop(dentry); 867e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 868e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 8691da177e4SLinus Torvalds dput(parent); 8701e7cb3dcSChuck Lever dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 8713110ff80SHarvey Harrison __func__, dentry->d_parent->d_name.name, 8721e7cb3dcSChuck Lever dentry->d_name.name); 8731da177e4SLinus Torvalds return 0; 874e1fb4d05STrond Myklebust out_error: 875e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 876e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 877e1fb4d05STrond Myklebust dput(parent); 878e1fb4d05STrond Myklebust dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", 879e1fb4d05STrond Myklebust __func__, dentry->d_parent->d_name.name, 880e1fb4d05STrond Myklebust dentry->d_name.name, error); 881e1fb4d05STrond Myklebust return error; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds /* 8851da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 8861da177e4SLinus Torvalds */ 8871da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry) 8881da177e4SLinus Torvalds { 8891da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 8901da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 8911da177e4SLinus Torvalds dentry->d_flags); 8921da177e4SLinus Torvalds 89377f11192STrond Myklebust /* Unhash any dentry with a stale inode */ 89477f11192STrond Myklebust if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) 89577f11192STrond Myklebust return 1; 89677f11192STrond Myklebust 8971da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 8981da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 8991da177e4SLinus Torvalds return 1; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 9021da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 9031da177e4SLinus Torvalds * files will be cleaned up during umount */ 9041da177e4SLinus Torvalds return 1; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds return 0; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode) 9111b83d707STrond Myklebust { 9121b83d707STrond Myklebust spin_lock(&inode->i_lock); 9131b83d707STrond Myklebust if (inode->i_nlink > 0) 9141b83d707STrond Myklebust drop_nlink(inode); 9151b83d707STrond Myklebust spin_unlock(&inode->i_lock); 9161b83d707STrond Myklebust } 9171b83d707STrond Myklebust 9181da177e4SLinus Torvalds /* 9191da177e4SLinus Torvalds * Called when the dentry loses inode. 9201da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 9211da177e4SLinus Torvalds */ 9221da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 9231da177e4SLinus Torvalds { 92483672d39SNeil Brown if (S_ISDIR(inode->i_mode)) 92583672d39SNeil Brown /* drop any readdir cache as it could easily be old */ 92683672d39SNeil Brown NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 92783672d39SNeil Brown 9281da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 9299a53c3a7SDave Hansen drop_nlink(inode); 930e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode); 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds iput(inode); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = { 9361da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 9371da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 9381da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 9391da177e4SLinus Torvalds }; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 9421da177e4SLinus Torvalds { 9431da177e4SLinus Torvalds struct dentry *res; 944565277f6STrond Myklebust struct dentry *parent; 9451da177e4SLinus Torvalds struct inode *inode = NULL; 946e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL; 947e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL; 9481da177e4SLinus Torvalds int error; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 9511da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 95291d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 9551da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 9561da177e4SLinus Torvalds goto out; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 9591da177e4SLinus Torvalds 960fd684071STrond Myklebust /* 961fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup 962fd684071STrond Myklebust * but don't hash the dentry. 963fd684071STrond Myklebust */ 964fd684071STrond Myklebust if (nfs_is_exclusive_create(dir, nd)) { 965fd684071STrond Myklebust d_instantiate(dentry, NULL); 966fd684071STrond Myklebust res = NULL; 967fc0f684cSTrond Myklebust goto out; 968fd684071STrond Myklebust } 9691da177e4SLinus Torvalds 970e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM); 971e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle(); 972e1fb4d05STrond Myklebust fattr = nfs_alloc_fattr(); 973e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL) 974e1fb4d05STrond Myklebust goto out; 975e1fb4d05STrond Myklebust 976565277f6STrond Myklebust parent = dentry->d_parent; 977565277f6STrond Myklebust /* Protect against concurrent sillydeletes */ 978565277f6STrond Myklebust nfs_block_sillyrename(parent); 979e1fb4d05STrond Myklebust error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 9801da177e4SLinus Torvalds if (error == -ENOENT) 9811da177e4SLinus Torvalds goto no_entry; 9821da177e4SLinus Torvalds if (error < 0) { 9831da177e4SLinus Torvalds res = ERR_PTR(error); 984565277f6STrond Myklebust goto out_unblock_sillyrename; 9851da177e4SLinus Torvalds } 986e1fb4d05STrond Myklebust inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 98703f28e3aSTrond Myklebust res = (struct dentry *)inode; 98803f28e3aSTrond Myklebust if (IS_ERR(res)) 989565277f6STrond Myklebust goto out_unblock_sillyrename; 99054ceac45SDavid Howells 9911da177e4SLinus Torvalds no_entry: 99254ceac45SDavid Howells res = d_materialise_unique(dentry, inode); 9939eaef27bSTrond Myklebust if (res != NULL) { 9949eaef27bSTrond Myklebust if (IS_ERR(res)) 995565277f6STrond Myklebust goto out_unblock_sillyrename; 9961da177e4SLinus Torvalds dentry = res; 9979eaef27bSTrond Myklebust } 9981da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 999565277f6STrond Myklebust out_unblock_sillyrename: 1000565277f6STrond Myklebust nfs_unblock_sillyrename(parent); 10011da177e4SLinus Torvalds out: 1002e1fb4d05STrond Myklebust nfs_free_fattr(fattr); 1003e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle); 10041da177e4SLinus Torvalds return res; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 10081da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 10091da177e4SLinus Torvalds 1010f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = { 10111da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 10121da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 10131da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 10141da177e4SLinus Torvalds }; 10151da177e4SLinus Torvalds 10161d6757fbSTrond Myklebust /* 10171d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 10181d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 10191d6757fbSTrond Myklebust */ 10205584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd) 10211da177e4SLinus Torvalds { 10221d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 10231da177e4SLinus Torvalds return 0; 10241da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 10251da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 10261da177e4SLinus Torvalds return 0; 10271da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 10282c463e95SDave Hansen if (__mnt_is_readonly(nd->path.mnt) && 10292c463e95SDave Hansen (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 10301da177e4SLinus Torvalds return 0; 10311da177e4SLinus Torvalds return 1; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 1034cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd) 1035cd9a1c0eSTrond Myklebust { 1036cd9a1c0eSTrond Myklebust struct path path = { 1037cd9a1c0eSTrond Myklebust .mnt = nd->path.mnt, 1038cd9a1c0eSTrond Myklebust .dentry = dentry, 1039cd9a1c0eSTrond Myklebust }; 1040cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1041cd9a1c0eSTrond Myklebust struct rpc_cred *cred; 1042cd9a1c0eSTrond Myklebust fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); 1043cd9a1c0eSTrond Myklebust 1044cd9a1c0eSTrond Myklebust cred = rpc_lookup_cred(); 1045cd9a1c0eSTrond Myklebust if (IS_ERR(cred)) 1046cd9a1c0eSTrond Myklebust return ERR_CAST(cred); 1047cd9a1c0eSTrond Myklebust ctx = alloc_nfs_open_context(&path, cred, fmode); 1048cd9a1c0eSTrond Myklebust put_rpccred(cred); 1049cd9a1c0eSTrond Myklebust if (ctx == NULL) 1050cd9a1c0eSTrond Myklebust return ERR_PTR(-ENOMEM); 1051cd9a1c0eSTrond Myklebust return ctx; 1052cd9a1c0eSTrond Myklebust } 1053cd9a1c0eSTrond Myklebust 1054cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp) 1055cd9a1c0eSTrond Myklebust { 1056cd9a1c0eSTrond Myklebust nfs_fscache_set_inode_cookie(inode, filp); 1057cd9a1c0eSTrond Myklebust return 0; 1058cd9a1c0eSTrond Myklebust } 1059cd9a1c0eSTrond Myklebust 1060cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) 1061cd9a1c0eSTrond Myklebust { 1062cd9a1c0eSTrond Myklebust struct file *filp; 1063cd9a1c0eSTrond Myklebust int ret = 0; 1064cd9a1c0eSTrond Myklebust 1065cd9a1c0eSTrond Myklebust /* If the open_intent is for execute, we have an extra check to make */ 1066cd9a1c0eSTrond Myklebust if (ctx->mode & FMODE_EXEC) { 1067cd9a1c0eSTrond Myklebust ret = nfs_may_open(ctx->path.dentry->d_inode, 1068cd9a1c0eSTrond Myklebust ctx->cred, 1069cd9a1c0eSTrond Myklebust nd->intent.open.flags); 1070cd9a1c0eSTrond Myklebust if (ret < 0) 1071cd9a1c0eSTrond Myklebust goto out; 1072cd9a1c0eSTrond Myklebust } 1073cd9a1c0eSTrond Myklebust filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open); 1074cd9a1c0eSTrond Myklebust if (IS_ERR(filp)) 1075cd9a1c0eSTrond Myklebust ret = PTR_ERR(filp); 1076cd9a1c0eSTrond Myklebust else 1077cd9a1c0eSTrond Myklebust nfs_file_set_open_context(filp, ctx); 1078cd9a1c0eSTrond Myklebust out: 1079cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1080cd9a1c0eSTrond Myklebust return ret; 1081cd9a1c0eSTrond Myklebust } 1082cd9a1c0eSTrond Myklebust 10831da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 10841da177e4SLinus Torvalds { 1085cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx; 1086cd9a1c0eSTrond Myklebust struct iattr attr; 10871da177e4SLinus Torvalds struct dentry *res = NULL; 1088f46e0bd3STrond Myklebust struct inode *inode; 1089cd9a1c0eSTrond Myklebust int open_flags; 10901da177e4SLinus Torvalds 10911e7cb3dcSChuck Lever dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", 10921e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 10931e7cb3dcSChuck Lever 10941da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 10955584c306STrond Myklebust if (!is_atomic_open(nd)) 10961da177e4SLinus Torvalds goto no_open; 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 10991da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 11001da177e4SLinus Torvalds goto out; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 11031da177e4SLinus Torvalds 1104d4d9cdcbSTrond Myklebust /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash 1105d4d9cdcbSTrond Myklebust * the dentry. */ 11063516586aSAl Viro if (nd->flags & LOOKUP_EXCL) { 1107d4d9cdcbSTrond Myklebust d_instantiate(dentry, NULL); 110802a913a7STrond Myklebust goto out; 110902a913a7STrond Myklebust } 11101da177e4SLinus Torvalds 1111cd9a1c0eSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1112cd9a1c0eSTrond Myklebust res = ERR_CAST(ctx); 1113cd9a1c0eSTrond Myklebust if (IS_ERR(ctx)) 1114cd9a1c0eSTrond Myklebust goto out; 1115cd9a1c0eSTrond Myklebust 1116cd9a1c0eSTrond Myklebust open_flags = nd->intent.open.flags; 1117cd9a1c0eSTrond Myklebust if (nd->flags & LOOKUP_CREATE) { 1118cd9a1c0eSTrond Myklebust attr.ia_mode = nd->intent.open.create_mode; 1119cd9a1c0eSTrond Myklebust attr.ia_valid = ATTR_MODE; 1120cd9a1c0eSTrond Myklebust if (!IS_POSIXACL(dir)) 1121cd9a1c0eSTrond Myklebust attr.ia_mode &= ~current_umask(); 1122cd9a1c0eSTrond Myklebust } else { 1123cd9a1c0eSTrond Myklebust open_flags &= ~O_EXCL; 1124cd9a1c0eSTrond Myklebust attr.ia_valid = 0; 1125cd9a1c0eSTrond Myklebust BUG_ON(open_flags & O_CREAT); 1126cd9a1c0eSTrond Myklebust } 1127cd9a1c0eSTrond Myklebust 11281da177e4SLinus Torvalds /* Open the file on the server */ 1129f46e0bd3STrond Myklebust nfs_block_sillyrename(dentry->d_parent); 1130f46e0bd3STrond Myklebust inode = nfs4_atomic_open(dir, ctx, open_flags, &attr); 1131f46e0bd3STrond Myklebust if (IS_ERR(inode)) { 1132f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 1133cd9a1c0eSTrond Myklebust put_nfs_open_context(ctx); 1134f46e0bd3STrond Myklebust switch (PTR_ERR(inode)) { 11351da177e4SLinus Torvalds /* Make a negative dentry */ 11361da177e4SLinus Torvalds case -ENOENT: 1137f46e0bd3STrond Myklebust d_add(dentry, NULL); 113802a913a7STrond Myklebust res = NULL; 113902a913a7STrond Myklebust goto out; 11401da177e4SLinus Torvalds /* This turned out not to be a regular file */ 114180e60639STrond Myklebust case -EISDIR: 11426f926b5bSTrond Myklebust case -ENOTDIR: 11436f926b5bSTrond Myklebust goto no_open; 11441da177e4SLinus Torvalds case -ELOOP: 11451da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 11461da177e4SLinus Torvalds goto no_open; 11471da177e4SLinus Torvalds /* case -EINVAL: */ 11481da177e4SLinus Torvalds default: 1149f46e0bd3STrond Myklebust res = ERR_CAST(inode); 11501da177e4SLinus Torvalds goto out; 11511da177e4SLinus Torvalds } 1152cd9a1c0eSTrond Myklebust } 1153f46e0bd3STrond Myklebust res = d_add_unique(dentry, inode); 1154f46e0bd3STrond Myklebust if (res != NULL) { 1155f46e0bd3STrond Myklebust dput(ctx->path.dentry); 1156f46e0bd3STrond Myklebust ctx->path.dentry = dget(res); 11571da177e4SLinus Torvalds dentry = res; 1158f46e0bd3STrond Myklebust } 1159cd9a1c0eSTrond Myklebust nfs_intent_set_file(nd, ctx); 1160f46e0bd3STrond Myklebust nfs_unblock_sillyrename(dentry->d_parent); 11611da177e4SLinus Torvalds out: 1162f46e0bd3STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11631da177e4SLinus Torvalds return res; 11641da177e4SLinus Torvalds no_open: 11651da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 11691da177e4SLinus Torvalds { 11701da177e4SLinus Torvalds struct dentry *parent = NULL; 11711da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11721da177e4SLinus Torvalds struct inode *dir; 1173b8d4caddSTrond Myklebust struct nfs_open_context *ctx; 11741da177e4SLinus Torvalds int openflags, ret = 0; 11751da177e4SLinus Torvalds 11761f063d2cSTrond Myklebust if (!is_atomic_open(nd) || d_mountpoint(dentry)) 11775584c306STrond Myklebust goto no_open; 11781da177e4SLinus Torvalds parent = dget_parent(dentry); 11791da177e4SLinus Torvalds dir = parent->d_inode; 11801da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 11811da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 11821da177e4SLinus Torvalds */ 1183216d5d06STrond Myklebust if (inode == NULL) { 1184216d5d06STrond Myklebust if (!nfs_neg_need_reval(dir, dentry, nd)) 1185216d5d06STrond Myklebust ret = 1; 11861da177e4SLinus Torvalds goto out; 1187216d5d06STrond Myklebust } 1188216d5d06STrond Myklebust 11891da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 11901da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 11915584c306STrond Myklebust goto no_open_dput; 11921da177e4SLinus Torvalds openflags = nd->intent.open.flags; 11931da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 11941da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 11955584c306STrond Myklebust goto no_open_dput; 11961da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 11970a377cffSTrond Myklebust openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); 11981da177e4SLinus Torvalds 1199b8d4caddSTrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1200b8d4caddSTrond Myklebust ret = PTR_ERR(ctx); 1201b8d4caddSTrond Myklebust if (IS_ERR(ctx)) 1202b8d4caddSTrond Myklebust goto out; 12031da177e4SLinus Torvalds /* 12041b1dcc1bSJes Sorensen * Note: we're not holding inode->i_mutex and so may be racing with 12051da177e4SLinus Torvalds * operations that change the directory. We therefore save the 12061da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 12071da177e4SLinus Torvalds */ 1208535918f1STrond Myklebust inode = nfs4_atomic_open(dir, ctx, openflags, NULL); 1209535918f1STrond Myklebust if (IS_ERR(inode)) { 1210535918f1STrond Myklebust ret = PTR_ERR(inode); 1211535918f1STrond Myklebust switch (ret) { 1212535918f1STrond Myklebust case -EPERM: 1213535918f1STrond Myklebust case -EACCES: 1214535918f1STrond Myklebust case -EDQUOT: 1215535918f1STrond Myklebust case -ENOSPC: 1216535918f1STrond Myklebust case -EROFS: 1217535918f1STrond Myklebust goto out_put_ctx; 1218535918f1STrond Myklebust default: 1219535918f1STrond Myklebust goto out_drop; 1220535918f1STrond Myklebust } 1221535918f1STrond Myklebust } 1222535918f1STrond Myklebust iput(inode); 1223535918f1STrond Myklebust if (inode == dentry->d_inode) { 1224535918f1STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1225b8d4caddSTrond Myklebust nfs_intent_set_file(nd, ctx); 1226535918f1STrond Myklebust } else 1227535918f1STrond Myklebust goto out_drop; 12281da177e4SLinus Torvalds out: 12291da177e4SLinus Torvalds dput(parent); 12301da177e4SLinus Torvalds return ret; 1231535918f1STrond Myklebust out_drop: 1232535918f1STrond Myklebust d_drop(dentry); 1233535918f1STrond Myklebust ret = 0; 1234535918f1STrond Myklebust out_put_ctx: 1235535918f1STrond Myklebust put_nfs_open_context(ctx); 1236535918f1STrond Myklebust goto out; 1237535918f1STrond Myklebust 12385584c306STrond Myklebust no_open_dput: 12391da177e4SLinus Torvalds dput(parent); 12405584c306STrond Myklebust no_open: 12411da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 12421da177e4SLinus Torvalds } 1243*c0204fd2STrond Myklebust 1244*c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, 1245*c0204fd2STrond Myklebust struct nameidata *nd) 1246*c0204fd2STrond Myklebust { 1247*c0204fd2STrond Myklebust struct nfs_open_context *ctx = NULL; 1248*c0204fd2STrond Myklebust struct iattr attr; 1249*c0204fd2STrond Myklebust int error; 1250*c0204fd2STrond Myklebust int open_flags = 0; 1251*c0204fd2STrond Myklebust 1252*c0204fd2STrond Myklebust dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 1253*c0204fd2STrond Myklebust dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 1254*c0204fd2STrond Myklebust 1255*c0204fd2STrond Myklebust attr.ia_mode = mode; 1256*c0204fd2STrond Myklebust attr.ia_valid = ATTR_MODE; 1257*c0204fd2STrond Myklebust 1258*c0204fd2STrond Myklebust if ((nd->flags & LOOKUP_CREATE) != 0) { 1259*c0204fd2STrond Myklebust open_flags = nd->intent.open.flags; 1260*c0204fd2STrond Myklebust 1261*c0204fd2STrond Myklebust ctx = nameidata_to_nfs_open_context(dentry, nd); 1262*c0204fd2STrond Myklebust error = PTR_ERR(ctx); 1263*c0204fd2STrond Myklebust if (IS_ERR(ctx)) 1264*c0204fd2STrond Myklebust goto out_err; 1265*c0204fd2STrond Myklebust } 1266*c0204fd2STrond Myklebust 1267*c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx); 1268*c0204fd2STrond Myklebust if (error != 0) 1269*c0204fd2STrond Myklebust goto out_put_ctx; 1270*c0204fd2STrond Myklebust if (ctx != NULL) 1271*c0204fd2STrond Myklebust nfs_intent_set_file(nd, ctx); 1272*c0204fd2STrond Myklebust return 0; 1273*c0204fd2STrond Myklebust out_put_ctx: 1274*c0204fd2STrond Myklebust if (ctx != NULL) 1275*c0204fd2STrond Myklebust put_nfs_open_context(ctx); 1276*c0204fd2STrond Myklebust out_err: 1277*c0204fd2STrond Myklebust d_drop(dentry); 1278*c0204fd2STrond Myklebust return error; 1279*c0204fd2STrond Myklebust } 1280*c0204fd2STrond Myklebust 12811da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc) 12841da177e4SLinus Torvalds { 128501cce933SJosef "Jeff" Sipek struct dentry *parent = desc->file->f_path.dentry; 12861da177e4SLinus Torvalds struct inode *dir = parent->d_inode; 12871da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 12881da177e4SLinus Torvalds struct dentry *dentry, *alias; 12891da177e4SLinus Torvalds struct qstr name = { 12901da177e4SLinus Torvalds .name = entry->name, 12911da177e4SLinus Torvalds .len = entry->len, 12921da177e4SLinus Torvalds }; 12931da177e4SLinus Torvalds struct inode *inode; 129457fa76f2STrond Myklebust unsigned long verf = nfs_save_change_attribute(dir); 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds switch (name.len) { 12971da177e4SLinus Torvalds case 2: 12981da177e4SLinus Torvalds if (name.name[0] == '.' && name.name[1] == '.') 12991da177e4SLinus Torvalds return dget_parent(parent); 13001da177e4SLinus Torvalds break; 13011da177e4SLinus Torvalds case 1: 13021da177e4SLinus Torvalds if (name.name[0] == '.') 13031da177e4SLinus Torvalds return dget(parent); 13041da177e4SLinus Torvalds } 130557fa76f2STrond Myklebust 130657fa76f2STrond Myklebust spin_lock(&dir->i_lock); 130757fa76f2STrond Myklebust if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) { 130857fa76f2STrond Myklebust spin_unlock(&dir->i_lock); 130957fa76f2STrond Myklebust return NULL; 131057fa76f2STrond Myklebust } 131157fa76f2STrond Myklebust spin_unlock(&dir->i_lock); 131257fa76f2STrond Myklebust 13131da177e4SLinus Torvalds name.hash = full_name_hash(name.name, name.len); 13141da177e4SLinus Torvalds dentry = d_lookup(parent, &name); 1315df1d5d23STrond Myklebust if (dentry != NULL) { 1316ef75c797STrond Myklebust /* Is this a positive dentry that matches the readdir info? */ 1317ef75c797STrond Myklebust if (dentry->d_inode != NULL && 1318ef75c797STrond Myklebust (NFS_FILEID(dentry->d_inode) == entry->ino || 1319ef75c797STrond Myklebust d_mountpoint(dentry))) { 1320ef75c797STrond Myklebust if (!desc->plus || entry->fh->size == 0) 13211da177e4SLinus Torvalds return dentry; 1322ef75c797STrond Myklebust if (nfs_compare_fh(NFS_FH(dentry->d_inode), 1323ef75c797STrond Myklebust entry->fh) == 0) 1324ef75c797STrond Myklebust goto out_renew; 1325ef75c797STrond Myklebust } 1326df1d5d23STrond Myklebust /* No, so d_drop to allow one to be created */ 1327df1d5d23STrond Myklebust d_drop(dentry); 1328df1d5d23STrond Myklebust dput(dentry); 1329df1d5d23STrond Myklebust } 13301da177e4SLinus Torvalds if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR)) 13311da177e4SLinus Torvalds return NULL; 133254af3bb5STrond Myklebust if (name.len > NFS_SERVER(dir)->namelen) 133354af3bb5STrond Myklebust return NULL; 13341b1dcc1bSJes Sorensen /* Note: caller is already holding the dir->i_mutex! */ 13351da177e4SLinus Torvalds dentry = d_alloc(parent, &name); 13361da177e4SLinus Torvalds if (dentry == NULL) 13371da177e4SLinus Torvalds return NULL; 13381da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 13391da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 134003f28e3aSTrond Myklebust if (IS_ERR(inode)) { 13411da177e4SLinus Torvalds dput(dentry); 13421da177e4SLinus Torvalds return NULL; 13431da177e4SLinus Torvalds } 134454ceac45SDavid Howells 134554ceac45SDavid Howells alias = d_materialise_unique(dentry, inode); 13461da177e4SLinus Torvalds if (alias != NULL) { 13471da177e4SLinus Torvalds dput(dentry); 13489eaef27bSTrond Myklebust if (IS_ERR(alias)) 13499eaef27bSTrond Myklebust return NULL; 13501da177e4SLinus Torvalds dentry = alias; 13511da177e4SLinus Torvalds } 135254ceac45SDavid Howells 1353c79ba787STrond Myklebust out_renew: 135457fa76f2STrond Myklebust nfs_set_verifier(dentry, verf); 1355c79ba787STrond Myklebust return dentry; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds /* 13591da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 13601da177e4SLinus Torvalds */ 13611da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 13621da177e4SLinus Torvalds struct nfs_fattr *fattr) 13631da177e4SLinus Torvalds { 1364fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry); 1365fab728e1STrond Myklebust struct inode *dir = parent->d_inode; 13661da177e4SLinus Torvalds struct inode *inode; 13671da177e4SLinus Torvalds int error = -EACCES; 13681da177e4SLinus Torvalds 1369fab728e1STrond Myklebust d_drop(dentry); 1370fab728e1STrond Myklebust 13711da177e4SLinus Torvalds /* We may have been initialized further down */ 13721da177e4SLinus Torvalds if (dentry->d_inode) 1373fab728e1STrond Myklebust goto out; 13741da177e4SLinus Torvalds if (fhandle->size == 0) { 13751da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 13761da177e4SLinus Torvalds if (error) 1377fab728e1STrond Myklebust goto out_error; 13781da177e4SLinus Torvalds } 13795724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 13801da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 13811da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 13828fa5c000SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); 13831da177e4SLinus Torvalds if (error < 0) 1384fab728e1STrond Myklebust goto out_error; 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 138703f28e3aSTrond Myklebust error = PTR_ERR(inode); 138803f28e3aSTrond Myklebust if (IS_ERR(inode)) 1389fab728e1STrond Myklebust goto out_error; 1390fab728e1STrond Myklebust d_add(dentry, inode); 1391fab728e1STrond Myklebust out: 1392fab728e1STrond Myklebust dput(parent); 13931da177e4SLinus Torvalds return 0; 1394fab728e1STrond Myklebust out_error: 1395fab728e1STrond Myklebust nfs_mark_for_revalidate(dir); 1396fab728e1STrond Myklebust dput(parent); 1397fab728e1STrond Myklebust return error; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds /* 14011da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 14021da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 14031da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 14041da177e4SLinus Torvalds * reply path made it appear to have failed. 14051da177e4SLinus Torvalds */ 14061da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 14071da177e4SLinus Torvalds struct nameidata *nd) 14081da177e4SLinus Torvalds { 14091da177e4SLinus Torvalds struct iattr attr; 14101da177e4SLinus Torvalds int error; 14111da177e4SLinus Torvalds 14121e7cb3dcSChuck Lever dfprintk(VFS, "NFS: create(%s/%ld), %s\n", 14131e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds attr.ia_mode = mode; 14161da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 14171da177e4SLinus Torvalds 1418*c0204fd2STrond Myklebust error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL); 14191da177e4SLinus Torvalds if (error != 0) 14201da177e4SLinus Torvalds goto out_err; 14211da177e4SLinus Torvalds return 0; 14221da177e4SLinus Torvalds out_err: 14231da177e4SLinus Torvalds d_drop(dentry); 14241da177e4SLinus Torvalds return error; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds /* 14281da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 14291da177e4SLinus Torvalds */ 14301da177e4SLinus Torvalds static int 14311da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds struct iattr attr; 14341da177e4SLinus Torvalds int status; 14351da177e4SLinus Torvalds 14361e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", 14371e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 14401da177e4SLinus Torvalds return -EINVAL; 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds attr.ia_mode = mode; 14431da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 14461da177e4SLinus Torvalds if (status != 0) 14471da177e4SLinus Torvalds goto out_err; 14481da177e4SLinus Torvalds return 0; 14491da177e4SLinus Torvalds out_err: 14501da177e4SLinus Torvalds d_drop(dentry); 14511da177e4SLinus Torvalds return status; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds /* 14551da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 14561da177e4SLinus Torvalds */ 14571da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 14581da177e4SLinus Torvalds { 14591da177e4SLinus Torvalds struct iattr attr; 14601da177e4SLinus Torvalds int error; 14611da177e4SLinus Torvalds 14621e7cb3dcSChuck Lever dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", 14631e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 14661da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 14691da177e4SLinus Torvalds if (error != 0) 14701da177e4SLinus Torvalds goto out_err; 14711da177e4SLinus Torvalds return 0; 14721da177e4SLinus Torvalds out_err: 14731da177e4SLinus Torvalds d_drop(dentry); 14741da177e4SLinus Torvalds return error; 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds 1477d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry) 1478d45b9d8bSTrond Myklebust { 1479d45b9d8bSTrond Myklebust if (dentry->d_inode != NULL && !d_unhashed(dentry)) 1480d45b9d8bSTrond Myklebust d_delete(dentry); 1481d45b9d8bSTrond Myklebust } 1482d45b9d8bSTrond Myklebust 14831da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 14841da177e4SLinus Torvalds { 14851da177e4SLinus Torvalds int error; 14861da177e4SLinus Torvalds 14871e7cb3dcSChuck Lever dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 14881e7cb3dcSChuck Lever dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 14911da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 14921da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 1493ce71ec36SDave Hansen clear_nlink(dentry->d_inode); 1494d45b9d8bSTrond Myklebust else if (error == -ENOENT) 1495d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds return error; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) 15011da177e4SLinus Torvalds { 15021da177e4SLinus Torvalds static unsigned int sillycounter; 15034e769b93SPeter Staubach const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2; 15041da177e4SLinus Torvalds const int countersize = sizeof(sillycounter)*2; 15054e769b93SPeter Staubach const int slen = sizeof(".nfs")+fileidsize+countersize-1; 15061da177e4SLinus Torvalds char silly[slen+1]; 15071da177e4SLinus Torvalds struct qstr qsilly; 15081da177e4SLinus Torvalds struct dentry *sdentry; 15091da177e4SLinus Torvalds int error = -EIO; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n", 15121da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 15131da177e4SLinus Torvalds atomic_read(&dentry->d_count)); 151491d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_SILLYRENAME); 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds /* 15171da177e4SLinus Torvalds * We don't allow a dentry to be silly-renamed twice. 15181da177e4SLinus Torvalds */ 15191da177e4SLinus Torvalds error = -EBUSY; 15201da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 15211da177e4SLinus Torvalds goto out; 15221da177e4SLinus Torvalds 15234e769b93SPeter Staubach sprintf(silly, ".nfs%*.*Lx", 15244e769b93SPeter Staubach fileidsize, fileidsize, 15254e769b93SPeter Staubach (unsigned long long)NFS_FILEID(dentry->d_inode)); 15261da177e4SLinus Torvalds 152734ea8188STrond Myklebust /* Return delegation in anticipation of the rename */ 152834ea8188STrond Myklebust nfs_inode_return_delegation(dentry->d_inode); 152934ea8188STrond Myklebust 15301da177e4SLinus Torvalds sdentry = NULL; 15311da177e4SLinus Torvalds do { 15321da177e4SLinus Torvalds char *suffix = silly + slen - countersize; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds dput(sdentry); 15351da177e4SLinus Torvalds sillycounter++; 15361da177e4SLinus Torvalds sprintf(suffix, "%*.*x", countersize, countersize, sillycounter); 15371da177e4SLinus Torvalds 15381e7cb3dcSChuck Lever dfprintk(VFS, "NFS: trying to rename %s to %s\n", 15391da177e4SLinus Torvalds dentry->d_name.name, silly); 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds sdentry = lookup_one_len(silly, dentry->d_parent, slen); 15421da177e4SLinus Torvalds /* 15431da177e4SLinus Torvalds * N.B. Better to return EBUSY here ... it could be 15441da177e4SLinus Torvalds * dangerous to delete the file while it's in use. 15451da177e4SLinus Torvalds */ 15461da177e4SLinus Torvalds if (IS_ERR(sdentry)) 15471da177e4SLinus Torvalds goto out; 15481da177e4SLinus Torvalds } while(sdentry->d_inode != NULL); /* need negative lookup */ 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds qsilly.name = silly; 15511da177e4SLinus Torvalds qsilly.len = strlen(silly); 15521da177e4SLinus Torvalds if (dentry->d_inode) { 15531da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 15541da177e4SLinus Torvalds dir, &qsilly); 15555ba7cc48STrond Myklebust nfs_mark_for_revalidate(dentry->d_inode); 15561da177e4SLinus Torvalds } else 15571da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 15581da177e4SLinus Torvalds dir, &qsilly); 15591da177e4SLinus Torvalds if (!error) { 15601da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 15611da177e4SLinus Torvalds d_move(dentry, sdentry); 1562e4eff1a6STrond Myklebust error = nfs_async_unlink(dir, dentry); 15631da177e4SLinus Torvalds /* If we return 0 we don't unlink */ 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds dput(sdentry); 15661da177e4SLinus Torvalds out: 15671da177e4SLinus Torvalds return error; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds /* 15711da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 15721da177e4SLinus Torvalds * and after checking that the file has only one user. 15731da177e4SLinus Torvalds * 15741da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 15751da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 15761da177e4SLinus Torvalds */ 15771da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 15781da177e4SLinus Torvalds { 15791da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 15801da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15811da177e4SLinus Torvalds int error = -EBUSY; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 15841da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 15871da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 15881da177e4SLinus Torvalds error = 0; 15891da177e4SLinus Torvalds goto out; 15901da177e4SLinus Torvalds } 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds if (inode != NULL) { 1593cae7a073STrond Myklebust nfs_inode_return_delegation(inode); 15941da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 15951da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 15961da177e4SLinus Torvalds if (error == 0) 15971b83d707STrond Myklebust nfs_drop_nlink(inode); 15985ba7cc48STrond Myklebust nfs_mark_for_revalidate(inode); 15991da177e4SLinus Torvalds } else 16001da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1601d45b9d8bSTrond Myklebust if (error == -ENOENT) 1602d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry); 16031da177e4SLinus Torvalds out: 16041da177e4SLinus Torvalds return error; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 16081da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 16091da177e4SLinus Torvalds * 16101da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 16111da177e4SLinus Torvalds */ 16121da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds int error; 16151da177e4SLinus Torvalds int need_rehash = 0; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 16181da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds spin_lock(&dcache_lock); 16211da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 16221da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) > 1) { 16231da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 16241da177e4SLinus Torvalds spin_unlock(&dcache_lock); 1625ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */ 1626ccfeb506STrond Myklebust write_inode_now(dentry->d_inode, 0); 16271da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 16281da177e4SLinus Torvalds return error; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 16311da177e4SLinus Torvalds __d_drop(dentry); 16321da177e4SLinus Torvalds need_rehash = 1; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 16351da177e4SLinus Torvalds spin_unlock(&dcache_lock); 16361da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 1637d45b9d8bSTrond Myklebust if (!error || error == -ENOENT) { 16381da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 16391da177e4SLinus Torvalds } else if (need_rehash) 16401da177e4SLinus Torvalds d_rehash(dentry); 16411da177e4SLinus Torvalds return error; 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 1644873101b3SChuck Lever /* 1645873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode, 1646873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk 1647873101b3SChuck Lever * using prepare_write/commit_write. 1648873101b3SChuck Lever * 1649873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first 1650873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see 1651873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 1652873101b3SChuck Lever * symlink request has completed on the server. 1653873101b3SChuck Lever * 1654873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do 1655873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we 1656873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode 1657873101b3SChuck Lever * and move the raw page into its mapping. 1658873101b3SChuck Lever */ 1659873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 16601da177e4SLinus Torvalds { 1661873101b3SChuck Lever struct pagevec lru_pvec; 1662873101b3SChuck Lever struct page *page; 1663873101b3SChuck Lever char *kaddr; 16641da177e4SLinus Torvalds struct iattr attr; 1665873101b3SChuck Lever unsigned int pathlen = strlen(symname); 16661da177e4SLinus Torvalds int error; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 16691da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 16701da177e4SLinus Torvalds 1671873101b3SChuck Lever if (pathlen > PAGE_SIZE) 1672873101b3SChuck Lever return -ENAMETOOLONG; 16731da177e4SLinus Torvalds 1674873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO; 1675873101b3SChuck Lever attr.ia_valid = ATTR_MODE; 16761da177e4SLinus Torvalds 167783d93f22SJeff Layton page = alloc_page(GFP_HIGHUSER); 167876566991STrond Myklebust if (!page) 1679873101b3SChuck Lever return -ENOMEM; 1680873101b3SChuck Lever 1681873101b3SChuck Lever kaddr = kmap_atomic(page, KM_USER0); 1682873101b3SChuck Lever memcpy(kaddr, symname, pathlen); 1683873101b3SChuck Lever if (pathlen < PAGE_SIZE) 1684873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 1685873101b3SChuck Lever kunmap_atomic(kaddr, KM_USER0); 1686873101b3SChuck Lever 168794a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 1688873101b3SChuck Lever if (error != 0) { 1689873101b3SChuck Lever dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", 1690873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino, 1691873101b3SChuck Lever dentry->d_name.name, symname, error); 16921da177e4SLinus Torvalds d_drop(dentry); 1693873101b3SChuck Lever __free_page(page); 16941da177e4SLinus Torvalds return error; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 1697873101b3SChuck Lever /* 1698873101b3SChuck Lever * No big deal if we can't add this page to the page cache here. 1699873101b3SChuck Lever * READLINK will get the missing page from the server if needed. 1700873101b3SChuck Lever */ 1701873101b3SChuck Lever pagevec_init(&lru_pvec, 0); 1702873101b3SChuck Lever if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, 1703873101b3SChuck Lever GFP_KERNEL)) { 170439cf8a13SChuck Lever pagevec_add(&lru_pvec, page); 17054f98a2feSRik van Riel pagevec_lru_add_file(&lru_pvec); 1706873101b3SChuck Lever SetPageUptodate(page); 1707873101b3SChuck Lever unlock_page(page); 1708873101b3SChuck Lever } else 1709873101b3SChuck Lever __free_page(page); 1710873101b3SChuck Lever 1711873101b3SChuck Lever return 0; 1712873101b3SChuck Lever } 1713873101b3SChuck Lever 17141da177e4SLinus Torvalds static int 17151da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 17161da177e4SLinus Torvalds { 17171da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 17181da177e4SLinus Torvalds int error; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 17211da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 17221da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 17231da177e4SLinus Torvalds 17249a3936aaSTrond Myklebust nfs_inode_return_delegation(inode); 17259a3936aaSTrond Myklebust 17269697d234STrond Myklebust d_drop(dentry); 17271da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1728cf809556STrond Myklebust if (error == 0) { 1729cf809556STrond Myklebust atomic_inc(&inode->i_count); 17309697d234STrond Myklebust d_add(dentry, inode); 1731cf809556STrond Myklebust } 17321da177e4SLinus Torvalds return error; 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds /* 17361da177e4SLinus Torvalds * RENAME 17371da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 17381da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 17391da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 17401da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 17411da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 17421da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 17431da177e4SLinus Torvalds * 17441da177e4SLinus Torvalds * FIXED. 17451da177e4SLinus Torvalds * 17461da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 17471da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 17481da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 17491da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 17501da177e4SLinus Torvalds * using the inode layer 17511da177e4SLinus Torvalds * 17521da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 17531da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 17541da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 17551da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 17561da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 17571da177e4SLinus Torvalds * the rename. 17581da177e4SLinus Torvalds */ 17591da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 17601da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 17611da177e4SLinus Torvalds { 17621da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 17631da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 17641da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 17651da177e4SLinus Torvalds int error = -EBUSY; 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 17681da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 17691da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 17701da177e4SLinus Torvalds atomic_read(&new_dentry->d_count)); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds /* 177328f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so, 177428f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the 177528f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes 177628f79a1aSMiklos Szeredi * the new target. 17771da177e4SLinus Torvalds */ 177827226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) { 177927226104SMiklos Szeredi /* 178027226104SMiklos Szeredi * To prevent any new references to the target during the 178127226104SMiklos Szeredi * rename, we unhash the dentry in advance. 178227226104SMiklos Szeredi */ 178327226104SMiklos Szeredi if (!d_unhashed(new_dentry)) { 178427226104SMiklos Szeredi d_drop(new_dentry); 178527226104SMiklos Szeredi rehash = new_dentry; 178627226104SMiklos Szeredi } 178727226104SMiklos Szeredi 178827226104SMiklos Szeredi if (atomic_read(&new_dentry->d_count) > 2) { 17891da177e4SLinus Torvalds int err; 179027226104SMiklos Szeredi 17911da177e4SLinus Torvalds /* copy the target dentry's name */ 17921da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 17931da177e4SLinus Torvalds &new_dentry->d_name); 17941da177e4SLinus Torvalds if (!dentry) 17951da177e4SLinus Torvalds goto out; 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds /* silly-rename the existing target ... */ 17981da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 179924e93025SMiklos Szeredi if (err) 18001da177e4SLinus Torvalds goto out; 180124e93025SMiklos Szeredi 180224e93025SMiklos Szeredi new_dentry = dentry; 180356335936SOGAWA Hirofumi rehash = NULL; 180424e93025SMiklos Szeredi new_inode = NULL; 1805b1e4adf4STrond Myklebust } 180627226104SMiklos Szeredi } 18071da177e4SLinus Torvalds 1808cae7a073STrond Myklebust nfs_inode_return_delegation(old_inode); 1809b1e4adf4STrond Myklebust if (new_inode != NULL) 181024174119STrond Myklebust nfs_inode_return_delegation(new_inode); 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 18131da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 18145ba7cc48STrond Myklebust nfs_mark_for_revalidate(old_inode); 18151da177e4SLinus Torvalds out: 18161da177e4SLinus Torvalds if (rehash) 18171da177e4SLinus Torvalds d_rehash(rehash); 18181da177e4SLinus Torvalds if (!error) { 1819b1e4adf4STrond Myklebust if (new_inode != NULL) 1820b1e4adf4STrond Myklebust nfs_drop_nlink(new_inode); 18211da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 18228fb559f8SChuck Lever nfs_set_verifier(new_dentry, 18238fb559f8SChuck Lever nfs_save_change_attribute(new_dir)); 1824d45b9d8bSTrond Myklebust } else if (error == -ENOENT) 1825d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(old_dentry); 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds /* new dentry created? */ 18281da177e4SLinus Torvalds if (dentry) 18291da177e4SLinus Torvalds dput(dentry); 18301da177e4SLinus Torvalds return error; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 1833cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock); 1834cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list); 1835cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries; 1836cfcea3e8STrond Myklebust 18371c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry) 18381c3c07e9STrond Myklebust { 18391c3c07e9STrond Myklebust put_rpccred(entry->cred); 18401c3c07e9STrond Myklebust kfree(entry); 1841cfcea3e8STrond Myklebust smp_mb__before_atomic_dec(); 1842cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries); 1843cfcea3e8STrond Myklebust smp_mb__after_atomic_dec(); 18441c3c07e9STrond Myklebust } 18451c3c07e9STrond Myklebust 18461a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head) 18471a81bb8aSTrond Myklebust { 18481a81bb8aSTrond Myklebust struct nfs_access_entry *cache; 18491a81bb8aSTrond Myklebust 18501a81bb8aSTrond Myklebust while (!list_empty(head)) { 18511a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru); 18521a81bb8aSTrond Myklebust list_del(&cache->lru); 18531a81bb8aSTrond Myklebust nfs_access_free_entry(cache); 18541a81bb8aSTrond Myklebust } 18551a81bb8aSTrond Myklebust } 18561a81bb8aSTrond Myklebust 18577f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) 1858979df72eSTrond Myklebust { 1859979df72eSTrond Myklebust LIST_HEAD(head); 1860979df72eSTrond Myklebust struct nfs_inode *nfsi; 1861979df72eSTrond Myklebust struct nfs_access_entry *cache; 1862979df72eSTrond Myklebust 186361d5eb29STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 186461d5eb29STrond Myklebust return (nr_to_scan == 0) ? 0 : -1; 18659c7e7e23STrond Myklebust 1866a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock); 1867979df72eSTrond Myklebust list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) { 1868979df72eSTrond Myklebust struct inode *inode; 1869979df72eSTrond Myklebust 1870979df72eSTrond Myklebust if (nr_to_scan-- == 0) 1871979df72eSTrond Myklebust break; 18729c7e7e23STrond Myklebust inode = &nfsi->vfs_inode; 1873979df72eSTrond Myklebust spin_lock(&inode->i_lock); 1874979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru)) 1875979df72eSTrond Myklebust goto remove_lru_entry; 1876979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next, 1877979df72eSTrond Myklebust struct nfs_access_entry, lru); 1878979df72eSTrond Myklebust list_move(&cache->lru, &head); 1879979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1880979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru)) 1881979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru, 1882979df72eSTrond Myklebust &nfs_access_lru_list); 1883979df72eSTrond Myklebust else { 1884979df72eSTrond Myklebust remove_lru_entry: 1885979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru); 18869c7e7e23STrond Myklebust smp_mb__before_clear_bit(); 1887979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 18889c7e7e23STrond Myklebust smp_mb__after_clear_bit(); 1889979df72eSTrond Myklebust } 189059844a9bSTrond Myklebust spin_unlock(&inode->i_lock); 1891979df72eSTrond Myklebust } 1892979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 18931a81bb8aSTrond Myklebust nfs_access_free_list(&head); 1894979df72eSTrond Myklebust return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; 1895979df72eSTrond Myklebust } 1896979df72eSTrond Myklebust 18971a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 18981c3c07e9STrond Myklebust { 18991c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 19001a81bb8aSTrond Myklebust struct rb_node *n; 19011c3c07e9STrond Myklebust struct nfs_access_entry *entry; 19021c3c07e9STrond Myklebust 19031c3c07e9STrond Myklebust /* Unhook entries from the cache */ 19041c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) { 19051c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 19061c3c07e9STrond Myklebust rb_erase(n, root_node); 19071a81bb8aSTrond Myklebust list_move(&entry->lru, head); 19081c3c07e9STrond Myklebust } 19091c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 19101c3c07e9STrond Myklebust } 19111c3c07e9STrond Myklebust 19121c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode) 19131c3c07e9STrond Myklebust { 19141a81bb8aSTrond Myklebust LIST_HEAD(head); 19151a81bb8aSTrond Myklebust 19161a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 19171a81bb8aSTrond Myklebust return; 1918cfcea3e8STrond Myklebust /* Remove from global LRU init */ 1919cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 19201a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 1921cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru); 1922cfcea3e8STrond Myklebust 19231c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 19241a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head); 19251a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 19261a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock); 19271a81bb8aSTrond Myklebust nfs_access_free_list(&head); 19281c3c07e9STrond Myklebust } 19291c3c07e9STrond Myklebust 19301c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) 19311c3c07e9STrond Myklebust { 19321c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 19331c3c07e9STrond Myklebust struct nfs_access_entry *entry; 19341c3c07e9STrond Myklebust 19351c3c07e9STrond Myklebust while (n != NULL) { 19361c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node); 19371c3c07e9STrond Myklebust 19381c3c07e9STrond Myklebust if (cred < entry->cred) 19391c3c07e9STrond Myklebust n = n->rb_left; 19401c3c07e9STrond Myklebust else if (cred > entry->cred) 19411c3c07e9STrond Myklebust n = n->rb_right; 19421c3c07e9STrond Myklebust else 19431c3c07e9STrond Myklebust return entry; 19441c3c07e9STrond Myklebust } 19451c3c07e9STrond Myklebust return NULL; 19461c3c07e9STrond Myklebust } 19471c3c07e9STrond Myklebust 1948af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 19491da177e4SLinus Torvalds { 195055296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode); 19511c3c07e9STrond Myklebust struct nfs_access_entry *cache; 19521c3c07e9STrond Myklebust int err = -ENOENT; 19531da177e4SLinus Torvalds 19541c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 19551c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 19561c3c07e9STrond Myklebust goto out_zap; 19571c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred); 19581c3c07e9STrond Myklebust if (cache == NULL) 19591c3c07e9STrond Myklebust goto out; 1960b4d2314bSTrond Myklebust if (!nfs_have_delegated_attributes(inode) && 196164672d55SPeter Staubach !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) 19621c3c07e9STrond Myklebust goto out_stale; 19631c3c07e9STrond Myklebust res->jiffies = cache->jiffies; 19641c3c07e9STrond Myklebust res->cred = cache->cred; 19651c3c07e9STrond Myklebust res->mask = cache->mask; 1966cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 19671c3c07e9STrond Myklebust err = 0; 19681c3c07e9STrond Myklebust out: 19691c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 19701c3c07e9STrond Myklebust return err; 19711c3c07e9STrond Myklebust out_stale: 19721c3c07e9STrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache); 1973cfcea3e8STrond Myklebust list_del(&cache->lru); 19741c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 19751c3c07e9STrond Myklebust nfs_access_free_entry(cache); 19761da177e4SLinus Torvalds return -ENOENT; 19771c3c07e9STrond Myklebust out_zap: 19781a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock); 19791a81bb8aSTrond Myklebust nfs_access_zap_cache(inode); 19801c3c07e9STrond Myklebust return -ENOENT; 19811c3c07e9STrond Myklebust } 19821c3c07e9STrond Myklebust 19831c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 19841c3c07e9STrond Myklebust { 1985cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1986cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache; 19871c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node; 19881c3c07e9STrond Myklebust struct rb_node *parent = NULL; 19891c3c07e9STrond Myklebust struct nfs_access_entry *entry; 19901c3c07e9STrond Myklebust 19911c3c07e9STrond Myklebust spin_lock(&inode->i_lock); 19921c3c07e9STrond Myklebust while (*p != NULL) { 19931c3c07e9STrond Myklebust parent = *p; 19941c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node); 19951c3c07e9STrond Myklebust 19961c3c07e9STrond Myklebust if (set->cred < entry->cred) 19971c3c07e9STrond Myklebust p = &parent->rb_left; 19981c3c07e9STrond Myklebust else if (set->cred > entry->cred) 19991c3c07e9STrond Myklebust p = &parent->rb_right; 20001c3c07e9STrond Myklebust else 20011c3c07e9STrond Myklebust goto found; 20021c3c07e9STrond Myklebust } 20031c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p); 20041c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node); 2005cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 20061c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20071c3c07e9STrond Myklebust return; 20081c3c07e9STrond Myklebust found: 20091c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node); 2010cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2011cfcea3e8STrond Myklebust list_del(&entry->lru); 20121c3c07e9STrond Myklebust spin_unlock(&inode->i_lock); 20131c3c07e9STrond Myklebust nfs_access_free_entry(entry); 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 2016af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 20171da177e4SLinus Torvalds { 20181c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 20191c3c07e9STrond Myklebust if (cache == NULL) 20201c3c07e9STrond Myklebust return; 20211c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node); 20221da177e4SLinus Torvalds cache->jiffies = set->jiffies; 20231c3c07e9STrond Myklebust cache->cred = get_rpccred(set->cred); 20241da177e4SLinus Torvalds cache->mask = set->mask; 20251c3c07e9STrond Myklebust 20261c3c07e9STrond Myklebust nfs_access_add_rbtree(inode, cache); 2027cfcea3e8STrond Myklebust 2028cfcea3e8STrond Myklebust /* Update accounting */ 2029cfcea3e8STrond Myklebust smp_mb__before_atomic_inc(); 2030cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries); 2031cfcea3e8STrond Myklebust smp_mb__after_atomic_inc(); 2032cfcea3e8STrond Myklebust 2033cfcea3e8STrond Myklebust /* Add inode to global LRU list */ 20341a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2035cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock); 20361a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 20371a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 20381a81bb8aSTrond Myklebust &nfs_access_lru_list); 2039cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock); 2040cfcea3e8STrond Myklebust } 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 20441da177e4SLinus Torvalds { 20451da177e4SLinus Torvalds struct nfs_access_entry cache; 20461da177e4SLinus Torvalds int status; 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 20491da177e4SLinus Torvalds if (status == 0) 20501da177e4SLinus Torvalds goto out; 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 20531da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 20541da177e4SLinus Torvalds cache.cred = cred; 20551da177e4SLinus Torvalds cache.jiffies = jiffies; 20561da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 2057a71ee337SSuresh Jayaraman if (status != 0) { 2058a71ee337SSuresh Jayaraman if (status == -ESTALE) { 2059a71ee337SSuresh Jayaraman nfs_zap_caches(inode); 2060a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode)) 2061a71ee337SSuresh Jayaraman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 2062a71ee337SSuresh Jayaraman } 20631da177e4SLinus Torvalds return status; 2064a71ee337SSuresh Jayaraman } 20651da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 20661da177e4SLinus Torvalds out: 2067e6305c43SAl Viro if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 20681da177e4SLinus Torvalds return 0; 20691da177e4SLinus Torvalds return -EACCES; 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 2072af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags) 2073af22f94aSTrond Myklebust { 2074af22f94aSTrond Myklebust int mask = 0; 2075af22f94aSTrond Myklebust 2076af22f94aSTrond Myklebust if (openflags & FMODE_READ) 2077af22f94aSTrond Myklebust mask |= MAY_READ; 2078af22f94aSTrond Myklebust if (openflags & FMODE_WRITE) 2079af22f94aSTrond Myklebust mask |= MAY_WRITE; 2080af22f94aSTrond Myklebust if (openflags & FMODE_EXEC) 2081af22f94aSTrond Myklebust mask |= MAY_EXEC; 2082af22f94aSTrond Myklebust return mask; 2083af22f94aSTrond Myklebust } 2084af22f94aSTrond Myklebust 2085af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) 2086af22f94aSTrond Myklebust { 2087af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2088af22f94aSTrond Myklebust } 2089af22f94aSTrond Myklebust 2090e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask) 20911da177e4SLinus Torvalds { 20921da177e4SLinus Torvalds struct rpc_cred *cred; 20931da177e4SLinus Torvalds int res = 0; 20941da177e4SLinus Torvalds 209591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS); 209691d5b470SChuck Lever 2097e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 20981da177e4SLinus Torvalds goto out; 20991da177e4SLinus Torvalds /* Is this sys_access() ? */ 21009cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR)) 21011da177e4SLinus Torvalds goto force_lookup; 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 21041da177e4SLinus Torvalds case S_IFLNK: 21051da177e4SLinus Torvalds goto out; 21061da177e4SLinus Torvalds case S_IFREG: 21071da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 21081da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 21097ee2cb7fSFrank Filz && (mask & MAY_OPEN) 21107ee2cb7fSFrank Filz && !(mask & MAY_EXEC)) 21111da177e4SLinus Torvalds goto out; 21121da177e4SLinus Torvalds break; 21131da177e4SLinus Torvalds case S_IFDIR: 21141da177e4SLinus Torvalds /* 21151da177e4SLinus Torvalds * Optimize away all write operations, since the server 21161da177e4SLinus Torvalds * will check permissions when we perform the op. 21171da177e4SLinus Torvalds */ 21181da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 21191da177e4SLinus Torvalds goto out; 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds force_lookup: 21231da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 21241da177e4SLinus Torvalds goto out_notsup; 21251da177e4SLinus Torvalds 212698a8e323STrond Myklebust cred = rpc_lookup_cred(); 21271da177e4SLinus Torvalds if (!IS_ERR(cred)) { 21281da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 21291da177e4SLinus Torvalds put_rpccred(cred); 21301da177e4SLinus Torvalds } else 21311da177e4SLinus Torvalds res = PTR_ERR(cred); 21321da177e4SLinus Torvalds out: 2133f696a365SMiklos Szeredi if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) 2134f696a365SMiklos Szeredi res = -EACCES; 2135f696a365SMiklos Szeredi 21361e7cb3dcSChuck Lever dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 21371e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res); 21381da177e4SLinus Torvalds return res; 21391da177e4SLinus Torvalds out_notsup: 21401da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 21411da177e4SLinus Torvalds if (res == 0) 21421da177e4SLinus Torvalds res = generic_permission(inode, mask, NULL); 21431e7cb3dcSChuck Lever goto out; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds /* 21471da177e4SLinus Torvalds * Local variables: 21481da177e4SLinus Torvalds * version-control: t 21491da177e4SLinus Torvalds * kept-new-versions: 5 21501da177e4SLinus Torvalds * End: 21511da177e4SLinus Torvalds */ 2152