blob: affd3ae52e5518887abe95e20e4d27a6e8fd1883 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Howells54ceac42006-08-22 20:06:13 -040034#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Trond Myklebust4ce79712005-06-22 17:16:21 +000036#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050038#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define NFS_PARANOIA 1
41/* #define NFS_DEBUG_VERBOSE 1 */
42
43static int nfs_opendir(struct inode *, struct file *);
44static int nfs_readdir(struct file *, void *, filldir_t);
45static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
46static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
47static int nfs_mkdir(struct inode *, struct dentry *, int);
48static int nfs_rmdir(struct inode *, struct dentry *);
49static int nfs_unlink(struct inode *, struct dentry *);
50static int nfs_symlink(struct inode *, struct dentry *, const char *);
51static int nfs_link(struct dentry *, struct inode *, struct dentry *);
52static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
53static int nfs_rename(struct inode *, struct dentry *,
54 struct inode *, struct dentry *);
55static int nfs_fsync_dir(struct file *, struct dentry *, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000056static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080058const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000059 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .read = generic_read_dir,
61 .readdir = nfs_readdir,
62 .open = nfs_opendir,
63 .release = nfs_release,
64 .fsync = nfs_fsync_dir,
65};
66
67struct inode_operations nfs_dir_inode_operations = {
68 .create = nfs_create,
69 .lookup = nfs_lookup,
70 .link = nfs_link,
71 .unlink = nfs_unlink,
72 .symlink = nfs_symlink,
73 .mkdir = nfs_mkdir,
74 .rmdir = nfs_rmdir,
75 .mknod = nfs_mknod,
76 .rename = nfs_rename,
77 .permission = nfs_permission,
78 .getattr = nfs_getattr,
79 .setattr = nfs_setattr,
80};
81
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000082#ifdef CONFIG_NFS_V3
83struct inode_operations nfs3_dir_inode_operations = {
84 .create = nfs_create,
85 .lookup = nfs_lookup,
86 .link = nfs_link,
87 .unlink = nfs_unlink,
88 .symlink = nfs_symlink,
89 .mkdir = nfs_mkdir,
90 .rmdir = nfs_rmdir,
91 .mknod = nfs_mknod,
92 .rename = nfs_rename,
93 .permission = nfs_permission,
94 .getattr = nfs_getattr,
95 .setattr = nfs_setattr,
96 .listxattr = nfs3_listxattr,
97 .getxattr = nfs3_getxattr,
98 .setxattr = nfs3_setxattr,
99 .removexattr = nfs3_removexattr,
100};
101#endif /* CONFIG_NFS_V3 */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifdef CONFIG_NFS_V4
104
105static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
106struct inode_operations nfs4_dir_inode_operations = {
107 .create = nfs_create,
108 .lookup = nfs_atomic_lookup,
109 .link = nfs_link,
110 .unlink = nfs_unlink,
111 .symlink = nfs_symlink,
112 .mkdir = nfs_mkdir,
113 .rmdir = nfs_rmdir,
114 .mknod = nfs_mknod,
115 .rename = nfs_rename,
116 .permission = nfs_permission,
117 .getattr = nfs_getattr,
118 .setattr = nfs_setattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +0000119 .getxattr = nfs4_getxattr,
120 .setxattr = nfs4_setxattr,
121 .listxattr = nfs4_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124#endif /* CONFIG_NFS_V4 */
125
126/*
127 * Open file
128 */
129static int
130nfs_opendir(struct inode *inode, struct file *filp)
131{
Carsten Otte7451c4f2006-04-19 13:06:37 -0400132 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500134 dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
135 inode->i_sb->s_id, inode->i_ino);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 lock_kernel();
138 /* Call generic open code in order to cache credentials */
Carsten Otte7451c4f2006-04-19 13:06:37 -0400139 res = nfs_open(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unlock_kernel();
141 return res;
142}
143
144typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
145typedef struct {
146 struct file *file;
147 struct page *page;
148 unsigned long page_index;
149 u32 *ptr;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000150 u64 *dir_cookie;
151 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct nfs_entry *entry;
153 decode_dirent_t decode;
154 int plus;
155 int error;
156} nfs_readdir_descriptor_t;
157
158/* Now we cache directories properly, by stuffing the dirent
159 * data directly in the page cache.
160 *
161 * Inode invalidation due to refresh etc. takes care of
162 * _everything_, no sloppy entry flushing logic, no extraneous
163 * copying, network direct to page cache, the way it was meant
164 * to be.
165 *
166 * NOTE: Dirent information verification is done always by the
167 * page-in of the RPC reply, nowhere else, this simplies
168 * things substantially.
169 */
170static
171int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
172{
173 struct file *file = desc->file;
174 struct inode *inode = file->f_dentry->d_inode;
175 struct rpc_cred *cred = nfs_file_cred(file);
176 unsigned long timestamp;
177 int error;
178
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500179 dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
180 __FUNCTION__, (long long)desc->entry->cookie,
181 page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 again:
184 timestamp = jiffies;
185 error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
186 NFS_SERVER(inode)->dtsize, desc->plus);
187 if (error < 0) {
188 /* We requested READDIRPLUS, but the server doesn't grok it */
189 if (error == -ENOTSUPP && desc->plus) {
190 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Chuck Lever412d5822005-08-18 11:24:11 -0700191 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 desc->plus = 0;
193 goto again;
194 }
195 goto error;
196 }
197 SetPageUptodate(page);
Chuck Leverdc592502005-08-18 11:24:12 -0700198 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700199 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700200 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Ensure consistent page alignment of the data.
202 * Note: assumes we have exclusive access to this mapping either
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800203 * through inode->i_mutex or some other mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
Trond Myklebusta656db92005-06-22 17:16:21 +0000205 if (page->index == 0)
206 invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unlock_page(page);
208 return 0;
209 error:
210 SetPageError(page);
211 unlock_page(page);
212 nfs_zap_caches(inode);
213 desc->error = error;
214 return -EIO;
215}
216
217static inline
218int dir_decode(nfs_readdir_descriptor_t *desc)
219{
220 u32 *p = desc->ptr;
221 p = desc->decode(p, desc->entry, desc->plus);
222 if (IS_ERR(p))
223 return PTR_ERR(p);
224 desc->ptr = p;
225 return 0;
226}
227
228static inline
229void dir_page_release(nfs_readdir_descriptor_t *desc)
230{
231 kunmap(desc->page);
232 page_cache_release(desc->page);
233 desc->page = NULL;
234 desc->ptr = NULL;
235}
236
237/*
238 * Given a pointer to a buffer that has already been filled by a call
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000239 * to readdir, find the next entry with cookie '*desc->dir_cookie'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *
241 * If the end of the buffer has been reached, return -EAGAIN, if not,
242 * return the offset within the buffer of the next entry to be
243 * read.
244 */
245static inline
Olivier Galibert00a92642005-06-22 17:16:29 +0000246int find_dirent(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct nfs_entry *entry = desc->entry;
249 int loop_count = 0,
250 status;
251
252 while((status = dir_decode(desc)) == 0) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500253 dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
254 __FUNCTION__, (unsigned long long)entry->cookie);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000255 if (entry->prev_cookie == *desc->dir_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 if (loop_count++ > 200) {
258 loop_count = 0;
259 schedule();
260 }
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return status;
263}
264
265/*
Olivier Galibert00a92642005-06-22 17:16:29 +0000266 * Given a pointer to a buffer that has already been filled by a call
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000267 * to readdir, find the entry at offset 'desc->file->f_pos'.
Olivier Galibert00a92642005-06-22 17:16:29 +0000268 *
269 * If the end of the buffer has been reached, return -EAGAIN, if not,
270 * return the offset within the buffer of the next entry to be
271 * read.
272 */
273static inline
274int find_dirent_index(nfs_readdir_descriptor_t *desc)
275{
276 struct nfs_entry *entry = desc->entry;
277 int loop_count = 0,
278 status;
279
280 for(;;) {
281 status = dir_decode(desc);
282 if (status)
283 break;
284
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500285 dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
286 (unsigned long long)entry->cookie, desc->current_index);
Olivier Galibert00a92642005-06-22 17:16:29 +0000287
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000288 if (desc->file->f_pos == desc->current_index) {
289 *desc->dir_cookie = entry->cookie;
Olivier Galibert00a92642005-06-22 17:16:29 +0000290 break;
291 }
292 desc->current_index++;
293 if (loop_count++ > 200) {
294 loop_count = 0;
295 schedule();
296 }
297 }
Olivier Galibert00a92642005-06-22 17:16:29 +0000298 return status;
299}
300
301/*
302 * Find the given page, and call find_dirent() or find_dirent_index in
303 * order to try to return the next entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305static inline
306int find_dirent_page(nfs_readdir_descriptor_t *desc)
307{
308 struct inode *inode = desc->file->f_dentry->d_inode;
309 struct page *page;
310 int status;
311
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500312 dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
313 __FUNCTION__, desc->page_index,
314 (long long) *desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 page = read_cache_page(inode->i_mapping, desc->page_index,
317 (filler_t *)nfs_readdir_filler, desc);
318 if (IS_ERR(page)) {
319 status = PTR_ERR(page);
320 goto out;
321 }
322 if (!PageUptodate(page))
323 goto read_error;
324
325 /* NOTE: Someone else may have changed the READDIRPLUS flag */
326 desc->page = page;
327 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000328 if (*desc->dir_cookie != 0)
Olivier Galibert00a92642005-06-22 17:16:29 +0000329 status = find_dirent(desc);
330 else
331 status = find_dirent_index(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (status < 0)
333 dir_page_release(desc);
334 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500335 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return status;
337 read_error:
338 page_cache_release(page);
339 return -EIO;
340}
341
342/*
343 * Recurse through the page cache pages, and return a
344 * filled nfs_entry structure of the next directory entry if possible.
345 *
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000346 * The target for the search is '*desc->dir_cookie' if non-0,
347 * 'desc->file->f_pos' otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
349static inline
350int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
351{
352 int loop_count = 0;
353 int res;
354
Olivier Galibert00a92642005-06-22 17:16:29 +0000355 /* Always search-by-index from the beginning of the cache */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000356 if (*desc->dir_cookie == 0) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500357 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
358 (long long)desc->file->f_pos);
Olivier Galibert00a92642005-06-22 17:16:29 +0000359 desc->page_index = 0;
360 desc->entry->cookie = desc->entry->prev_cookie = 0;
361 desc->entry->eof = 0;
362 desc->current_index = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000363 } else
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500364 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
365 (unsigned long long)*desc->dir_cookie);
Olivier Galibert00a92642005-06-22 17:16:29 +0000366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 for (;;) {
368 res = find_dirent_page(desc);
369 if (res != -EAGAIN)
370 break;
371 /* Align to beginning of next page */
372 desc->page_index ++;
373 if (loop_count++ > 200) {
374 loop_count = 0;
375 schedule();
376 }
377 }
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500378
379 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return res;
381}
382
383static inline unsigned int dt_type(struct inode *inode)
384{
385 return (inode->i_mode >> 12) & 15;
386}
387
388static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
389
390/*
391 * Once we've found the start of the dirent within a page: fill 'er up...
392 */
393static
394int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
395 filldir_t filldir)
396{
397 struct file *file = desc->file;
398 struct nfs_entry *entry = desc->entry;
399 struct dentry *dentry = NULL;
400 unsigned long fileid;
401 int loop_count = 0,
402 res;
403
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500404 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
405 (unsigned long long)entry->cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 for(;;) {
408 unsigned d_type = DT_UNKNOWN;
409 /* Note: entry->prev_cookie contains the cookie for
410 * retrieving the current dirent on the server */
411 fileid = nfs_fileid_to_ino_t(entry->ino);
412
413 /* Get a dentry if we have one */
414 if (dentry != NULL)
415 dput(dentry);
416 dentry = nfs_readdir_lookup(desc);
417
418 /* Use readdirplus info */
419 if (dentry != NULL && dentry->d_inode != NULL) {
420 d_type = dt_type(dentry->d_inode);
421 fileid = dentry->d_inode->i_ino;
422 }
423
424 res = filldir(dirent, entry->name, entry->len,
Olivier Galibert00a92642005-06-22 17:16:29 +0000425 file->f_pos, fileid, d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (res < 0)
427 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000428 file->f_pos++;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000429 *desc->dir_cookie = entry->cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (dir_decode(desc) != 0) {
431 desc->page_index ++;
432 break;
433 }
434 if (loop_count++ > 200) {
435 loop_count = 0;
436 schedule();
437 }
438 }
439 dir_page_release(desc);
440 if (dentry != NULL)
441 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500442 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
443 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return res;
445}
446
447/*
448 * If we cannot find a cookie in our cache, we suspect that this is
449 * because it points to a deleted file, so we ask the server to return
450 * whatever it thinks is the next entry. We then feed this to filldir.
451 * If all goes well, we should then be able to find our way round the
452 * cache on the next call to readdir_search_pagecache();
453 *
454 * NOTE: we cannot add the anonymous page to the pagecache because
455 * the data it contains might not be page aligned. Besides,
456 * we should already have a complete representation of the
457 * directory in the page cache by the time we get here.
458 */
459static inline
460int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
461 filldir_t filldir)
462{
463 struct file *file = desc->file;
464 struct inode *inode = file->f_dentry->d_inode;
465 struct rpc_cred *cred = nfs_file_cred(file);
466 struct page *page = NULL;
467 int status;
468
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500469 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
470 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 page = alloc_page(GFP_HIGHUSER);
473 if (!page) {
474 status = -ENOMEM;
475 goto out;
476 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000477 desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, *desc->dir_cookie,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 page,
479 NFS_SERVER(inode)->dtsize,
480 desc->plus);
Chuck Leverdc592502005-08-18 11:24:12 -0700481 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700482 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700483 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 desc->page = page;
485 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
486 if (desc->error >= 0) {
487 if ((status = dir_decode(desc)) == 0)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000488 desc->entry->prev_cookie = *desc->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else
490 status = -EIO;
491 if (status < 0)
492 goto out_release;
493
494 status = nfs_do_filldir(desc, dirent, filldir);
495
496 /* Reset read descriptor so it searches the page cache from
497 * the start upon the next call to readdir_search_pagecache() */
498 desc->page_index = 0;
499 desc->entry->cookie = desc->entry->prev_cookie = 0;
500 desc->entry->eof = 0;
501 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500502 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
503 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return status;
505 out_release:
506 dir_page_release(desc);
507 goto out;
508}
509
Olivier Galibert00a92642005-06-22 17:16:29 +0000510/* The file offset position represents the dirent entry number. A
511 last cookie cache takes care of the common case of reading the
512 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
514static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
515{
516 struct dentry *dentry = filp->f_dentry;
517 struct inode *inode = dentry->d_inode;
518 nfs_readdir_descriptor_t my_desc,
519 *desc = &my_desc;
520 struct nfs_entry my_entry;
521 struct nfs_fh fh;
522 struct nfs_fattr fattr;
523 long res;
524
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500525 dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
526 dentry->d_parent->d_name.name, dentry->d_name.name,
527 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500528 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 lock_kernel();
531
Trond Myklebust44b11872006-05-25 01:40:59 -0400532 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (res < 0) {
534 unlock_kernel();
535 return res;
536 }
537
538 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000539 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000540 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000541 * to either find the entry with the appropriate number or
542 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544 memset(desc, 0, sizeof(*desc));
545
546 desc->file = filp;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000547 desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 desc->decode = NFS_PROTO(inode)->decode_dirent;
549 desc->plus = NFS_USE_READDIRPLUS(inode);
550
551 my_entry.cookie = my_entry.prev_cookie = 0;
552 my_entry.eof = 0;
553 my_entry.fh = &fh;
554 my_entry.fattr = &fattr;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400555 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 desc->entry = &my_entry;
557
558 while(!desc->entry->eof) {
559 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (res == -EBADCOOKIE) {
562 /* This means either end of directory */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000563 if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Or that the server has 'lost' a cookie */
565 res = uncached_readdir(desc, dirent, filldir);
566 if (res >= 0)
567 continue;
568 }
569 res = 0;
570 break;
571 }
572 if (res == -ETOOSMALL && desc->plus) {
Chuck Lever412d5822005-08-18 11:24:11 -0700573 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 nfs_zap_caches(inode);
575 desc->plus = 0;
576 desc->entry->eof = 0;
577 continue;
578 }
579 if (res < 0)
580 break;
581
582 res = nfs_do_filldir(desc, dirent, filldir);
583 if (res < 0) {
584 res = 0;
585 break;
586 }
587 }
588 unlock_kernel();
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500589 if (res > 0)
590 res = 0;
591 dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
592 dentry->d_parent->d_name.name, dentry->d_name.name,
593 res);
594 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000597loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
598{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800599 mutex_lock(&filp->f_dentry->d_inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000600 switch (origin) {
601 case 1:
602 offset += filp->f_pos;
603 case 0:
604 if (offset >= 0)
605 break;
606 default:
607 offset = -EINVAL;
608 goto out;
609 }
610 if (offset != filp->f_pos) {
611 filp->f_pos = offset;
612 ((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
613 }
614out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800615 mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000616 return offset;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * All directory operations under NFS are synchronous, so fsync()
621 * is a dummy operation.
622 */
623int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
624{
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500625 dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
626 dentry->d_parent->d_name.name, dentry->d_name.name,
627 datasync);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630}
631
632/*
633 * A check for whether or not the parent directory has changed.
634 * In the case it has, we assume that the dentries are untrustworthy
635 * and may need to be looked up again.
636 */
637static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
638{
639 if (IS_ROOT(dentry))
640 return 1;
Chuck Lever55296802005-08-18 11:24:09 -0700641 if ((NFS_I(dir)->cache_validity & NFS_INO_INVALID_ATTR) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 || nfs_attribute_timeout(dir))
643 return 0;
644 return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
645}
646
647static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
648{
649 dentry->d_fsdata = (void *)verf;
650}
651
652/*
653 * Whenever an NFS operation succeeds, we know that the dentry
654 * is valid, so we update the revalidation timestamp.
655 */
656static inline void nfs_renew_times(struct dentry * dentry)
657{
658 dentry->d_time = jiffies;
659}
660
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400661/*
662 * Return the intent data that applies to this particular path component
663 *
664 * Note that the current set of intents only apply to the very last
665 * component of the path.
666 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
667 */
668static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
669{
670 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
671 return 0;
672 return nd->flags & mask;
673}
674
675/*
676 * Inode and filehandle revalidation for lookups.
677 *
678 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
679 * or if the intent information indicates that we're about to open this
680 * particular file and the "nocto" mount flag is not set.
681 *
682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static inline
684int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
685{
686 struct nfs_server *server = NFS_SERVER(inode);
687
688 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400690 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto out_force;
692 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400693 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400694 !(server->flags & NFS_MOUNT_NOCTO) &&
695 (S_ISREG(inode->i_mode) ||
696 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto out_force;
698 }
699 return nfs_revalidate_inode(server, inode);
700out_force:
701 return __nfs_revalidate_inode(server, inode);
702}
703
704/*
705 * We judge how long we want to trust negative
706 * dentries by looking at the parent inode mtime.
707 *
708 * If parent mtime has changed, we revalidate, else we wait for a
709 * period corresponding to the parent's attribute cache timeout value.
710 */
711static inline
712int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
713 struct nameidata *nd)
714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400716 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718 return !nfs_check_verifier(dir, dentry);
719}
720
721/*
722 * This is called every time the dcache has a lookup hit,
723 * and we should check whether we can really trust that
724 * lookup.
725 *
726 * NOTE! The hit can be a negative hit too, don't assume
727 * we have an inode!
728 *
729 * If the parent directory is seen to have changed, we throw out the
730 * cached dentry and do a new lookup.
731 */
732static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
733{
734 struct inode *dir;
735 struct inode *inode;
736 struct dentry *parent;
737 int error;
738 struct nfs_fh fhandle;
739 struct nfs_fattr fattr;
740 unsigned long verifier;
741
742 parent = dget_parent(dentry);
743 lock_kernel();
744 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -0500745 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 inode = dentry->d_inode;
747
748 if (!inode) {
749 if (nfs_neg_need_reval(dir, dentry, nd))
750 goto out_bad;
751 goto out_valid;
752 }
753
754 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500755 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
756 __FUNCTION__, dentry->d_parent->d_name.name,
757 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goto out_bad;
759 }
760
761 /* Revalidate parent directory attribute cache */
762 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
763 goto out_zap_parent;
764
765 /* Force a full look up iff the parent directory has changed */
766 if (nfs_check_verifier(dir, dentry)) {
767 if (nfs_lookup_verify_inode(inode, nd))
768 goto out_zap_parent;
769 goto out_valid;
770 }
771
772 if (NFS_STALE(inode))
773 goto out_bad;
774
775 verifier = nfs_save_change_attribute(dir);
776 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
777 if (error)
778 goto out_bad;
779 if (nfs_compare_fh(NFS_FH(inode), &fhandle))
780 goto out_bad;
781 if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
782 goto out_bad;
783
784 nfs_renew_times(dentry);
785 nfs_set_verifier(dentry, verifier);
786 out_valid:
787 unlock_kernel();
788 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500789 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
790 __FUNCTION__, dentry->d_parent->d_name.name,
791 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 1;
793out_zap_parent:
794 nfs_zap_caches(dir);
795 out_bad:
796 NFS_CACHEINV(dir);
797 if (inode && S_ISDIR(inode->i_mode)) {
798 /* Purge readdir caches. */
799 nfs_zap_caches(inode);
800 /* If we have submounts, don't unhash ! */
801 if (have_submounts(dentry))
802 goto out_valid;
803 shrink_dcache_parent(dentry);
804 }
805 d_drop(dentry);
806 unlock_kernel();
807 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500808 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
809 __FUNCTION__, dentry->d_parent->d_name.name,
810 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return 0;
812}
813
814/*
815 * This is called from dput() when d_count is going to 0.
816 */
817static int nfs_dentry_delete(struct dentry *dentry)
818{
819 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
820 dentry->d_parent->d_name.name, dentry->d_name.name,
821 dentry->d_flags);
822
823 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
824 /* Unhash it, so that ->d_iput() would be called */
825 return 1;
826 }
827 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
828 /* Unhash it, so that ancestors of killed async unlink
829 * files will be cleaned up during umount */
830 return 1;
831 }
832 return 0;
833
834}
835
836/*
837 * Called when the dentry loses inode.
838 * We use it to clean up silly-renamed files.
839 */
840static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
841{
Trond Myklebustcae7a072005-10-18 14:20:19 -0700842 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
844 lock_kernel();
845 inode->i_nlink--;
846 nfs_complete_unlink(dentry);
847 unlock_kernel();
848 }
849 /* When creating a negative dentry, we want to renew d_time */
850 nfs_renew_times(dentry);
851 iput(inode);
852}
853
854struct dentry_operations nfs_dentry_operations = {
855 .d_revalidate = nfs_lookup_revalidate,
856 .d_delete = nfs_dentry_delete,
857 .d_iput = nfs_dentry_iput,
858};
859
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400860/*
861 * Use intent information to check whether or not we're going to do
862 * an O_EXCL create using this path component.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static inline
865int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
866{
867 if (NFS_PROTO(dir)->version == 2)
868 return 0;
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400869 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871 return (nd->intent.open.flags & O_EXCL) != 0;
872}
873
David Howells54ceac42006-08-22 20:06:13 -0400874static inline int nfs_reval_fsid(struct vfsmount *mnt, struct inode *dir,
875 struct nfs_fh *fh, struct nfs_fattr *fattr)
Trond Myklebust55a97592006-06-09 09:34:19 -0400876{
877 struct nfs_server *server = NFS_SERVER(dir);
878
879 if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
880 /* Revalidate fsid on root dir */
David Howells54ceac42006-08-22 20:06:13 -0400881 return __nfs_revalidate_inode(server, mnt->mnt_root->d_inode);
Trond Myklebust55a97592006-06-09 09:34:19 -0400882 return 0;
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
886{
887 struct dentry *res;
888 struct inode *inode = NULL;
889 int error;
890 struct nfs_fh fhandle;
891 struct nfs_fattr fattr;
892
893 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
894 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -0500895 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 res = ERR_PTR(-ENAMETOOLONG);
898 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
899 goto out;
900
901 res = ERR_PTR(-ENOMEM);
902 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
903
904 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* If we're doing an exclusive create, optimize away the lookup */
907 if (nfs_is_exclusive_create(dir, nd))
908 goto no_entry;
909
910 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
911 if (error == -ENOENT)
912 goto no_entry;
913 if (error < 0) {
914 res = ERR_PTR(error);
915 goto out_unlock;
916 }
David Howells54ceac42006-08-22 20:06:13 -0400917 error = nfs_reval_fsid(nd->mnt, dir, &fhandle, &fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400918 if (error < 0) {
919 res = ERR_PTR(error);
920 goto out_unlock;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500923 res = (struct dentry *)inode;
924 if (IS_ERR(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto out_unlock;
David Howells54ceac42006-08-22 20:06:13 -0400926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927no_entry:
David Howells54ceac42006-08-22 20:06:13 -0400928 res = d_materialise_unique(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (res != NULL)
930 dentry = res;
931 nfs_renew_times(dentry);
932 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
933out_unlock:
934 unlock_kernel();
935out:
936 return res;
937}
938
939#ifdef CONFIG_NFS_V4
940static int nfs_open_revalidate(struct dentry *, struct nameidata *);
941
942struct dentry_operations nfs4_dentry_operations = {
943 .d_revalidate = nfs_open_revalidate,
944 .d_delete = nfs_dentry_delete,
945 .d_iput = nfs_dentry_iput,
946};
947
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400948/*
949 * Use intent information to determine whether we need to substitute
950 * the NFSv4-style stateful OPEN for the LOOKUP call
951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static int is_atomic_open(struct inode *dir, struct nameidata *nd)
953{
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400954 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956 /* NFS does not (yet) have a stateful open for directories */
957 if (nd->flags & LOOKUP_DIRECTORY)
958 return 0;
959 /* Are we trying to write to a read only partition? */
960 if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
961 return 0;
962 return 1;
963}
964
965static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
966{
967 struct dentry *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int error;
969
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500970 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
971 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* Check that we are indeed trying to open this file */
974 if (!is_atomic_open(dir, nd))
975 goto no_open;
976
977 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
978 res = ERR_PTR(-ENAMETOOLONG);
979 goto out;
980 }
981 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
982
983 /* Let vfs_create() deal with O_EXCL */
Trond Myklebust02a913a2005-10-18 14:20:17 -0700984 if (nd->intent.open.flags & O_EXCL) {
985 d_add(dentry, NULL);
986 goto out;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* Open the file on the server */
990 lock_kernel();
991 /* Revalidate parent directory attribute cache */
992 error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
993 if (error < 0) {
994 res = ERR_PTR(error);
Steve Dickson01c314a2005-08-19 17:57:48 -0700995 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto out;
997 }
998
999 if (nd->intent.open.flags & O_CREAT) {
1000 nfs_begin_data_update(dir);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001001 res = nfs4_atomic_open(dir, dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 nfs_end_data_update(dir);
1003 } else
Trond Myklebust02a913a2005-10-18 14:20:17 -07001004 res = nfs4_atomic_open(dir, dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unlock_kernel();
Trond Myklebust02a913a2005-10-18 14:20:17 -07001006 if (IS_ERR(res)) {
1007 error = PTR_ERR(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 switch (error) {
1009 /* Make a negative dentry */
1010 case -ENOENT:
Trond Myklebust02a913a2005-10-18 14:20:17 -07001011 res = NULL;
1012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* This turned out not to be a regular file */
Trond Myklebust6f926b52005-10-18 14:20:18 -07001014 case -EISDIR:
1015 case -ENOTDIR:
1016 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 case -ELOOP:
1018 if (!(nd->intent.open.flags & O_NOFOLLOW))
1019 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* case -EINVAL: */
1021 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto out;
1023 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001024 } else if (res != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 dentry = res;
1026 nfs_renew_times(dentry);
1027 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1028out:
1029 return res;
1030no_open:
1031 return nfs_lookup(dir, dentry, nd);
1032}
1033
1034static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1035{
1036 struct dentry *parent = NULL;
1037 struct inode *inode = dentry->d_inode;
1038 struct inode *dir;
1039 unsigned long verifier;
1040 int openflags, ret = 0;
1041
1042 parent = dget_parent(dentry);
1043 dir = parent->d_inode;
1044 if (!is_atomic_open(dir, nd))
1045 goto no_open;
1046 /* We can't create new files in nfs_open_revalidate(), so we
1047 * optimize away revalidation of negative dentries.
1048 */
1049 if (inode == NULL)
1050 goto out;
1051 /* NFS only supports OPEN on regular files */
1052 if (!S_ISREG(inode->i_mode))
1053 goto no_open;
1054 openflags = nd->intent.open.flags;
1055 /* We cannot do exclusive creation on a positive dentry */
1056 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1057 goto no_open;
1058 /* We can't create new files, or truncate existing ones here */
1059 openflags &= ~(O_CREAT|O_TRUNC);
1060
1061 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001062 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * operations that change the directory. We therefore save the
1064 * change attribute *before* we do the RPC call.
1065 */
1066 lock_kernel();
1067 verifier = nfs_save_change_attribute(dir);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001068 ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!ret)
1070 nfs_set_verifier(dentry, verifier);
1071 unlock_kernel();
1072out:
1073 dput(parent);
1074 if (!ret)
1075 d_drop(dentry);
1076 return ret;
1077no_open:
1078 dput(parent);
1079 if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
1080 return 1;
1081 return nfs_lookup_revalidate(dentry, nd);
1082}
1083#endif /* CONFIG_NFSV4 */
1084
1085static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
1086{
1087 struct dentry *parent = desc->file->f_dentry;
1088 struct inode *dir = parent->d_inode;
1089 struct nfs_entry *entry = desc->entry;
1090 struct dentry *dentry, *alias;
1091 struct qstr name = {
1092 .name = entry->name,
1093 .len = entry->len,
1094 };
1095 struct inode *inode;
1096
1097 switch (name.len) {
1098 case 2:
1099 if (name.name[0] == '.' && name.name[1] == '.')
1100 return dget_parent(parent);
1101 break;
1102 case 1:
1103 if (name.name[0] == '.')
1104 return dget(parent);
1105 }
1106 name.hash = full_name_hash(name.name, name.len);
1107 dentry = d_lookup(parent, &name);
1108 if (dentry != NULL)
1109 return dentry;
1110 if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
1111 return NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001112 /* Note: caller is already holding the dir->i_mutex! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 dentry = d_alloc(parent, &name);
1114 if (dentry == NULL)
1115 return NULL;
1116 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1117 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001118 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 dput(dentry);
1120 return NULL;
1121 }
David Howells54ceac42006-08-22 20:06:13 -04001122
1123 alias = d_materialise_unique(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (alias != NULL) {
1125 dput(dentry);
1126 dentry = alias;
1127 }
David Howells54ceac42006-08-22 20:06:13 -04001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 nfs_renew_times(dentry);
1130 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1131 return dentry;
1132}
1133
1134/*
1135 * Code common to create, mkdir, and mknod.
1136 */
1137int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1138 struct nfs_fattr *fattr)
1139{
1140 struct inode *inode;
1141 int error = -EACCES;
1142
1143 /* We may have been initialized further down */
1144 if (dentry->d_inode)
1145 return 0;
1146 if (fhandle->size == 0) {
1147 struct inode *dir = dentry->d_parent->d_inode;
1148 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1149 if (error)
Chuck Lever4f390c12006-08-22 20:06:22 -04001150 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1153 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001154 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (error < 0)
Chuck Lever4f390c12006-08-22 20:06:22 -04001156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001159 error = PTR_ERR(inode);
1160 if (IS_ERR(inode))
Chuck Lever4f390c12006-08-22 20:06:22 -04001161 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 d_instantiate(dentry, inode);
1163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166/*
1167 * Following a failed create operation, we drop the dentry rather
1168 * than retain a negative dentry. This avoids a problem in the event
1169 * that the operation succeeded on the server, but an error in the
1170 * reply path made it appear to have failed.
1171 */
1172static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1173 struct nameidata *nd)
1174{
1175 struct iattr attr;
1176 int error;
1177 int open_flags = 0;
1178
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001179 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1180 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 attr.ia_mode = mode;
1183 attr.ia_valid = ATTR_MODE;
1184
1185 if (nd && (nd->flags & LOOKUP_CREATE))
1186 open_flags = nd->intent.open.flags;
1187
1188 lock_kernel();
1189 nfs_begin_data_update(dir);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001190 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 nfs_end_data_update(dir);
1192 if (error != 0)
1193 goto out_err;
1194 nfs_renew_times(dentry);
1195 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1196 unlock_kernel();
1197 return 0;
1198out_err:
1199 unlock_kernel();
1200 d_drop(dentry);
1201 return error;
1202}
1203
1204/*
1205 * See comments for nfs_proc_create regarding failed operations.
1206 */
1207static int
1208nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1209{
1210 struct iattr attr;
1211 int status;
1212
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001213 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1214 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (!new_valid_dev(rdev))
1217 return -EINVAL;
1218
1219 attr.ia_mode = mode;
1220 attr.ia_valid = ATTR_MODE;
1221
1222 lock_kernel();
1223 nfs_begin_data_update(dir);
1224 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1225 nfs_end_data_update(dir);
1226 if (status != 0)
1227 goto out_err;
1228 nfs_renew_times(dentry);
1229 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1230 unlock_kernel();
1231 return 0;
1232out_err:
1233 unlock_kernel();
1234 d_drop(dentry);
1235 return status;
1236}
1237
1238/*
1239 * See comments for nfs_proc_create regarding failed operations.
1240 */
1241static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1242{
1243 struct iattr attr;
1244 int error;
1245
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001246 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1247 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 attr.ia_valid = ATTR_MODE;
1250 attr.ia_mode = mode | S_IFDIR;
1251
1252 lock_kernel();
1253 nfs_begin_data_update(dir);
1254 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1255 nfs_end_data_update(dir);
1256 if (error != 0)
1257 goto out_err;
1258 nfs_renew_times(dentry);
1259 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1260 unlock_kernel();
1261 return 0;
1262out_err:
1263 d_drop(dentry);
1264 unlock_kernel();
1265 return error;
1266}
1267
1268static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1269{
1270 int error;
1271
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001272 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1273 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 lock_kernel();
1276 nfs_begin_data_update(dir);
1277 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1278 /* Ensure the VFS deletes this inode */
1279 if (error == 0 && dentry->d_inode != NULL)
1280 dentry->d_inode->i_nlink = 0;
1281 nfs_end_data_update(dir);
1282 unlock_kernel();
1283
1284 return error;
1285}
1286
1287static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
1288{
1289 static unsigned int sillycounter;
1290 const int i_inosize = sizeof(dir->i_ino)*2;
1291 const int countersize = sizeof(sillycounter)*2;
1292 const int slen = sizeof(".nfs") + i_inosize + countersize - 1;
1293 char silly[slen+1];
1294 struct qstr qsilly;
1295 struct dentry *sdentry;
1296 int error = -EIO;
1297
1298 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
1299 dentry->d_parent->d_name.name, dentry->d_name.name,
1300 atomic_read(&dentry->d_count));
Chuck Lever91d5b472006-03-20 13:44:14 -05001301 nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303#ifdef NFS_PARANOIA
1304if (!dentry->d_inode)
1305printk("NFS: silly-renaming %s/%s, negative dentry??\n",
1306dentry->d_parent->d_name.name, dentry->d_name.name);
1307#endif
1308 /*
1309 * We don't allow a dentry to be silly-renamed twice.
1310 */
1311 error = -EBUSY;
1312 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1313 goto out;
1314
1315 sprintf(silly, ".nfs%*.*lx",
1316 i_inosize, i_inosize, dentry->d_inode->i_ino);
1317
Trond Myklebust34ea8182005-11-04 15:35:02 -05001318 /* Return delegation in anticipation of the rename */
1319 nfs_inode_return_delegation(dentry->d_inode);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sdentry = NULL;
1322 do {
1323 char *suffix = silly + slen - countersize;
1324
1325 dput(sdentry);
1326 sillycounter++;
1327 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
1328
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001329 dfprintk(VFS, "NFS: trying to rename %s to %s\n",
1330 dentry->d_name.name, silly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
1333 /*
1334 * N.B. Better to return EBUSY here ... it could be
1335 * dangerous to delete the file while it's in use.
1336 */
1337 if (IS_ERR(sdentry))
1338 goto out;
1339 } while(sdentry->d_inode != NULL); /* need negative lookup */
1340
1341 qsilly.name = silly;
1342 qsilly.len = strlen(silly);
1343 nfs_begin_data_update(dir);
1344 if (dentry->d_inode) {
1345 nfs_begin_data_update(dentry->d_inode);
1346 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1347 dir, &qsilly);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001348 nfs_mark_for_revalidate(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 nfs_end_data_update(dentry->d_inode);
1350 } else
1351 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1352 dir, &qsilly);
1353 nfs_end_data_update(dir);
1354 if (!error) {
1355 nfs_renew_times(dentry);
1356 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1357 d_move(dentry, sdentry);
1358 error = nfs_async_unlink(dentry);
1359 /* If we return 0 we don't unlink */
1360 }
1361 dput(sdentry);
1362out:
1363 return error;
1364}
1365
1366/*
1367 * Remove a file after making sure there are no pending writes,
1368 * and after checking that the file has only one user.
1369 *
1370 * We invalidate the attribute cache and free the inode prior to the operation
1371 * to avoid possible races if the server reuses the inode.
1372 */
1373static int nfs_safe_remove(struct dentry *dentry)
1374{
1375 struct inode *dir = dentry->d_parent->d_inode;
1376 struct inode *inode = dentry->d_inode;
1377 int error = -EBUSY;
1378
1379 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1380 dentry->d_parent->d_name.name, dentry->d_name.name);
1381
1382 /* If the dentry was sillyrenamed, we simply call d_delete() */
1383 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1384 error = 0;
1385 goto out;
1386 }
1387
1388 nfs_begin_data_update(dir);
1389 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001390 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 nfs_begin_data_update(inode);
1392 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1393 /* The VFS may want to delete this inode */
1394 if (error == 0)
1395 inode->i_nlink--;
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001396 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 nfs_end_data_update(inode);
1398 } else
1399 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1400 nfs_end_data_update(dir);
1401out:
1402 return error;
1403}
1404
1405/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1406 * belongs to an active ".nfs..." file and we return -EBUSY.
1407 *
1408 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1409 */
1410static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1411{
1412 int error;
1413 int need_rehash = 0;
1414
1415 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1416 dir->i_ino, dentry->d_name.name);
1417
1418 lock_kernel();
1419 spin_lock(&dcache_lock);
1420 spin_lock(&dentry->d_lock);
1421 if (atomic_read(&dentry->d_count) > 1) {
1422 spin_unlock(&dentry->d_lock);
1423 spin_unlock(&dcache_lock);
1424 error = nfs_sillyrename(dir, dentry);
1425 unlock_kernel();
1426 return error;
1427 }
1428 if (!d_unhashed(dentry)) {
1429 __d_drop(dentry);
1430 need_rehash = 1;
1431 }
1432 spin_unlock(&dentry->d_lock);
1433 spin_unlock(&dcache_lock);
1434 error = nfs_safe_remove(dentry);
1435 if (!error) {
1436 nfs_renew_times(dentry);
1437 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1438 } else if (need_rehash)
1439 d_rehash(dentry);
1440 unlock_kernel();
1441 return error;
1442}
1443
1444static int
1445nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1446{
1447 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct qstr qsymname;
1449 int error;
1450
1451 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1452 dir->i_ino, dentry->d_name.name, symname);
1453
1454#ifdef NFS_PARANOIA
1455if (dentry->d_inode)
1456printk("nfs_proc_symlink: %s/%s not negative!\n",
1457dentry->d_parent->d_name.name, dentry->d_name.name);
1458#endif
1459 /*
1460 * Fill in the sattr for the call.
1461 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1462 */
1463 attr.ia_valid = ATTR_MODE;
1464 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1465
1466 qsymname.name = symname;
1467 qsymname.len = strlen(symname);
1468
1469 lock_kernel();
1470 nfs_begin_data_update(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04001471 error = NFS_PROTO(dir)->symlink(dir, dentry, &qsymname, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 nfs_end_data_update(dir);
Chuck Leverd3db90e2006-08-22 20:06:22 -04001473 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 unlock_kernel();
1476 return error;
1477}
1478
1479static int
1480nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1481{
1482 struct inode *inode = old_dentry->d_inode;
1483 int error;
1484
1485 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1486 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1487 dentry->d_parent->d_name.name, dentry->d_name.name);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 nfs_begin_data_update(dir);
1491 nfs_begin_data_update(inode);
1492 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001493 if (error == 0) {
1494 atomic_inc(&inode->i_count);
1495 d_instantiate(dentry, inode);
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 nfs_end_data_update(inode);
1498 nfs_end_data_update(dir);
1499 unlock_kernel();
1500 return error;
1501}
1502
1503/*
1504 * RENAME
1505 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1506 * different file handle for the same inode after a rename (e.g. when
1507 * moving to a different directory). A fail-safe method to do so would
1508 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1509 * rename the old file using the sillyrename stuff. This way, the original
1510 * file in old_dir will go away when the last process iput()s the inode.
1511 *
1512 * FIXED.
1513 *
1514 * It actually works quite well. One needs to have the possibility for
1515 * at least one ".nfs..." file in each directory the file ever gets
1516 * moved or linked to which happens automagically with the new
1517 * implementation that only depends on the dcache stuff instead of
1518 * using the inode layer
1519 *
1520 * Unfortunately, things are a little more complicated than indicated
1521 * above. For a cross-directory move, we want to make sure we can get
1522 * rid of the old inode after the operation. This means there must be
1523 * no pending writes (if it's a file), and the use count must be 1.
1524 * If these conditions are met, we can drop the dentries before doing
1525 * the rename.
1526 */
1527static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1528 struct inode *new_dir, struct dentry *new_dentry)
1529{
1530 struct inode *old_inode = old_dentry->d_inode;
1531 struct inode *new_inode = new_dentry->d_inode;
1532 struct dentry *dentry = NULL, *rehash = NULL;
1533 int error = -EBUSY;
1534
1535 /*
1536 * To prevent any new references to the target during the rename,
1537 * we unhash the dentry and free the inode in advance.
1538 */
1539 lock_kernel();
1540 if (!d_unhashed(new_dentry)) {
1541 d_drop(new_dentry);
1542 rehash = new_dentry;
1543 }
1544
1545 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1546 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1547 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1548 atomic_read(&new_dentry->d_count));
1549
1550 /*
1551 * First check whether the target is busy ... we can't
1552 * safely do _any_ rename if the target is in use.
1553 *
1554 * For files, make a copy of the dentry and then do a
1555 * silly-rename. If the silly-rename succeeds, the
1556 * copied dentry is hashed and becomes the new target.
1557 */
1558 if (!new_inode)
1559 goto go_ahead;
Trond Myklebust6fe43f92005-10-18 14:20:22 -07001560 if (S_ISDIR(new_inode->i_mode)) {
1561 error = -EISDIR;
1562 if (!S_ISDIR(old_inode->i_mode))
1563 goto out;
1564 } else if (atomic_read(&new_dentry->d_count) > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int err;
1566 /* copy the target dentry's name */
1567 dentry = d_alloc(new_dentry->d_parent,
1568 &new_dentry->d_name);
1569 if (!dentry)
1570 goto out;
1571
1572 /* silly-rename the existing target ... */
1573 err = nfs_sillyrename(new_dir, new_dentry);
1574 if (!err) {
1575 new_dentry = rehash = dentry;
1576 new_inode = NULL;
1577 /* instantiate the replacement target */
1578 d_instantiate(new_dentry, NULL);
1579 } else if (atomic_read(&new_dentry->d_count) > 1) {
1580 /* dentry still busy? */
1581#ifdef NFS_PARANOIA
1582 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1583 new_dentry->d_parent->d_name.name,
1584 new_dentry->d_name.name,
1585 atomic_read(&new_dentry->d_count));
1586#endif
1587 goto out;
1588 }
Trond Myklebust20509f12005-08-25 16:25:34 -07001589 } else
1590 new_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592go_ahead:
1593 /*
1594 * ... prune child dentries and writebacks if needed.
1595 */
1596 if (atomic_read(&old_dentry->d_count) > 1) {
1597 nfs_wb_all(old_inode);
1598 shrink_dcache_parent(old_dentry);
1599 }
Trond Myklebustcae7a072005-10-18 14:20:19 -07001600 nfs_inode_return_delegation(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Trond Myklebust24174112006-01-03 09:55:33 +01001602 if (new_inode != NULL) {
1603 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 d_delete(new_dentry);
Trond Myklebust24174112006-01-03 09:55:33 +01001605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 nfs_begin_data_update(old_dir);
1608 nfs_begin_data_update(new_dir);
1609 nfs_begin_data_update(old_inode);
1610 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1611 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001612 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 nfs_end_data_update(old_inode);
1614 nfs_end_data_update(new_dir);
1615 nfs_end_data_update(old_dir);
1616out:
1617 if (rehash)
1618 d_rehash(rehash);
1619 if (!error) {
1620 if (!S_ISDIR(old_inode->i_mode))
1621 d_move(old_dentry, new_dentry);
1622 nfs_renew_times(new_dentry);
1623 nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
1624 }
1625
1626 /* new dentry created? */
1627 if (dentry)
1628 dput(dentry);
1629 unlock_kernel();
1630 return error;
1631}
1632
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001633static DEFINE_SPINLOCK(nfs_access_lru_lock);
1634static LIST_HEAD(nfs_access_lru_list);
1635static atomic_long_t nfs_access_nr_entries;
1636
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001637static void nfs_access_free_entry(struct nfs_access_entry *entry)
1638{
1639 put_rpccred(entry->cred);
1640 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001641 smp_mb__before_atomic_dec();
1642 atomic_long_dec(&nfs_access_nr_entries);
1643 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001644}
1645
Trond Myklebust979df722006-07-25 11:28:19 -04001646int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
1647{
1648 LIST_HEAD(head);
1649 struct nfs_inode *nfsi;
1650 struct nfs_access_entry *cache;
1651
1652 spin_lock(&nfs_access_lru_lock);
1653restart:
1654 list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
1655 struct inode *inode;
1656
1657 if (nr_to_scan-- == 0)
1658 break;
1659 inode = igrab(&nfsi->vfs_inode);
1660 if (inode == NULL)
1661 continue;
1662 spin_lock(&inode->i_lock);
1663 if (list_empty(&nfsi->access_cache_entry_lru))
1664 goto remove_lru_entry;
1665 cache = list_entry(nfsi->access_cache_entry_lru.next,
1666 struct nfs_access_entry, lru);
1667 list_move(&cache->lru, &head);
1668 rb_erase(&cache->rb_node, &nfsi->access_cache);
1669 if (!list_empty(&nfsi->access_cache_entry_lru))
1670 list_move_tail(&nfsi->access_cache_inode_lru,
1671 &nfs_access_lru_list);
1672 else {
1673remove_lru_entry:
1674 list_del_init(&nfsi->access_cache_inode_lru);
1675 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
1676 }
1677 spin_unlock(&inode->i_lock);
1678 iput(inode);
1679 goto restart;
1680 }
1681 spin_unlock(&nfs_access_lru_lock);
1682 while (!list_empty(&head)) {
1683 cache = list_entry(head.next, struct nfs_access_entry, lru);
1684 list_del(&cache->lru);
1685 nfs_access_free_entry(cache);
1686 }
1687 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1688}
1689
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001690static void __nfs_access_zap_cache(struct inode *inode)
1691{
1692 struct nfs_inode *nfsi = NFS_I(inode);
1693 struct rb_root *root_node = &nfsi->access_cache;
1694 struct rb_node *n, *dispose = NULL;
1695 struct nfs_access_entry *entry;
1696
1697 /* Unhook entries from the cache */
1698 while ((n = rb_first(root_node)) != NULL) {
1699 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1700 rb_erase(n, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001701 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001702 n->rb_left = dispose;
1703 dispose = n;
1704 }
1705 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
1706 spin_unlock(&inode->i_lock);
1707
1708 /* Now kill them all! */
1709 while (dispose != NULL) {
1710 n = dispose;
1711 dispose = n->rb_left;
1712 nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
1713 }
1714}
1715
1716void nfs_access_zap_cache(struct inode *inode)
1717{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001718 /* Remove from global LRU init */
1719 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1720 spin_lock(&nfs_access_lru_lock);
1721 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1722 spin_unlock(&nfs_access_lru_lock);
1723 }
1724
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001725 spin_lock(&inode->i_lock);
1726 /* This will release the spinlock */
1727 __nfs_access_zap_cache(inode);
1728}
1729
1730static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
1731{
1732 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
1733 struct nfs_access_entry *entry;
1734
1735 while (n != NULL) {
1736 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1737
1738 if (cred < entry->cred)
1739 n = n->rb_left;
1740 else if (cred > entry->cred)
1741 n = n->rb_right;
1742 else
1743 return entry;
1744 }
1745 return NULL;
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
1749{
Chuck Lever55296802005-08-18 11:24:09 -07001750 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001751 struct nfs_access_entry *cache;
1752 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001754 spin_lock(&inode->i_lock);
1755 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
1756 goto out_zap;
1757 cache = nfs_access_search_rbtree(inode, cred);
1758 if (cache == NULL)
1759 goto out;
1760 if (time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
1761 goto out_stale;
1762 res->jiffies = cache->jiffies;
1763 res->cred = cache->cred;
1764 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001765 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001766 err = 0;
1767out:
1768 spin_unlock(&inode->i_lock);
1769 return err;
1770out_stale:
1771 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001772 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001773 spin_unlock(&inode->i_lock);
1774 nfs_access_free_entry(cache);
1775 return -ENOENT;
1776out_zap:
1777 /* This will release the spinlock */
1778 __nfs_access_zap_cache(inode);
1779 return -ENOENT;
1780}
1781
1782static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
1783{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001784 struct nfs_inode *nfsi = NFS_I(inode);
1785 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001786 struct rb_node **p = &root_node->rb_node;
1787 struct rb_node *parent = NULL;
1788 struct nfs_access_entry *entry;
1789
1790 spin_lock(&inode->i_lock);
1791 while (*p != NULL) {
1792 parent = *p;
1793 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
1794
1795 if (set->cred < entry->cred)
1796 p = &parent->rb_left;
1797 else if (set->cred > entry->cred)
1798 p = &parent->rb_right;
1799 else
1800 goto found;
1801 }
1802 rb_link_node(&set->rb_node, parent, p);
1803 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001804 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001805 spin_unlock(&inode->i_lock);
1806 return;
1807found:
1808 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001809 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
1810 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001811 spin_unlock(&inode->i_lock);
1812 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1816{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001817 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
1818 if (cache == NULL)
1819 return;
1820 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001822 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001824
1825 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001826
1827 /* Update accounting */
1828 smp_mb__before_atomic_inc();
1829 atomic_long_inc(&nfs_access_nr_entries);
1830 smp_mb__after_atomic_inc();
1831
1832 /* Add inode to global LRU list */
1833 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1834 spin_lock(&nfs_access_lru_lock);
1835 list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
1836 spin_unlock(&nfs_access_lru_lock);
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
1841{
1842 struct nfs_access_entry cache;
1843 int status;
1844
1845 status = nfs_access_get_cached(inode, cred, &cache);
1846 if (status == 0)
1847 goto out;
1848
1849 /* Be clever: ask server to check for all possible rights */
1850 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
1851 cache.cred = cred;
1852 cache.jiffies = jiffies;
1853 status = NFS_PROTO(inode)->access(inode, &cache);
1854 if (status != 0)
1855 return status;
1856 nfs_access_add_cache(inode, &cache);
1857out:
1858 if ((cache.mask & mask) == mask)
1859 return 0;
1860 return -EACCES;
1861}
1862
1863int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1864{
1865 struct rpc_cred *cred;
1866 int res = 0;
1867
Chuck Lever91d5b472006-03-20 13:44:14 -05001868 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (mask == 0)
1871 goto out;
1872 /* Is this sys_access() ? */
1873 if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
1874 goto force_lookup;
1875
1876 switch (inode->i_mode & S_IFMT) {
1877 case S_IFLNK:
1878 goto out;
1879 case S_IFREG:
1880 /* NFSv4 has atomic_open... */
1881 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
1882 && nd != NULL
1883 && (nd->flags & LOOKUP_OPEN))
1884 goto out;
1885 break;
1886 case S_IFDIR:
1887 /*
1888 * Optimize away all write operations, since the server
1889 * will check permissions when we perform the op.
1890 */
1891 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
1892 goto out;
1893 }
1894
1895force_lookup:
1896 lock_kernel();
1897
1898 if (!NFS_PROTO(inode)->access)
1899 goto out_notsup;
1900
1901 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1902 if (!IS_ERR(cred)) {
1903 res = nfs_do_access(inode, cred, mask);
1904 put_rpccred(cred);
1905 } else
1906 res = PTR_ERR(cred);
1907 unlock_kernel();
1908out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001909 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
1910 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return res;
1912out_notsup:
1913 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1914 if (res == 0)
1915 res = generic_permission(inode, mask, NULL);
1916 unlock_kernel();
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
1920/*
1921 * Local variables:
1922 * version-control: t
1923 * kept-new-versions: 5
1924 * End:
1925 */