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> 321da177e4SLinus Torvalds #include <linux/smp_lock.h> 331da177e4SLinus Torvalds #include <linux/namei.h> 341da177e4SLinus Torvalds 354ce79717STrond Myklebust #include "nfs4_fs.h" 361da177e4SLinus Torvalds #include "delegation.h" 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #define NFS_PARANOIA 1 391da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *); 421da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t); 431da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); 441da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); 451da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int); 461da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *); 471da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *); 481da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *); 491da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *); 501da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); 511da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *, 521da177e4SLinus Torvalds struct inode *, struct dentry *); 531da177e4SLinus Torvalds static int nfs_fsync_dir(struct file *, struct dentry *, int); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds struct file_operations nfs_dir_operations = { 561da177e4SLinus Torvalds .read = generic_read_dir, 571da177e4SLinus Torvalds .readdir = nfs_readdir, 581da177e4SLinus Torvalds .open = nfs_opendir, 591da177e4SLinus Torvalds .release = nfs_release, 601da177e4SLinus Torvalds .fsync = nfs_fsync_dir, 611da177e4SLinus Torvalds }; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds struct inode_operations nfs_dir_inode_operations = { 641da177e4SLinus Torvalds .create = nfs_create, 651da177e4SLinus Torvalds .lookup = nfs_lookup, 661da177e4SLinus Torvalds .link = nfs_link, 671da177e4SLinus Torvalds .unlink = nfs_unlink, 681da177e4SLinus Torvalds .symlink = nfs_symlink, 691da177e4SLinus Torvalds .mkdir = nfs_mkdir, 701da177e4SLinus Torvalds .rmdir = nfs_rmdir, 711da177e4SLinus Torvalds .mknod = nfs_mknod, 721da177e4SLinus Torvalds .rename = nfs_rename, 731da177e4SLinus Torvalds .permission = nfs_permission, 741da177e4SLinus Torvalds .getattr = nfs_getattr, 751da177e4SLinus Torvalds .setattr = nfs_setattr, 761da177e4SLinus Torvalds }; 771da177e4SLinus Torvalds 78*b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 79*b7fa0554SAndreas Gruenbacher struct inode_operations nfs3_dir_inode_operations = { 80*b7fa0554SAndreas Gruenbacher .create = nfs_create, 81*b7fa0554SAndreas Gruenbacher .lookup = nfs_lookup, 82*b7fa0554SAndreas Gruenbacher .link = nfs_link, 83*b7fa0554SAndreas Gruenbacher .unlink = nfs_unlink, 84*b7fa0554SAndreas Gruenbacher .symlink = nfs_symlink, 85*b7fa0554SAndreas Gruenbacher .mkdir = nfs_mkdir, 86*b7fa0554SAndreas Gruenbacher .rmdir = nfs_rmdir, 87*b7fa0554SAndreas Gruenbacher .mknod = nfs_mknod, 88*b7fa0554SAndreas Gruenbacher .rename = nfs_rename, 89*b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 90*b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 91*b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 92*b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 93*b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 94*b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 95*b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 96*b7fa0554SAndreas Gruenbacher }; 97*b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_V3 */ 98*b7fa0554SAndreas Gruenbacher 991da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); 1021da177e4SLinus Torvalds struct inode_operations nfs4_dir_inode_operations = { 1031da177e4SLinus Torvalds .create = nfs_create, 1041da177e4SLinus Torvalds .lookup = nfs_atomic_lookup, 1051da177e4SLinus Torvalds .link = nfs_link, 1061da177e4SLinus Torvalds .unlink = nfs_unlink, 1071da177e4SLinus Torvalds .symlink = nfs_symlink, 1081da177e4SLinus Torvalds .mkdir = nfs_mkdir, 1091da177e4SLinus Torvalds .rmdir = nfs_rmdir, 1101da177e4SLinus Torvalds .mknod = nfs_mknod, 1111da177e4SLinus Torvalds .rename = nfs_rename, 1121da177e4SLinus Torvalds .permission = nfs_permission, 1131da177e4SLinus Torvalds .getattr = nfs_getattr, 1141da177e4SLinus Torvalds .setattr = nfs_setattr, 1156b3b5496SJ. Bruce Fields .getxattr = nfs4_getxattr, 1166b3b5496SJ. Bruce Fields .setxattr = nfs4_setxattr, 1176b3b5496SJ. Bruce Fields .listxattr = nfs4_listxattr, 1181da177e4SLinus Torvalds }; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */ 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* 1231da177e4SLinus Torvalds * Open file 1241da177e4SLinus Torvalds */ 1251da177e4SLinus Torvalds static int 1261da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp) 1271da177e4SLinus Torvalds { 1281da177e4SLinus Torvalds int res = 0; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds lock_kernel(); 1311da177e4SLinus Torvalds /* Call generic open code in order to cache credentials */ 1321da177e4SLinus Torvalds if (!res) 1331da177e4SLinus Torvalds res = nfs_open(inode, filp); 1341da177e4SLinus Torvalds unlock_kernel(); 1351da177e4SLinus Torvalds return res; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int); 1391da177e4SLinus Torvalds typedef struct { 1401da177e4SLinus Torvalds struct file *file; 1411da177e4SLinus Torvalds struct page *page; 1421da177e4SLinus Torvalds unsigned long page_index; 1431da177e4SLinus Torvalds u32 *ptr; 1441da177e4SLinus Torvalds u64 target; 1451da177e4SLinus Torvalds struct nfs_entry *entry; 1461da177e4SLinus Torvalds decode_dirent_t decode; 1471da177e4SLinus Torvalds int plus; 1481da177e4SLinus Torvalds int error; 1491da177e4SLinus Torvalds } nfs_readdir_descriptor_t; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent 1521da177e4SLinus Torvalds * data directly in the page cache. 1531da177e4SLinus Torvalds * 1541da177e4SLinus Torvalds * Inode invalidation due to refresh etc. takes care of 1551da177e4SLinus Torvalds * _everything_, no sloppy entry flushing logic, no extraneous 1561da177e4SLinus Torvalds * copying, network direct to page cache, the way it was meant 1571da177e4SLinus Torvalds * to be. 1581da177e4SLinus Torvalds * 1591da177e4SLinus Torvalds * NOTE: Dirent information verification is done always by the 1601da177e4SLinus Torvalds * page-in of the RPC reply, nowhere else, this simplies 1611da177e4SLinus Torvalds * things substantially. 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds static 1641da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds struct file *file = desc->file; 1671da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 1681da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 1691da177e4SLinus Torvalds unsigned long timestamp; 1701da177e4SLinus Torvalds int error; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds again: 1751da177e4SLinus Torvalds timestamp = jiffies; 1761da177e4SLinus Torvalds error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page, 1771da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, desc->plus); 1781da177e4SLinus Torvalds if (error < 0) { 1791da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */ 1801da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) { 1811da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 1821da177e4SLinus Torvalds NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS; 1831da177e4SLinus Torvalds desc->plus = 0; 1841da177e4SLinus Torvalds goto again; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds goto error; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds SetPageUptodate(page); 1891da177e4SLinus Torvalds NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME; 1901da177e4SLinus Torvalds /* Ensure consistent page alignment of the data. 1911da177e4SLinus Torvalds * Note: assumes we have exclusive access to this mapping either 192a656db99STrond Myklebust * through inode->i_sem or some other mechanism. 1931da177e4SLinus Torvalds */ 194a656db99STrond Myklebust if (page->index == 0) 195a656db99STrond Myklebust invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1); 1961da177e4SLinus Torvalds unlock_page(page); 1971da177e4SLinus Torvalds return 0; 1981da177e4SLinus Torvalds error: 1991da177e4SLinus Torvalds SetPageError(page); 2001da177e4SLinus Torvalds unlock_page(page); 2011da177e4SLinus Torvalds nfs_zap_caches(inode); 2021da177e4SLinus Torvalds desc->error = error; 2031da177e4SLinus Torvalds return -EIO; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static inline 2071da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds u32 *p = desc->ptr; 2101da177e4SLinus Torvalds p = desc->decode(p, desc->entry, desc->plus); 2111da177e4SLinus Torvalds if (IS_ERR(p)) 2121da177e4SLinus Torvalds return PTR_ERR(p); 2131da177e4SLinus Torvalds desc->ptr = p; 2141da177e4SLinus Torvalds return 0; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds static inline 2181da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds kunmap(desc->page); 2211da177e4SLinus Torvalds page_cache_release(desc->page); 2221da177e4SLinus Torvalds desc->page = NULL; 2231da177e4SLinus Torvalds desc->ptr = NULL; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* 2271da177e4SLinus Torvalds * Given a pointer to a buffer that has already been filled by a call 2281da177e4SLinus Torvalds * to readdir, find the next entry. 2291da177e4SLinus Torvalds * 2301da177e4SLinus Torvalds * If the end of the buffer has been reached, return -EAGAIN, if not, 2311da177e4SLinus Torvalds * return the offset within the buffer of the next entry to be 2321da177e4SLinus Torvalds * read. 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds static inline 2351da177e4SLinus Torvalds int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 2381da177e4SLinus Torvalds int loop_count = 0, 2391da177e4SLinus Torvalds status; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds while((status = dir_decode(desc)) == 0) { 2421da177e4SLinus Torvalds dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie); 2431da177e4SLinus Torvalds if (entry->prev_cookie == desc->target) 2441da177e4SLinus Torvalds break; 2451da177e4SLinus Torvalds if (loop_count++ > 200) { 2461da177e4SLinus Torvalds loop_count = 0; 2471da177e4SLinus Torvalds schedule(); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds dfprintk(VFS, "NFS: find_dirent() returns %d\n", status); 2511da177e4SLinus Torvalds return status; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /* 2551da177e4SLinus Torvalds * Find the given page, and call find_dirent() in order to try to 2561da177e4SLinus Torvalds * return the next entry. 2571da177e4SLinus Torvalds */ 2581da177e4SLinus Torvalds static inline 2591da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds struct inode *inode = desc->file->f_dentry->d_inode; 2621da177e4SLinus Torvalds struct page *page; 2631da177e4SLinus Torvalds int status; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds page = read_cache_page(inode->i_mapping, desc->page_index, 2681da177e4SLinus Torvalds (filler_t *)nfs_readdir_filler, desc); 2691da177e4SLinus Torvalds if (IS_ERR(page)) { 2701da177e4SLinus Torvalds status = PTR_ERR(page); 2711da177e4SLinus Torvalds goto out; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds if (!PageUptodate(page)) 2741da177e4SLinus Torvalds goto read_error; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* NOTE: Someone else may have changed the READDIRPLUS flag */ 2771da177e4SLinus Torvalds desc->page = page; 2781da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 2791da177e4SLinus Torvalds status = find_dirent(desc, page); 2801da177e4SLinus Torvalds if (status < 0) 2811da177e4SLinus Torvalds dir_page_release(desc); 2821da177e4SLinus Torvalds out: 2831da177e4SLinus Torvalds dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status); 2841da177e4SLinus Torvalds return status; 2851da177e4SLinus Torvalds read_error: 2861da177e4SLinus Torvalds page_cache_release(page); 2871da177e4SLinus Torvalds return -EIO; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* 2911da177e4SLinus Torvalds * Recurse through the page cache pages, and return a 2921da177e4SLinus Torvalds * filled nfs_entry structure of the next directory entry if possible. 2931da177e4SLinus Torvalds * 2941da177e4SLinus Torvalds * The target for the search is 'desc->target'. 2951da177e4SLinus Torvalds */ 2961da177e4SLinus Torvalds static inline 2971da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds int loop_count = 0; 3001da177e4SLinus Torvalds int res; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target); 3031da177e4SLinus Torvalds for (;;) { 3041da177e4SLinus Torvalds res = find_dirent_page(desc); 3051da177e4SLinus Torvalds if (res != -EAGAIN) 3061da177e4SLinus Torvalds break; 3071da177e4SLinus Torvalds /* Align to beginning of next page */ 3081da177e4SLinus Torvalds desc->page_index ++; 3091da177e4SLinus Torvalds if (loop_count++ > 200) { 3101da177e4SLinus Torvalds loop_count = 0; 3111da177e4SLinus Torvalds schedule(); 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res); 3151da177e4SLinus Torvalds return res; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc); 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* 3261da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up... 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds static 3291da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, 3301da177e4SLinus Torvalds filldir_t filldir) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct file *file = desc->file; 3331da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 3341da177e4SLinus Torvalds struct dentry *dentry = NULL; 3351da177e4SLinus Torvalds unsigned long fileid; 3361da177e4SLinus Torvalds int loop_count = 0, 3371da177e4SLinus Torvalds res; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target); 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds for(;;) { 3421da177e4SLinus Torvalds unsigned d_type = DT_UNKNOWN; 3431da177e4SLinus Torvalds /* Note: entry->prev_cookie contains the cookie for 3441da177e4SLinus Torvalds * retrieving the current dirent on the server */ 3451da177e4SLinus Torvalds fileid = nfs_fileid_to_ino_t(entry->ino); 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* Get a dentry if we have one */ 3481da177e4SLinus Torvalds if (dentry != NULL) 3491da177e4SLinus Torvalds dput(dentry); 3501da177e4SLinus Torvalds dentry = nfs_readdir_lookup(desc); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* Use readdirplus info */ 3531da177e4SLinus Torvalds if (dentry != NULL && dentry->d_inode != NULL) { 3541da177e4SLinus Torvalds d_type = dt_type(dentry->d_inode); 3551da177e4SLinus Torvalds fileid = dentry->d_inode->i_ino; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds res = filldir(dirent, entry->name, entry->len, 3591da177e4SLinus Torvalds entry->prev_cookie, fileid, d_type); 3601da177e4SLinus Torvalds if (res < 0) 3611da177e4SLinus Torvalds break; 3621da177e4SLinus Torvalds file->f_pos = desc->target = entry->cookie; 3631da177e4SLinus Torvalds if (dir_decode(desc) != 0) { 3641da177e4SLinus Torvalds desc->page_index ++; 3651da177e4SLinus Torvalds break; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds if (loop_count++ > 200) { 3681da177e4SLinus Torvalds loop_count = 0; 3691da177e4SLinus Torvalds schedule(); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds dir_page_release(desc); 3731da177e4SLinus Torvalds if (dentry != NULL) 3741da177e4SLinus Torvalds dput(dentry); 3751da177e4SLinus Torvalds dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res); 3761da177e4SLinus Torvalds return res; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds /* 3801da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is 3811da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return 3821da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir. 3831da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the 3841da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache(); 3851da177e4SLinus Torvalds * 3861da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because 3871da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides, 3881da177e4SLinus Torvalds * we should already have a complete representation of the 3891da177e4SLinus Torvalds * directory in the page cache by the time we get here. 3901da177e4SLinus Torvalds */ 3911da177e4SLinus Torvalds static inline 3921da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, 3931da177e4SLinus Torvalds filldir_t filldir) 3941da177e4SLinus Torvalds { 3951da177e4SLinus Torvalds struct file *file = desc->file; 3961da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 3971da177e4SLinus Torvalds struct rpc_cred *cred = nfs_file_cred(file); 3981da177e4SLinus Torvalds struct page *page = NULL; 3991da177e4SLinus Torvalds int status; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds page = alloc_page(GFP_HIGHUSER); 4041da177e4SLinus Torvalds if (!page) { 4051da177e4SLinus Torvalds status = -ENOMEM; 4061da177e4SLinus Torvalds goto out; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target, 4091da177e4SLinus Torvalds page, 4101da177e4SLinus Torvalds NFS_SERVER(inode)->dtsize, 4111da177e4SLinus Torvalds desc->plus); 4121da177e4SLinus Torvalds NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME; 4131da177e4SLinus Torvalds desc->page = page; 4141da177e4SLinus Torvalds desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ 4151da177e4SLinus Torvalds if (desc->error >= 0) { 4161da177e4SLinus Torvalds if ((status = dir_decode(desc)) == 0) 4171da177e4SLinus Torvalds desc->entry->prev_cookie = desc->target; 4181da177e4SLinus Torvalds } else 4191da177e4SLinus Torvalds status = -EIO; 4201da177e4SLinus Torvalds if (status < 0) 4211da177e4SLinus Torvalds goto out_release; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds status = nfs_do_filldir(desc, dirent, filldir); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds /* Reset read descriptor so it searches the page cache from 4261da177e4SLinus Torvalds * the start upon the next call to readdir_search_pagecache() */ 4271da177e4SLinus Torvalds desc->page_index = 0; 4281da177e4SLinus Torvalds desc->entry->cookie = desc->entry->prev_cookie = 0; 4291da177e4SLinus Torvalds desc->entry->eof = 0; 4301da177e4SLinus Torvalds out: 4311da177e4SLinus Torvalds dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status); 4321da177e4SLinus Torvalds return status; 4331da177e4SLinus Torvalds out_release: 4341da177e4SLinus Torvalds dir_page_release(desc); 4351da177e4SLinus Torvalds goto out; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* The file offset position is now represented as a true offset into the 4391da177e4SLinus Torvalds * page cache as is the case in most of the other filesystems. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 4421da177e4SLinus Torvalds { 4431da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 4441da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 4451da177e4SLinus Torvalds nfs_readdir_descriptor_t my_desc, 4461da177e4SLinus Torvalds *desc = &my_desc; 4471da177e4SLinus Torvalds struct nfs_entry my_entry; 4481da177e4SLinus Torvalds struct nfs_fh fh; 4491da177e4SLinus Torvalds struct nfs_fattr fattr; 4501da177e4SLinus Torvalds long res; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds lock_kernel(); 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 4551da177e4SLinus Torvalds if (res < 0) { 4561da177e4SLinus Torvalds unlock_kernel(); 4571da177e4SLinus Torvalds return res; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * filp->f_pos points to the file offset in the page cache. 4621da177e4SLinus Torvalds * but if the cache has meanwhile been zapped, we need to 4631da177e4SLinus Torvalds * read from the last dirent to revalidate f_pos 4641da177e4SLinus Torvalds * itself. 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds memset(desc, 0, sizeof(*desc)); 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds desc->file = filp; 4691da177e4SLinus Torvalds desc->target = filp->f_pos; 4701da177e4SLinus Torvalds desc->decode = NFS_PROTO(inode)->decode_dirent; 4711da177e4SLinus Torvalds desc->plus = NFS_USE_READDIRPLUS(inode); 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds my_entry.cookie = my_entry.prev_cookie = 0; 4741da177e4SLinus Torvalds my_entry.eof = 0; 4751da177e4SLinus Torvalds my_entry.fh = &fh; 4761da177e4SLinus Torvalds my_entry.fattr = &fattr; 4771da177e4SLinus Torvalds desc->entry = &my_entry; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds while(!desc->entry->eof) { 4801da177e4SLinus Torvalds res = readdir_search_pagecache(desc); 4811da177e4SLinus Torvalds if (res == -EBADCOOKIE) { 4821da177e4SLinus Torvalds /* This means either end of directory */ 4831da177e4SLinus Torvalds if (desc->entry->cookie != desc->target) { 4841da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */ 4851da177e4SLinus Torvalds res = uncached_readdir(desc, dirent, filldir); 4861da177e4SLinus Torvalds if (res >= 0) 4871da177e4SLinus Torvalds continue; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds res = 0; 4901da177e4SLinus Torvalds break; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) { 4931da177e4SLinus Torvalds NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS; 4941da177e4SLinus Torvalds nfs_zap_caches(inode); 4951da177e4SLinus Torvalds desc->plus = 0; 4961da177e4SLinus Torvalds desc->entry->eof = 0; 4971da177e4SLinus Torvalds continue; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds if (res < 0) 5001da177e4SLinus Torvalds break; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds res = nfs_do_filldir(desc, dirent, filldir); 5031da177e4SLinus Torvalds if (res < 0) { 5041da177e4SLinus Torvalds res = 0; 5051da177e4SLinus Torvalds break; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds unlock_kernel(); 5091da177e4SLinus Torvalds if (desc->error < 0) 5101da177e4SLinus Torvalds return desc->error; 5111da177e4SLinus Torvalds if (res < 0) 5121da177e4SLinus Torvalds return res; 5131da177e4SLinus Torvalds return 0; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync() 5181da177e4SLinus Torvalds * is a dummy operation. 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync) 5211da177e4SLinus Torvalds { 5221da177e4SLinus Torvalds return 0; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds /* 5261da177e4SLinus Torvalds * A check for whether or not the parent directory has changed. 5271da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy 5281da177e4SLinus Torvalds * and may need to be looked up again. 5291da177e4SLinus Torvalds */ 5301da177e4SLinus Torvalds static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry) 5311da177e4SLinus Torvalds { 5321da177e4SLinus Torvalds if (IS_ROOT(dentry)) 5331da177e4SLinus Torvalds return 1; 5341da177e4SLinus Torvalds if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0 5351da177e4SLinus Torvalds || nfs_attribute_timeout(dir)) 5361da177e4SLinus Torvalds return 0; 5371da177e4SLinus Torvalds return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds dentry->d_fsdata = (void *)verf; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* 5461da177e4SLinus Torvalds * Whenever an NFS operation succeeds, we know that the dentry 5471da177e4SLinus Torvalds * is valid, so we update the revalidation timestamp. 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds static inline void nfs_renew_times(struct dentry * dentry) 5501da177e4SLinus Torvalds { 5511da177e4SLinus Torvalds dentry->d_time = jiffies; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541d6757fbSTrond Myklebust /* 5551d6757fbSTrond Myklebust * Return the intent data that applies to this particular path component 5561d6757fbSTrond Myklebust * 5571d6757fbSTrond Myklebust * Note that the current set of intents only apply to the very last 5581d6757fbSTrond Myklebust * component of the path. 5591d6757fbSTrond Myklebust * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. 5601d6757fbSTrond Myklebust */ 5611d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) 5621d6757fbSTrond Myklebust { 5631d6757fbSTrond Myklebust if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) 5641d6757fbSTrond Myklebust return 0; 5651d6757fbSTrond Myklebust return nd->flags & mask; 5661d6757fbSTrond Myklebust } 5671d6757fbSTrond Myklebust 5681d6757fbSTrond Myklebust /* 5691d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups. 5701d6757fbSTrond Myklebust * 5711d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 5721d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this 5731d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set. 5741d6757fbSTrond Myklebust * 5751d6757fbSTrond Myklebust */ 5761da177e4SLinus Torvalds static inline 5771da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds if (nd != NULL) { 5821da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */ 5831d6757fbSTrond Myklebust if (nd->flags & LOOKUP_REVAL) 5841da177e4SLinus Torvalds goto out_force; 5851da177e4SLinus Torvalds /* This is an open(2) */ 5861d6757fbSTrond Myklebust if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && 5871da177e4SLinus Torvalds !(server->flags & NFS_MOUNT_NOCTO)) 5881da177e4SLinus Torvalds goto out_force; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds return nfs_revalidate_inode(server, inode); 5911da177e4SLinus Torvalds out_force: 5921da177e4SLinus Torvalds return __nfs_revalidate_inode(server, inode); 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* 5961da177e4SLinus Torvalds * We judge how long we want to trust negative 5971da177e4SLinus Torvalds * dentries by looking at the parent inode mtime. 5981da177e4SLinus Torvalds * 5991da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a 6001da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value. 6011da177e4SLinus Torvalds */ 6021da177e4SLinus Torvalds static inline 6031da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 6041da177e4SLinus Torvalds struct nameidata *nd) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds /* Don't revalidate a negative dentry if we're creating a new file */ 6071d6757fbSTrond Myklebust if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) 6081da177e4SLinus Torvalds return 0; 6091da177e4SLinus Torvalds return !nfs_check_verifier(dir, dentry); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* 6131da177e4SLinus Torvalds * This is called every time the dcache has a lookup hit, 6141da177e4SLinus Torvalds * and we should check whether we can really trust that 6151da177e4SLinus Torvalds * lookup. 6161da177e4SLinus Torvalds * 6171da177e4SLinus Torvalds * NOTE! The hit can be a negative hit too, don't assume 6181da177e4SLinus Torvalds * we have an inode! 6191da177e4SLinus Torvalds * 6201da177e4SLinus Torvalds * If the parent directory is seen to have changed, we throw out the 6211da177e4SLinus Torvalds * cached dentry and do a new lookup. 6221da177e4SLinus Torvalds */ 6231da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) 6241da177e4SLinus Torvalds { 6251da177e4SLinus Torvalds struct inode *dir; 6261da177e4SLinus Torvalds struct inode *inode; 6271da177e4SLinus Torvalds struct dentry *parent; 6281da177e4SLinus Torvalds int error; 6291da177e4SLinus Torvalds struct nfs_fh fhandle; 6301da177e4SLinus Torvalds struct nfs_fattr fattr; 6311da177e4SLinus Torvalds unsigned long verifier; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds parent = dget_parent(dentry); 6341da177e4SLinus Torvalds lock_kernel(); 6351da177e4SLinus Torvalds dir = parent->d_inode; 6361da177e4SLinus Torvalds inode = dentry->d_inode; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (!inode) { 6391da177e4SLinus Torvalds if (nfs_neg_need_reval(dir, dentry, nd)) 6401da177e4SLinus Torvalds goto out_bad; 6411da177e4SLinus Torvalds goto out_valid; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds if (is_bad_inode(inode)) { 6451da177e4SLinus Torvalds dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n", 6461da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 6471da177e4SLinus Torvalds goto out_bad; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* Revalidate parent directory attribute cache */ 6511da177e4SLinus Torvalds if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 6521da177e4SLinus Torvalds goto out_zap_parent; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds /* Force a full look up iff the parent directory has changed */ 6551da177e4SLinus Torvalds if (nfs_check_verifier(dir, dentry)) { 6561da177e4SLinus Torvalds if (nfs_lookup_verify_inode(inode, nd)) 6571da177e4SLinus Torvalds goto out_zap_parent; 6581da177e4SLinus Torvalds goto out_valid; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds if (NFS_STALE(inode)) 6621da177e4SLinus Torvalds goto out_bad; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds verifier = nfs_save_change_attribute(dir); 6651da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr); 6661da177e4SLinus Torvalds if (error) 6671da177e4SLinus Torvalds goto out_bad; 6681da177e4SLinus Torvalds if (nfs_compare_fh(NFS_FH(inode), &fhandle)) 6691da177e4SLinus Torvalds goto out_bad; 6701da177e4SLinus Torvalds if ((error = nfs_refresh_inode(inode, &fattr)) != 0) 6711da177e4SLinus Torvalds goto out_bad; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds nfs_renew_times(dentry); 6741da177e4SLinus Torvalds nfs_set_verifier(dentry, verifier); 6751da177e4SLinus Torvalds out_valid: 6761da177e4SLinus Torvalds unlock_kernel(); 6771da177e4SLinus Torvalds dput(parent); 6781da177e4SLinus Torvalds return 1; 6791da177e4SLinus Torvalds out_zap_parent: 6801da177e4SLinus Torvalds nfs_zap_caches(dir); 6811da177e4SLinus Torvalds out_bad: 6821da177e4SLinus Torvalds NFS_CACHEINV(dir); 6831da177e4SLinus Torvalds if (inode && S_ISDIR(inode->i_mode)) { 6841da177e4SLinus Torvalds /* Purge readdir caches. */ 6851da177e4SLinus Torvalds nfs_zap_caches(inode); 6861da177e4SLinus Torvalds /* If we have submounts, don't unhash ! */ 6871da177e4SLinus Torvalds if (have_submounts(dentry)) 6881da177e4SLinus Torvalds goto out_valid; 6891da177e4SLinus Torvalds shrink_dcache_parent(dentry); 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds d_drop(dentry); 6921da177e4SLinus Torvalds unlock_kernel(); 6931da177e4SLinus Torvalds dput(parent); 6941da177e4SLinus Torvalds return 0; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0. 6991da177e4SLinus Torvalds */ 7001da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry) 7011da177e4SLinus Torvalds { 7021da177e4SLinus Torvalds dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", 7031da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 7041da177e4SLinus Torvalds dentry->d_flags); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 7071da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */ 7081da177e4SLinus Torvalds return 1; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { 7111da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink 7121da177e4SLinus Torvalds * files will be cleaned up during umount */ 7131da177e4SLinus Torvalds return 1; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds return 0; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* 7201da177e4SLinus Torvalds * Called when the dentry loses inode. 7211da177e4SLinus Torvalds * We use it to clean up silly-renamed files. 7221da177e4SLinus Torvalds */ 7231da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 7241da177e4SLinus Torvalds { 7251da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 7261da177e4SLinus Torvalds lock_kernel(); 7271da177e4SLinus Torvalds inode->i_nlink--; 7281da177e4SLinus Torvalds nfs_complete_unlink(dentry); 7291da177e4SLinus Torvalds unlock_kernel(); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds /* When creating a negative dentry, we want to renew d_time */ 7321da177e4SLinus Torvalds nfs_renew_times(dentry); 7331da177e4SLinus Torvalds iput(inode); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds struct dentry_operations nfs_dentry_operations = { 7371da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate, 7381da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 7391da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 7401da177e4SLinus Torvalds }; 7411da177e4SLinus Torvalds 7421d6757fbSTrond Myklebust /* 7431d6757fbSTrond Myklebust * Use intent information to check whether or not we're going to do 7441d6757fbSTrond Myklebust * an O_EXCL create using this path component. 7451d6757fbSTrond Myklebust */ 7461da177e4SLinus Torvalds static inline 7471da177e4SLinus Torvalds int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds if (NFS_PROTO(dir)->version == 2) 7501da177e4SLinus Torvalds return 0; 7511d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0) 7521da177e4SLinus Torvalds return 0; 7531da177e4SLinus Torvalds return (nd->intent.open.flags & O_EXCL) != 0; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 7571da177e4SLinus Torvalds { 7581da177e4SLinus Torvalds struct dentry *res; 7591da177e4SLinus Torvalds struct inode *inode = NULL; 7601da177e4SLinus Torvalds int error; 7611da177e4SLinus Torvalds struct nfs_fh fhandle; 7621da177e4SLinus Torvalds struct nfs_fattr fattr; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds dfprintk(VFS, "NFS: lookup(%s/%s)\n", 7651da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 7681da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 7691da177e4SLinus Torvalds goto out; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds res = ERR_PTR(-ENOMEM); 7721da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds lock_kernel(); 7751da177e4SLinus Torvalds /* Revalidate parent directory attribute cache */ 7761da177e4SLinus Torvalds error = nfs_revalidate_inode(NFS_SERVER(dir), dir); 7771da177e4SLinus Torvalds if (error < 0) { 7781da177e4SLinus Torvalds res = ERR_PTR(error); 7791da177e4SLinus Torvalds goto out_unlock; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* If we're doing an exclusive create, optimize away the lookup */ 7831da177e4SLinus Torvalds if (nfs_is_exclusive_create(dir, nd)) 7841da177e4SLinus Torvalds goto no_entry; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr); 7871da177e4SLinus Torvalds if (error == -ENOENT) 7881da177e4SLinus Torvalds goto no_entry; 7891da177e4SLinus Torvalds if (error < 0) { 7901da177e4SLinus Torvalds res = ERR_PTR(error); 7911da177e4SLinus Torvalds goto out_unlock; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds res = ERR_PTR(-EACCES); 7941da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr); 7951da177e4SLinus Torvalds if (!inode) 7961da177e4SLinus Torvalds goto out_unlock; 7971da177e4SLinus Torvalds no_entry: 7981da177e4SLinus Torvalds res = d_add_unique(dentry, inode); 7991da177e4SLinus Torvalds if (res != NULL) 8001da177e4SLinus Torvalds dentry = res; 8011da177e4SLinus Torvalds nfs_renew_times(dentry); 8021da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 8031da177e4SLinus Torvalds out_unlock: 8041da177e4SLinus Torvalds unlock_kernel(); 8051da177e4SLinus Torvalds out: 8061da177e4SLinus Torvalds return res; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4 8101da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *); 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds struct dentry_operations nfs4_dentry_operations = { 8131da177e4SLinus Torvalds .d_revalidate = nfs_open_revalidate, 8141da177e4SLinus Torvalds .d_delete = nfs_dentry_delete, 8151da177e4SLinus Torvalds .d_iput = nfs_dentry_iput, 8161da177e4SLinus Torvalds }; 8171da177e4SLinus Torvalds 8181d6757fbSTrond Myklebust /* 8191d6757fbSTrond Myklebust * Use intent information to determine whether we need to substitute 8201d6757fbSTrond Myklebust * the NFSv4-style stateful OPEN for the LOOKUP call 8211d6757fbSTrond Myklebust */ 8221da177e4SLinus Torvalds static int is_atomic_open(struct inode *dir, struct nameidata *nd) 8231da177e4SLinus Torvalds { 8241d6757fbSTrond Myklebust if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) 8251da177e4SLinus Torvalds return 0; 8261da177e4SLinus Torvalds /* NFS does not (yet) have a stateful open for directories */ 8271da177e4SLinus Torvalds if (nd->flags & LOOKUP_DIRECTORY) 8281da177e4SLinus Torvalds return 0; 8291da177e4SLinus Torvalds /* Are we trying to write to a read only partition? */ 8301da177e4SLinus Torvalds if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) 8311da177e4SLinus Torvalds return 0; 8321da177e4SLinus Torvalds return 1; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds struct dentry *res = NULL; 8381da177e4SLinus Torvalds struct inode *inode = NULL; 8391da177e4SLinus Torvalds int error; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds /* Check that we are indeed trying to open this file */ 8421da177e4SLinus Torvalds if (!is_atomic_open(dir, nd)) 8431da177e4SLinus Torvalds goto no_open; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { 8461da177e4SLinus Torvalds res = ERR_PTR(-ENAMETOOLONG); 8471da177e4SLinus Torvalds goto out; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds /* Let vfs_create() deal with O_EXCL */ 8521da177e4SLinus Torvalds if (nd->intent.open.flags & O_EXCL) 8531da177e4SLinus Torvalds goto no_entry; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* Open the file on the server */ 8561da177e4SLinus Torvalds lock_kernel(); 8571da177e4SLinus Torvalds /* Revalidate parent directory attribute cache */ 8581da177e4SLinus Torvalds error = nfs_revalidate_inode(NFS_SERVER(dir), dir); 8591da177e4SLinus Torvalds if (error < 0) { 8601da177e4SLinus Torvalds res = ERR_PTR(error); 8611da177e4SLinus Torvalds goto out; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds if (nd->intent.open.flags & O_CREAT) { 8651da177e4SLinus Torvalds nfs_begin_data_update(dir); 8661da177e4SLinus Torvalds inode = nfs4_atomic_open(dir, dentry, nd); 8671da177e4SLinus Torvalds nfs_end_data_update(dir); 8681da177e4SLinus Torvalds } else 8691da177e4SLinus Torvalds inode = nfs4_atomic_open(dir, dentry, nd); 8701da177e4SLinus Torvalds unlock_kernel(); 8711da177e4SLinus Torvalds if (IS_ERR(inode)) { 8721da177e4SLinus Torvalds error = PTR_ERR(inode); 8731da177e4SLinus Torvalds switch (error) { 8741da177e4SLinus Torvalds /* Make a negative dentry */ 8751da177e4SLinus Torvalds case -ENOENT: 8761da177e4SLinus Torvalds inode = NULL; 8771da177e4SLinus Torvalds break; 8781da177e4SLinus Torvalds /* This turned out not to be a regular file */ 8791da177e4SLinus Torvalds case -ELOOP: 8801da177e4SLinus Torvalds if (!(nd->intent.open.flags & O_NOFOLLOW)) 8811da177e4SLinus Torvalds goto no_open; 8821da177e4SLinus Torvalds /* case -EISDIR: */ 8831da177e4SLinus Torvalds /* case -EINVAL: */ 8841da177e4SLinus Torvalds default: 8851da177e4SLinus Torvalds res = ERR_PTR(error); 8861da177e4SLinus Torvalds goto out; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds no_entry: 8901da177e4SLinus Torvalds res = d_add_unique(dentry, inode); 8911da177e4SLinus Torvalds if (res != NULL) 8921da177e4SLinus Torvalds dentry = res; 8931da177e4SLinus Torvalds nfs_renew_times(dentry); 8941da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 8951da177e4SLinus Torvalds out: 8961da177e4SLinus Torvalds return res; 8971da177e4SLinus Torvalds no_open: 8981da177e4SLinus Torvalds return nfs_lookup(dir, dentry, nd); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) 9021da177e4SLinus Torvalds { 9031da177e4SLinus Torvalds struct dentry *parent = NULL; 9041da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9051da177e4SLinus Torvalds struct inode *dir; 9061da177e4SLinus Torvalds unsigned long verifier; 9071da177e4SLinus Torvalds int openflags, ret = 0; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds parent = dget_parent(dentry); 9101da177e4SLinus Torvalds dir = parent->d_inode; 9111da177e4SLinus Torvalds if (!is_atomic_open(dir, nd)) 9121da177e4SLinus Torvalds goto no_open; 9131da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we 9141da177e4SLinus Torvalds * optimize away revalidation of negative dentries. 9151da177e4SLinus Torvalds */ 9161da177e4SLinus Torvalds if (inode == NULL) 9171da177e4SLinus Torvalds goto out; 9181da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */ 9191da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 9201da177e4SLinus Torvalds goto no_open; 9211da177e4SLinus Torvalds openflags = nd->intent.open.flags; 9221da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */ 9231da177e4SLinus Torvalds if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) 9241da177e4SLinus Torvalds goto no_open; 9251da177e4SLinus Torvalds /* We can't create new files, or truncate existing ones here */ 9261da177e4SLinus Torvalds openflags &= ~(O_CREAT|O_TRUNC); 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* 9291da177e4SLinus Torvalds * Note: we're not holding inode->i_sem and so may be racing with 9301da177e4SLinus Torvalds * operations that change the directory. We therefore save the 9311da177e4SLinus Torvalds * change attribute *before* we do the RPC call. 9321da177e4SLinus Torvalds */ 9331da177e4SLinus Torvalds lock_kernel(); 9341da177e4SLinus Torvalds verifier = nfs_save_change_attribute(dir); 9351da177e4SLinus Torvalds ret = nfs4_open_revalidate(dir, dentry, openflags); 9361da177e4SLinus Torvalds if (!ret) 9371da177e4SLinus Torvalds nfs_set_verifier(dentry, verifier); 9381da177e4SLinus Torvalds unlock_kernel(); 9391da177e4SLinus Torvalds out: 9401da177e4SLinus Torvalds dput(parent); 9411da177e4SLinus Torvalds if (!ret) 9421da177e4SLinus Torvalds d_drop(dentry); 9431da177e4SLinus Torvalds return ret; 9441da177e4SLinus Torvalds no_open: 9451da177e4SLinus Torvalds dput(parent); 9461da177e4SLinus Torvalds if (inode != NULL && nfs_have_delegation(inode, FMODE_READ)) 9471da177e4SLinus Torvalds return 1; 9481da177e4SLinus Torvalds return nfs_lookup_revalidate(dentry, nd); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */ 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds struct dentry *parent = desc->file->f_dentry; 9551da177e4SLinus Torvalds struct inode *dir = parent->d_inode; 9561da177e4SLinus Torvalds struct nfs_entry *entry = desc->entry; 9571da177e4SLinus Torvalds struct dentry *dentry, *alias; 9581da177e4SLinus Torvalds struct qstr name = { 9591da177e4SLinus Torvalds .name = entry->name, 9601da177e4SLinus Torvalds .len = entry->len, 9611da177e4SLinus Torvalds }; 9621da177e4SLinus Torvalds struct inode *inode; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds switch (name.len) { 9651da177e4SLinus Torvalds case 2: 9661da177e4SLinus Torvalds if (name.name[0] == '.' && name.name[1] == '.') 9671da177e4SLinus Torvalds return dget_parent(parent); 9681da177e4SLinus Torvalds break; 9691da177e4SLinus Torvalds case 1: 9701da177e4SLinus Torvalds if (name.name[0] == '.') 9711da177e4SLinus Torvalds return dget(parent); 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds name.hash = full_name_hash(name.name, name.len); 9741da177e4SLinus Torvalds dentry = d_lookup(parent, &name); 9751da177e4SLinus Torvalds if (dentry != NULL) 9761da177e4SLinus Torvalds return dentry; 9771da177e4SLinus Torvalds if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR)) 9781da177e4SLinus Torvalds return NULL; 9791da177e4SLinus Torvalds /* Note: caller is already holding the dir->i_sem! */ 9801da177e4SLinus Torvalds dentry = d_alloc(parent, &name); 9811da177e4SLinus Torvalds if (dentry == NULL) 9821da177e4SLinus Torvalds return NULL; 9831da177e4SLinus Torvalds dentry->d_op = NFS_PROTO(dir)->dentry_ops; 9841da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); 9851da177e4SLinus Torvalds if (!inode) { 9861da177e4SLinus Torvalds dput(dentry); 9871da177e4SLinus Torvalds return NULL; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds alias = d_add_unique(dentry, inode); 9901da177e4SLinus Torvalds if (alias != NULL) { 9911da177e4SLinus Torvalds dput(dentry); 9921da177e4SLinus Torvalds dentry = alias; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds nfs_renew_times(dentry); 9951da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 9961da177e4SLinus Torvalds return dentry; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* 10001da177e4SLinus Torvalds * Code common to create, mkdir, and mknod. 10011da177e4SLinus Torvalds */ 10021da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 10031da177e4SLinus Torvalds struct nfs_fattr *fattr) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds struct inode *inode; 10061da177e4SLinus Torvalds int error = -EACCES; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* We may have been initialized further down */ 10091da177e4SLinus Torvalds if (dentry->d_inode) 10101da177e4SLinus Torvalds return 0; 10111da177e4SLinus Torvalds if (fhandle->size == 0) { 10121da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 10131da177e4SLinus Torvalds error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); 10141da177e4SLinus Torvalds if (error) 10151da177e4SLinus Torvalds goto out_err; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) { 10181da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb); 10191da177e4SLinus Torvalds error = server->rpc_ops->getattr(server, fhandle, fattr); 10201da177e4SLinus Torvalds if (error < 0) 10211da177e4SLinus Torvalds goto out_err; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds error = -ENOMEM; 10241da177e4SLinus Torvalds inode = nfs_fhget(dentry->d_sb, fhandle, fattr); 10251da177e4SLinus Torvalds if (inode == NULL) 10261da177e4SLinus Torvalds goto out_err; 10271da177e4SLinus Torvalds d_instantiate(dentry, inode); 10281da177e4SLinus Torvalds return 0; 10291da177e4SLinus Torvalds out_err: 10301da177e4SLinus Torvalds d_drop(dentry); 10311da177e4SLinus Torvalds return error; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /* 10351da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather 10361da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event 10371da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the 10381da177e4SLinus Torvalds * reply path made it appear to have failed. 10391da177e4SLinus Torvalds */ 10401da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, 10411da177e4SLinus Torvalds struct nameidata *nd) 10421da177e4SLinus Torvalds { 10431da177e4SLinus Torvalds struct iattr attr; 10441da177e4SLinus Torvalds int error; 10451da177e4SLinus Torvalds int open_flags = 0; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id, 10481da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds attr.ia_mode = mode; 10511da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds if (nd && (nd->flags & LOOKUP_CREATE)) 10541da177e4SLinus Torvalds open_flags = nd->intent.open.flags; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds lock_kernel(); 10571da177e4SLinus Torvalds nfs_begin_data_update(dir); 10581da177e4SLinus Torvalds error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 10591da177e4SLinus Torvalds nfs_end_data_update(dir); 10601da177e4SLinus Torvalds if (error != 0) 10611da177e4SLinus Torvalds goto out_err; 10621da177e4SLinus Torvalds nfs_renew_times(dentry); 10631da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 10641da177e4SLinus Torvalds unlock_kernel(); 10651da177e4SLinus Torvalds return 0; 10661da177e4SLinus Torvalds out_err: 10671da177e4SLinus Torvalds unlock_kernel(); 10681da177e4SLinus Torvalds d_drop(dentry); 10691da177e4SLinus Torvalds return error; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds /* 10731da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 10741da177e4SLinus Torvalds */ 10751da177e4SLinus Torvalds static int 10761da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds struct iattr attr; 10791da177e4SLinus Torvalds int status; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id, 10821da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds if (!new_valid_dev(rdev)) 10851da177e4SLinus Torvalds return -EINVAL; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds attr.ia_mode = mode; 10881da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds lock_kernel(); 10911da177e4SLinus Torvalds nfs_begin_data_update(dir); 10921da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 10931da177e4SLinus Torvalds nfs_end_data_update(dir); 10941da177e4SLinus Torvalds if (status != 0) 10951da177e4SLinus Torvalds goto out_err; 10961da177e4SLinus Torvalds nfs_renew_times(dentry); 10971da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 10981da177e4SLinus Torvalds unlock_kernel(); 10991da177e4SLinus Torvalds return 0; 11001da177e4SLinus Torvalds out_err: 11011da177e4SLinus Torvalds unlock_kernel(); 11021da177e4SLinus Torvalds d_drop(dentry); 11031da177e4SLinus Torvalds return status; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* 11071da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations. 11081da177e4SLinus Torvalds */ 11091da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds struct iattr attr; 11121da177e4SLinus Torvalds int error; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id, 11151da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 11181da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds lock_kernel(); 11211da177e4SLinus Torvalds nfs_begin_data_update(dir); 11221da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 11231da177e4SLinus Torvalds nfs_end_data_update(dir); 11241da177e4SLinus Torvalds if (error != 0) 11251da177e4SLinus Torvalds goto out_err; 11261da177e4SLinus Torvalds nfs_renew_times(dentry); 11271da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 11281da177e4SLinus Torvalds unlock_kernel(); 11291da177e4SLinus Torvalds return 0; 11301da177e4SLinus Torvalds out_err: 11311da177e4SLinus Torvalds d_drop(dentry); 11321da177e4SLinus Torvalds unlock_kernel(); 11331da177e4SLinus Torvalds return error; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry) 11371da177e4SLinus Torvalds { 11381da177e4SLinus Torvalds int error; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id, 11411da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds lock_kernel(); 11441da177e4SLinus Torvalds nfs_begin_data_update(dir); 11451da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 11461da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */ 11471da177e4SLinus Torvalds if (error == 0 && dentry->d_inode != NULL) 11481da177e4SLinus Torvalds dentry->d_inode->i_nlink = 0; 11491da177e4SLinus Torvalds nfs_end_data_update(dir); 11501da177e4SLinus Torvalds unlock_kernel(); 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds return error; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds static unsigned int sillycounter; 11581da177e4SLinus Torvalds const int i_inosize = sizeof(dir->i_ino)*2; 11591da177e4SLinus Torvalds const int countersize = sizeof(sillycounter)*2; 11601da177e4SLinus Torvalds const int slen = sizeof(".nfs") + i_inosize + countersize - 1; 11611da177e4SLinus Torvalds char silly[slen+1]; 11621da177e4SLinus Torvalds struct qstr qsilly; 11631da177e4SLinus Torvalds struct dentry *sdentry; 11641da177e4SLinus Torvalds int error = -EIO; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n", 11671da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 11681da177e4SLinus Torvalds atomic_read(&dentry->d_count)); 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds #ifdef NFS_PARANOIA 11711da177e4SLinus Torvalds if (!dentry->d_inode) 11721da177e4SLinus Torvalds printk("NFS: silly-renaming %s/%s, negative dentry??\n", 11731da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 11741da177e4SLinus Torvalds #endif 11751da177e4SLinus Torvalds /* 11761da177e4SLinus Torvalds * We don't allow a dentry to be silly-renamed twice. 11771da177e4SLinus Torvalds */ 11781da177e4SLinus Torvalds error = -EBUSY; 11791da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 11801da177e4SLinus Torvalds goto out; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds sprintf(silly, ".nfs%*.*lx", 11831da177e4SLinus Torvalds i_inosize, i_inosize, dentry->d_inode->i_ino); 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds sdentry = NULL; 11861da177e4SLinus Torvalds do { 11871da177e4SLinus Torvalds char *suffix = silly + slen - countersize; 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds dput(sdentry); 11901da177e4SLinus Torvalds sillycounter++; 11911da177e4SLinus Torvalds sprintf(suffix, "%*.*x", countersize, countersize, sillycounter); 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds dfprintk(VFS, "trying to rename %s to %s\n", 11941da177e4SLinus Torvalds dentry->d_name.name, silly); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds sdentry = lookup_one_len(silly, dentry->d_parent, slen); 11971da177e4SLinus Torvalds /* 11981da177e4SLinus Torvalds * N.B. Better to return EBUSY here ... it could be 11991da177e4SLinus Torvalds * dangerous to delete the file while it's in use. 12001da177e4SLinus Torvalds */ 12011da177e4SLinus Torvalds if (IS_ERR(sdentry)) 12021da177e4SLinus Torvalds goto out; 12031da177e4SLinus Torvalds } while(sdentry->d_inode != NULL); /* need negative lookup */ 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds qsilly.name = silly; 12061da177e4SLinus Torvalds qsilly.len = strlen(silly); 12071da177e4SLinus Torvalds nfs_begin_data_update(dir); 12081da177e4SLinus Torvalds if (dentry->d_inode) { 12091da177e4SLinus Torvalds nfs_begin_data_update(dentry->d_inode); 12101da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 12111da177e4SLinus Torvalds dir, &qsilly); 12121da177e4SLinus Torvalds nfs_end_data_update(dentry->d_inode); 12131da177e4SLinus Torvalds } else 12141da177e4SLinus Torvalds error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, 12151da177e4SLinus Torvalds dir, &qsilly); 12161da177e4SLinus Torvalds nfs_end_data_update(dir); 12171da177e4SLinus Torvalds if (!error) { 12181da177e4SLinus Torvalds nfs_renew_times(dentry); 12191da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12201da177e4SLinus Torvalds d_move(dentry, sdentry); 12211da177e4SLinus Torvalds error = nfs_async_unlink(dentry); 12221da177e4SLinus Torvalds /* If we return 0 we don't unlink */ 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds dput(sdentry); 12251da177e4SLinus Torvalds out: 12261da177e4SLinus Torvalds return error; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds /* 12301da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes, 12311da177e4SLinus Torvalds * and after checking that the file has only one user. 12321da177e4SLinus Torvalds * 12331da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation 12341da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode. 12351da177e4SLinus Torvalds */ 12361da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry) 12371da177e4SLinus Torvalds { 12381da177e4SLinus Torvalds struct inode *dir = dentry->d_parent->d_inode; 12391da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 12401da177e4SLinus Torvalds int error = -EBUSY; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", 12431da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */ 12461da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 12471da177e4SLinus Torvalds error = 0; 12481da177e4SLinus Torvalds goto out; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds nfs_begin_data_update(dir); 12521da177e4SLinus Torvalds if (inode != NULL) { 12531da177e4SLinus Torvalds nfs_begin_data_update(inode); 12541da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 12551da177e4SLinus Torvalds /* The VFS may want to delete this inode */ 12561da177e4SLinus Torvalds if (error == 0) 12571da177e4SLinus Torvalds inode->i_nlink--; 12581da177e4SLinus Torvalds nfs_end_data_update(inode); 12591da177e4SLinus Torvalds } else 12601da177e4SLinus Torvalds error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 12611da177e4SLinus Torvalds nfs_end_data_update(dir); 12621da177e4SLinus Torvalds out: 12631da177e4SLinus Torvalds return error; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 12671da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY. 12681da177e4SLinus Torvalds * 12691da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink. 12701da177e4SLinus Torvalds */ 12711da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds int error; 12741da177e4SLinus Torvalds int need_rehash = 0; 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 12771da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name); 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds lock_kernel(); 12801da177e4SLinus Torvalds spin_lock(&dcache_lock); 12811da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 12821da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) > 1) { 12831da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 12841da177e4SLinus Torvalds spin_unlock(&dcache_lock); 12851da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry); 12861da177e4SLinus Torvalds unlock_kernel(); 12871da177e4SLinus Torvalds return error; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds if (!d_unhashed(dentry)) { 12901da177e4SLinus Torvalds __d_drop(dentry); 12911da177e4SLinus Torvalds need_rehash = 1; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 12941da177e4SLinus Torvalds spin_unlock(&dcache_lock); 12951da177e4SLinus Torvalds error = nfs_safe_remove(dentry); 12961da177e4SLinus Torvalds if (!error) { 12971da177e4SLinus Torvalds nfs_renew_times(dentry); 12981da177e4SLinus Torvalds nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 12991da177e4SLinus Torvalds } else if (need_rehash) 13001da177e4SLinus Torvalds d_rehash(dentry); 13011da177e4SLinus Torvalds unlock_kernel(); 13021da177e4SLinus Torvalds return error; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds static int 13061da177e4SLinus Torvalds nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 13071da177e4SLinus Torvalds { 13081da177e4SLinus Torvalds struct iattr attr; 13091da177e4SLinus Torvalds struct nfs_fattr sym_attr; 13101da177e4SLinus Torvalds struct nfs_fh sym_fh; 13111da177e4SLinus Torvalds struct qstr qsymname; 13121da177e4SLinus Torvalds int error; 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, 13151da177e4SLinus Torvalds dir->i_ino, dentry->d_name.name, symname); 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds #ifdef NFS_PARANOIA 13181da177e4SLinus Torvalds if (dentry->d_inode) 13191da177e4SLinus Torvalds printk("nfs_proc_symlink: %s/%s not negative!\n", 13201da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 13211da177e4SLinus Torvalds #endif 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Fill in the sattr for the call. 13241da177e4SLinus Torvalds * Note: SunOS 4.1.2 crashes if the mode isn't initialized! 13251da177e4SLinus Torvalds */ 13261da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE; 13271da177e4SLinus Torvalds attr.ia_mode = S_IFLNK | S_IRWXUGO; 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds qsymname.name = symname; 13301da177e4SLinus Torvalds qsymname.len = strlen(symname); 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds lock_kernel(); 13331da177e4SLinus Torvalds nfs_begin_data_update(dir); 13341da177e4SLinus Torvalds error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname, 13351da177e4SLinus Torvalds &attr, &sym_fh, &sym_attr); 13361da177e4SLinus Torvalds nfs_end_data_update(dir); 13371da177e4SLinus Torvalds if (!error) { 13381da177e4SLinus Torvalds error = nfs_instantiate(dentry, &sym_fh, &sym_attr); 13391da177e4SLinus Torvalds } else { 13401da177e4SLinus Torvalds if (error == -EEXIST) 13411da177e4SLinus Torvalds printk("nfs_proc_symlink: %s/%s already exists??\n", 13421da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 13431da177e4SLinus Torvalds d_drop(dentry); 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds unlock_kernel(); 13461da177e4SLinus Torvalds return error; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds static int 13501da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 13531da177e4SLinus Torvalds int error; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", 13561da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 13571da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds /* 13601da177e4SLinus Torvalds * Drop the dentry in advance to force a new lookup. 13611da177e4SLinus Torvalds * Since nfs_proc_link doesn't return a file handle, 13621da177e4SLinus Torvalds * we can't use the existing dentry. 13631da177e4SLinus Torvalds */ 13641da177e4SLinus Torvalds lock_kernel(); 13651da177e4SLinus Torvalds d_drop(dentry); 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds nfs_begin_data_update(dir); 13681da177e4SLinus Torvalds nfs_begin_data_update(inode); 13691da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 13701da177e4SLinus Torvalds nfs_end_data_update(inode); 13711da177e4SLinus Torvalds nfs_end_data_update(dir); 13721da177e4SLinus Torvalds unlock_kernel(); 13731da177e4SLinus Torvalds return error; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* 13771da177e4SLinus Torvalds * RENAME 13781da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 13791da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when 13801da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would 13811da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and 13821da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original 13831da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode. 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * FIXED. 13861da177e4SLinus Torvalds * 13871da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for 13881da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets 13891da177e4SLinus Torvalds * moved or linked to which happens automagically with the new 13901da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of 13911da177e4SLinus Torvalds * using the inode layer 13921da177e4SLinus Torvalds * 13931da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated 13941da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get 13951da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be 13961da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1. 13971da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing 13981da177e4SLinus Torvalds * the rename. 13991da177e4SLinus Torvalds */ 14001da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, 14011da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 14021da177e4SLinus Torvalds { 14031da177e4SLinus Torvalds struct inode *old_inode = old_dentry->d_inode; 14041da177e4SLinus Torvalds struct inode *new_inode = new_dentry->d_inode; 14051da177e4SLinus Torvalds struct dentry *dentry = NULL, *rehash = NULL; 14061da177e4SLinus Torvalds int error = -EBUSY; 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds /* 14091da177e4SLinus Torvalds * To prevent any new references to the target during the rename, 14101da177e4SLinus Torvalds * we unhash the dentry and free the inode in advance. 14111da177e4SLinus Torvalds */ 14121da177e4SLinus Torvalds lock_kernel(); 14131da177e4SLinus Torvalds if (!d_unhashed(new_dentry)) { 14141da177e4SLinus Torvalds d_drop(new_dentry); 14151da177e4SLinus Torvalds rehash = new_dentry; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", 14191da177e4SLinus Torvalds old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 14201da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, new_dentry->d_name.name, 14211da177e4SLinus Torvalds atomic_read(&new_dentry->d_count)); 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds /* 14241da177e4SLinus Torvalds * First check whether the target is busy ... we can't 14251da177e4SLinus Torvalds * safely do _any_ rename if the target is in use. 14261da177e4SLinus Torvalds * 14271da177e4SLinus Torvalds * For files, make a copy of the dentry and then do a 14281da177e4SLinus Torvalds * silly-rename. If the silly-rename succeeds, the 14291da177e4SLinus Torvalds * copied dentry is hashed and becomes the new target. 14301da177e4SLinus Torvalds */ 14311da177e4SLinus Torvalds if (!new_inode) 14321da177e4SLinus Torvalds goto go_ahead; 14331da177e4SLinus Torvalds if (S_ISDIR(new_inode->i_mode)) 14341da177e4SLinus Torvalds goto out; 14351da177e4SLinus Torvalds else if (atomic_read(&new_dentry->d_count) > 2) { 14361da177e4SLinus Torvalds int err; 14371da177e4SLinus Torvalds /* copy the target dentry's name */ 14381da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent, 14391da177e4SLinus Torvalds &new_dentry->d_name); 14401da177e4SLinus Torvalds if (!dentry) 14411da177e4SLinus Torvalds goto out; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* silly-rename the existing target ... */ 14441da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry); 14451da177e4SLinus Torvalds if (!err) { 14461da177e4SLinus Torvalds new_dentry = rehash = dentry; 14471da177e4SLinus Torvalds new_inode = NULL; 14481da177e4SLinus Torvalds /* instantiate the replacement target */ 14491da177e4SLinus Torvalds d_instantiate(new_dentry, NULL); 14501da177e4SLinus Torvalds } else if (atomic_read(&new_dentry->d_count) > 1) { 14511da177e4SLinus Torvalds /* dentry still busy? */ 14521da177e4SLinus Torvalds #ifdef NFS_PARANOIA 14531da177e4SLinus Torvalds printk("nfs_rename: target %s/%s busy, d_count=%d\n", 14541da177e4SLinus Torvalds new_dentry->d_parent->d_name.name, 14551da177e4SLinus Torvalds new_dentry->d_name.name, 14561da177e4SLinus Torvalds atomic_read(&new_dentry->d_count)); 14571da177e4SLinus Torvalds #endif 14581da177e4SLinus Torvalds goto out; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds go_ahead: 14631da177e4SLinus Torvalds /* 14641da177e4SLinus Torvalds * ... prune child dentries and writebacks if needed. 14651da177e4SLinus Torvalds */ 14661da177e4SLinus Torvalds if (atomic_read(&old_dentry->d_count) > 1) { 14671da177e4SLinus Torvalds nfs_wb_all(old_inode); 14681da177e4SLinus Torvalds shrink_dcache_parent(old_dentry); 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds if (new_inode) 14721da177e4SLinus Torvalds d_delete(new_dentry); 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds nfs_begin_data_update(old_dir); 14751da177e4SLinus Torvalds nfs_begin_data_update(new_dir); 14761da177e4SLinus Torvalds nfs_begin_data_update(old_inode); 14771da177e4SLinus Torvalds error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, 14781da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 14791da177e4SLinus Torvalds nfs_end_data_update(old_inode); 14801da177e4SLinus Torvalds nfs_end_data_update(new_dir); 14811da177e4SLinus Torvalds nfs_end_data_update(old_dir); 14821da177e4SLinus Torvalds out: 14831da177e4SLinus Torvalds if (rehash) 14841da177e4SLinus Torvalds d_rehash(rehash); 14851da177e4SLinus Torvalds if (!error) { 14861da177e4SLinus Torvalds if (!S_ISDIR(old_inode->i_mode)) 14871da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 14881da177e4SLinus Torvalds nfs_renew_times(new_dentry); 14891da177e4SLinus Torvalds nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir)); 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* new dentry created? */ 14931da177e4SLinus Torvalds if (dentry) 14941da177e4SLinus Torvalds dput(dentry); 14951da177e4SLinus Torvalds unlock_kernel(); 14961da177e4SLinus Torvalds return error; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) 15001da177e4SLinus Torvalds { 15011da177e4SLinus Torvalds struct nfs_access_entry *cache = &NFS_I(inode)->cache_access; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds if (cache->cred != cred 15041da177e4SLinus Torvalds || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)) 15051da177e4SLinus Torvalds || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS)) 15061da177e4SLinus Torvalds return -ENOENT; 15071da177e4SLinus Torvalds memcpy(res, cache, sizeof(*res)); 15081da177e4SLinus Torvalds return 0; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 15121da177e4SLinus Torvalds { 15131da177e4SLinus Torvalds struct nfs_access_entry *cache = &NFS_I(inode)->cache_access; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds if (cache->cred != set->cred) { 15161da177e4SLinus Torvalds if (cache->cred) 15171da177e4SLinus Torvalds put_rpccred(cache->cred); 15181da177e4SLinus Torvalds cache->cred = get_rpccred(set->cred); 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS; 15211da177e4SLinus Torvalds cache->jiffies = set->jiffies; 15221da177e4SLinus Torvalds cache->mask = set->mask; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct nfs_access_entry cache; 15281da177e4SLinus Torvalds int status; 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds status = nfs_access_get_cached(inode, cred, &cache); 15311da177e4SLinus Torvalds if (status == 0) 15321da177e4SLinus Torvalds goto out; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds /* Be clever: ask server to check for all possible rights */ 15351da177e4SLinus Torvalds cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; 15361da177e4SLinus Torvalds cache.cred = cred; 15371da177e4SLinus Torvalds cache.jiffies = jiffies; 15381da177e4SLinus Torvalds status = NFS_PROTO(inode)->access(inode, &cache); 15391da177e4SLinus Torvalds if (status != 0) 15401da177e4SLinus Torvalds return status; 15411da177e4SLinus Torvalds nfs_access_add_cache(inode, &cache); 15421da177e4SLinus Torvalds out: 15431da177e4SLinus Torvalds if ((cache.mask & mask) == mask) 15441da177e4SLinus Torvalds return 0; 15451da177e4SLinus Torvalds return -EACCES; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds int nfs_permission(struct inode *inode, int mask, struct nameidata *nd) 15491da177e4SLinus Torvalds { 15501da177e4SLinus Torvalds struct rpc_cred *cred; 15511da177e4SLinus Torvalds int res = 0; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds if (mask == 0) 15541da177e4SLinus Torvalds goto out; 15551da177e4SLinus Torvalds /* Is this sys_access() ? */ 15561da177e4SLinus Torvalds if (nd != NULL && (nd->flags & LOOKUP_ACCESS)) 15571da177e4SLinus Torvalds goto force_lookup; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) { 15601da177e4SLinus Torvalds case S_IFLNK: 15611da177e4SLinus Torvalds goto out; 15621da177e4SLinus Torvalds case S_IFREG: 15631da177e4SLinus Torvalds /* NFSv4 has atomic_open... */ 15641da177e4SLinus Torvalds if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) 15651da177e4SLinus Torvalds && nd != NULL 15661da177e4SLinus Torvalds && (nd->flags & LOOKUP_OPEN)) 15671da177e4SLinus Torvalds goto out; 15681da177e4SLinus Torvalds break; 15691da177e4SLinus Torvalds case S_IFDIR: 15701da177e4SLinus Torvalds /* 15711da177e4SLinus Torvalds * Optimize away all write operations, since the server 15721da177e4SLinus Torvalds * will check permissions when we perform the op. 15731da177e4SLinus Torvalds */ 15741da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 15751da177e4SLinus Torvalds goto out; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds force_lookup: 15791da177e4SLinus Torvalds lock_kernel(); 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access) 15821da177e4SLinus Torvalds goto out_notsup; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0); 15851da177e4SLinus Torvalds if (!IS_ERR(cred)) { 15861da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask); 15871da177e4SLinus Torvalds put_rpccred(cred); 15881da177e4SLinus Torvalds } else 15891da177e4SLinus Torvalds res = PTR_ERR(cred); 15901da177e4SLinus Torvalds unlock_kernel(); 15911da177e4SLinus Torvalds out: 15921da177e4SLinus Torvalds return res; 15931da177e4SLinus Torvalds out_notsup: 15941da177e4SLinus Torvalds res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 15951da177e4SLinus Torvalds if (res == 0) 15961da177e4SLinus Torvalds res = generic_permission(inode, mask, NULL); 15971da177e4SLinus Torvalds unlock_kernel(); 15981da177e4SLinus Torvalds return res; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds /* 16021da177e4SLinus Torvalds * Local variables: 16031da177e4SLinus Torvalds * version-control: t 16041da177e4SLinus Torvalds * kept-new-versions: 5 16051da177e4SLinus Torvalds * End: 16061da177e4SLinus Torvalds */ 1607