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> |
| 32 | #include <linux/smp_lock.h> |
| 33 | #include <linux/namei.h> |
| 34 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 35 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "delegation.h" |
| 37 | |
| 38 | #define NFS_PARANOIA 1 |
| 39 | /* #define NFS_DEBUG_VERBOSE 1 */ |
| 40 | |
| 41 | static int nfs_opendir(struct inode *, struct file *); |
| 42 | static int nfs_readdir(struct file *, void *, filldir_t); |
| 43 | static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *); |
| 44 | static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *); |
| 45 | static int nfs_mkdir(struct inode *, struct dentry *, int); |
| 46 | static int nfs_rmdir(struct inode *, struct dentry *); |
| 47 | static int nfs_unlink(struct inode *, struct dentry *); |
| 48 | static int nfs_symlink(struct inode *, struct dentry *, const char *); |
| 49 | static int nfs_link(struct dentry *, struct inode *, struct dentry *); |
| 50 | static int nfs_mknod(struct inode *, struct dentry *, int, dev_t); |
| 51 | static int nfs_rename(struct inode *, struct dentry *, |
| 52 | struct inode *, struct dentry *); |
| 53 | static int nfs_fsync_dir(struct file *, struct dentry *, int); |
| 54 | |
| 55 | struct file_operations nfs_dir_operations = { |
| 56 | .read = generic_read_dir, |
| 57 | .readdir = nfs_readdir, |
| 58 | .open = nfs_opendir, |
| 59 | .release = nfs_release, |
| 60 | .fsync = nfs_fsync_dir, |
| 61 | }; |
| 62 | |
| 63 | struct inode_operations nfs_dir_inode_operations = { |
| 64 | .create = nfs_create, |
| 65 | .lookup = nfs_lookup, |
| 66 | .link = nfs_link, |
| 67 | .unlink = nfs_unlink, |
| 68 | .symlink = nfs_symlink, |
| 69 | .mkdir = nfs_mkdir, |
| 70 | .rmdir = nfs_rmdir, |
| 71 | .mknod = nfs_mknod, |
| 72 | .rename = nfs_rename, |
| 73 | .permission = nfs_permission, |
| 74 | .getattr = nfs_getattr, |
| 75 | .setattr = nfs_setattr, |
| 76 | }; |
| 77 | |
Andreas Gruenbacher | b7fa055 | 2005-06-22 17:16:27 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_NFS_V3 |
| 79 | struct inode_operations nfs3_dir_inode_operations = { |
| 80 | .create = nfs_create, |
| 81 | .lookup = nfs_lookup, |
| 82 | .link = nfs_link, |
| 83 | .unlink = nfs_unlink, |
| 84 | .symlink = nfs_symlink, |
| 85 | .mkdir = nfs_mkdir, |
| 86 | .rmdir = nfs_rmdir, |
| 87 | .mknod = nfs_mknod, |
| 88 | .rename = nfs_rename, |
| 89 | .permission = nfs_permission, |
| 90 | .getattr = nfs_getattr, |
| 91 | .setattr = nfs_setattr, |
| 92 | .listxattr = nfs3_listxattr, |
| 93 | .getxattr = nfs3_getxattr, |
| 94 | .setxattr = nfs3_setxattr, |
| 95 | .removexattr = nfs3_removexattr, |
| 96 | }; |
| 97 | #endif /* CONFIG_NFS_V3 */ |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #ifdef CONFIG_NFS_V4 |
| 100 | |
| 101 | static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *); |
| 102 | struct inode_operations nfs4_dir_inode_operations = { |
| 103 | .create = nfs_create, |
| 104 | .lookup = nfs_atomic_lookup, |
| 105 | .link = nfs_link, |
| 106 | .unlink = nfs_unlink, |
| 107 | .symlink = nfs_symlink, |
| 108 | .mkdir = nfs_mkdir, |
| 109 | .rmdir = nfs_rmdir, |
| 110 | .mknod = nfs_mknod, |
| 111 | .rename = nfs_rename, |
| 112 | .permission = nfs_permission, |
| 113 | .getattr = nfs_getattr, |
| 114 | .setattr = nfs_setattr, |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 115 | .getxattr = nfs4_getxattr, |
| 116 | .setxattr = nfs4_setxattr, |
| 117 | .listxattr = nfs4_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | #endif /* CONFIG_NFS_V4 */ |
| 121 | |
| 122 | /* |
| 123 | * Open file |
| 124 | */ |
| 125 | static int |
| 126 | nfs_opendir(struct inode *inode, struct file *filp) |
| 127 | { |
| 128 | int res = 0; |
| 129 | |
| 130 | lock_kernel(); |
| 131 | /* Call generic open code in order to cache credentials */ |
| 132 | if (!res) |
| 133 | res = nfs_open(inode, filp); |
| 134 | unlock_kernel(); |
| 135 | return res; |
| 136 | } |
| 137 | |
| 138 | typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int); |
| 139 | typedef struct { |
| 140 | struct file *file; |
| 141 | struct page *page; |
| 142 | unsigned long page_index; |
| 143 | u32 *ptr; |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 144 | u64 target_cookie; |
| 145 | int target_index; |
| 146 | int current_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | struct nfs_entry *entry; |
| 148 | decode_dirent_t decode; |
| 149 | int plus; |
| 150 | int error; |
| 151 | } nfs_readdir_descriptor_t; |
| 152 | |
| 153 | /* Now we cache directories properly, by stuffing the dirent |
| 154 | * data directly in the page cache. |
| 155 | * |
| 156 | * Inode invalidation due to refresh etc. takes care of |
| 157 | * _everything_, no sloppy entry flushing logic, no extraneous |
| 158 | * copying, network direct to page cache, the way it was meant |
| 159 | * to be. |
| 160 | * |
| 161 | * NOTE: Dirent information verification is done always by the |
| 162 | * page-in of the RPC reply, nowhere else, this simplies |
| 163 | * things substantially. |
| 164 | */ |
| 165 | static |
| 166 | int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page) |
| 167 | { |
| 168 | struct file *file = desc->file; |
| 169 | struct inode *inode = file->f_dentry->d_inode; |
| 170 | struct rpc_cred *cred = nfs_file_cred(file); |
| 171 | unsigned long timestamp; |
| 172 | int error; |
| 173 | |
| 174 | dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index); |
| 175 | |
| 176 | again: |
| 177 | timestamp = jiffies; |
| 178 | error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page, |
| 179 | NFS_SERVER(inode)->dtsize, desc->plus); |
| 180 | if (error < 0) { |
| 181 | /* We requested READDIRPLUS, but the server doesn't grok it */ |
| 182 | if (error == -ENOTSUPP && desc->plus) { |
| 183 | NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; |
| 184 | NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS; |
| 185 | desc->plus = 0; |
| 186 | goto again; |
| 187 | } |
| 188 | goto error; |
| 189 | } |
| 190 | SetPageUptodate(page); |
| 191 | NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME; |
| 192 | /* Ensure consistent page alignment of the data. |
| 193 | * Note: assumes we have exclusive access to this mapping either |
Trond Myklebust | a656db9 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 194 | * through inode->i_sem or some other mechanism. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | */ |
Trond Myklebust | a656db9 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 196 | if (page->index == 0) |
| 197 | invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | unlock_page(page); |
| 199 | return 0; |
| 200 | error: |
| 201 | SetPageError(page); |
| 202 | unlock_page(page); |
| 203 | nfs_zap_caches(inode); |
| 204 | desc->error = error; |
| 205 | return -EIO; |
| 206 | } |
| 207 | |
| 208 | static inline |
| 209 | int dir_decode(nfs_readdir_descriptor_t *desc) |
| 210 | { |
| 211 | u32 *p = desc->ptr; |
| 212 | p = desc->decode(p, desc->entry, desc->plus); |
| 213 | if (IS_ERR(p)) |
| 214 | return PTR_ERR(p); |
| 215 | desc->ptr = p; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static inline |
| 220 | void dir_page_release(nfs_readdir_descriptor_t *desc) |
| 221 | { |
| 222 | kunmap(desc->page); |
| 223 | page_cache_release(desc->page); |
| 224 | desc->page = NULL; |
| 225 | desc->ptr = NULL; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Given a pointer to a buffer that has already been filled by a call |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 230 | * to readdir, find the next entry with cookie 'desc->target_cookie'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | * |
| 232 | * If the end of the buffer has been reached, return -EAGAIN, if not, |
| 233 | * return the offset within the buffer of the next entry to be |
| 234 | * read. |
| 235 | */ |
| 236 | static inline |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 237 | int find_dirent(nfs_readdir_descriptor_t *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | struct nfs_entry *entry = desc->entry; |
| 240 | int loop_count = 0, |
| 241 | status; |
| 242 | |
| 243 | while((status = dir_decode(desc)) == 0) { |
| 244 | dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 245 | if (entry->prev_cookie == desc->target_cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | break; |
| 247 | if (loop_count++ > 200) { |
| 248 | loop_count = 0; |
| 249 | schedule(); |
| 250 | } |
| 251 | } |
| 252 | dfprintk(VFS, "NFS: find_dirent() returns %d\n", status); |
| 253 | return status; |
| 254 | } |
| 255 | |
| 256 | /* |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 257 | * Given a pointer to a buffer that has already been filled by a call |
| 258 | * to readdir, find the entry at offset 'desc->target_index'. |
| 259 | * |
| 260 | * If the end of the buffer has been reached, return -EAGAIN, if not, |
| 261 | * return the offset within the buffer of the next entry to be |
| 262 | * read. |
| 263 | */ |
| 264 | static inline |
| 265 | int find_dirent_index(nfs_readdir_descriptor_t *desc) |
| 266 | { |
| 267 | struct nfs_entry *entry = desc->entry; |
| 268 | int loop_count = 0, |
| 269 | status; |
| 270 | |
| 271 | for(;;) { |
| 272 | status = dir_decode(desc); |
| 273 | if (status) |
| 274 | break; |
| 275 | |
| 276 | dfprintk(VFS, "NFS: found cookie %Lu at index %d\n", (long long)entry->cookie, desc->current_index); |
| 277 | |
| 278 | if (desc->target_index == desc->current_index) { |
| 279 | desc->target_cookie = entry->cookie; |
| 280 | break; |
| 281 | } |
| 282 | desc->current_index++; |
| 283 | if (loop_count++ > 200) { |
| 284 | loop_count = 0; |
| 285 | schedule(); |
| 286 | } |
| 287 | } |
| 288 | dfprintk(VFS, "NFS: find_dirent_index() returns %d\n", status); |
| 289 | return status; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Find the given page, and call find_dirent() or find_dirent_index in |
| 294 | * order to try to return the next entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | */ |
| 296 | static inline |
| 297 | int find_dirent_page(nfs_readdir_descriptor_t *desc) |
| 298 | { |
| 299 | struct inode *inode = desc->file->f_dentry->d_inode; |
| 300 | struct page *page; |
| 301 | int status; |
| 302 | |
| 303 | dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index); |
| 304 | |
| 305 | page = read_cache_page(inode->i_mapping, desc->page_index, |
| 306 | (filler_t *)nfs_readdir_filler, desc); |
| 307 | if (IS_ERR(page)) { |
| 308 | status = PTR_ERR(page); |
| 309 | goto out; |
| 310 | } |
| 311 | if (!PageUptodate(page)) |
| 312 | goto read_error; |
| 313 | |
| 314 | /* NOTE: Someone else may have changed the READDIRPLUS flag */ |
| 315 | desc->page = page; |
| 316 | desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 317 | if (desc->target_cookie) |
| 318 | status = find_dirent(desc); |
| 319 | else |
| 320 | status = find_dirent_index(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | if (status < 0) |
| 322 | dir_page_release(desc); |
| 323 | out: |
| 324 | dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status); |
| 325 | return status; |
| 326 | read_error: |
| 327 | page_cache_release(page); |
| 328 | return -EIO; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Recurse through the page cache pages, and return a |
| 333 | * filled nfs_entry structure of the next directory entry if possible. |
| 334 | * |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 335 | * The target for the search is 'desc->target_cookie' if non-0, |
| 336 | * 'desc->target_index' otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | */ |
| 338 | static inline |
| 339 | int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) |
| 340 | { |
| 341 | int loop_count = 0; |
| 342 | int res; |
| 343 | |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 344 | if (desc->target_cookie) |
| 345 | dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target_cookie); |
| 346 | else |
| 347 | dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie number %d\n", desc->target_index); |
| 348 | |
| 349 | /* Always search-by-index from the beginning of the cache */ |
| 350 | if (!(desc->target_cookie)) { |
| 351 | desc->page_index = 0; |
| 352 | desc->entry->cookie = desc->entry->prev_cookie = 0; |
| 353 | desc->entry->eof = 0; |
| 354 | desc->current_index = 0; |
| 355 | } |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | for (;;) { |
| 358 | res = find_dirent_page(desc); |
| 359 | if (res != -EAGAIN) |
| 360 | break; |
| 361 | /* Align to beginning of next page */ |
| 362 | desc->page_index ++; |
| 363 | if (loop_count++ > 200) { |
| 364 | loop_count = 0; |
| 365 | schedule(); |
| 366 | } |
| 367 | } |
| 368 | dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res); |
| 369 | return res; |
| 370 | } |
| 371 | |
| 372 | static inline unsigned int dt_type(struct inode *inode) |
| 373 | { |
| 374 | return (inode->i_mode >> 12) & 15; |
| 375 | } |
| 376 | |
| 377 | static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc); |
| 378 | |
| 379 | /* |
| 380 | * Once we've found the start of the dirent within a page: fill 'er up... |
| 381 | */ |
| 382 | static |
| 383 | int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, |
| 384 | filldir_t filldir) |
| 385 | { |
| 386 | struct file *file = desc->file; |
| 387 | struct nfs_entry *entry = desc->entry; |
| 388 | struct dentry *dentry = NULL; |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 389 | struct nfs_open_context *ctx = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | unsigned long fileid; |
| 391 | int loop_count = 0, |
| 392 | res; |
| 393 | |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 394 | dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)entry->cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | for(;;) { |
| 397 | unsigned d_type = DT_UNKNOWN; |
| 398 | /* Note: entry->prev_cookie contains the cookie for |
| 399 | * retrieving the current dirent on the server */ |
| 400 | fileid = nfs_fileid_to_ino_t(entry->ino); |
| 401 | |
| 402 | /* Get a dentry if we have one */ |
| 403 | if (dentry != NULL) |
| 404 | dput(dentry); |
| 405 | dentry = nfs_readdir_lookup(desc); |
| 406 | |
| 407 | /* Use readdirplus info */ |
| 408 | if (dentry != NULL && dentry->d_inode != NULL) { |
| 409 | d_type = dt_type(dentry->d_inode); |
| 410 | fileid = dentry->d_inode->i_ino; |
| 411 | } |
| 412 | |
| 413 | res = filldir(dirent, entry->name, entry->len, |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 414 | file->f_pos, fileid, d_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (res < 0) |
| 416 | break; |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 417 | file->f_pos++; |
| 418 | desc->target_cookie = entry->cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (dir_decode(desc) != 0) { |
| 420 | desc->page_index ++; |
| 421 | break; |
| 422 | } |
| 423 | if (loop_count++ > 200) { |
| 424 | loop_count = 0; |
| 425 | schedule(); |
| 426 | } |
| 427 | } |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 428 | ctx->dir_pos = file->f_pos; |
| 429 | ctx->dir_cookie = desc->target_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | dir_page_release(desc); |
| 431 | if (dentry != NULL) |
| 432 | dput(dentry); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 433 | dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target_cookie, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return res; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * If we cannot find a cookie in our cache, we suspect that this is |
| 439 | * because it points to a deleted file, so we ask the server to return |
| 440 | * whatever it thinks is the next entry. We then feed this to filldir. |
| 441 | * If all goes well, we should then be able to find our way round the |
| 442 | * cache on the next call to readdir_search_pagecache(); |
| 443 | * |
| 444 | * NOTE: we cannot add the anonymous page to the pagecache because |
| 445 | * the data it contains might not be page aligned. Besides, |
| 446 | * we should already have a complete representation of the |
| 447 | * directory in the page cache by the time we get here. |
| 448 | */ |
| 449 | static inline |
| 450 | int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, |
| 451 | filldir_t filldir) |
| 452 | { |
| 453 | struct file *file = desc->file; |
| 454 | struct inode *inode = file->f_dentry->d_inode; |
| 455 | struct rpc_cred *cred = nfs_file_cred(file); |
| 456 | struct page *page = NULL; |
| 457 | int status; |
| 458 | |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 459 | dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | page = alloc_page(GFP_HIGHUSER); |
| 462 | if (!page) { |
| 463 | status = -ENOMEM; |
| 464 | goto out; |
| 465 | } |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 466 | desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target_cookie, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | page, |
| 468 | NFS_SERVER(inode)->dtsize, |
| 469 | desc->plus); |
| 470 | NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME; |
| 471 | desc->page = page; |
| 472 | desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ |
| 473 | if (desc->error >= 0) { |
| 474 | if ((status = dir_decode(desc)) == 0) |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 475 | desc->entry->prev_cookie = desc->target_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } else |
| 477 | status = -EIO; |
| 478 | if (status < 0) |
| 479 | goto out_release; |
| 480 | |
| 481 | status = nfs_do_filldir(desc, dirent, filldir); |
| 482 | |
| 483 | /* Reset read descriptor so it searches the page cache from |
| 484 | * the start upon the next call to readdir_search_pagecache() */ |
| 485 | desc->page_index = 0; |
| 486 | desc->entry->cookie = desc->entry->prev_cookie = 0; |
| 487 | desc->entry->eof = 0; |
| 488 | out: |
| 489 | dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status); |
| 490 | return status; |
| 491 | out_release: |
| 492 | dir_page_release(desc); |
| 493 | goto out; |
| 494 | } |
| 495 | |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 496 | /* The file offset position represents the dirent entry number. A |
| 497 | last cookie cache takes care of the common case of reading the |
| 498 | whole directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | */ |
| 500 | static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 501 | { |
| 502 | struct dentry *dentry = filp->f_dentry; |
| 503 | struct inode *inode = dentry->d_inode; |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 504 | struct nfs_open_context *ctx = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | nfs_readdir_descriptor_t my_desc, |
| 506 | *desc = &my_desc; |
| 507 | struct nfs_entry my_entry; |
| 508 | struct nfs_fh fh; |
| 509 | struct nfs_fattr fattr; |
| 510 | long res; |
| 511 | |
| 512 | lock_kernel(); |
| 513 | |
| 514 | res = nfs_revalidate_inode(NFS_SERVER(inode), inode); |
| 515 | if (res < 0) { |
| 516 | unlock_kernel(); |
| 517 | return res; |
| 518 | } |
| 519 | |
| 520 | /* |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 521 | * filp->f_pos points to the dirent entry number. |
| 522 | * ctx->dir_pos has the number of the cached cookie. We have |
| 523 | * to either find the entry with the appropriate number or |
| 524 | * revalidate the cookie. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | */ |
| 526 | memset(desc, 0, sizeof(*desc)); |
| 527 | |
| 528 | desc->file = filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | desc->decode = NFS_PROTO(inode)->decode_dirent; |
| 530 | desc->plus = NFS_USE_READDIRPLUS(inode); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 531 | desc->target_index = filp->f_pos; |
| 532 | |
| 533 | if (filp->f_pos == ctx->dir_pos) |
| 534 | desc->target_cookie = ctx->dir_cookie; |
| 535 | else |
| 536 | desc->target_cookie = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | my_entry.cookie = my_entry.prev_cookie = 0; |
| 539 | my_entry.eof = 0; |
| 540 | my_entry.fh = &fh; |
| 541 | my_entry.fattr = &fattr; |
| 542 | desc->entry = &my_entry; |
| 543 | |
| 544 | while(!desc->entry->eof) { |
| 545 | res = readdir_search_pagecache(desc); |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (res == -EBADCOOKIE) { |
| 548 | /* This means either end of directory */ |
Olivier Galibert | 00a9264 | 2005-06-22 17:16:29 +0000 | [diff] [blame^] | 549 | if (desc->target_cookie && desc->entry->cookie != desc->target_cookie) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | /* Or that the server has 'lost' a cookie */ |
| 551 | res = uncached_readdir(desc, dirent, filldir); |
| 552 | if (res >= 0) |
| 553 | continue; |
| 554 | } |
| 555 | res = 0; |
| 556 | break; |
| 557 | } |
| 558 | if (res == -ETOOSMALL && desc->plus) { |
| 559 | NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS; |
| 560 | nfs_zap_caches(inode); |
| 561 | desc->plus = 0; |
| 562 | desc->entry->eof = 0; |
| 563 | continue; |
| 564 | } |
| 565 | if (res < 0) |
| 566 | break; |
| 567 | |
| 568 | res = nfs_do_filldir(desc, dirent, filldir); |
| 569 | if (res < 0) { |
| 570 | res = 0; |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | unlock_kernel(); |
| 575 | if (desc->error < 0) |
| 576 | return desc->error; |
| 577 | if (res < 0) |
| 578 | return res; |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * All directory operations under NFS are synchronous, so fsync() |
| 584 | * is a dummy operation. |
| 585 | */ |
| 586 | int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync) |
| 587 | { |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * A check for whether or not the parent directory has changed. |
| 593 | * In the case it has, we assume that the dentries are untrustworthy |
| 594 | * and may need to be looked up again. |
| 595 | */ |
| 596 | static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry) |
| 597 | { |
| 598 | if (IS_ROOT(dentry)) |
| 599 | return 1; |
| 600 | if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0 |
| 601 | || nfs_attribute_timeout(dir)) |
| 602 | return 0; |
| 603 | return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata); |
| 604 | } |
| 605 | |
| 606 | static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf) |
| 607 | { |
| 608 | dentry->d_fsdata = (void *)verf; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Whenever an NFS operation succeeds, we know that the dentry |
| 613 | * is valid, so we update the revalidation timestamp. |
| 614 | */ |
| 615 | static inline void nfs_renew_times(struct dentry * dentry) |
| 616 | { |
| 617 | dentry->d_time = jiffies; |
| 618 | } |
| 619 | |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 620 | /* |
| 621 | * Return the intent data that applies to this particular path component |
| 622 | * |
| 623 | * Note that the current set of intents only apply to the very last |
| 624 | * component of the path. |
| 625 | * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT. |
| 626 | */ |
| 627 | static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask) |
| 628 | { |
| 629 | if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT)) |
| 630 | return 0; |
| 631 | return nd->flags & mask; |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Inode and filehandle revalidation for lookups. |
| 636 | * |
| 637 | * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, |
| 638 | * or if the intent information indicates that we're about to open this |
| 639 | * particular file and the "nocto" mount flag is not set. |
| 640 | * |
| 641 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | static inline |
| 643 | int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd) |
| 644 | { |
| 645 | struct nfs_server *server = NFS_SERVER(inode); |
| 646 | |
| 647 | if (nd != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* VFS wants an on-the-wire revalidation */ |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 649 | if (nd->flags & LOOKUP_REVAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | goto out_force; |
| 651 | /* This is an open(2) */ |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 652 | if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | !(server->flags & NFS_MOUNT_NOCTO)) |
| 654 | goto out_force; |
| 655 | } |
| 656 | return nfs_revalidate_inode(server, inode); |
| 657 | out_force: |
| 658 | return __nfs_revalidate_inode(server, inode); |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * We judge how long we want to trust negative |
| 663 | * dentries by looking at the parent inode mtime. |
| 664 | * |
| 665 | * If parent mtime has changed, we revalidate, else we wait for a |
| 666 | * period corresponding to the parent's attribute cache timeout value. |
| 667 | */ |
| 668 | static inline |
| 669 | int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, |
| 670 | struct nameidata *nd) |
| 671 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* 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] | 673 | if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return 0; |
| 675 | return !nfs_check_verifier(dir, dentry); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * This is called every time the dcache has a lookup hit, |
| 680 | * and we should check whether we can really trust that |
| 681 | * lookup. |
| 682 | * |
| 683 | * NOTE! The hit can be a negative hit too, don't assume |
| 684 | * we have an inode! |
| 685 | * |
| 686 | * If the parent directory is seen to have changed, we throw out the |
| 687 | * cached dentry and do a new lookup. |
| 688 | */ |
| 689 | static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd) |
| 690 | { |
| 691 | struct inode *dir; |
| 692 | struct inode *inode; |
| 693 | struct dentry *parent; |
| 694 | int error; |
| 695 | struct nfs_fh fhandle; |
| 696 | struct nfs_fattr fattr; |
| 697 | unsigned long verifier; |
| 698 | |
| 699 | parent = dget_parent(dentry); |
| 700 | lock_kernel(); |
| 701 | dir = parent->d_inode; |
| 702 | inode = dentry->d_inode; |
| 703 | |
| 704 | if (!inode) { |
| 705 | if (nfs_neg_need_reval(dir, dentry, nd)) |
| 706 | goto out_bad; |
| 707 | goto out_valid; |
| 708 | } |
| 709 | |
| 710 | if (is_bad_inode(inode)) { |
| 711 | dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n", |
| 712 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 713 | goto out_bad; |
| 714 | } |
| 715 | |
| 716 | /* Revalidate parent directory attribute cache */ |
| 717 | if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) |
| 718 | goto out_zap_parent; |
| 719 | |
| 720 | /* Force a full look up iff the parent directory has changed */ |
| 721 | if (nfs_check_verifier(dir, dentry)) { |
| 722 | if (nfs_lookup_verify_inode(inode, nd)) |
| 723 | goto out_zap_parent; |
| 724 | goto out_valid; |
| 725 | } |
| 726 | |
| 727 | if (NFS_STALE(inode)) |
| 728 | goto out_bad; |
| 729 | |
| 730 | verifier = nfs_save_change_attribute(dir); |
| 731 | error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr); |
| 732 | if (error) |
| 733 | goto out_bad; |
| 734 | if (nfs_compare_fh(NFS_FH(inode), &fhandle)) |
| 735 | goto out_bad; |
| 736 | if ((error = nfs_refresh_inode(inode, &fattr)) != 0) |
| 737 | goto out_bad; |
| 738 | |
| 739 | nfs_renew_times(dentry); |
| 740 | nfs_set_verifier(dentry, verifier); |
| 741 | out_valid: |
| 742 | unlock_kernel(); |
| 743 | dput(parent); |
| 744 | return 1; |
| 745 | out_zap_parent: |
| 746 | nfs_zap_caches(dir); |
| 747 | out_bad: |
| 748 | NFS_CACHEINV(dir); |
| 749 | if (inode && S_ISDIR(inode->i_mode)) { |
| 750 | /* Purge readdir caches. */ |
| 751 | nfs_zap_caches(inode); |
| 752 | /* If we have submounts, don't unhash ! */ |
| 753 | if (have_submounts(dentry)) |
| 754 | goto out_valid; |
| 755 | shrink_dcache_parent(dentry); |
| 756 | } |
| 757 | d_drop(dentry); |
| 758 | unlock_kernel(); |
| 759 | dput(parent); |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * This is called from dput() when d_count is going to 0. |
| 765 | */ |
| 766 | static int nfs_dentry_delete(struct dentry *dentry) |
| 767 | { |
| 768 | dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n", |
| 769 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 770 | dentry->d_flags); |
| 771 | |
| 772 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 773 | /* Unhash it, so that ->d_iput() would be called */ |
| 774 | return 1; |
| 775 | } |
| 776 | if (!(dentry->d_sb->s_flags & MS_ACTIVE)) { |
| 777 | /* Unhash it, so that ancestors of killed async unlink |
| 778 | * files will be cleaned up during umount */ |
| 779 | return 1; |
| 780 | } |
| 781 | return 0; |
| 782 | |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Called when the dentry loses inode. |
| 787 | * We use it to clean up silly-renamed files. |
| 788 | */ |
| 789 | static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) |
| 790 | { |
| 791 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 792 | lock_kernel(); |
| 793 | inode->i_nlink--; |
| 794 | nfs_complete_unlink(dentry); |
| 795 | unlock_kernel(); |
| 796 | } |
| 797 | /* When creating a negative dentry, we want to renew d_time */ |
| 798 | nfs_renew_times(dentry); |
| 799 | iput(inode); |
| 800 | } |
| 801 | |
| 802 | struct dentry_operations nfs_dentry_operations = { |
| 803 | .d_revalidate = nfs_lookup_revalidate, |
| 804 | .d_delete = nfs_dentry_delete, |
| 805 | .d_iput = nfs_dentry_iput, |
| 806 | }; |
| 807 | |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 808 | /* |
| 809 | * Use intent information to check whether or not we're going to do |
| 810 | * an O_EXCL create using this path component. |
| 811 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | static inline |
| 813 | int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd) |
| 814 | { |
| 815 | if (NFS_PROTO(dir)->version == 2) |
| 816 | return 0; |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 817 | if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return 0; |
| 819 | return (nd->intent.open.flags & O_EXCL) != 0; |
| 820 | } |
| 821 | |
| 822 | static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) |
| 823 | { |
| 824 | struct dentry *res; |
| 825 | struct inode *inode = NULL; |
| 826 | int error; |
| 827 | struct nfs_fh fhandle; |
| 828 | struct nfs_fattr fattr; |
| 829 | |
| 830 | dfprintk(VFS, "NFS: lookup(%s/%s)\n", |
| 831 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 832 | |
| 833 | res = ERR_PTR(-ENAMETOOLONG); |
| 834 | if (dentry->d_name.len > NFS_SERVER(dir)->namelen) |
| 835 | goto out; |
| 836 | |
| 837 | res = ERR_PTR(-ENOMEM); |
| 838 | dentry->d_op = NFS_PROTO(dir)->dentry_ops; |
| 839 | |
| 840 | lock_kernel(); |
| 841 | /* Revalidate parent directory attribute cache */ |
| 842 | error = nfs_revalidate_inode(NFS_SERVER(dir), dir); |
| 843 | if (error < 0) { |
| 844 | res = ERR_PTR(error); |
| 845 | goto out_unlock; |
| 846 | } |
| 847 | |
| 848 | /* If we're doing an exclusive create, optimize away the lookup */ |
| 849 | if (nfs_is_exclusive_create(dir, nd)) |
| 850 | goto no_entry; |
| 851 | |
| 852 | error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr); |
| 853 | if (error == -ENOENT) |
| 854 | goto no_entry; |
| 855 | if (error < 0) { |
| 856 | res = ERR_PTR(error); |
| 857 | goto out_unlock; |
| 858 | } |
| 859 | res = ERR_PTR(-EACCES); |
| 860 | inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr); |
| 861 | if (!inode) |
| 862 | goto out_unlock; |
| 863 | no_entry: |
| 864 | res = d_add_unique(dentry, inode); |
| 865 | if (res != NULL) |
| 866 | dentry = res; |
| 867 | nfs_renew_times(dentry); |
| 868 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 869 | out_unlock: |
| 870 | unlock_kernel(); |
| 871 | out: |
| 872 | return res; |
| 873 | } |
| 874 | |
| 875 | #ifdef CONFIG_NFS_V4 |
| 876 | static int nfs_open_revalidate(struct dentry *, struct nameidata *); |
| 877 | |
| 878 | struct dentry_operations nfs4_dentry_operations = { |
| 879 | .d_revalidate = nfs_open_revalidate, |
| 880 | .d_delete = nfs_dentry_delete, |
| 881 | .d_iput = nfs_dentry_iput, |
| 882 | }; |
| 883 | |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 884 | /* |
| 885 | * Use intent information to determine whether we need to substitute |
| 886 | * the NFSv4-style stateful OPEN for the LOOKUP call |
| 887 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | static int is_atomic_open(struct inode *dir, struct nameidata *nd) |
| 889 | { |
Trond Myklebust | 1d6757f | 2005-06-07 18:37:01 -0400 | [diff] [blame] | 890 | if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return 0; |
| 892 | /* NFS does not (yet) have a stateful open for directories */ |
| 893 | if (nd->flags & LOOKUP_DIRECTORY) |
| 894 | return 0; |
| 895 | /* Are we trying to write to a read only partition? */ |
| 896 | if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE))) |
| 897 | return 0; |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 902 | { |
| 903 | struct dentry *res = NULL; |
| 904 | struct inode *inode = NULL; |
| 905 | int error; |
| 906 | |
| 907 | /* Check that we are indeed trying to open this file */ |
| 908 | if (!is_atomic_open(dir, nd)) |
| 909 | goto no_open; |
| 910 | |
| 911 | if (dentry->d_name.len > NFS_SERVER(dir)->namelen) { |
| 912 | res = ERR_PTR(-ENAMETOOLONG); |
| 913 | goto out; |
| 914 | } |
| 915 | dentry->d_op = NFS_PROTO(dir)->dentry_ops; |
| 916 | |
| 917 | /* Let vfs_create() deal with O_EXCL */ |
| 918 | if (nd->intent.open.flags & O_EXCL) |
| 919 | goto no_entry; |
| 920 | |
| 921 | /* Open the file on the server */ |
| 922 | lock_kernel(); |
| 923 | /* Revalidate parent directory attribute cache */ |
| 924 | error = nfs_revalidate_inode(NFS_SERVER(dir), dir); |
| 925 | if (error < 0) { |
| 926 | res = ERR_PTR(error); |
| 927 | goto out; |
| 928 | } |
| 929 | |
| 930 | if (nd->intent.open.flags & O_CREAT) { |
| 931 | nfs_begin_data_update(dir); |
| 932 | inode = nfs4_atomic_open(dir, dentry, nd); |
| 933 | nfs_end_data_update(dir); |
| 934 | } else |
| 935 | inode = nfs4_atomic_open(dir, dentry, nd); |
| 936 | unlock_kernel(); |
| 937 | if (IS_ERR(inode)) { |
| 938 | error = PTR_ERR(inode); |
| 939 | switch (error) { |
| 940 | /* Make a negative dentry */ |
| 941 | case -ENOENT: |
| 942 | inode = NULL; |
| 943 | break; |
| 944 | /* This turned out not to be a regular file */ |
| 945 | case -ELOOP: |
| 946 | if (!(nd->intent.open.flags & O_NOFOLLOW)) |
| 947 | goto no_open; |
| 948 | /* case -EISDIR: */ |
| 949 | /* case -EINVAL: */ |
| 950 | default: |
| 951 | res = ERR_PTR(error); |
| 952 | goto out; |
| 953 | } |
| 954 | } |
| 955 | no_entry: |
| 956 | res = d_add_unique(dentry, inode); |
| 957 | if (res != NULL) |
| 958 | dentry = res; |
| 959 | nfs_renew_times(dentry); |
| 960 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 961 | out: |
| 962 | return res; |
| 963 | no_open: |
| 964 | return nfs_lookup(dir, dentry, nd); |
| 965 | } |
| 966 | |
| 967 | static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 968 | { |
| 969 | struct dentry *parent = NULL; |
| 970 | struct inode *inode = dentry->d_inode; |
| 971 | struct inode *dir; |
| 972 | unsigned long verifier; |
| 973 | int openflags, ret = 0; |
| 974 | |
| 975 | parent = dget_parent(dentry); |
| 976 | dir = parent->d_inode; |
| 977 | if (!is_atomic_open(dir, nd)) |
| 978 | goto no_open; |
| 979 | /* We can't create new files in nfs_open_revalidate(), so we |
| 980 | * optimize away revalidation of negative dentries. |
| 981 | */ |
| 982 | if (inode == NULL) |
| 983 | goto out; |
| 984 | /* NFS only supports OPEN on regular files */ |
| 985 | if (!S_ISREG(inode->i_mode)) |
| 986 | goto no_open; |
| 987 | openflags = nd->intent.open.flags; |
| 988 | /* We cannot do exclusive creation on a positive dentry */ |
| 989 | if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) |
| 990 | goto no_open; |
| 991 | /* We can't create new files, or truncate existing ones here */ |
| 992 | openflags &= ~(O_CREAT|O_TRUNC); |
| 993 | |
| 994 | /* |
| 995 | * Note: we're not holding inode->i_sem and so may be racing with |
| 996 | * operations that change the directory. We therefore save the |
| 997 | * change attribute *before* we do the RPC call. |
| 998 | */ |
| 999 | lock_kernel(); |
| 1000 | verifier = nfs_save_change_attribute(dir); |
| 1001 | ret = nfs4_open_revalidate(dir, dentry, openflags); |
| 1002 | if (!ret) |
| 1003 | nfs_set_verifier(dentry, verifier); |
| 1004 | unlock_kernel(); |
| 1005 | out: |
| 1006 | dput(parent); |
| 1007 | if (!ret) |
| 1008 | d_drop(dentry); |
| 1009 | return ret; |
| 1010 | no_open: |
| 1011 | dput(parent); |
| 1012 | if (inode != NULL && nfs_have_delegation(inode, FMODE_READ)) |
| 1013 | return 1; |
| 1014 | return nfs_lookup_revalidate(dentry, nd); |
| 1015 | } |
| 1016 | #endif /* CONFIG_NFSV4 */ |
| 1017 | |
| 1018 | static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc) |
| 1019 | { |
| 1020 | struct dentry *parent = desc->file->f_dentry; |
| 1021 | struct inode *dir = parent->d_inode; |
| 1022 | struct nfs_entry *entry = desc->entry; |
| 1023 | struct dentry *dentry, *alias; |
| 1024 | struct qstr name = { |
| 1025 | .name = entry->name, |
| 1026 | .len = entry->len, |
| 1027 | }; |
| 1028 | struct inode *inode; |
| 1029 | |
| 1030 | switch (name.len) { |
| 1031 | case 2: |
| 1032 | if (name.name[0] == '.' && name.name[1] == '.') |
| 1033 | return dget_parent(parent); |
| 1034 | break; |
| 1035 | case 1: |
| 1036 | if (name.name[0] == '.') |
| 1037 | return dget(parent); |
| 1038 | } |
| 1039 | name.hash = full_name_hash(name.name, name.len); |
| 1040 | dentry = d_lookup(parent, &name); |
| 1041 | if (dentry != NULL) |
| 1042 | return dentry; |
| 1043 | if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR)) |
| 1044 | return NULL; |
| 1045 | /* Note: caller is already holding the dir->i_sem! */ |
| 1046 | dentry = d_alloc(parent, &name); |
| 1047 | if (dentry == NULL) |
| 1048 | return NULL; |
| 1049 | dentry->d_op = NFS_PROTO(dir)->dentry_ops; |
| 1050 | inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr); |
| 1051 | if (!inode) { |
| 1052 | dput(dentry); |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | alias = d_add_unique(dentry, inode); |
| 1056 | if (alias != NULL) { |
| 1057 | dput(dentry); |
| 1058 | dentry = alias; |
| 1059 | } |
| 1060 | nfs_renew_times(dentry); |
| 1061 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1062 | return dentry; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Code common to create, mkdir, and mknod. |
| 1067 | */ |
| 1068 | int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, |
| 1069 | struct nfs_fattr *fattr) |
| 1070 | { |
| 1071 | struct inode *inode; |
| 1072 | int error = -EACCES; |
| 1073 | |
| 1074 | /* We may have been initialized further down */ |
| 1075 | if (dentry->d_inode) |
| 1076 | return 0; |
| 1077 | if (fhandle->size == 0) { |
| 1078 | struct inode *dir = dentry->d_parent->d_inode; |
| 1079 | error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr); |
| 1080 | if (error) |
| 1081 | goto out_err; |
| 1082 | } |
| 1083 | if (!(fattr->valid & NFS_ATTR_FATTR)) { |
| 1084 | struct nfs_server *server = NFS_SB(dentry->d_sb); |
| 1085 | error = server->rpc_ops->getattr(server, fhandle, fattr); |
| 1086 | if (error < 0) |
| 1087 | goto out_err; |
| 1088 | } |
| 1089 | error = -ENOMEM; |
| 1090 | inode = nfs_fhget(dentry->d_sb, fhandle, fattr); |
| 1091 | if (inode == NULL) |
| 1092 | goto out_err; |
| 1093 | d_instantiate(dentry, inode); |
| 1094 | return 0; |
| 1095 | out_err: |
| 1096 | d_drop(dentry); |
| 1097 | return error; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Following a failed create operation, we drop the dentry rather |
| 1102 | * than retain a negative dentry. This avoids a problem in the event |
| 1103 | * that the operation succeeded on the server, but an error in the |
| 1104 | * reply path made it appear to have failed. |
| 1105 | */ |
| 1106 | static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 1107 | struct nameidata *nd) |
| 1108 | { |
| 1109 | struct iattr attr; |
| 1110 | int error; |
| 1111 | int open_flags = 0; |
| 1112 | |
| 1113 | dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id, |
| 1114 | dir->i_ino, dentry->d_name.name); |
| 1115 | |
| 1116 | attr.ia_mode = mode; |
| 1117 | attr.ia_valid = ATTR_MODE; |
| 1118 | |
| 1119 | if (nd && (nd->flags & LOOKUP_CREATE)) |
| 1120 | open_flags = nd->intent.open.flags; |
| 1121 | |
| 1122 | lock_kernel(); |
| 1123 | nfs_begin_data_update(dir); |
| 1124 | error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); |
| 1125 | nfs_end_data_update(dir); |
| 1126 | if (error != 0) |
| 1127 | goto out_err; |
| 1128 | nfs_renew_times(dentry); |
| 1129 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1130 | unlock_kernel(); |
| 1131 | return 0; |
| 1132 | out_err: |
| 1133 | unlock_kernel(); |
| 1134 | d_drop(dentry); |
| 1135 | return error; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * See comments for nfs_proc_create regarding failed operations. |
| 1140 | */ |
| 1141 | static int |
| 1142 | nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) |
| 1143 | { |
| 1144 | struct iattr attr; |
| 1145 | int status; |
| 1146 | |
| 1147 | dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id, |
| 1148 | dir->i_ino, dentry->d_name.name); |
| 1149 | |
| 1150 | if (!new_valid_dev(rdev)) |
| 1151 | return -EINVAL; |
| 1152 | |
| 1153 | attr.ia_mode = mode; |
| 1154 | attr.ia_valid = ATTR_MODE; |
| 1155 | |
| 1156 | lock_kernel(); |
| 1157 | nfs_begin_data_update(dir); |
| 1158 | status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); |
| 1159 | nfs_end_data_update(dir); |
| 1160 | if (status != 0) |
| 1161 | goto out_err; |
| 1162 | nfs_renew_times(dentry); |
| 1163 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1164 | unlock_kernel(); |
| 1165 | return 0; |
| 1166 | out_err: |
| 1167 | unlock_kernel(); |
| 1168 | d_drop(dentry); |
| 1169 | return status; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * See comments for nfs_proc_create regarding failed operations. |
| 1174 | */ |
| 1175 | static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1176 | { |
| 1177 | struct iattr attr; |
| 1178 | int error; |
| 1179 | |
| 1180 | dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id, |
| 1181 | dir->i_ino, dentry->d_name.name); |
| 1182 | |
| 1183 | attr.ia_valid = ATTR_MODE; |
| 1184 | attr.ia_mode = mode | S_IFDIR; |
| 1185 | |
| 1186 | lock_kernel(); |
| 1187 | nfs_begin_data_update(dir); |
| 1188 | error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); |
| 1189 | nfs_end_data_update(dir); |
| 1190 | if (error != 0) |
| 1191 | goto out_err; |
| 1192 | nfs_renew_times(dentry); |
| 1193 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1194 | unlock_kernel(); |
| 1195 | return 0; |
| 1196 | out_err: |
| 1197 | d_drop(dentry); |
| 1198 | unlock_kernel(); |
| 1199 | return error; |
| 1200 | } |
| 1201 | |
| 1202 | static int nfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 1203 | { |
| 1204 | int error; |
| 1205 | |
| 1206 | dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id, |
| 1207 | dir->i_ino, dentry->d_name.name); |
| 1208 | |
| 1209 | lock_kernel(); |
| 1210 | nfs_begin_data_update(dir); |
| 1211 | error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); |
| 1212 | /* Ensure the VFS deletes this inode */ |
| 1213 | if (error == 0 && dentry->d_inode != NULL) |
| 1214 | dentry->d_inode->i_nlink = 0; |
| 1215 | nfs_end_data_update(dir); |
| 1216 | unlock_kernel(); |
| 1217 | |
| 1218 | return error; |
| 1219 | } |
| 1220 | |
| 1221 | static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) |
| 1222 | { |
| 1223 | static unsigned int sillycounter; |
| 1224 | const int i_inosize = sizeof(dir->i_ino)*2; |
| 1225 | const int countersize = sizeof(sillycounter)*2; |
| 1226 | const int slen = sizeof(".nfs") + i_inosize + countersize - 1; |
| 1227 | char silly[slen+1]; |
| 1228 | struct qstr qsilly; |
| 1229 | struct dentry *sdentry; |
| 1230 | int error = -EIO; |
| 1231 | |
| 1232 | dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n", |
| 1233 | dentry->d_parent->d_name.name, dentry->d_name.name, |
| 1234 | atomic_read(&dentry->d_count)); |
| 1235 | |
| 1236 | #ifdef NFS_PARANOIA |
| 1237 | if (!dentry->d_inode) |
| 1238 | printk("NFS: silly-renaming %s/%s, negative dentry??\n", |
| 1239 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1240 | #endif |
| 1241 | /* |
| 1242 | * We don't allow a dentry to be silly-renamed twice. |
| 1243 | */ |
| 1244 | error = -EBUSY; |
| 1245 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) |
| 1246 | goto out; |
| 1247 | |
| 1248 | sprintf(silly, ".nfs%*.*lx", |
| 1249 | i_inosize, i_inosize, dentry->d_inode->i_ino); |
| 1250 | |
| 1251 | sdentry = NULL; |
| 1252 | do { |
| 1253 | char *suffix = silly + slen - countersize; |
| 1254 | |
| 1255 | dput(sdentry); |
| 1256 | sillycounter++; |
| 1257 | sprintf(suffix, "%*.*x", countersize, countersize, sillycounter); |
| 1258 | |
| 1259 | dfprintk(VFS, "trying to rename %s to %s\n", |
| 1260 | dentry->d_name.name, silly); |
| 1261 | |
| 1262 | sdentry = lookup_one_len(silly, dentry->d_parent, slen); |
| 1263 | /* |
| 1264 | * N.B. Better to return EBUSY here ... it could be |
| 1265 | * dangerous to delete the file while it's in use. |
| 1266 | */ |
| 1267 | if (IS_ERR(sdentry)) |
| 1268 | goto out; |
| 1269 | } while(sdentry->d_inode != NULL); /* need negative lookup */ |
| 1270 | |
| 1271 | qsilly.name = silly; |
| 1272 | qsilly.len = strlen(silly); |
| 1273 | nfs_begin_data_update(dir); |
| 1274 | if (dentry->d_inode) { |
| 1275 | nfs_begin_data_update(dentry->d_inode); |
| 1276 | error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, |
| 1277 | dir, &qsilly); |
| 1278 | nfs_end_data_update(dentry->d_inode); |
| 1279 | } else |
| 1280 | error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, |
| 1281 | dir, &qsilly); |
| 1282 | nfs_end_data_update(dir); |
| 1283 | if (!error) { |
| 1284 | nfs_renew_times(dentry); |
| 1285 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1286 | d_move(dentry, sdentry); |
| 1287 | error = nfs_async_unlink(dentry); |
| 1288 | /* If we return 0 we don't unlink */ |
| 1289 | } |
| 1290 | dput(sdentry); |
| 1291 | out: |
| 1292 | return error; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Remove a file after making sure there are no pending writes, |
| 1297 | * and after checking that the file has only one user. |
| 1298 | * |
| 1299 | * We invalidate the attribute cache and free the inode prior to the operation |
| 1300 | * to avoid possible races if the server reuses the inode. |
| 1301 | */ |
| 1302 | static int nfs_safe_remove(struct dentry *dentry) |
| 1303 | { |
| 1304 | struct inode *dir = dentry->d_parent->d_inode; |
| 1305 | struct inode *inode = dentry->d_inode; |
| 1306 | int error = -EBUSY; |
| 1307 | |
| 1308 | dfprintk(VFS, "NFS: safe_remove(%s/%s)\n", |
| 1309 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1310 | |
| 1311 | /* If the dentry was sillyrenamed, we simply call d_delete() */ |
| 1312 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
| 1313 | error = 0; |
| 1314 | goto out; |
| 1315 | } |
| 1316 | |
| 1317 | nfs_begin_data_update(dir); |
| 1318 | if (inode != NULL) { |
| 1319 | nfs_begin_data_update(inode); |
| 1320 | error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); |
| 1321 | /* The VFS may want to delete this inode */ |
| 1322 | if (error == 0) |
| 1323 | inode->i_nlink--; |
| 1324 | nfs_end_data_update(inode); |
| 1325 | } else |
| 1326 | error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); |
| 1327 | nfs_end_data_update(dir); |
| 1328 | out: |
| 1329 | return error; |
| 1330 | } |
| 1331 | |
| 1332 | /* We do silly rename. In case sillyrename() returns -EBUSY, the inode |
| 1333 | * belongs to an active ".nfs..." file and we return -EBUSY. |
| 1334 | * |
| 1335 | * If sillyrename() returns 0, we do nothing, otherwise we unlink. |
| 1336 | */ |
| 1337 | static int nfs_unlink(struct inode *dir, struct dentry *dentry) |
| 1338 | { |
| 1339 | int error; |
| 1340 | int need_rehash = 0; |
| 1341 | |
| 1342 | dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, |
| 1343 | dir->i_ino, dentry->d_name.name); |
| 1344 | |
| 1345 | lock_kernel(); |
| 1346 | spin_lock(&dcache_lock); |
| 1347 | spin_lock(&dentry->d_lock); |
| 1348 | if (atomic_read(&dentry->d_count) > 1) { |
| 1349 | spin_unlock(&dentry->d_lock); |
| 1350 | spin_unlock(&dcache_lock); |
| 1351 | error = nfs_sillyrename(dir, dentry); |
| 1352 | unlock_kernel(); |
| 1353 | return error; |
| 1354 | } |
| 1355 | if (!d_unhashed(dentry)) { |
| 1356 | __d_drop(dentry); |
| 1357 | need_rehash = 1; |
| 1358 | } |
| 1359 | spin_unlock(&dentry->d_lock); |
| 1360 | spin_unlock(&dcache_lock); |
| 1361 | error = nfs_safe_remove(dentry); |
| 1362 | if (!error) { |
| 1363 | nfs_renew_times(dentry); |
| 1364 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 1365 | } else if (need_rehash) |
| 1366 | d_rehash(dentry); |
| 1367 | unlock_kernel(); |
| 1368 | return error; |
| 1369 | } |
| 1370 | |
| 1371 | static int |
| 1372 | nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 1373 | { |
| 1374 | struct iattr attr; |
| 1375 | struct nfs_fattr sym_attr; |
| 1376 | struct nfs_fh sym_fh; |
| 1377 | struct qstr qsymname; |
| 1378 | int error; |
| 1379 | |
| 1380 | dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id, |
| 1381 | dir->i_ino, dentry->d_name.name, symname); |
| 1382 | |
| 1383 | #ifdef NFS_PARANOIA |
| 1384 | if (dentry->d_inode) |
| 1385 | printk("nfs_proc_symlink: %s/%s not negative!\n", |
| 1386 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1387 | #endif |
| 1388 | /* |
| 1389 | * Fill in the sattr for the call. |
| 1390 | * Note: SunOS 4.1.2 crashes if the mode isn't initialized! |
| 1391 | */ |
| 1392 | attr.ia_valid = ATTR_MODE; |
| 1393 | attr.ia_mode = S_IFLNK | S_IRWXUGO; |
| 1394 | |
| 1395 | qsymname.name = symname; |
| 1396 | qsymname.len = strlen(symname); |
| 1397 | |
| 1398 | lock_kernel(); |
| 1399 | nfs_begin_data_update(dir); |
| 1400 | error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname, |
| 1401 | &attr, &sym_fh, &sym_attr); |
| 1402 | nfs_end_data_update(dir); |
| 1403 | if (!error) { |
| 1404 | error = nfs_instantiate(dentry, &sym_fh, &sym_attr); |
| 1405 | } else { |
| 1406 | if (error == -EEXIST) |
| 1407 | printk("nfs_proc_symlink: %s/%s already exists??\n", |
| 1408 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1409 | d_drop(dentry); |
| 1410 | } |
| 1411 | unlock_kernel(); |
| 1412 | return error; |
| 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 1417 | { |
| 1418 | struct inode *inode = old_dentry->d_inode; |
| 1419 | int error; |
| 1420 | |
| 1421 | dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n", |
| 1422 | old_dentry->d_parent->d_name.name, old_dentry->d_name.name, |
| 1423 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 1424 | |
| 1425 | /* |
| 1426 | * Drop the dentry in advance to force a new lookup. |
| 1427 | * Since nfs_proc_link doesn't return a file handle, |
| 1428 | * we can't use the existing dentry. |
| 1429 | */ |
| 1430 | lock_kernel(); |
| 1431 | d_drop(dentry); |
| 1432 | |
| 1433 | nfs_begin_data_update(dir); |
| 1434 | nfs_begin_data_update(inode); |
| 1435 | error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); |
| 1436 | nfs_end_data_update(inode); |
| 1437 | nfs_end_data_update(dir); |
| 1438 | unlock_kernel(); |
| 1439 | return error; |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * RENAME |
| 1444 | * FIXME: Some nfsds, like the Linux user space nfsd, may generate a |
| 1445 | * different file handle for the same inode after a rename (e.g. when |
| 1446 | * moving to a different directory). A fail-safe method to do so would |
| 1447 | * be to look up old_dir/old_name, create a link to new_dir/new_name and |
| 1448 | * rename the old file using the sillyrename stuff. This way, the original |
| 1449 | * file in old_dir will go away when the last process iput()s the inode. |
| 1450 | * |
| 1451 | * FIXED. |
| 1452 | * |
| 1453 | * It actually works quite well. One needs to have the possibility for |
| 1454 | * at least one ".nfs..." file in each directory the file ever gets |
| 1455 | * moved or linked to which happens automagically with the new |
| 1456 | * implementation that only depends on the dcache stuff instead of |
| 1457 | * using the inode layer |
| 1458 | * |
| 1459 | * Unfortunately, things are a little more complicated than indicated |
| 1460 | * above. For a cross-directory move, we want to make sure we can get |
| 1461 | * rid of the old inode after the operation. This means there must be |
| 1462 | * no pending writes (if it's a file), and the use count must be 1. |
| 1463 | * If these conditions are met, we can drop the dentries before doing |
| 1464 | * the rename. |
| 1465 | */ |
| 1466 | static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1467 | struct inode *new_dir, struct dentry *new_dentry) |
| 1468 | { |
| 1469 | struct inode *old_inode = old_dentry->d_inode; |
| 1470 | struct inode *new_inode = new_dentry->d_inode; |
| 1471 | struct dentry *dentry = NULL, *rehash = NULL; |
| 1472 | int error = -EBUSY; |
| 1473 | |
| 1474 | /* |
| 1475 | * To prevent any new references to the target during the rename, |
| 1476 | * we unhash the dentry and free the inode in advance. |
| 1477 | */ |
| 1478 | lock_kernel(); |
| 1479 | if (!d_unhashed(new_dentry)) { |
| 1480 | d_drop(new_dentry); |
| 1481 | rehash = new_dentry; |
| 1482 | } |
| 1483 | |
| 1484 | dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n", |
| 1485 | old_dentry->d_parent->d_name.name, old_dentry->d_name.name, |
| 1486 | new_dentry->d_parent->d_name.name, new_dentry->d_name.name, |
| 1487 | atomic_read(&new_dentry->d_count)); |
| 1488 | |
| 1489 | /* |
| 1490 | * First check whether the target is busy ... we can't |
| 1491 | * safely do _any_ rename if the target is in use. |
| 1492 | * |
| 1493 | * For files, make a copy of the dentry and then do a |
| 1494 | * silly-rename. If the silly-rename succeeds, the |
| 1495 | * copied dentry is hashed and becomes the new target. |
| 1496 | */ |
| 1497 | if (!new_inode) |
| 1498 | goto go_ahead; |
| 1499 | if (S_ISDIR(new_inode->i_mode)) |
| 1500 | goto out; |
| 1501 | else if (atomic_read(&new_dentry->d_count) > 2) { |
| 1502 | int err; |
| 1503 | /* copy the target dentry's name */ |
| 1504 | dentry = d_alloc(new_dentry->d_parent, |
| 1505 | &new_dentry->d_name); |
| 1506 | if (!dentry) |
| 1507 | goto out; |
| 1508 | |
| 1509 | /* silly-rename the existing target ... */ |
| 1510 | err = nfs_sillyrename(new_dir, new_dentry); |
| 1511 | if (!err) { |
| 1512 | new_dentry = rehash = dentry; |
| 1513 | new_inode = NULL; |
| 1514 | /* instantiate the replacement target */ |
| 1515 | d_instantiate(new_dentry, NULL); |
| 1516 | } else if (atomic_read(&new_dentry->d_count) > 1) { |
| 1517 | /* dentry still busy? */ |
| 1518 | #ifdef NFS_PARANOIA |
| 1519 | printk("nfs_rename: target %s/%s busy, d_count=%d\n", |
| 1520 | new_dentry->d_parent->d_name.name, |
| 1521 | new_dentry->d_name.name, |
| 1522 | atomic_read(&new_dentry->d_count)); |
| 1523 | #endif |
| 1524 | goto out; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | go_ahead: |
| 1529 | /* |
| 1530 | * ... prune child dentries and writebacks if needed. |
| 1531 | */ |
| 1532 | if (atomic_read(&old_dentry->d_count) > 1) { |
| 1533 | nfs_wb_all(old_inode); |
| 1534 | shrink_dcache_parent(old_dentry); |
| 1535 | } |
| 1536 | |
| 1537 | if (new_inode) |
| 1538 | d_delete(new_dentry); |
| 1539 | |
| 1540 | nfs_begin_data_update(old_dir); |
| 1541 | nfs_begin_data_update(new_dir); |
| 1542 | nfs_begin_data_update(old_inode); |
| 1543 | error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name, |
| 1544 | new_dir, &new_dentry->d_name); |
| 1545 | nfs_end_data_update(old_inode); |
| 1546 | nfs_end_data_update(new_dir); |
| 1547 | nfs_end_data_update(old_dir); |
| 1548 | out: |
| 1549 | if (rehash) |
| 1550 | d_rehash(rehash); |
| 1551 | if (!error) { |
| 1552 | if (!S_ISDIR(old_inode->i_mode)) |
| 1553 | d_move(old_dentry, new_dentry); |
| 1554 | nfs_renew_times(new_dentry); |
| 1555 | nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir)); |
| 1556 | } |
| 1557 | |
| 1558 | /* new dentry created? */ |
| 1559 | if (dentry) |
| 1560 | dput(dentry); |
| 1561 | unlock_kernel(); |
| 1562 | return error; |
| 1563 | } |
| 1564 | |
| 1565 | int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res) |
| 1566 | { |
| 1567 | struct nfs_access_entry *cache = &NFS_I(inode)->cache_access; |
| 1568 | |
| 1569 | if (cache->cred != cred |
| 1570 | || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)) |
| 1571 | || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS)) |
| 1572 | return -ENOENT; |
| 1573 | memcpy(res, cache, sizeof(*res)); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) |
| 1578 | { |
| 1579 | struct nfs_access_entry *cache = &NFS_I(inode)->cache_access; |
| 1580 | |
| 1581 | if (cache->cred != set->cred) { |
| 1582 | if (cache->cred) |
| 1583 | put_rpccred(cache->cred); |
| 1584 | cache->cred = get_rpccred(set->cred); |
| 1585 | } |
| 1586 | NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS; |
| 1587 | cache->jiffies = set->jiffies; |
| 1588 | cache->mask = set->mask; |
| 1589 | } |
| 1590 | |
| 1591 | static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask) |
| 1592 | { |
| 1593 | struct nfs_access_entry cache; |
| 1594 | int status; |
| 1595 | |
| 1596 | status = nfs_access_get_cached(inode, cred, &cache); |
| 1597 | if (status == 0) |
| 1598 | goto out; |
| 1599 | |
| 1600 | /* Be clever: ask server to check for all possible rights */ |
| 1601 | cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; |
| 1602 | cache.cred = cred; |
| 1603 | cache.jiffies = jiffies; |
| 1604 | status = NFS_PROTO(inode)->access(inode, &cache); |
| 1605 | if (status != 0) |
| 1606 | return status; |
| 1607 | nfs_access_add_cache(inode, &cache); |
| 1608 | out: |
| 1609 | if ((cache.mask & mask) == mask) |
| 1610 | return 0; |
| 1611 | return -EACCES; |
| 1612 | } |
| 1613 | |
| 1614 | int nfs_permission(struct inode *inode, int mask, struct nameidata *nd) |
| 1615 | { |
| 1616 | struct rpc_cred *cred; |
| 1617 | int res = 0; |
| 1618 | |
| 1619 | if (mask == 0) |
| 1620 | goto out; |
| 1621 | /* Is this sys_access() ? */ |
| 1622 | if (nd != NULL && (nd->flags & LOOKUP_ACCESS)) |
| 1623 | goto force_lookup; |
| 1624 | |
| 1625 | switch (inode->i_mode & S_IFMT) { |
| 1626 | case S_IFLNK: |
| 1627 | goto out; |
| 1628 | case S_IFREG: |
| 1629 | /* NFSv4 has atomic_open... */ |
| 1630 | if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN) |
| 1631 | && nd != NULL |
| 1632 | && (nd->flags & LOOKUP_OPEN)) |
| 1633 | goto out; |
| 1634 | break; |
| 1635 | case S_IFDIR: |
| 1636 | /* |
| 1637 | * Optimize away all write operations, since the server |
| 1638 | * will check permissions when we perform the op. |
| 1639 | */ |
| 1640 | if ((mask & MAY_WRITE) && !(mask & MAY_READ)) |
| 1641 | goto out; |
| 1642 | } |
| 1643 | |
| 1644 | force_lookup: |
| 1645 | lock_kernel(); |
| 1646 | |
| 1647 | if (!NFS_PROTO(inode)->access) |
| 1648 | goto out_notsup; |
| 1649 | |
| 1650 | cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0); |
| 1651 | if (!IS_ERR(cred)) { |
| 1652 | res = nfs_do_access(inode, cred, mask); |
| 1653 | put_rpccred(cred); |
| 1654 | } else |
| 1655 | res = PTR_ERR(cred); |
| 1656 | unlock_kernel(); |
| 1657 | out: |
| 1658 | return res; |
| 1659 | out_notsup: |
| 1660 | res = nfs_revalidate_inode(NFS_SERVER(inode), inode); |
| 1661 | if (res == 0) |
| 1662 | res = generic_permission(inode, mask, NULL); |
| 1663 | unlock_kernel(); |
| 1664 | return res; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * Local variables: |
| 1669 | * version-control: t |
| 1670 | * kept-new-versions: 5 |
| 1671 | * End: |
| 1672 | */ |