Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/dir.c |
| 3 | * |
| 4 | * Copyright (C) 1992 Rick Sladkey |
| 5 | * |
| 6 | * nfs directory handling functions |
| 7 | * |
| 8 | * 10 Apr 1996 Added silly rename for unlink --okir |
| 9 | * 28 Sep 1996 Improved directory cache --okir |
| 10 | * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de |
| 11 | * Re-implemented silly rename for unlink, newly implemented |
| 12 | * silly rename for nfs_rename() following the suggestions |
| 13 | * of Olaf Kirch (okir) found in this file. |
| 14 | * Following Linus comments on my original hack, this version |
| 15 | * depends only on the dcache stuff and doesn't touch the inode |
| 16 | * layer (iput() and friends). |
| 17 | * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM |
| 18 | */ |
| 19 | |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/stat.h> |
| 23 | #include <linux/fcntl.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/sunrpc/clnt.h> |
| 29 | #include <linux/nfs_fs.h> |
| 30 | #include <linux/nfs_mount.h> |
| 31 | #include <linux/pagemap.h> |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 32 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/namei.h> |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 34 | #include <linux/mount.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 37 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "delegation.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 39 | #include "iostat.h" |
Adrian Bunk | 4c30d56 | 2007-11-21 15:04:31 -0800 | [diff] [blame] | 40 | #include "internal.h" |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 41 | #include "fscache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* #define NFS_DEBUG_VERBOSE 1 */ |
| 44 | |
| 45 | static int nfs_opendir(struct inode *, struct file *); |
| 46 | static int nfs_readdir(struct file *, void *, filldir_t); |
| 47 | static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); |
| 48 | static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); |
| 49 | static int nfs_mkdir(struct inode *, struct dentry *, int); |
| 50 | static int nfs_rmdir(struct inode *, struct dentry *); |
| 51 | static int nfs_unlink(struct inode *, struct dentry *); |
| 52 | static int nfs_symlink(struct inode *, struct dentry *, const char *); |
| 53 | static int nfs_link(struct dentry *, struct inode *, struct dentry *); |
| 54 | static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); |
| 55 | static int nfs_rename(struct inode *, struct dentry *, |
| 56 | struct inode *, struct dentry *); |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 57 | static int nfs_fsync_dir(struct file *, int); |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 58 | static loff_t nfs_llseek_dir(struct file *, loff_t, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 60 | const struct file_operations nfs_dir_operations = { |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 61 | .llseek = nfs_llseek_dir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | .read = generic_read_dir, |
| 63 | .readdir = nfs_readdir, |
| 64 | .open = nfs_opendir, |
| 65 | .release = nfs_release, |
| 66 | .fsync = nfs_fsync_dir, |
| 67 | }; |
| 68 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 69 | const struct inode_operations nfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | .create = nfs_create, |
| 71 | .lookup = nfs_lookup, |
| 72 | .link = nfs_link, |
| 73 | .unlink = nfs_unlink, |
| 74 | .symlink = nfs_symlink, |
| 75 | .mkdir = nfs_mkdir, |
| 76 | .rmdir = nfs_rmdir, |
| 77 | .mknod = nfs_mknod, |
| 78 | .rename = nfs_rename, |
| 79 | .permission = nfs_permission, |
| 80 | .getattr = nfs_getattr, |
| 81 | .setattr = nfs_setattr, |
| 82 | }; |
| 83 | |
Andreas Gruenbacher | b7fa055 | 2005-06-22 17:16:27 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_NFS_V3 |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 85 | const struct inode_operations nfs3_dir_inode_operations = { |
Andreas Gruenbacher | b7fa055 | 2005-06-22 17:16:27 +0000 | [diff] [blame] | 86 | .create = nfs_create, |
| 87 | .lookup = nfs_lookup, |
| 88 | .link = nfs_link, |
| 89 | .unlink = nfs_unlink, |
| 90 | .symlink = nfs_symlink, |
| 91 | .mkdir = nfs_mkdir, |
| 92 | .rmdir = nfs_rmdir, |
| 93 | .mknod = nfs_mknod, |
| 94 | .rename = nfs_rename, |
| 95 | .permission = nfs_permission, |
| 96 | .getattr = nfs_getattr, |
| 97 | .setattr = nfs_setattr, |
| 98 | .listxattr = nfs3_listxattr, |
| 99 | .getxattr = nfs3_getxattr, |
| 100 | .setxattr = nfs3_setxattr, |
| 101 | .removexattr = nfs3_removexattr, |
| 102 | }; |
| 103 | #endif /* CONFIG_NFS_V3 */ |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #ifdef CONFIG_NFS_V4 |
| 106 | |
| 107 | static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 108 | const struct inode_operations nfs4_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | .create = nfs_create, |
| 110 | .lookup = nfs_atomic_lookup, |
| 111 | .link = nfs_link, |
| 112 | .unlink = nfs_unlink, |
| 113 | .symlink = nfs_symlink, |
| 114 | .mkdir = nfs_mkdir, |
| 115 | .rmdir = nfs_rmdir, |
| 116 | .mknod = nfs_mknod, |
| 117 | .rename = nfs_rename, |
| 118 | .permission = nfs_permission, |
| 119 | .getattr = nfs_getattr, |
| 120 | .setattr = nfs_setattr, |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 121 | .getxattr = nfs4_getxattr, |
| 122 | .setxattr = nfs4_setxattr, |
| 123 | .listxattr = nfs4_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | #endif /* CONFIG_NFS_V4 */ |
| 127 | |
| 128 | /* |
| 129 | * Open file |
| 130 | */ |
| 131 | static int |
| 132 | nfs_opendir(struct inode *inode, struct file *filp) |
| 133 | { |
Carsten Otte | 7451c4f | 2006-04-19 13:06:37 -0400 | [diff] [blame] | 134 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Chuck Lever | 6da24bc | 2008-06-11 17:55:58 -0400 | [diff] [blame] | 136 | dfprintk(FILE, "NFS: open dir(%s/%s)\n", |
Chuck Lever | cc0dd2d | 2008-06-11 17:55:42 -0400 | [diff] [blame] | 137 | filp->f_path.dentry->d_parent->d_name.name, |
| 138 | filp->f_path.dentry->d_name.name); |
| 139 | |
| 140 | nfs_inc_stats(inode, NFSIOS_VFSOPEN); |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* Call generic open code in order to cache credentials */ |
Carsten Otte | 7451c4f | 2006-04-19 13:06:37 -0400 | [diff] [blame] | 143 | res = nfs_open(inode, filp); |
Neil Brown | f5a7367 | 2010-08-10 10:20:05 -0400 | [diff] [blame] | 144 | if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) { |
| 145 | /* This is a mountpoint, so d_revalidate will never |
| 146 | * have been called, so we need to refresh the |
| 147 | * inode (for close-open consistency) ourselves. |
| 148 | */ |
| 149 | __nfs_revalidate_inode(NFS_SERVER(inode), inode); |
| 150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return res; |
| 152 | } |
| 153 | |
Al Viro | 0dbb4c6 | 2006-10-19 23:28:49 -0700 | [diff] [blame] | 154 | typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | typedef struct { |
| 156 | struct file *file; |
| 157 | struct page *page; |
| 158 | unsigned long page_index; |
Al Viro | 0dbb4c6 | 2006-10-19 23:28:49 -0700 | [diff] [blame] | 159 | __be32 *ptr; |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 160 | u64 *dir_cookie; |
| 161 | loff_t current_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | struct nfs_entry *entry; |
| 163 | decode_dirent_t decode; |
| 164 | int plus; |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 165 | unsigned long timestamp; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 166 | unsigned long gencount; |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 167 | int timestamp_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } nfs_readdir_descriptor_t; |
| 169 | |
| 170 | /* Now we cache directories properly, by stuffing the dirent |
| 171 | * data directly in the page cache. |
| 172 | * |
| 173 | * Inode invalidation due to refresh etc. takes care of |
| 174 | * _everything_, no sloppy entry flushing logic, no extraneous |
| 175 | * copying, network direct to page cache, the way it was meant |
| 176 | * to be. |
| 177 | * |
| 178 | * NOTE: Dirent information verification is done always by the |
| 179 | * page-in of the RPC reply, nowhere else, this simplies |
| 180 | * things substantially. |
| 181 | */ |
| 182 | static |
| 183 | int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page) |
| 184 | { |
| 185 | struct file *file = desc->file; |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 186 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | struct rpc_cred *cred = nfs_file_cred(file); |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 188 | unsigned long timestamp, gencount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | int error; |
| 190 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 191 | dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 192 | __func__, (long long)desc->entry->cookie, |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 193 | page->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | again: |
| 196 | timestamp = jiffies; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 197 | gencount = nfs_inc_attr_generation_counter(); |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 198 | error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | NFS_SERVER(inode)->dtsize, desc->plus); |
| 200 | if (error < 0) { |
| 201 | /* We requested READDIRPLUS, but the server doesn't grok it */ |
| 202 | if (error == -ENOTSUPP && desc->plus) { |
| 203 | NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; |
Benny Halevy | 3a10c30 | 2008-01-23 08:58:59 +0200 | [diff] [blame] | 204 | clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | desc->plus = 0; |
| 206 | goto again; |
| 207 | } |
| 208 | goto error; |
| 209 | } |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 210 | desc->timestamp = timestamp; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 211 | desc->gencount = gencount; |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 212 | desc->timestamp_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* Ensure consistent page alignment of the data. |
| 215 | * Note: assumes we have exclusive access to this mapping either |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 216 | * through inode->i_mutex or some other mechanism. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | */ |
Trond Myklebust | 2aac05a | 2008-07-07 13:26:10 -0400 | [diff] [blame] | 218 | if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) { |
Trond Myklebust | cd9ae2b | 2006-10-19 23:28:40 -0700 | [diff] [blame] | 219 | /* Should never happen */ |
| 220 | nfs_zap_mapping(inode, inode->i_mapping); |
| 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | unlock_page(page); |
| 223 | return 0; |
| 224 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return -EIO; |
| 227 | } |
| 228 | |
| 229 | static inline |
| 230 | int dir_decode(nfs_readdir_descriptor_t *desc) |
| 231 | { |
Al Viro | 0dbb4c6 | 2006-10-19 23:28:49 -0700 | [diff] [blame] | 232 | __be32 *p = desc->ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | p = desc->decode(p, desc->entry, desc->plus); |
| 234 | if (IS_ERR(p)) |
| 235 | return PTR_ERR(p); |
| 236 | desc->ptr = p; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 237 | if (desc->timestamp_valid) { |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 238 | desc->entry->fattr->time_start = desc->timestamp; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 239 | desc->entry->fattr->gencount = desc->gencount; |
| 240 | } else |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 241 | desc->entry->fattr->valid &= ~NFS_ATTR_FATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static inline |
| 246 | void dir_page_release(nfs_readdir_descriptor_t *desc) |
| 247 | { |
| 248 | kunmap(desc->page); |
| 249 | page_cache_release(desc->page); |
| 250 | desc->page = NULL; |
| 251 | desc->ptr = NULL; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Given a pointer to a buffer that has already been filled by a call |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 256 | * to readdir, find the next entry with cookie '*desc->dir_cookie'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | * |
| 258 | * If the end of the buffer has been reached, return -EAGAIN, if not, |
| 259 | * return the offset within the buffer of the next entry to be |
| 260 | * read. |
| 261 | */ |
| 262 | static inline |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 263 | int find_dirent(nfs_readdir_descriptor_t *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | struct nfs_entry *entry = desc->entry; |
| 266 | int loop_count = 0, |
| 267 | status; |
| 268 | |
| 269 | while((status = dir_decode(desc)) == 0) { |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 270 | dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 271 | __func__, (unsigned long long)entry->cookie); |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 272 | if (entry->prev_cookie == *desc->dir_cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | break; |
| 274 | if (loop_count++ > 200) { |
| 275 | loop_count = 0; |
| 276 | schedule(); |
| 277 | } |
| 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return status; |
| 280 | } |
| 281 | |
| 282 | /* |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 283 | * Given a pointer to a buffer that has already been filled by a call |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 284 | * to readdir, find the entry at offset 'desc->file->f_pos'. |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 285 | * |
| 286 | * If the end of the buffer has been reached, return -EAGAIN, if not, |
| 287 | * return the offset within the buffer of the next entry to be |
| 288 | * read. |
| 289 | */ |
| 290 | static inline |
| 291 | int find_dirent_index(nfs_readdir_descriptor_t *desc) |
| 292 | { |
| 293 | struct nfs_entry *entry = desc->entry; |
| 294 | int loop_count = 0, |
| 295 | status; |
| 296 | |
| 297 | for(;;) { |
| 298 | status = dir_decode(desc); |
| 299 | if (status) |
| 300 | break; |
| 301 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 302 | dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n", |
| 303 | (unsigned long long)entry->cookie, desc->current_index); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 304 | |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 305 | if (desc->file->f_pos == desc->current_index) { |
| 306 | *desc->dir_cookie = entry->cookie; |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | desc->current_index++; |
| 310 | if (loop_count++ > 200) { |
| 311 | loop_count = 0; |
| 312 | schedule(); |
| 313 | } |
| 314 | } |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 315 | return status; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Find the given page, and call find_dirent() or find_dirent_index in |
| 320 | * order to try to return the next entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | */ |
| 322 | static inline |
| 323 | int find_dirent_page(nfs_readdir_descriptor_t *desc) |
| 324 | { |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 325 | struct inode *inode = desc->file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | struct page *page; |
| 327 | int status; |
| 328 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 329 | dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 330 | __func__, desc->page_index, |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 331 | (long long) *desc->dir_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 333 | /* If we find the page in the page_cache, we cannot be sure |
| 334 | * how fresh the data is, so we will ignore readdir_plus attributes. |
| 335 | */ |
| 336 | desc->timestamp_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | page = read_cache_page(inode->i_mapping, desc->page_index, |
| 338 | (filler_t *)nfs_readdir_filler, desc); |
| 339 | if (IS_ERR(page)) { |
| 340 | status = PTR_ERR(page); |
| 341 | goto out; |
| 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | /* NOTE: Someone else may have changed the READDIRPLUS flag */ |
| 345 | desc->page = page; |
| 346 | desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 347 | if (*desc->dir_cookie != 0) |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 348 | status = find_dirent(desc); |
| 349 | else |
| 350 | status = find_dirent_index(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (status < 0) |
| 352 | dir_page_release(desc); |
| 353 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 354 | dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Recurse through the page cache pages, and return a |
| 360 | * filled nfs_entry structure of the next directory entry if possible. |
| 361 | * |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 362 | * The target for the search is '*desc->dir_cookie' if non-0, |
| 363 | * 'desc->file->f_pos' otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | */ |
| 365 | static inline |
| 366 | int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) |
| 367 | { |
| 368 | int loop_count = 0; |
| 369 | int res; |
| 370 | |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 371 | /* Always search-by-index from the beginning of the cache */ |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 372 | if (*desc->dir_cookie == 0) { |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 373 | dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n", |
| 374 | (long long)desc->file->f_pos); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 375 | desc->page_index = 0; |
| 376 | desc->entry->cookie = desc->entry->prev_cookie = 0; |
| 377 | desc->entry->eof = 0; |
| 378 | desc->current_index = 0; |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 379 | } else |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 380 | dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", |
| 381 | (unsigned long long)*desc->dir_cookie); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | for (;;) { |
| 384 | res = find_dirent_page(desc); |
| 385 | if (res != -EAGAIN) |
| 386 | break; |
| 387 | /* Align to beginning of next page */ |
| 388 | desc->page_index ++; |
| 389 | if (loop_count++ > 200) { |
| 390 | loop_count = 0; |
| 391 | schedule(); |
| 392 | } |
| 393 | } |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 394 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 395 | dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return res; |
| 397 | } |
| 398 | |
| 399 | static inline unsigned int dt_type(struct inode *inode) |
| 400 | { |
| 401 | return (inode->i_mode >> 12) & 15; |
| 402 | } |
| 403 | |
| 404 | static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc); |
| 405 | |
| 406 | /* |
| 407 | * Once we've found the start of the dirent within a page: fill 'er up... |
| 408 | */ |
| 409 | static |
| 410 | int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, |
| 411 | filldir_t filldir) |
| 412 | { |
| 413 | struct file *file = desc->file; |
| 414 | struct nfs_entry *entry = desc->entry; |
| 415 | struct dentry *dentry = NULL; |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 416 | u64 fileid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | int loop_count = 0, |
| 418 | res; |
| 419 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 420 | dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", |
| 421 | (unsigned long long)entry->cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | for(;;) { |
| 424 | unsigned d_type = DT_UNKNOWN; |
| 425 | /* Note: entry->prev_cookie contains the cookie for |
| 426 | * retrieving the current dirent on the server */ |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 427 | fileid = entry->ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | /* Get a dentry if we have one */ |
| 430 | if (dentry != NULL) |
| 431 | dput(dentry); |
| 432 | dentry = nfs_readdir_lookup(desc); |
| 433 | |
| 434 | /* Use readdirplus info */ |
| 435 | if (dentry != NULL && dentry->d_inode != NULL) { |
| 436 | d_type = dt_type(dentry->d_inode); |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 437 | fileid = NFS_FILEID(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | res = filldir(dirent, entry->name, entry->len, |
Trond Myklebust | f43bf0b | 2007-10-09 12:01:04 -0400 | [diff] [blame] | 441 | file->f_pos, nfs_compat_user_ino64(fileid), |
| 442 | d_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (res < 0) |
| 444 | break; |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 445 | file->f_pos++; |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 446 | *desc->dir_cookie = entry->cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (dir_decode(desc) != 0) { |
| 448 | desc->page_index ++; |
| 449 | break; |
| 450 | } |
| 451 | if (loop_count++ > 200) { |
| 452 | loop_count = 0; |
| 453 | schedule(); |
| 454 | } |
| 455 | } |
| 456 | dir_page_release(desc); |
| 457 | if (dentry != NULL) |
| 458 | dput(dentry); |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 459 | dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", |
| 460 | (unsigned long long)*desc->dir_cookie, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return res; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * If we cannot find a cookie in our cache, we suspect that this is |
| 466 | * because it points to a deleted file, so we ask the server to return |
| 467 | * whatever it thinks is the next entry. We then feed this to filldir. |
| 468 | * If all goes well, we should then be able to find our way round the |
| 469 | * cache on the next call to readdir_search_pagecache(); |
| 470 | * |
| 471 | * NOTE: we cannot add the anonymous page to the pagecache because |
| 472 | * the data it contains might not be page aligned. Besides, |
| 473 | * we should already have a complete representation of the |
| 474 | * directory in the page cache by the time we get here. |
| 475 | */ |
| 476 | static inline |
| 477 | int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, |
| 478 | filldir_t filldir) |
| 479 | { |
| 480 | struct file *file = desc->file; |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 481 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | struct rpc_cred *cred = nfs_file_cred(file); |
| 483 | struct page *page = NULL; |
| 484 | int status; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 485 | unsigned long timestamp, gencount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 487 | dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", |
| 488 | (unsigned long long)*desc->dir_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | page = alloc_page(GFP_HIGHUSER); |
| 491 | if (!page) { |
| 492 | status = -ENOMEM; |
| 493 | goto out; |
| 494 | } |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 495 | timestamp = jiffies; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 496 | gencount = nfs_inc_attr_generation_counter(); |
Jeff Layton | 2560665 | 2008-02-12 06:49:01 -0500 | [diff] [blame] | 497 | status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, |
| 498 | *desc->dir_cookie, page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | NFS_SERVER(inode)->dtsize, |
| 500 | desc->plus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | desc->page = page; |
| 502 | desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ |
Jeff Layton | 2560665 | 2008-02-12 06:49:01 -0500 | [diff] [blame] | 503 | if (status >= 0) { |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 504 | desc->timestamp = timestamp; |
Trond Myklebust | 4704f0e | 2008-10-14 19:16:07 -0400 | [diff] [blame] | 505 | desc->gencount = gencount; |
Neil Brown | 1f4eab7 | 2007-04-16 09:35:27 +1000 | [diff] [blame] | 506 | desc->timestamp_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if ((status = dir_decode(desc)) == 0) |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 508 | desc->entry->prev_cookie = *desc->dir_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } else |
| 510 | status = -EIO; |
| 511 | if (status < 0) |
| 512 | goto out_release; |
| 513 | |
| 514 | status = nfs_do_filldir(desc, dirent, filldir); |
| 515 | |
| 516 | /* Reset read descriptor so it searches the page cache from |
| 517 | * the start upon the next call to readdir_search_pagecache() */ |
| 518 | desc->page_index = 0; |
| 519 | desc->entry->cookie = desc->entry->prev_cookie = 0; |
| 520 | desc->entry->eof = 0; |
| 521 | out: |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 522 | dfprintk(DIRCACHE, "NFS: %s: returns %d\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 523 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return status; |
| 525 | out_release: |
| 526 | dir_page_release(desc); |
| 527 | goto out; |
| 528 | } |
| 529 | |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 530 | /* The file offset position represents the dirent entry number. A |
| 531 | last cookie cache takes care of the common case of reading the |
| 532 | whole directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | */ |
| 534 | static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 535 | { |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 536 | struct dentry *dentry = filp->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | struct inode *inode = dentry->d_inode; |
| 538 | nfs_readdir_descriptor_t my_desc, |
| 539 | *desc = &my_desc; |
| 540 | struct nfs_entry my_entry; |
Trond Myklebust | aa49b4c | 2010-04-16 16:22:49 -0400 | [diff] [blame] | 541 | int res = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Chuck Lever | 6da24bc | 2008-06-11 17:55:58 -0400 | [diff] [blame] | 543 | dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n", |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 544 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 545 | (long long)filp->f_pos); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 546 | nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | /* |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 549 | * filp->f_pos points to the dirent entry number. |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 550 | * *desc->dir_cookie has the cookie for the next entry. We have |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 551 | * to either find the entry with the appropriate number or |
| 552 | * revalidate the cookie. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | */ |
| 554 | memset(desc, 0, sizeof(*desc)); |
| 555 | |
| 556 | desc->file = filp; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 557 | desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | desc->decode = NFS_PROTO(inode)->decode_dirent; |
| 559 | desc->plus = NFS_USE_READDIRPLUS(inode); |
| 560 | |
| 561 | my_entry.cookie = my_entry.prev_cookie = 0; |
| 562 | my_entry.eof = 0; |
Trond Myklebust | aa49b4c | 2010-04-16 16:22:49 -0400 | [diff] [blame] | 563 | my_entry.fh = nfs_alloc_fhandle(); |
| 564 | my_entry.fattr = nfs_alloc_fattr(); |
| 565 | if (my_entry.fh == NULL || my_entry.fattr == NULL) |
| 566 | goto out_alloc_failed; |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | desc->entry = &my_entry; |
| 569 | |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 570 | nfs_block_sillyrename(dentry); |
Trond Myklebust | 1cda707 | 2010-02-19 17:03:30 -0800 | [diff] [blame] | 571 | res = nfs_revalidate_mapping(inode, filp->f_mapping); |
Trond Myklebust | fccca7f | 2008-01-26 17:37:47 -0500 | [diff] [blame] | 572 | if (res < 0) |
| 573 | goto out; |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | while(!desc->entry->eof) { |
| 576 | res = readdir_search_pagecache(desc); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (res == -EBADCOOKIE) { |
| 579 | /* This means either end of directory */ |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 580 | if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | /* Or that the server has 'lost' a cookie */ |
| 582 | res = uncached_readdir(desc, dirent, filldir); |
| 583 | if (res >= 0) |
| 584 | continue; |
| 585 | } |
| 586 | res = 0; |
| 587 | break; |
| 588 | } |
| 589 | if (res == -ETOOSMALL && desc->plus) { |
Benny Halevy | 3a10c30 | 2008-01-23 08:58:59 +0200 | [diff] [blame] | 590 | clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | nfs_zap_caches(inode); |
| 592 | desc->plus = 0; |
| 593 | desc->entry->eof = 0; |
| 594 | continue; |
| 595 | } |
| 596 | if (res < 0) |
| 597 | break; |
| 598 | |
| 599 | res = nfs_do_filldir(desc, dirent, filldir); |
| 600 | if (res < 0) { |
| 601 | res = 0; |
| 602 | break; |
| 603 | } |
| 604 | } |
Trond Myklebust | fccca7f | 2008-01-26 17:37:47 -0500 | [diff] [blame] | 605 | out: |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 606 | nfs_unblock_sillyrename(dentry); |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 607 | if (res > 0) |
| 608 | res = 0; |
Trond Myklebust | aa49b4c | 2010-04-16 16:22:49 -0400 | [diff] [blame] | 609 | out_alloc_failed: |
| 610 | nfs_free_fattr(my_entry.fattr); |
| 611 | nfs_free_fhandle(my_entry.fh); |
| 612 | dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n", |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 613 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 614 | res); |
| 615 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Trond Myklebust | 10afec9 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 618 | static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 619 | { |
Chuck Lever | b84e06c | 2008-06-11 17:55:34 -0400 | [diff] [blame] | 620 | struct dentry *dentry = filp->f_path.dentry; |
| 621 | struct inode *inode = dentry->d_inode; |
| 622 | |
Chuck Lever | 6da24bc | 2008-06-11 17:55:58 -0400 | [diff] [blame] | 623 | dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n", |
Chuck Lever | b84e06c | 2008-06-11 17:55:34 -0400 | [diff] [blame] | 624 | dentry->d_parent->d_name.name, |
| 625 | dentry->d_name.name, |
| 626 | offset, origin); |
| 627 | |
| 628 | mutex_lock(&inode->i_mutex); |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 629 | switch (origin) { |
| 630 | case 1: |
| 631 | offset += filp->f_pos; |
| 632 | case 0: |
| 633 | if (offset >= 0) |
| 634 | break; |
| 635 | default: |
| 636 | offset = -EINVAL; |
| 637 | goto out; |
| 638 | } |
| 639 | if (offset != filp->f_pos) { |
| 640 | filp->f_pos = offset; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 641 | nfs_file_open_context(filp)->dir_cookie = 0; |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 642 | } |
| 643 | out: |
Chuck Lever | b84e06c | 2008-06-11 17:55:34 -0400 | [diff] [blame] | 644 | mutex_unlock(&inode->i_mutex); |
Trond Myklebust | f0dd213 | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 645 | return offset; |
| 646 | } |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* |
| 649 | * All directory operations under NFS are synchronous, so fsync() |
| 650 | * is a dummy operation. |
| 651 | */ |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 652 | static int nfs_fsync_dir(struct file *filp, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 654 | struct dentry *dentry = filp->f_path.dentry; |
| 655 | |
Chuck Lever | 6da24bc | 2008-06-11 17:55:58 -0400 | [diff] [blame] | 656 | dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n", |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 657 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 658 | datasync); |
| 659 | |
Chuck Lever | 5491778 | 2008-05-27 16:29:07 -0400 | [diff] [blame] | 660 | nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
Trond Myklebust | bfc69a4 | 2007-10-15 18:18:29 -0400 | [diff] [blame] | 664 | /** |
| 665 | * nfs_force_lookup_revalidate - Mark the directory as having changed |
| 666 | * @dir - pointer to directory inode |
| 667 | * |
| 668 | * This forces the revalidation code in nfs_lookup_revalidate() to do a |
| 669 | * full lookup on all child dentries of 'dir' whenever a change occurs |
| 670 | * on the server that might have invalidated our dcache. |
| 671 | * |
| 672 | * The caller should be holding dir->i_lock |
| 673 | */ |
| 674 | void nfs_force_lookup_revalidate(struct inode *dir) |
| 675 | { |
Trond Myklebust | 011935a0 | 2008-10-14 19:24:50 -0400 | [diff] [blame] | 676 | NFS_I(dir)->cache_change_attribute++; |
Trond Myklebust | bfc69a4 | 2007-10-15 18:18:29 -0400 | [diff] [blame] | 677 | } |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /* |
| 680 | * A check for whether or not the parent directory has changed. |
| 681 | * In the case it has, we assume that the dentries are untrustworthy |
| 682 | * and may need to be looked up again. |
| 683 | */ |
Trond Myklebust | c79ba78 | 2007-01-31 08:16:24 -0500 | [diff] [blame] | 684 | static int nfs_check_verifier(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
| 686 | if (IS_ROOT(dentry)) |
| 687 | return 1; |
Trond Myklebust | 4eec952 | 2008-07-15 17:58:13 -0400 | [diff] [blame] | 688 | if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) |
| 689 | return 0; |
Trond Myklebust | f2c77f4 | 2007-10-02 12:54:39 -0400 | [diff] [blame] | 690 | if (!nfs_verify_change_attribute(dir, dentry->d_time)) |
| 691 | return 0; |
| 692 | /* Revalidate nfsi->cache_change_attribute before we declare a match */ |
| 693 | if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) |
| 694 | return 0; |
| 695 | if (!nfs_verify_change_attribute(dir, dentry->d_time)) |
| 696 | return 0; |
| 697 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | /* |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 701 | * Return the intent data that applies to this particular path component |
| 702 | * |
| 703 | * Note that the current set of intents only apply to the very last |
| 704 | * component of the path. |
| 705 | * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. |
| 706 | */ |
| 707 | static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) |
| 708 | { |
| 709 | if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) |
| 710 | return 0; |
| 711 | return nd->flags & mask; |
| 712 | } |
| 713 | |
| 714 | /* |
Trond Myklebust | a12802c | 2007-10-02 19:13:04 -0400 | [diff] [blame] | 715 | * Use intent information to check whether or not we're going to do |
| 716 | * an O_EXCL create using this path component. |
| 717 | */ |
| 718 | static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) |
| 719 | { |
| 720 | if (NFS_PROTO(dir)->version == 2) |
| 721 | return 0; |
Al Viro | 3516586 | 2008-08-05 03:00:49 -0400 | [diff] [blame] | 722 | return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL); |
Trond Myklebust | a12802c | 2007-10-02 19:13:04 -0400 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /* |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 726 | * Inode and filehandle revalidation for lookups. |
| 727 | * |
| 728 | * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, |
| 729 | * or if the intent information indicates that we're about to open this |
| 730 | * particular file and the "nocto" mount flag is not set. |
| 731 | * |
| 732 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | static inline |
| 734 | int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) |
| 735 | { |
| 736 | struct nfs_server *server = NFS_SERVER(inode); |
| 737 | |
Trond Myklebust | 4e99a1f | 2008-03-06 12:34:59 -0500 | [diff] [blame] | 738 | if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags)) |
| 739 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (nd != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | /* VFS wants an on-the-wire revalidation */ |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 742 | if (nd->flags & LOOKUP_REVAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | goto out_force; |
| 744 | /* This is an open(2) */ |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 745 | if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && |
Trond Myklebust | 4e0641a | 2006-07-05 13:05:13 -0400 | [diff] [blame] | 746 | !(server->flags & NFS_MOUNT_NOCTO) && |
| 747 | (S_ISREG(inode->i_mode) || |
| 748 | S_ISDIR(inode->i_mode))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | goto out_force; |
Trond Myklebust | 4f48af4 | 2007-10-02 23:13:32 -0400 | [diff] [blame] | 750 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | return nfs_revalidate_inode(server, inode); |
| 753 | out_force: |
| 754 | return __nfs_revalidate_inode(server, inode); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * We judge how long we want to trust negative |
| 759 | * dentries by looking at the parent inode mtime. |
| 760 | * |
| 761 | * If parent mtime has changed, we revalidate, else we wait for a |
| 762 | * period corresponding to the parent's attribute cache timeout value. |
| 763 | */ |
| 764 | static inline |
| 765 | int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, |
| 766 | struct nameidata *nd) |
| 767 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /* Don't revalidate a negative dentry if we're creating a new file */ |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 769 | if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | return 0; |
Trond Myklebust | 4eec952 | 2008-07-15 17:58:13 -0400 | [diff] [blame] | 771 | if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) |
| 772 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | return !nfs_check_verifier(dir, dentry); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * This is called every time the dcache has a lookup hit, |
| 778 | * and we should check whether we can really trust that |
| 779 | * lookup. |
| 780 | * |
| 781 | * NOTE! The hit can be a negative hit too, don't assume |
| 782 | * we have an inode! |
| 783 | * |
| 784 | * If the parent directory is seen to have changed, we throw out the |
| 785 | * cached dentry and do a new lookup. |
| 786 | */ |
| 787 | static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) |
| 788 | { |
| 789 | struct inode *dir; |
| 790 | struct inode *inode; |
| 791 | struct dentry *parent; |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 792 | struct nfs_fh *fhandle = NULL; |
| 793 | struct nfs_fattr *fattr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | parent = dget_parent(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | dir = parent->d_inode; |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 798 | nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | inode = dentry->d_inode; |
| 800 | |
| 801 | if (!inode) { |
| 802 | if (nfs_neg_need_reval(dir, dentry, nd)) |
| 803 | goto out_bad; |
| 804 | goto out_valid; |
| 805 | } |
| 806 | |
| 807 | if (is_bad_inode(inode)) { |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 808 | dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 809 | __func__, dentry->d_parent->d_name.name, |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 810 | dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | goto out_bad; |
| 812 | } |
| 813 | |
Trond Myklebust | 15860ab | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 814 | if (nfs_have_delegation(inode, FMODE_READ)) |
| 815 | goto out_set_verifier; |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | /* Force a full look up iff the parent directory has changed */ |
Trond Myklebust | a12802c | 2007-10-02 19:13:04 -0400 | [diff] [blame] | 818 | if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | if (nfs_lookup_verify_inode(inode, nd)) |
| 820 | goto out_zap_parent; |
| 821 | goto out_valid; |
| 822 | } |
| 823 | |
| 824 | if (NFS_STALE(inode)) |
| 825 | goto out_bad; |
| 826 | |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 827 | error = -ENOMEM; |
| 828 | fhandle = nfs_alloc_fhandle(); |
| 829 | fattr = nfs_alloc_fattr(); |
| 830 | if (fhandle == NULL || fattr == NULL) |
| 831 | goto out_error; |
| 832 | |
| 833 | error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | if (error) |
| 835 | goto out_bad; |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 836 | if (nfs_compare_fh(NFS_FH(inode), fhandle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | goto out_bad; |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 838 | if ((error = nfs_refresh_inode(inode, fattr)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | goto out_bad; |
| 840 | |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 841 | nfs_free_fattr(fattr); |
| 842 | nfs_free_fhandle(fhandle); |
Trond Myklebust | 15860ab | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 843 | out_set_verifier: |
Trond Myklebust | cf8ba45 | 2007-10-01 13:46:53 -0400 | [diff] [blame] | 844 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | out_valid: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | dput(parent); |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 847 | dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 848 | __func__, dentry->d_parent->d_name.name, |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 849 | dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | return 1; |
| 851 | out_zap_parent: |
| 852 | nfs_zap_caches(dir); |
| 853 | out_bad: |
Trond Myklebust | a1643a9 | 2007-09-29 17:25:43 -0400 | [diff] [blame] | 854 | nfs_mark_for_revalidate(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | if (inode && S_ISDIR(inode->i_mode)) { |
| 856 | /* Purge readdir caches. */ |
| 857 | nfs_zap_caches(inode); |
| 858 | /* If we have submounts, don't unhash ! */ |
| 859 | if (have_submounts(dentry)) |
| 860 | goto out_valid; |
Al Viro | d9e80b7 | 2010-04-29 03:10:43 +0100 | [diff] [blame] | 861 | if (dentry->d_flags & DCACHE_DISCONNECTED) |
| 862 | goto out_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | shrink_dcache_parent(dentry); |
| 864 | } |
| 865 | d_drop(dentry); |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 866 | nfs_free_fattr(fattr); |
| 867 | nfs_free_fhandle(fhandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | dput(parent); |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 869 | dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 870 | __func__, dentry->d_parent->d_name.name, |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 871 | dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | return 0; |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 873 | out_error: |
| 874 | nfs_free_fattr(fattr); |
| 875 | nfs_free_fhandle(fhandle); |
| 876 | dput(parent); |
| 877 | dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n", |
| 878 | __func__, dentry->d_parent->d_name.name, |
| 879 | dentry->d_name.name, error); |
| 880 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* |
| 884 | * This is called from dput() when d_count is going to 0. |
| 885 | */ |
| 886 | static int nfs_dentry_delete(struct dentry *dentry) |
| 887 | { |
| 888 | dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", |
| 889 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 890 | dentry->d_flags); |
| 891 | |
Trond Myklebust | 77f1119 | 2008-01-28 19:43:19 -0500 | [diff] [blame] | 892 | /* Unhash any dentry with a stale inode */ |
| 893 | if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode)) |
| 894 | return 1; |
| 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 897 | /* Unhash it, so that ->d_iput() would be called */ |
| 898 | return 1; |
| 899 | } |
| 900 | if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { |
| 901 | /* Unhash it, so that ancestors of killed async unlink |
| 902 | * files will be cleaned up during umount */ |
| 903 | return 1; |
| 904 | } |
| 905 | return 0; |
| 906 | |
| 907 | } |
| 908 | |
Trond Myklebust | 1b83d70 | 2008-06-11 15:44:04 -0400 | [diff] [blame] | 909 | static void nfs_drop_nlink(struct inode *inode) |
| 910 | { |
| 911 | spin_lock(&inode->i_lock); |
| 912 | if (inode->i_nlink > 0) |
| 913 | drop_nlink(inode); |
| 914 | spin_unlock(&inode->i_lock); |
| 915 | } |
| 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | /* |
| 918 | * Called when the dentry loses inode. |
| 919 | * We use it to clean up silly-renamed files. |
| 920 | */ |
| 921 | static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) |
| 922 | { |
Neil Brown | 83672d3 | 2007-02-26 12:48:25 +1100 | [diff] [blame] | 923 | if (S_ISDIR(inode->i_mode)) |
| 924 | /* drop any readdir cache as it could easily be old */ |
| 925 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; |
| 926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 928 | drop_nlink(inode); |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 929 | nfs_complete_unlink(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | iput(inode); |
| 932 | } |
| 933 | |
Al Viro | f786aa9 | 2009-02-20 05:51:22 +0000 | [diff] [blame] | 934 | const struct dentry_operations nfs_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | .d_revalidate = nfs_lookup_revalidate, |
| 936 | .d_delete = nfs_dentry_delete, |
| 937 | .d_iput = nfs_dentry_iput, |
| 938 | }; |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) |
| 941 | { |
| 942 | struct dentry *res; |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 943 | struct dentry *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | struct inode *inode = NULL; |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 945 | struct nfs_fh *fhandle = NULL; |
| 946 | struct nfs_fattr *fattr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | dfprintk(VFS, "NFS: lookup(%s/%s)\n", |
| 950 | dentry->d_parent->d_name.name, dentry->d_name.name); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 951 | nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
| 953 | res = ERR_PTR(-ENAMETOOLONG); |
| 954 | if (dentry->d_name.len > NFS_SERVER(dir)->namelen) |
| 955 | goto out; |
| 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | dentry->d_op = NFS_PROTO(dir)->dentry_ops; |
| 958 | |
Trond Myklebust | fd68407 | 2006-09-05 12:27:44 -0400 | [diff] [blame] | 959 | /* |
| 960 | * If we're doing an exclusive create, optimize away the lookup |
| 961 | * but don't hash the dentry. |
| 962 | */ |
| 963 | if (nfs_is_exclusive_create(dir, nd)) { |
| 964 | d_instantiate(dentry, NULL); |
| 965 | res = NULL; |
Trond Myklebust | fc0f684 | 2008-06-11 15:44:20 -0400 | [diff] [blame] | 966 | goto out; |
Trond Myklebust | fd68407 | 2006-09-05 12:27:44 -0400 | [diff] [blame] | 967 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 969 | res = ERR_PTR(-ENOMEM); |
| 970 | fhandle = nfs_alloc_fhandle(); |
| 971 | fattr = nfs_alloc_fattr(); |
| 972 | if (fhandle == NULL || fattr == NULL) |
| 973 | goto out; |
| 974 | |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 975 | parent = dentry->d_parent; |
| 976 | /* Protect against concurrent sillydeletes */ |
| 977 | nfs_block_sillyrename(parent); |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 978 | error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (error == -ENOENT) |
| 980 | goto no_entry; |
| 981 | if (error < 0) { |
| 982 | res = ERR_PTR(error); |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 983 | goto out_unblock_sillyrename; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 985 | inode = nfs_fhget(dentry->d_sb, fhandle, fattr); |
Trond Myklebust | 03f28e3 | 2006-03-20 13:44:48 -0500 | [diff] [blame] | 986 | res = (struct dentry *)inode; |
| 987 | if (IS_ERR(res)) |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 988 | goto out_unblock_sillyrename; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | no_entry: |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 991 | res = d_materialise_unique(dentry, inode); |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 992 | if (res != NULL) { |
| 993 | if (IS_ERR(res)) |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 994 | goto out_unblock_sillyrename; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | dentry = res; |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 998 | out_unblock_sillyrename: |
| 999 | nfs_unblock_sillyrename(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | out: |
Trond Myklebust | e1fb4d0 | 2010-04-16 16:22:47 -0400 | [diff] [blame] | 1001 | nfs_free_fattr(fattr); |
| 1002 | nfs_free_fhandle(fhandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | return res; |
| 1004 | } |
| 1005 | |
| 1006 | #ifdef CONFIG_NFS_V4 |
| 1007 | static int nfs_open_revalidate(struct dentry *, struct nameidata *); |
| 1008 | |
Al Viro | f786aa9 | 2009-02-20 05:51:22 +0000 | [diff] [blame] | 1009 | const struct dentry_operations nfs4_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | .d_revalidate = nfs_open_revalidate, |
| 1011 | .d_delete = nfs_dentry_delete, |
| 1012 | .d_iput = nfs_dentry_iput, |
| 1013 | }; |
| 1014 | |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 1015 | /* |
| 1016 | * Use intent information to determine whether we need to substitute |
| 1017 | * the NFSv4-style stateful OPEN for the LOOKUP call |
| 1018 | */ |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1019 | static int is_atomic_open(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | { |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 1021 | if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return 0; |
| 1023 | /* NFS does not (yet) have a stateful open for directories */ |
| 1024 | if (nd->flags & LOOKUP_DIRECTORY) |
| 1025 | return 0; |
| 1026 | /* Are we trying to write to a read only partition? */ |
Dave Hansen | 2c463e9 | 2008-02-15 14:37:56 -0800 | [diff] [blame] | 1027 | if (__mnt_is_readonly(nd->path.mnt) && |
| 1028 | (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | return 0; |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1033 | static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd) |
| 1034 | { |
| 1035 | struct path path = { |
| 1036 | .mnt = nd->path.mnt, |
| 1037 | .dentry = dentry, |
| 1038 | }; |
| 1039 | struct nfs_open_context *ctx; |
| 1040 | struct rpc_cred *cred; |
| 1041 | fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); |
| 1042 | |
| 1043 | cred = rpc_lookup_cred(); |
| 1044 | if (IS_ERR(cred)) |
| 1045 | return ERR_CAST(cred); |
| 1046 | ctx = alloc_nfs_open_context(&path, cred, fmode); |
| 1047 | put_rpccred(cred); |
| 1048 | if (ctx == NULL) |
| 1049 | return ERR_PTR(-ENOMEM); |
| 1050 | return ctx; |
| 1051 | } |
| 1052 | |
| 1053 | static int do_open(struct inode *inode, struct file *filp) |
| 1054 | { |
| 1055 | nfs_fscache_set_inode_cookie(inode, filp); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx) |
| 1060 | { |
| 1061 | struct file *filp; |
| 1062 | int ret = 0; |
| 1063 | |
| 1064 | /* If the open_intent is for execute, we have an extra check to make */ |
| 1065 | if (ctx->mode & FMODE_EXEC) { |
| 1066 | ret = nfs_may_open(ctx->path.dentry->d_inode, |
| 1067 | ctx->cred, |
| 1068 | nd->intent.open.flags); |
| 1069 | if (ret < 0) |
| 1070 | goto out; |
| 1071 | } |
| 1072 | filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open); |
| 1073 | if (IS_ERR(filp)) |
| 1074 | ret = PTR_ERR(filp); |
| 1075 | else |
| 1076 | nfs_file_set_open_context(filp, ctx); |
| 1077 | out: |
| 1078 | put_nfs_open_context(ctx); |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 1083 | { |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1084 | struct nfs_open_context *ctx; |
| 1085 | struct iattr attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | struct dentry *res = NULL; |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1087 | struct inode *inode; |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1088 | int open_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 1090 | dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n", |
| 1091 | dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | /* Check that we are indeed trying to open this file */ |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1094 | if (!is_atomic_open(nd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | goto no_open; |
| 1096 | |
| 1097 | if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { |
| 1098 | res = ERR_PTR(-ENAMETOOLONG); |
| 1099 | goto out; |
| 1100 | } |
| 1101 | dentry->d_op = NFS_PROTO(dir)->dentry_ops; |
| 1102 | |
Trond Myklebust | d4d9cdc | 2007-10-02 18:38:53 -0400 | [diff] [blame] | 1103 | /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash |
| 1104 | * the dentry. */ |
Al Viro | 3516586 | 2008-08-05 03:00:49 -0400 | [diff] [blame] | 1105 | if (nd->flags & LOOKUP_EXCL) { |
Trond Myklebust | d4d9cdc | 2007-10-02 18:38:53 -0400 | [diff] [blame] | 1106 | d_instantiate(dentry, NULL); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1107 | goto out; |
| 1108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1110 | ctx = nameidata_to_nfs_open_context(dentry, nd); |
| 1111 | res = ERR_CAST(ctx); |
| 1112 | if (IS_ERR(ctx)) |
| 1113 | goto out; |
| 1114 | |
| 1115 | open_flags = nd->intent.open.flags; |
| 1116 | if (nd->flags & LOOKUP_CREATE) { |
| 1117 | attr.ia_mode = nd->intent.open.create_mode; |
| 1118 | attr.ia_valid = ATTR_MODE; |
| 1119 | if (!IS_POSIXACL(dir)) |
| 1120 | attr.ia_mode &= ~current_umask(); |
| 1121 | } else { |
| 1122 | open_flags &= ~O_EXCL; |
| 1123 | attr.ia_valid = 0; |
| 1124 | BUG_ON(open_flags & O_CREAT); |
| 1125 | } |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | /* Open the file on the server */ |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1128 | nfs_block_sillyrename(dentry->d_parent); |
| 1129 | inode = nfs4_atomic_open(dir, ctx, open_flags, &attr); |
| 1130 | if (IS_ERR(inode)) { |
| 1131 | nfs_unblock_sillyrename(dentry->d_parent); |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1132 | put_nfs_open_context(ctx); |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1133 | switch (PTR_ERR(inode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | /* Make a negative dentry */ |
| 1135 | case -ENOENT: |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1136 | d_add(dentry, NULL); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1137 | res = NULL; |
| 1138 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | /* This turned out not to be a regular file */ |
Trond Myklebust | 80e6063 | 2010-03-25 13:51:05 -0400 | [diff] [blame] | 1140 | case -EISDIR: |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1141 | case -ENOTDIR: |
| 1142 | goto no_open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | case -ELOOP: |
| 1144 | if (!(nd->intent.open.flags & O_NOFOLLOW)) |
| 1145 | goto no_open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | /* case -EINVAL: */ |
| 1147 | default: |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1148 | res = ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | goto out; |
| 1150 | } |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1151 | } |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1152 | res = d_add_unique(dentry, inode); |
| 1153 | if (res != NULL) { |
| 1154 | dput(ctx->path.dentry); |
| 1155 | ctx->path.dentry = dget(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | dentry = res; |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1157 | } |
Trond Myklebust | cd9a1c0 | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1158 | nfs_intent_set_file(nd, ctx); |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1159 | nfs_unblock_sillyrename(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | out: |
Trond Myklebust | f46e0bd | 2010-09-17 10:56:50 -0400 | [diff] [blame] | 1161 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return res; |
| 1163 | no_open: |
| 1164 | return nfs_lookup(dir, dentry, nd); |
| 1165 | } |
| 1166 | |
| 1167 | static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 1168 | { |
| 1169 | struct dentry *parent = NULL; |
| 1170 | struct inode *inode = dentry->d_inode; |
| 1171 | struct inode *dir; |
Trond Myklebust | b8d4cad | 2010-09-17 10:56:51 -0400 | [diff] [blame^] | 1172 | struct nfs_open_context *ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | int openflags, ret = 0; |
| 1174 | |
Trond Myklebust | 1f063d2 | 2010-04-22 15:35:55 -0400 | [diff] [blame] | 1175 | if (!is_atomic_open(nd) || d_mountpoint(dentry)) |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1176 | goto no_open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | parent = dget_parent(dentry); |
| 1178 | dir = parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | /* We can't create new files in nfs_open_revalidate(), so we |
| 1180 | * optimize away revalidation of negative dentries. |
| 1181 | */ |
Trond Myklebust | 216d5d0 | 2007-10-01 20:10:12 -0400 | [diff] [blame] | 1182 | if (inode == NULL) { |
| 1183 | if (!nfs_neg_need_reval(dir, dentry, nd)) |
| 1184 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | goto out; |
Trond Myklebust | 216d5d0 | 2007-10-01 20:10:12 -0400 | [diff] [blame] | 1186 | } |
| 1187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | /* NFS only supports OPEN on regular files */ |
| 1189 | if (!S_ISREG(inode->i_mode)) |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1190 | goto no_open_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | openflags = nd->intent.open.flags; |
| 1192 | /* We cannot do exclusive creation on a positive dentry */ |
| 1193 | if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1194 | goto no_open_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | /* We can't create new files, or truncate existing ones here */ |
Trond Myklebust | 0a377cf | 2010-08-18 09:25:42 -0400 | [diff] [blame] | 1196 | openflags &= ~(O_CREAT|O_EXCL|O_TRUNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
Trond Myklebust | b8d4cad | 2010-09-17 10:56:51 -0400 | [diff] [blame^] | 1198 | ctx = nameidata_to_nfs_open_context(dentry, nd); |
| 1199 | ret = PTR_ERR(ctx); |
| 1200 | if (IS_ERR(ctx)) |
| 1201 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1203 | * Note: we're not holding inode->i_mutex and so may be racing with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | * operations that change the directory. We therefore save the |
| 1205 | * change attribute *before* we do the RPC call. |
| 1206 | */ |
Trond Myklebust | b8d4cad | 2010-09-17 10:56:51 -0400 | [diff] [blame^] | 1207 | ret = nfs4_open_revalidate(dir, ctx, openflags); |
| 1208 | if (ret == 1) |
| 1209 | nfs_intent_set_file(nd, ctx); |
| 1210 | else |
| 1211 | put_nfs_open_context(ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | out: |
| 1213 | dput(parent); |
| 1214 | if (!ret) |
| 1215 | d_drop(dentry); |
| 1216 | return ret; |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1217 | no_open_dput: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | dput(parent); |
Trond Myklebust | 5584c30 | 2008-12-23 15:21:54 -0500 | [diff] [blame] | 1219 | no_open: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | return nfs_lookup_revalidate(dentry, nd); |
| 1221 | } |
| 1222 | #endif /* CONFIG_NFSV4 */ |
| 1223 | |
| 1224 | static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc) |
| 1225 | { |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 1226 | struct dentry *parent = desc->file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | struct inode *dir = parent->d_inode; |
| 1228 | struct nfs_entry *entry = desc->entry; |
| 1229 | struct dentry *dentry, *alias; |
| 1230 | struct qstr name = { |
| 1231 | .name = entry->name, |
| 1232 | .len = entry->len, |
| 1233 | }; |
| 1234 | struct inode *inode; |
Trond Myklebust | 57fa76f | 2007-09-30 18:01:13 -0400 | [diff] [blame] | 1235 | unsigned long verf = nfs_save_change_attribute(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
| 1237 | switch (name.len) { |
| 1238 | case 2: |
| 1239 | if (name.name[0] == '.' && name.name[1] == '.') |
| 1240 | return dget_parent(parent); |
| 1241 | break; |
| 1242 | case 1: |
| 1243 | if (name.name[0] == '.') |
| 1244 | return dget(parent); |
| 1245 | } |
Trond Myklebust | 57fa76f | 2007-09-30 18:01:13 -0400 | [diff] [blame] | 1246 | |
| 1247 | spin_lock(&dir->i_lock); |
| 1248 | if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) { |
| 1249 | spin_unlock(&dir->i_lock); |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | spin_unlock(&dir->i_lock); |
| 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | name.hash = full_name_hash(name.name, name.len); |
| 1255 | dentry = d_lookup(parent, &name); |
Trond Myklebust | df1d5d2 | 2007-01-15 13:56:29 -0500 | [diff] [blame] | 1256 | if (dentry != NULL) { |
Trond Myklebust | ef75c79 | 2007-01-16 10:09:44 -0500 | [diff] [blame] | 1257 | /* Is this a positive dentry that matches the readdir info? */ |
| 1258 | if (dentry->d_inode != NULL && |
| 1259 | (NFS_FILEID(dentry->d_inode) == entry->ino || |
| 1260 | d_mountpoint(dentry))) { |
| 1261 | if (!desc->plus || entry->fh->size == 0) |
| 1262 | return dentry; |
| 1263 | if (nfs_compare_fh(NFS_FH(dentry->d_inode), |
| 1264 | entry->fh) == 0) |
| 1265 | goto out_renew; |
| 1266 | } |
Trond Myklebust | df1d5d2 | 2007-01-15 13:56:29 -0500 | [diff] [blame] | 1267 | /* No, so d_drop to allow one to be created */ |
| 1268 | d_drop(dentry); |
| 1269 | dput(dentry); |
| 1270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR)) |
| 1272 | return NULL; |
Trond Myklebust | 54af3bb | 2007-09-28 12:27:41 -0400 | [diff] [blame] | 1273 | if (name.len > NFS_SERVER(dir)->namelen) |
| 1274 | return NULL; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1275 | /* Note: caller is already holding the dir->i_mutex! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | dentry = d_alloc(parent, &name); |
| 1277 | if (dentry == NULL) |
| 1278 | return NULL; |
| 1279 | dentry->d_op = NFS_PROTO(dir)->dentry_ops; |
| 1280 | inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); |
Trond Myklebust | 03f28e3 | 2006-03-20 13:44:48 -0500 | [diff] [blame] | 1281 | if (IS_ERR(inode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | dput(dentry); |
| 1283 | return NULL; |
| 1284 | } |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1285 | |
| 1286 | alias = d_materialise_unique(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | if (alias != NULL) { |
| 1288 | dput(dentry); |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1289 | if (IS_ERR(alias)) |
| 1290 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | dentry = alias; |
| 1292 | } |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1293 | |
Trond Myklebust | c79ba78 | 2007-01-31 08:16:24 -0500 | [diff] [blame] | 1294 | out_renew: |
Trond Myklebust | 57fa76f | 2007-09-30 18:01:13 -0400 | [diff] [blame] | 1295 | nfs_set_verifier(dentry, verf); |
Trond Myklebust | c79ba78 | 2007-01-31 08:16:24 -0500 | [diff] [blame] | 1296 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Code common to create, mkdir, and mknod. |
| 1301 | */ |
| 1302 | int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, |
| 1303 | struct nfs_fattr *fattr) |
| 1304 | { |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1305 | struct dentry *parent = dget_parent(dentry); |
| 1306 | struct inode *dir = parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | struct inode *inode; |
| 1308 | int error = -EACCES; |
| 1309 | |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1310 | d_drop(dentry); |
| 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | /* We may have been initialized further down */ |
| 1313 | if (dentry->d_inode) |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1314 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (fhandle->size == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); |
| 1317 | if (error) |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1318 | goto out_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | } |
Trond Myklebust | 5724ab3 | 2007-10-01 21:51:38 -0400 | [diff] [blame] | 1320 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | if (!(fattr->valid & NFS_ATTR_FATTR)) { |
| 1322 | struct nfs_server *server = NFS_SB(dentry->d_sb); |
David Howells | 8fa5c00 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 1323 | error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | if (error < 0) |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1325 | goto out_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | inode = nfs_fhget(dentry->d_sb, fhandle, fattr); |
Trond Myklebust | 03f28e3 | 2006-03-20 13:44:48 -0500 | [diff] [blame] | 1328 | error = PTR_ERR(inode); |
| 1329 | if (IS_ERR(inode)) |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1330 | goto out_error; |
| 1331 | d_add(dentry, inode); |
| 1332 | out: |
| 1333 | dput(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | return 0; |
Trond Myklebust | fab728e | 2007-09-29 17:41:33 -0400 | [diff] [blame] | 1335 | out_error: |
| 1336 | nfs_mark_for_revalidate(dir); |
| 1337 | dput(parent); |
| 1338 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * Following a failed create operation, we drop the dentry rather |
| 1343 | * than retain a negative dentry. This avoids a problem in the event |
| 1344 | * that the operation succeeded on the server, but an error in the |
| 1345 | * reply path made it appear to have failed. |
| 1346 | */ |
| 1347 | static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 1348 | struct nameidata *nd) |
| 1349 | { |
| 1350 | struct iattr attr; |
| 1351 | int error; |
| 1352 | int open_flags = 0; |
| 1353 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 1354 | dfprintk(VFS, "NFS: create(%s/%ld), %s\n", |
| 1355 | dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
| 1357 | attr.ia_mode = mode; |
| 1358 | attr.ia_valid = ATTR_MODE; |
| 1359 | |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 1360 | if ((nd->flags & LOOKUP_CREATE) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | open_flags = nd->intent.open.flags; |
| 1362 | |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1363 | error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | if (error != 0) |
| 1365 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | return 0; |
| 1367 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | d_drop(dentry); |
| 1369 | return error; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * See comments for nfs_proc_create regarding failed operations. |
| 1374 | */ |
| 1375 | static int |
| 1376 | nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) |
| 1377 | { |
| 1378 | struct iattr attr; |
| 1379 | int status; |
| 1380 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 1381 | dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n", |
| 1382 | dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | if (!new_valid_dev(rdev)) |
| 1385 | return -EINVAL; |
| 1386 | |
| 1387 | attr.ia_mode = mode; |
| 1388 | attr.ia_valid = ATTR_MODE; |
| 1389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | if (status != 0) |
| 1392 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | return 0; |
| 1394 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | d_drop(dentry); |
| 1396 | return status; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * See comments for nfs_proc_create regarding failed operations. |
| 1401 | */ |
| 1402 | static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1403 | { |
| 1404 | struct iattr attr; |
| 1405 | int error; |
| 1406 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 1407 | dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n", |
| 1408 | dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
| 1410 | attr.ia_valid = ATTR_MODE; |
| 1411 | attr.ia_mode = mode | S_IFDIR; |
| 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | if (error != 0) |
| 1415 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | return 0; |
| 1417 | out_err: |
| 1418 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | return error; |
| 1420 | } |
| 1421 | |
Trond Myklebust | d45b9d8 | 2008-01-28 19:43:18 -0500 | [diff] [blame] | 1422 | static void nfs_dentry_handle_enoent(struct dentry *dentry) |
| 1423 | { |
| 1424 | if (dentry->d_inode != NULL && !d_unhashed(dentry)) |
| 1425 | d_delete(dentry); |
| 1426 | } |
| 1427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | static int nfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 1429 | { |
| 1430 | int error; |
| 1431 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 1432 | dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", |
| 1433 | dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); |
| 1436 | /* Ensure the VFS deletes this inode */ |
| 1437 | if (error == 0 && dentry->d_inode != NULL) |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 1438 | clear_nlink(dentry->d_inode); |
Trond Myklebust | d45b9d8 | 2008-01-28 19:43:18 -0500 | [diff] [blame] | 1439 | else if (error == -ENOENT) |
| 1440 | nfs_dentry_handle_enoent(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | |
| 1442 | return error; |
| 1443 | } |
| 1444 | |
| 1445 | static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) |
| 1446 | { |
| 1447 | static unsigned int sillycounter; |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 1448 | const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | const int countersize = sizeof(sillycounter)*2; |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 1450 | const int slen = sizeof(".nfs")+fileidsize+countersize-1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | char silly[slen+1]; |
| 1452 | struct qstr qsilly; |
| 1453 | struct dentry *sdentry; |
| 1454 | int error = -EIO; |
| 1455 | |
| 1456 | dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n", |
| 1457 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 1458 | atomic_read(&dentry->d_count)); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1459 | nfs_inc_stats(dir, NFSIOS_SILLYRENAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | /* |
| 1462 | * We don't allow a dentry to be silly-renamed twice. |
| 1463 | */ |
| 1464 | error = -EBUSY; |
| 1465 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) |
| 1466 | goto out; |
| 1467 | |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 1468 | sprintf(silly, ".nfs%*.*Lx", |
| 1469 | fileidsize, fileidsize, |
| 1470 | (unsigned long long)NFS_FILEID(dentry->d_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Trond Myklebust | 34ea818 | 2005-11-04 15:35:02 -0500 | [diff] [blame] | 1472 | /* Return delegation in anticipation of the rename */ |
| 1473 | nfs_inode_return_delegation(dentry->d_inode); |
| 1474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | sdentry = NULL; |
| 1476 | do { |
| 1477 | char *suffix = silly + slen - countersize; |
| 1478 | |
| 1479 | dput(sdentry); |
| 1480 | sillycounter++; |
| 1481 | sprintf(suffix, "%*.*x", countersize, countersize, sillycounter); |
| 1482 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 1483 | dfprintk(VFS, "NFS: trying to rename %s to %s\n", |
| 1484 | dentry->d_name.name, silly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
| 1486 | sdentry = lookup_one_len(silly, dentry->d_parent, slen); |
| 1487 | /* |
| 1488 | * N.B. Better to return EBUSY here ... it could be |
| 1489 | * dangerous to delete the file while it's in use. |
| 1490 | */ |
| 1491 | if (IS_ERR(sdentry)) |
| 1492 | goto out; |
| 1493 | } while(sdentry->d_inode != NULL); /* need negative lookup */ |
| 1494 | |
| 1495 | qsilly.name = silly; |
| 1496 | qsilly.len = strlen(silly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | if (dentry->d_inode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, |
| 1499 | dir, &qsilly); |
Trond Myklebust | 5ba7cc4 | 2005-12-03 15:20:17 -0500 | [diff] [blame] | 1500 | nfs_mark_for_revalidate(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | } else |
| 1502 | error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, |
| 1503 | dir, &qsilly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1506 | d_move(dentry, sdentry); |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 1507 | error = nfs_async_unlink(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | /* If we return 0 we don't unlink */ |
| 1509 | } |
| 1510 | dput(sdentry); |
| 1511 | out: |
| 1512 | return error; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | * Remove a file after making sure there are no pending writes, |
| 1517 | * and after checking that the file has only one user. |
| 1518 | * |
| 1519 | * We invalidate the attribute cache and free the inode prior to the operation |
| 1520 | * to avoid possible races if the server reuses the inode. |
| 1521 | */ |
| 1522 | static int nfs_safe_remove(struct dentry *dentry) |
| 1523 | { |
| 1524 | struct inode *dir = dentry->d_parent->d_inode; |
| 1525 | struct inode *inode = dentry->d_inode; |
| 1526 | int error = -EBUSY; |
| 1527 | |
| 1528 | dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", |
| 1529 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1530 | |
| 1531 | /* If the dentry was sillyrenamed, we simply call d_delete() */ |
| 1532 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 1533 | error = 0; |
| 1534 | goto out; |
| 1535 | } |
| 1536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | if (inode != NULL) { |
Trond Myklebust | cae7a07 | 2005-10-18 14:20:19 -0700 | [diff] [blame] | 1538 | nfs_inode_return_delegation(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); |
| 1540 | /* The VFS may want to delete this inode */ |
| 1541 | if (error == 0) |
Trond Myklebust | 1b83d70 | 2008-06-11 15:44:04 -0400 | [diff] [blame] | 1542 | nfs_drop_nlink(inode); |
Trond Myklebust | 5ba7cc4 | 2005-12-03 15:20:17 -0500 | [diff] [blame] | 1543 | nfs_mark_for_revalidate(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | } else |
| 1545 | error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); |
Trond Myklebust | d45b9d8 | 2008-01-28 19:43:18 -0500 | [diff] [blame] | 1546 | if (error == -ENOENT) |
| 1547 | nfs_dentry_handle_enoent(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | out: |
| 1549 | return error; |
| 1550 | } |
| 1551 | |
| 1552 | /* We do silly rename. In case sillyrename() returns -EBUSY, the inode |
| 1553 | * belongs to an active ".nfs..." file and we return -EBUSY. |
| 1554 | * |
| 1555 | * If sillyrename() returns 0, we do nothing, otherwise we unlink. |
| 1556 | */ |
| 1557 | static int nfs_unlink(struct inode *dir, struct dentry *dentry) |
| 1558 | { |
| 1559 | int error; |
| 1560 | int need_rehash = 0; |
| 1561 | |
| 1562 | dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, |
| 1563 | dir->i_ino, dentry->d_name.name); |
| 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | spin_lock(&dcache_lock); |
| 1566 | spin_lock(&dentry->d_lock); |
| 1567 | if (atomic_read(&dentry->d_count) > 1) { |
| 1568 | spin_unlock(&dentry->d_lock); |
| 1569 | spin_unlock(&dcache_lock); |
Trond Myklebust | ccfeb50 | 2007-01-13 02:28:12 -0500 | [diff] [blame] | 1570 | /* Start asynchronous writeout of the inode */ |
| 1571 | write_inode_now(dentry->d_inode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | error = nfs_sillyrename(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | return error; |
| 1574 | } |
| 1575 | if (!d_unhashed(dentry)) { |
| 1576 | __d_drop(dentry); |
| 1577 | need_rehash = 1; |
| 1578 | } |
| 1579 | spin_unlock(&dentry->d_lock); |
| 1580 | spin_unlock(&dcache_lock); |
| 1581 | error = nfs_safe_remove(dentry); |
Trond Myklebust | d45b9d8 | 2008-01-28 19:43:18 -0500 | [diff] [blame] | 1582 | if (!error || error == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1584 | } else if (need_rehash) |
| 1585 | d_rehash(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | return error; |
| 1587 | } |
| 1588 | |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1589 | /* |
| 1590 | * To create a symbolic link, most file systems instantiate a new inode, |
| 1591 | * add a page to it containing the path, then write it out to the disk |
| 1592 | * using prepare_write/commit_write. |
| 1593 | * |
| 1594 | * Unfortunately the NFS client can't create the in-core inode first |
| 1595 | * because it needs a file handle to create an in-core inode (see |
| 1596 | * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the |
| 1597 | * symlink request has completed on the server. |
| 1598 | * |
| 1599 | * So instead we allocate a raw page, copy the symname into it, then do |
| 1600 | * the SYMLINK request with the page as the buffer. If it succeeds, we |
| 1601 | * now have a new file handle and can instantiate an in-core NFS inode |
| 1602 | * and move the raw page into its mapping. |
| 1603 | */ |
| 1604 | static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | { |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1606 | struct pagevec lru_pvec; |
| 1607 | struct page *page; |
| 1608 | char *kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | struct iattr attr; |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1610 | unsigned int pathlen = strlen(symname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | int error; |
| 1612 | |
| 1613 | dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, |
| 1614 | dir->i_ino, dentry->d_name.name, symname); |
| 1615 | |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1616 | if (pathlen > PAGE_SIZE) |
| 1617 | return -ENAMETOOLONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1619 | attr.ia_mode = S_IFLNK | S_IRWXUGO; |
| 1620 | attr.ia_valid = ATTR_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
Jeff Layton | 83d93f2 | 2007-06-07 09:58:08 -0400 | [diff] [blame] | 1622 | page = alloc_page(GFP_HIGHUSER); |
Trond Myklebust | 7656699 | 2008-06-11 15:44:22 -0400 | [diff] [blame] | 1623 | if (!page) |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1624 | return -ENOMEM; |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1625 | |
| 1626 | kaddr = kmap_atomic(page, KM_USER0); |
| 1627 | memcpy(kaddr, symname, pathlen); |
| 1628 | if (pathlen < PAGE_SIZE) |
| 1629 | memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); |
| 1630 | kunmap_atomic(kaddr, KM_USER0); |
| 1631 | |
Chuck Lever | 94a6d75 | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1632 | error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1633 | if (error != 0) { |
| 1634 | dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n", |
| 1635 | dir->i_sb->s_id, dir->i_ino, |
| 1636 | dentry->d_name.name, symname, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | d_drop(dentry); |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1638 | __free_page(page); |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1639 | return error; |
| 1640 | } |
| 1641 | |
| 1642 | /* |
| 1643 | * No big deal if we can't add this page to the page cache here. |
| 1644 | * READLINK will get the missing page from the server if needed. |
| 1645 | */ |
| 1646 | pagevec_init(&lru_pvec, 0); |
| 1647 | if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0, |
| 1648 | GFP_KERNEL)) { |
Chuck Lever | 39cf8a1 | 2006-10-19 23:28:41 -0700 | [diff] [blame] | 1649 | pagevec_add(&lru_pvec, page); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1650 | pagevec_lru_add_file(&lru_pvec); |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1651 | SetPageUptodate(page); |
| 1652 | unlock_page(page); |
| 1653 | } else |
| 1654 | __free_page(page); |
| 1655 | |
Chuck Lever | 873101b | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 1656 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | static int |
| 1660 | nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 1661 | { |
| 1662 | struct inode *inode = old_dentry->d_inode; |
| 1663 | int error; |
| 1664 | |
| 1665 | dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", |
| 1666 | old_dentry->d_parent->d_name.name, old_dentry->d_name.name, |
| 1667 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1668 | |
Trond Myklebust | 9a3936a | 2009-10-26 08:09:46 -0400 | [diff] [blame] | 1669 | nfs_inode_return_delegation(inode); |
| 1670 | |
Trond Myklebust | 9697d23 | 2007-10-02 21:58:05 -0400 | [diff] [blame] | 1671 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); |
Trond Myklebust | cf80955 | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1673 | if (error == 0) { |
| 1674 | atomic_inc(&inode->i_count); |
Trond Myklebust | 9697d23 | 2007-10-02 21:58:05 -0400 | [diff] [blame] | 1675 | d_add(dentry, inode); |
Trond Myklebust | cf80955 | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | return error; |
| 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | * RENAME |
| 1682 | * FIXME: Some nfsds, like the Linux user space nfsd, may generate a |
| 1683 | * different file handle for the same inode after a rename (e.g. when |
| 1684 | * moving to a different directory). A fail-safe method to do so would |
| 1685 | * be to look up old_dir/old_name, create a link to new_dir/new_name and |
| 1686 | * rename the old file using the sillyrename stuff. This way, the original |
| 1687 | * file in old_dir will go away when the last process iput()s the inode. |
| 1688 | * |
| 1689 | * FIXED. |
| 1690 | * |
| 1691 | * It actually works quite well. One needs to have the possibility for |
| 1692 | * at least one ".nfs..." file in each directory the file ever gets |
| 1693 | * moved or linked to which happens automagically with the new |
| 1694 | * implementation that only depends on the dcache stuff instead of |
| 1695 | * using the inode layer |
| 1696 | * |
| 1697 | * Unfortunately, things are a little more complicated than indicated |
| 1698 | * above. For a cross-directory move, we want to make sure we can get |
| 1699 | * rid of the old inode after the operation. This means there must be |
| 1700 | * no pending writes (if it's a file), and the use count must be 1. |
| 1701 | * If these conditions are met, we can drop the dentries before doing |
| 1702 | * the rename. |
| 1703 | */ |
| 1704 | static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1705 | struct inode *new_dir, struct dentry *new_dentry) |
| 1706 | { |
| 1707 | struct inode *old_inode = old_dentry->d_inode; |
| 1708 | struct inode *new_inode = new_dentry->d_inode; |
| 1709 | struct dentry *dentry = NULL, *rehash = NULL; |
| 1710 | int error = -EBUSY; |
| 1711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", |
| 1713 | old_dentry->d_parent->d_name.name, old_dentry->d_name.name, |
| 1714 | new_dentry->d_parent->d_name.name, new_dentry->d_name.name, |
| 1715 | atomic_read(&new_dentry->d_count)); |
| 1716 | |
| 1717 | /* |
Miklos Szeredi | 28f79a1 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1718 | * For non-directories, check whether the target is busy and if so, |
| 1719 | * make a copy of the dentry and then do a silly-rename. If the |
| 1720 | * silly-rename succeeds, the copied dentry is hashed and becomes |
| 1721 | * the new target. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | */ |
Miklos Szeredi | 2722610 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1723 | if (new_inode && !S_ISDIR(new_inode->i_mode)) { |
| 1724 | /* |
| 1725 | * To prevent any new references to the target during the |
| 1726 | * rename, we unhash the dentry in advance. |
| 1727 | */ |
| 1728 | if (!d_unhashed(new_dentry)) { |
| 1729 | d_drop(new_dentry); |
| 1730 | rehash = new_dentry; |
| 1731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | |
Miklos Szeredi | 2722610 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1733 | if (atomic_read(&new_dentry->d_count) > 2) { |
| 1734 | int err; |
| 1735 | |
| 1736 | /* copy the target dentry's name */ |
| 1737 | dentry = d_alloc(new_dentry->d_parent, |
| 1738 | &new_dentry->d_name); |
| 1739 | if (!dentry) |
| 1740 | goto out; |
| 1741 | |
| 1742 | /* silly-rename the existing target ... */ |
| 1743 | err = nfs_sillyrename(new_dir, new_dentry); |
Miklos Szeredi | 24e9302 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1744 | if (err) |
Miklos Szeredi | 2722610 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1745 | goto out; |
Miklos Szeredi | 24e9302 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1746 | |
| 1747 | new_dentry = dentry; |
OGAWA Hirofumi | 5633593 | 2010-01-06 18:48:26 -0500 | [diff] [blame] | 1748 | rehash = NULL; |
Miklos Szeredi | 24e9302 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1749 | new_inode = NULL; |
Miklos Szeredi | 2722610 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1750 | } |
Trond Myklebust | b1e4adf | 2009-03-19 15:35:49 -0400 | [diff] [blame] | 1751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Trond Myklebust | cae7a07 | 2005-10-18 14:20:19 -0700 | [diff] [blame] | 1753 | nfs_inode_return_delegation(old_inode); |
Trond Myklebust | b1e4adf | 2009-03-19 15:35:49 -0400 | [diff] [blame] | 1754 | if (new_inode != NULL) |
Trond Myklebust | 2417411 | 2006-01-03 09:55:33 +0100 | [diff] [blame] | 1755 | nfs_inode_return_delegation(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, |
| 1758 | new_dir, &new_dentry->d_name); |
Trond Myklebust | 5ba7cc4 | 2005-12-03 15:20:17 -0500 | [diff] [blame] | 1759 | nfs_mark_for_revalidate(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | out: |
| 1761 | if (rehash) |
| 1762 | d_rehash(rehash); |
| 1763 | if (!error) { |
Trond Myklebust | b1e4adf | 2009-03-19 15:35:49 -0400 | [diff] [blame] | 1764 | if (new_inode != NULL) |
| 1765 | nfs_drop_nlink(new_inode); |
Mark Fasheh | 349457c | 2006-09-08 14:22:21 -0700 | [diff] [blame] | 1766 | d_move(old_dentry, new_dentry); |
Chuck Lever | 8fb559f | 2007-09-24 15:40:16 -0400 | [diff] [blame] | 1767 | nfs_set_verifier(new_dentry, |
| 1768 | nfs_save_change_attribute(new_dir)); |
Trond Myklebust | d45b9d8 | 2008-01-28 19:43:18 -0500 | [diff] [blame] | 1769 | } else if (error == -ENOENT) |
| 1770 | nfs_dentry_handle_enoent(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | /* new dentry created? */ |
| 1773 | if (dentry) |
| 1774 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | return error; |
| 1776 | } |
| 1777 | |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1778 | static DEFINE_SPINLOCK(nfs_access_lru_lock); |
| 1779 | static LIST_HEAD(nfs_access_lru_list); |
| 1780 | static atomic_long_t nfs_access_nr_entries; |
| 1781 | |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1782 | static void nfs_access_free_entry(struct nfs_access_entry *entry) |
| 1783 | { |
| 1784 | put_rpccred(entry->cred); |
| 1785 | kfree(entry); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1786 | smp_mb__before_atomic_dec(); |
| 1787 | atomic_long_dec(&nfs_access_nr_entries); |
| 1788 | smp_mb__after_atomic_dec(); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1789 | } |
| 1790 | |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1791 | static void nfs_access_free_list(struct list_head *head) |
| 1792 | { |
| 1793 | struct nfs_access_entry *cache; |
| 1794 | |
| 1795 | while (!list_empty(head)) { |
| 1796 | cache = list_entry(head->next, struct nfs_access_entry, lru); |
| 1797 | list_del(&cache->lru); |
| 1798 | nfs_access_free_entry(cache); |
| 1799 | } |
| 1800 | } |
| 1801 | |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 1802 | int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1803 | { |
| 1804 | LIST_HEAD(head); |
| 1805 | struct nfs_inode *nfsi; |
| 1806 | struct nfs_access_entry *cache; |
| 1807 | |
Trond Myklebust | 61d5eb2 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1808 | if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) |
| 1809 | return (nr_to_scan == 0) ? 0 : -1; |
Trond Myklebust | 9c7e7e2 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1810 | |
Trond Myklebust | a50f795 | 2007-06-05 19:23:43 -0400 | [diff] [blame] | 1811 | spin_lock(&nfs_access_lru_lock); |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1812 | list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) { |
| 1813 | struct inode *inode; |
| 1814 | |
| 1815 | if (nr_to_scan-- == 0) |
| 1816 | break; |
Trond Myklebust | 9c7e7e2 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1817 | inode = &nfsi->vfs_inode; |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1818 | spin_lock(&inode->i_lock); |
| 1819 | if (list_empty(&nfsi->access_cache_entry_lru)) |
| 1820 | goto remove_lru_entry; |
| 1821 | cache = list_entry(nfsi->access_cache_entry_lru.next, |
| 1822 | struct nfs_access_entry, lru); |
| 1823 | list_move(&cache->lru, &head); |
| 1824 | rb_erase(&cache->rb_node, &nfsi->access_cache); |
| 1825 | if (!list_empty(&nfsi->access_cache_entry_lru)) |
| 1826 | list_move_tail(&nfsi->access_cache_inode_lru, |
| 1827 | &nfs_access_lru_list); |
| 1828 | else { |
| 1829 | remove_lru_entry: |
| 1830 | list_del_init(&nfsi->access_cache_inode_lru); |
Trond Myklebust | 9c7e7e2 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1831 | smp_mb__before_clear_bit(); |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1832 | clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); |
Trond Myklebust | 9c7e7e2 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1833 | smp_mb__after_clear_bit(); |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1834 | } |
Trond Myklebust | 59844a9 | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 1835 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1836 | } |
| 1837 | spin_unlock(&nfs_access_lru_lock); |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1838 | nfs_access_free_list(&head); |
Trond Myklebust | 979df72 | 2006-07-25 11:28:19 -0400 | [diff] [blame] | 1839 | return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure; |
| 1840 | } |
| 1841 | |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1842 | static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1843 | { |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1844 | struct rb_root *root_node = &nfsi->access_cache; |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1845 | struct rb_node *n; |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1846 | struct nfs_access_entry *entry; |
| 1847 | |
| 1848 | /* Unhook entries from the cache */ |
| 1849 | while ((n = rb_first(root_node)) != NULL) { |
| 1850 | entry = rb_entry(n, struct nfs_access_entry, rb_node); |
| 1851 | rb_erase(n, root_node); |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1852 | list_move(&entry->lru, head); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1853 | } |
| 1854 | nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | void nfs_access_zap_cache(struct inode *inode) |
| 1858 | { |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1859 | LIST_HEAD(head); |
| 1860 | |
| 1861 | if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) |
| 1862 | return; |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1863 | /* Remove from global LRU init */ |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1864 | spin_lock(&nfs_access_lru_lock); |
| 1865 | if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1866 | list_del_init(&NFS_I(inode)->access_cache_inode_lru); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1867 | |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1868 | spin_lock(&inode->i_lock); |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1869 | __nfs_access_zap_cache(NFS_I(inode), &head); |
| 1870 | spin_unlock(&inode->i_lock); |
| 1871 | spin_unlock(&nfs_access_lru_lock); |
| 1872 | nfs_access_free_list(&head); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred) |
| 1876 | { |
| 1877 | struct rb_node *n = NFS_I(inode)->access_cache.rb_node; |
| 1878 | struct nfs_access_entry *entry; |
| 1879 | |
| 1880 | while (n != NULL) { |
| 1881 | entry = rb_entry(n, struct nfs_access_entry, rb_node); |
| 1882 | |
| 1883 | if (cred < entry->cred) |
| 1884 | n = n->rb_left; |
| 1885 | else if (cred > entry->cred) |
| 1886 | n = n->rb_right; |
| 1887 | else |
| 1888 | return entry; |
| 1889 | } |
| 1890 | return NULL; |
| 1891 | } |
| 1892 | |
Trond Myklebust | af22f94 | 2007-08-10 17:45:10 -0400 | [diff] [blame] | 1893 | static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | { |
Chuck Lever | 5529680 | 2005-08-18 11:24:09 -0700 | [diff] [blame] | 1895 | struct nfs_inode *nfsi = NFS_I(inode); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1896 | struct nfs_access_entry *cache; |
| 1897 | int err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1899 | spin_lock(&inode->i_lock); |
| 1900 | if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) |
| 1901 | goto out_zap; |
| 1902 | cache = nfs_access_search_rbtree(inode, cred); |
| 1903 | if (cache == NULL) |
| 1904 | goto out; |
Trond Myklebust | b4d2314 | 2010-03-10 15:21:44 -0500 | [diff] [blame] | 1905 | if (!nfs_have_delegated_attributes(inode) && |
Peter Staubach | 64672d5 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1906 | !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo)) |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1907 | goto out_stale; |
| 1908 | res->jiffies = cache->jiffies; |
| 1909 | res->cred = cache->cred; |
| 1910 | res->mask = cache->mask; |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1911 | list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1912 | err = 0; |
| 1913 | out: |
| 1914 | spin_unlock(&inode->i_lock); |
| 1915 | return err; |
| 1916 | out_stale: |
| 1917 | rb_erase(&cache->rb_node, &nfsi->access_cache); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1918 | list_del(&cache->lru); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1919 | spin_unlock(&inode->i_lock); |
| 1920 | nfs_access_free_entry(cache); |
| 1921 | return -ENOENT; |
| 1922 | out_zap: |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1923 | spin_unlock(&inode->i_lock); |
| 1924 | nfs_access_zap_cache(inode); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1925 | return -ENOENT; |
| 1926 | } |
| 1927 | |
| 1928 | static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) |
| 1929 | { |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1930 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1931 | struct rb_root *root_node = &nfsi->access_cache; |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1932 | struct rb_node **p = &root_node->rb_node; |
| 1933 | struct rb_node *parent = NULL; |
| 1934 | struct nfs_access_entry *entry; |
| 1935 | |
| 1936 | spin_lock(&inode->i_lock); |
| 1937 | while (*p != NULL) { |
| 1938 | parent = *p; |
| 1939 | entry = rb_entry(parent, struct nfs_access_entry, rb_node); |
| 1940 | |
| 1941 | if (set->cred < entry->cred) |
| 1942 | p = &parent->rb_left; |
| 1943 | else if (set->cred > entry->cred) |
| 1944 | p = &parent->rb_right; |
| 1945 | else |
| 1946 | goto found; |
| 1947 | } |
| 1948 | rb_link_node(&set->rb_node, parent, p); |
| 1949 | rb_insert_color(&set->rb_node, root_node); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1950 | list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1951 | spin_unlock(&inode->i_lock); |
| 1952 | return; |
| 1953 | found: |
| 1954 | rb_replace_node(parent, &set->rb_node, root_node); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1955 | list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); |
| 1956 | list_del(&entry->lru); |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1957 | spin_unlock(&inode->i_lock); |
| 1958 | nfs_access_free_entry(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Trond Myklebust | af22f94 | 2007-08-10 17:45:10 -0400 | [diff] [blame] | 1961 | static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | { |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1963 | struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); |
| 1964 | if (cache == NULL) |
| 1965 | return; |
| 1966 | RB_CLEAR_NODE(&cache->rb_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | cache->jiffies = set->jiffies; |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1968 | cache->cred = get_rpccred(set->cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | cache->mask = set->mask; |
Trond Myklebust | 1c3c07e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1970 | |
| 1971 | nfs_access_add_rbtree(inode, cache); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1972 | |
| 1973 | /* Update accounting */ |
| 1974 | smp_mb__before_atomic_inc(); |
| 1975 | atomic_long_inc(&nfs_access_nr_entries); |
| 1976 | smp_mb__after_atomic_inc(); |
| 1977 | |
| 1978 | /* Add inode to global LRU list */ |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1979 | if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1980 | spin_lock(&nfs_access_lru_lock); |
Trond Myklebust | 1a81bb8 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 1981 | if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) |
| 1982 | list_add_tail(&NFS_I(inode)->access_cache_inode_lru, |
| 1983 | &nfs_access_lru_list); |
Trond Myklebust | cfcea3e | 2006-07-25 11:28:18 -0400 | [diff] [blame] | 1984 | spin_unlock(&nfs_access_lru_lock); |
| 1985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) |
| 1989 | { |
| 1990 | struct nfs_access_entry cache; |
| 1991 | int status; |
| 1992 | |
| 1993 | status = nfs_access_get_cached(inode, cred, &cache); |
| 1994 | if (status == 0) |
| 1995 | goto out; |
| 1996 | |
| 1997 | /* Be clever: ask server to check for all possible rights */ |
| 1998 | cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; |
| 1999 | cache.cred = cred; |
| 2000 | cache.jiffies = jiffies; |
| 2001 | status = NFS_PROTO(inode)->access(inode, &cache); |
Suresh Jayaraman | a71ee33 | 2009-03-10 20:33:21 -0400 | [diff] [blame] | 2002 | if (status != 0) { |
| 2003 | if (status == -ESTALE) { |
| 2004 | nfs_zap_caches(inode); |
| 2005 | if (!S_ISDIR(inode->i_mode)) |
| 2006 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
| 2007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | return status; |
Suresh Jayaraman | a71ee33 | 2009-03-10 20:33:21 -0400 | [diff] [blame] | 2009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | nfs_access_add_cache(inode, &cache); |
| 2011 | out: |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 2012 | if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | return 0; |
| 2014 | return -EACCES; |
| 2015 | } |
| 2016 | |
Trond Myklebust | af22f94 | 2007-08-10 17:45:10 -0400 | [diff] [blame] | 2017 | static int nfs_open_permission_mask(int openflags) |
| 2018 | { |
| 2019 | int mask = 0; |
| 2020 | |
| 2021 | if (openflags & FMODE_READ) |
| 2022 | mask |= MAY_READ; |
| 2023 | if (openflags & FMODE_WRITE) |
| 2024 | mask |= MAY_WRITE; |
| 2025 | if (openflags & FMODE_EXEC) |
| 2026 | mask |= MAY_EXEC; |
| 2027 | return mask; |
| 2028 | } |
| 2029 | |
| 2030 | int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags) |
| 2031 | { |
| 2032 | return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); |
| 2033 | } |
| 2034 | |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 2035 | int nfs_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | { |
| 2037 | struct rpc_cred *cred; |
| 2038 | int res = 0; |
| 2039 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 2040 | nfs_inc_stats(inode, NFSIOS_VFSACCESS); |
| 2041 | |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 2042 | if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | goto out; |
| 2044 | /* Is this sys_access() ? */ |
Eric Paris | 9cfcac8 | 2010-07-23 11:43:51 -0400 | [diff] [blame] | 2045 | if (mask & (MAY_ACCESS | MAY_CHDIR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | goto force_lookup; |
| 2047 | |
| 2048 | switch (inode->i_mode & S_IFMT) { |
| 2049 | case S_IFLNK: |
| 2050 | goto out; |
| 2051 | case S_IFREG: |
| 2052 | /* NFSv4 has atomic_open... */ |
| 2053 | if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) |
Frank Filz | 7ee2cb7 | 2009-05-18 17:41:40 -0400 | [diff] [blame] | 2054 | && (mask & MAY_OPEN) |
| 2055 | && !(mask & MAY_EXEC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | goto out; |
| 2057 | break; |
| 2058 | case S_IFDIR: |
| 2059 | /* |
| 2060 | * Optimize away all write operations, since the server |
| 2061 | * will check permissions when we perform the op. |
| 2062 | */ |
| 2063 | if ((mask & MAY_WRITE) && !(mask & MAY_READ)) |
| 2064 | goto out; |
| 2065 | } |
| 2066 | |
| 2067 | force_lookup: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | if (!NFS_PROTO(inode)->access) |
| 2069 | goto out_notsup; |
| 2070 | |
Trond Myklebust | 98a8e32 | 2008-03-12 12:25:28 -0400 | [diff] [blame] | 2071 | cred = rpc_lookup_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | if (!IS_ERR(cred)) { |
| 2073 | res = nfs_do_access(inode, cred, mask); |
| 2074 | put_rpccred(cred); |
| 2075 | } else |
| 2076 | res = PTR_ERR(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | out: |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 2078 | if (!res && (mask & MAY_EXEC) && !execute_ok(inode)) |
| 2079 | res = -EACCES; |
| 2080 | |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 2081 | dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", |
| 2082 | inode->i_sb->s_id, inode->i_ino, mask, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | return res; |
| 2084 | out_notsup: |
| 2085 | res = nfs_revalidate_inode(NFS_SERVER(inode), inode); |
| 2086 | if (res == 0) |
| 2087 | res = generic_permission(inode, mask, NULL); |
Chuck Lever | 1e7cb3d | 2006-03-20 13:44:24 -0500 | [diff] [blame] | 2088 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | /* |
| 2092 | * Local variables: |
| 2093 | * version-control: t |
| 2094 | * kept-new-versions: 5 |
| 2095 | * End: |
| 2096 | */ |