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