blob: bf4f5ffda703c5f28ac6446bc6a7bbf4d6c98162 [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>
34
Trond Myklebust4ce79712005-06-22 17:16:21 +000035#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050037#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define NFS_PARANOIA 1
40/* #define NFS_DEBUG_VERBOSE 1 */
41
42static int nfs_opendir(struct inode *, struct file *);
43static int nfs_readdir(struct file *, void *, filldir_t);
44static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
45static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
46static int nfs_mkdir(struct inode *, struct dentry *, int);
47static int nfs_rmdir(struct inode *, struct dentry *);
48static int nfs_unlink(struct inode *, struct dentry *);
49static int nfs_symlink(struct inode *, struct dentry *, const char *);
50static int nfs_link(struct dentry *, struct inode *, struct dentry *);
51static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
52static int nfs_rename(struct inode *, struct dentry *,
53 struct inode *, struct dentry *);
54static int nfs_fsync_dir(struct file *, struct dentry *, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000055static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080057const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000058 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .read = generic_read_dir,
60 .readdir = nfs_readdir,
61 .open = nfs_opendir,
62 .release = nfs_release,
63 .fsync = nfs_fsync_dir,
64};
65
66struct inode_operations nfs_dir_inode_operations = {
67 .create = nfs_create,
68 .lookup = nfs_lookup,
69 .link = nfs_link,
70 .unlink = nfs_unlink,
71 .symlink = nfs_symlink,
72 .mkdir = nfs_mkdir,
73 .rmdir = nfs_rmdir,
74 .mknod = nfs_mknod,
75 .rename = nfs_rename,
76 .permission = nfs_permission,
77 .getattr = nfs_getattr,
78 .setattr = nfs_setattr,
79};
80
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000081#ifdef CONFIG_NFS_V3
82struct inode_operations nfs3_dir_inode_operations = {
83 .create = nfs_create,
84 .lookup = nfs_lookup,
85 .link = nfs_link,
86 .unlink = nfs_unlink,
87 .symlink = nfs_symlink,
88 .mkdir = nfs_mkdir,
89 .rmdir = nfs_rmdir,
90 .mknod = nfs_mknod,
91 .rename = nfs_rename,
92 .permission = nfs_permission,
93 .getattr = nfs_getattr,
94 .setattr = nfs_setattr,
95 .listxattr = nfs3_listxattr,
96 .getxattr = nfs3_getxattr,
97 .setxattr = nfs3_setxattr,
98 .removexattr = nfs3_removexattr,
99};
100#endif /* CONFIG_NFS_V3 */
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#ifdef CONFIG_NFS_V4
103
104static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
105struct inode_operations nfs4_dir_inode_operations = {
106 .create = nfs_create,
107 .lookup = nfs_atomic_lookup,
108 .link = nfs_link,
109 .unlink = nfs_unlink,
110 .symlink = nfs_symlink,
111 .mkdir = nfs_mkdir,
112 .rmdir = nfs_rmdir,
113 .mknod = nfs_mknod,
114 .rename = nfs_rename,
115 .permission = nfs_permission,
116 .getattr = nfs_getattr,
117 .setattr = nfs_setattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +0000118 .getxattr = nfs4_getxattr,
119 .setxattr = nfs4_setxattr,
120 .listxattr = nfs4_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123#endif /* CONFIG_NFS_V4 */
124
125/*
126 * Open file
127 */
128static int
129nfs_opendir(struct inode *inode, struct file *filp)
130{
Carsten Otte7451c4f2006-04-19 13:06:37 -0400131 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500133 dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
134 inode->i_sb->s_id, inode->i_ino);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 lock_kernel();
137 /* Call generic open code in order to cache credentials */
Carsten Otte7451c4f2006-04-19 13:06:37 -0400138 res = nfs_open(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unlock_kernel();
140 return res;
141}
142
143typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
144typedef struct {
145 struct file *file;
146 struct page *page;
147 unsigned long page_index;
148 u32 *ptr;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000149 u64 *dir_cookie;
150 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct nfs_entry *entry;
152 decode_dirent_t decode;
153 int plus;
154 int error;
155} nfs_readdir_descriptor_t;
156
157/* Now we cache directories properly, by stuffing the dirent
158 * data directly in the page cache.
159 *
160 * Inode invalidation due to refresh etc. takes care of
161 * _everything_, no sloppy entry flushing logic, no extraneous
162 * copying, network direct to page cache, the way it was meant
163 * to be.
164 *
165 * NOTE: Dirent information verification is done always by the
166 * page-in of the RPC reply, nowhere else, this simplies
167 * things substantially.
168 */
169static
170int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
171{
172 struct file *file = desc->file;
173 struct inode *inode = file->f_dentry->d_inode;
174 struct rpc_cred *cred = nfs_file_cred(file);
175 unsigned long timestamp;
176 int error;
177
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500178 dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
179 __FUNCTION__, (long long)desc->entry->cookie,
180 page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 again:
183 timestamp = jiffies;
184 error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
185 NFS_SERVER(inode)->dtsize, desc->plus);
186 if (error < 0) {
187 /* We requested READDIRPLUS, but the server doesn't grok it */
188 if (error == -ENOTSUPP && desc->plus) {
189 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Chuck Lever412d5822005-08-18 11:24:11 -0700190 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 desc->plus = 0;
192 goto again;
193 }
194 goto error;
195 }
196 SetPageUptodate(page);
Chuck Leverdc592502005-08-18 11:24:12 -0700197 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700198 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700199 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Ensure consistent page alignment of the data.
201 * Note: assumes we have exclusive access to this mapping either
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800202 * through inode->i_mutex or some other mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
Trond Myklebusta656db92005-06-22 17:16:21 +0000204 if (page->index == 0)
205 invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 unlock_page(page);
207 return 0;
208 error:
209 SetPageError(page);
210 unlock_page(page);
211 nfs_zap_caches(inode);
212 desc->error = error;
213 return -EIO;
214}
215
216static inline
217int dir_decode(nfs_readdir_descriptor_t *desc)
218{
219 u32 *p = desc->ptr;
220 p = desc->decode(p, desc->entry, desc->plus);
221 if (IS_ERR(p))
222 return PTR_ERR(p);
223 desc->ptr = p;
224 return 0;
225}
226
227static inline
228void dir_page_release(nfs_readdir_descriptor_t *desc)
229{
230 kunmap(desc->page);
231 page_cache_release(desc->page);
232 desc->page = NULL;
233 desc->ptr = NULL;
234}
235
236/*
237 * Given a pointer to a buffer that has already been filled by a call
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000238 * to readdir, find the next entry with cookie '*desc->dir_cookie'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
240 * If the end of the buffer has been reached, return -EAGAIN, if not,
241 * return the offset within the buffer of the next entry to be
242 * read.
243 */
244static inline
Olivier Galibert00a92642005-06-22 17:16:29 +0000245int find_dirent(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct nfs_entry *entry = desc->entry;
248 int loop_count = 0,
249 status;
250
251 while((status = dir_decode(desc)) == 0) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500252 dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
253 __FUNCTION__, (unsigned long long)entry->cookie);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000254 if (entry->prev_cookie == *desc->dir_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256 if (loop_count++ > 200) {
257 loop_count = 0;
258 schedule();
259 }
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return status;
262}
263
264/*
Olivier Galibert00a92642005-06-22 17:16:29 +0000265 * Given a pointer to a buffer that has already been filled by a call
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000266 * to readdir, find the entry at offset 'desc->file->f_pos'.
Olivier Galibert00a92642005-06-22 17:16:29 +0000267 *
268 * If the end of the buffer has been reached, return -EAGAIN, if not,
269 * return the offset within the buffer of the next entry to be
270 * read.
271 */
272static inline
273int find_dirent_index(nfs_readdir_descriptor_t *desc)
274{
275 struct nfs_entry *entry = desc->entry;
276 int loop_count = 0,
277 status;
278
279 for(;;) {
280 status = dir_decode(desc);
281 if (status)
282 break;
283
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500284 dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
285 (unsigned long long)entry->cookie, desc->current_index);
Olivier Galibert00a92642005-06-22 17:16:29 +0000286
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000287 if (desc->file->f_pos == desc->current_index) {
288 *desc->dir_cookie = entry->cookie;
Olivier Galibert00a92642005-06-22 17:16:29 +0000289 break;
290 }
291 desc->current_index++;
292 if (loop_count++ > 200) {
293 loop_count = 0;
294 schedule();
295 }
296 }
Olivier Galibert00a92642005-06-22 17:16:29 +0000297 return status;
298}
299
300/*
301 * Find the given page, and call find_dirent() or find_dirent_index in
302 * order to try to return the next entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304static inline
305int find_dirent_page(nfs_readdir_descriptor_t *desc)
306{
307 struct inode *inode = desc->file->f_dentry->d_inode;
308 struct page *page;
309 int status;
310
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500311 dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
312 __FUNCTION__, desc->page_index,
313 (long long) *desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 page = read_cache_page(inode->i_mapping, desc->page_index,
316 (filler_t *)nfs_readdir_filler, desc);
317 if (IS_ERR(page)) {
318 status = PTR_ERR(page);
319 goto out;
320 }
321 if (!PageUptodate(page))
322 goto read_error;
323
324 /* NOTE: Someone else may have changed the READDIRPLUS flag */
325 desc->page = page;
326 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000327 if (*desc->dir_cookie != 0)
Olivier Galibert00a92642005-06-22 17:16:29 +0000328 status = find_dirent(desc);
329 else
330 status = find_dirent_index(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (status < 0)
332 dir_page_release(desc);
333 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500334 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return status;
336 read_error:
337 page_cache_release(page);
338 return -EIO;
339}
340
341/*
342 * Recurse through the page cache pages, and return a
343 * filled nfs_entry structure of the next directory entry if possible.
344 *
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000345 * The target for the search is '*desc->dir_cookie' if non-0,
346 * 'desc->file->f_pos' otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348static inline
349int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
350{
351 int loop_count = 0;
352 int res;
353
Olivier Galibert00a92642005-06-22 17:16:29 +0000354 /* Always search-by-index from the beginning of the cache */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000355 if (*desc->dir_cookie == 0) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500356 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
357 (long long)desc->file->f_pos);
Olivier Galibert00a92642005-06-22 17:16:29 +0000358 desc->page_index = 0;
359 desc->entry->cookie = desc->entry->prev_cookie = 0;
360 desc->entry->eof = 0;
361 desc->current_index = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000362 } else
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500363 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
364 (unsigned long long)*desc->dir_cookie);
Olivier Galibert00a92642005-06-22 17:16:29 +0000365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 for (;;) {
367 res = find_dirent_page(desc);
368 if (res != -EAGAIN)
369 break;
370 /* Align to beginning of next page */
371 desc->page_index ++;
372 if (loop_count++ > 200) {
373 loop_count = 0;
374 schedule();
375 }
376 }
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500377
378 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return res;
380}
381
382static inline unsigned int dt_type(struct inode *inode)
383{
384 return (inode->i_mode >> 12) & 15;
385}
386
387static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
388
389/*
390 * Once we've found the start of the dirent within a page: fill 'er up...
391 */
392static
393int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
394 filldir_t filldir)
395{
396 struct file *file = desc->file;
397 struct nfs_entry *entry = desc->entry;
398 struct dentry *dentry = NULL;
399 unsigned long fileid;
400 int loop_count = 0,
401 res;
402
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500403 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
404 (unsigned long long)entry->cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 for(;;) {
407 unsigned d_type = DT_UNKNOWN;
408 /* Note: entry->prev_cookie contains the cookie for
409 * retrieving the current dirent on the server */
410 fileid = nfs_fileid_to_ino_t(entry->ino);
411
412 /* Get a dentry if we have one */
413 if (dentry != NULL)
414 dput(dentry);
415 dentry = nfs_readdir_lookup(desc);
416
417 /* Use readdirplus info */
418 if (dentry != NULL && dentry->d_inode != NULL) {
419 d_type = dt_type(dentry->d_inode);
420 fileid = dentry->d_inode->i_ino;
421 }
422
423 res = filldir(dirent, entry->name, entry->len,
Olivier Galibert00a92642005-06-22 17:16:29 +0000424 file->f_pos, fileid, d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (res < 0)
426 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000427 file->f_pos++;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000428 *desc->dir_cookie = entry->cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (dir_decode(desc) != 0) {
430 desc->page_index ++;
431 break;
432 }
433 if (loop_count++ > 200) {
434 loop_count = 0;
435 schedule();
436 }
437 }
438 dir_page_release(desc);
439 if (dentry != NULL)
440 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500441 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
442 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return res;
444}
445
446/*
447 * If we cannot find a cookie in our cache, we suspect that this is
448 * because it points to a deleted file, so we ask the server to return
449 * whatever it thinks is the next entry. We then feed this to filldir.
450 * If all goes well, we should then be able to find our way round the
451 * cache on the next call to readdir_search_pagecache();
452 *
453 * NOTE: we cannot add the anonymous page to the pagecache because
454 * the data it contains might not be page aligned. Besides,
455 * we should already have a complete representation of the
456 * directory in the page cache by the time we get here.
457 */
458static inline
459int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
460 filldir_t filldir)
461{
462 struct file *file = desc->file;
463 struct inode *inode = file->f_dentry->d_inode;
464 struct rpc_cred *cred = nfs_file_cred(file);
465 struct page *page = NULL;
466 int status;
467
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500468 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
469 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 page = alloc_page(GFP_HIGHUSER);
472 if (!page) {
473 status = -ENOMEM;
474 goto out;
475 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000476 desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, *desc->dir_cookie,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 page,
478 NFS_SERVER(inode)->dtsize,
479 desc->plus);
Chuck Leverdc592502005-08-18 11:24:12 -0700480 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700481 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
Chuck Leverdc592502005-08-18 11:24:12 -0700482 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 desc->page = page;
484 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
485 if (desc->error >= 0) {
486 if ((status = dir_decode(desc)) == 0)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000487 desc->entry->prev_cookie = *desc->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else
489 status = -EIO;
490 if (status < 0)
491 goto out_release;
492
493 status = nfs_do_filldir(desc, dirent, filldir);
494
495 /* Reset read descriptor so it searches the page cache from
496 * the start upon the next call to readdir_search_pagecache() */
497 desc->page_index = 0;
498 desc->entry->cookie = desc->entry->prev_cookie = 0;
499 desc->entry->eof = 0;
500 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500501 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
502 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return status;
504 out_release:
505 dir_page_release(desc);
506 goto out;
507}
508
Olivier Galibert00a92642005-06-22 17:16:29 +0000509/* The file offset position represents the dirent entry number. A
510 last cookie cache takes care of the common case of reading the
511 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
514{
515 struct dentry *dentry = filp->f_dentry;
516 struct inode *inode = dentry->d_inode;
517 nfs_readdir_descriptor_t my_desc,
518 *desc = &my_desc;
519 struct nfs_entry my_entry;
520 struct nfs_fh fh;
521 struct nfs_fattr fattr;
522 long res;
523
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500524 dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
525 dentry->d_parent->d_name.name, dentry->d_name.name,
526 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500527 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 lock_kernel();
530
Trond Myklebust44b11872006-05-25 01:40:59 -0400531 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (res < 0) {
533 unlock_kernel();
534 return res;
535 }
536
537 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000538 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000539 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000540 * to either find the entry with the appropriate number or
541 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543 memset(desc, 0, sizeof(*desc));
544
545 desc->file = filp;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000546 desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 desc->decode = NFS_PROTO(inode)->decode_dirent;
548 desc->plus = NFS_USE_READDIRPLUS(inode);
549
550 my_entry.cookie = my_entry.prev_cookie = 0;
551 my_entry.eof = 0;
552 my_entry.fh = &fh;
553 my_entry.fattr = &fattr;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400554 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 desc->entry = &my_entry;
556
557 while(!desc->entry->eof) {
558 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (res == -EBADCOOKIE) {
561 /* This means either end of directory */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000562 if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Or that the server has 'lost' a cookie */
564 res = uncached_readdir(desc, dirent, filldir);
565 if (res >= 0)
566 continue;
567 }
568 res = 0;
569 break;
570 }
571 if (res == -ETOOSMALL && desc->plus) {
Chuck Lever412d5822005-08-18 11:24:11 -0700572 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 nfs_zap_caches(inode);
574 desc->plus = 0;
575 desc->entry->eof = 0;
576 continue;
577 }
578 if (res < 0)
579 break;
580
581 res = nfs_do_filldir(desc, dirent, filldir);
582 if (res < 0) {
583 res = 0;
584 break;
585 }
586 }
587 unlock_kernel();
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500588 if (res > 0)
589 res = 0;
590 dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
591 dentry->d_parent->d_name.name, dentry->d_name.name,
592 res);
593 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000596loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
597{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800598 mutex_lock(&filp->f_dentry->d_inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000599 switch (origin) {
600 case 1:
601 offset += filp->f_pos;
602 case 0:
603 if (offset >= 0)
604 break;
605 default:
606 offset = -EINVAL;
607 goto out;
608 }
609 if (offset != filp->f_pos) {
610 filp->f_pos = offset;
611 ((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
612 }
613out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800614 mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000615 return offset;
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/*
619 * All directory operations under NFS are synchronous, so fsync()
620 * is a dummy operation.
621 */
622int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
623{
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500624 dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
625 dentry->d_parent->d_name.name, dentry->d_name.name,
626 datasync);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629}
630
631/*
632 * A check for whether or not the parent directory has changed.
633 * In the case it has, we assume that the dentries are untrustworthy
634 * and may need to be looked up again.
635 */
636static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
637{
638 if (IS_ROOT(dentry))
639 return 1;
Chuck Lever55296802005-08-18 11:24:09 -0700640 if ((NFS_I(dir)->cache_validity & NFS_INO_INVALID_ATTR) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 || nfs_attribute_timeout(dir))
642 return 0;
643 return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
644}
645
646static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
647{
648 dentry->d_fsdata = (void *)verf;
649}
650
651/*
652 * Whenever an NFS operation succeeds, we know that the dentry
653 * is valid, so we update the revalidation timestamp.
654 */
655static inline void nfs_renew_times(struct dentry * dentry)
656{
657 dentry->d_time = jiffies;
658}
659
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400660/*
661 * Return the intent data that applies to this particular path component
662 *
663 * Note that the current set of intents only apply to the very last
664 * component of the path.
665 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
666 */
667static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
668{
669 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
670 return 0;
671 return nd->flags & mask;
672}
673
674/*
675 * Inode and filehandle revalidation for lookups.
676 *
677 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
678 * or if the intent information indicates that we're about to open this
679 * particular file and the "nocto" mount flag is not set.
680 *
681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static inline
683int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
684{
685 struct nfs_server *server = NFS_SERVER(inode);
686
687 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400689 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 goto out_force;
691 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400692 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400693 !(server->flags & NFS_MOUNT_NOCTO) &&
694 (S_ISREG(inode->i_mode) ||
695 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto out_force;
697 }
698 return nfs_revalidate_inode(server, inode);
699out_force:
700 return __nfs_revalidate_inode(server, inode);
701}
702
703/*
704 * We judge how long we want to trust negative
705 * dentries by looking at the parent inode mtime.
706 *
707 * If parent mtime has changed, we revalidate, else we wait for a
708 * period corresponding to the parent's attribute cache timeout value.
709 */
710static inline
711int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
712 struct nameidata *nd)
713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400715 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717 return !nfs_check_verifier(dir, dentry);
718}
719
720/*
721 * This is called every time the dcache has a lookup hit,
722 * and we should check whether we can really trust that
723 * lookup.
724 *
725 * NOTE! The hit can be a negative hit too, don't assume
726 * we have an inode!
727 *
728 * If the parent directory is seen to have changed, we throw out the
729 * cached dentry and do a new lookup.
730 */
731static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
732{
733 struct inode *dir;
734 struct inode *inode;
735 struct dentry *parent;
736 int error;
737 struct nfs_fh fhandle;
738 struct nfs_fattr fattr;
739 unsigned long verifier;
740
741 parent = dget_parent(dentry);
742 lock_kernel();
743 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -0500744 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 inode = dentry->d_inode;
746
747 if (!inode) {
748 if (nfs_neg_need_reval(dir, dentry, nd))
749 goto out_bad;
750 goto out_valid;
751 }
752
753 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500754 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
755 __FUNCTION__, dentry->d_parent->d_name.name,
756 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto out_bad;
758 }
759
760 /* Revalidate parent directory attribute cache */
761 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
762 goto out_zap_parent;
763
764 /* Force a full look up iff the parent directory has changed */
765 if (nfs_check_verifier(dir, dentry)) {
766 if (nfs_lookup_verify_inode(inode, nd))
767 goto out_zap_parent;
768 goto out_valid;
769 }
770
771 if (NFS_STALE(inode))
772 goto out_bad;
773
774 verifier = nfs_save_change_attribute(dir);
775 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
776 if (error)
777 goto out_bad;
778 if (nfs_compare_fh(NFS_FH(inode), &fhandle))
779 goto out_bad;
780 if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
781 goto out_bad;
782
783 nfs_renew_times(dentry);
784 nfs_set_verifier(dentry, verifier);
785 out_valid:
786 unlock_kernel();
787 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500788 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
789 __FUNCTION__, dentry->d_parent->d_name.name,
790 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 1;
792out_zap_parent:
793 nfs_zap_caches(dir);
794 out_bad:
795 NFS_CACHEINV(dir);
796 if (inode && S_ISDIR(inode->i_mode)) {
797 /* Purge readdir caches. */
798 nfs_zap_caches(inode);
799 /* If we have submounts, don't unhash ! */
800 if (have_submounts(dentry))
801 goto out_valid;
802 shrink_dcache_parent(dentry);
803 }
804 d_drop(dentry);
805 unlock_kernel();
806 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500807 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
808 __FUNCTION__, dentry->d_parent->d_name.name,
809 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0;
811}
812
813/*
814 * This is called from dput() when d_count is going to 0.
815 */
816static int nfs_dentry_delete(struct dentry *dentry)
817{
818 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
819 dentry->d_parent->d_name.name, dentry->d_name.name,
820 dentry->d_flags);
821
822 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
823 /* Unhash it, so that ->d_iput() would be called */
824 return 1;
825 }
826 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
827 /* Unhash it, so that ancestors of killed async unlink
828 * files will be cleaned up during umount */
829 return 1;
830 }
831 return 0;
832
833}
834
835/*
836 * Called when the dentry loses inode.
837 * We use it to clean up silly-renamed files.
838 */
839static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
840{
Trond Myklebustcae7a072005-10-18 14:20:19 -0700841 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
843 lock_kernel();
844 inode->i_nlink--;
845 nfs_complete_unlink(dentry);
846 unlock_kernel();
847 }
848 /* When creating a negative dentry, we want to renew d_time */
849 nfs_renew_times(dentry);
850 iput(inode);
851}
852
853struct dentry_operations nfs_dentry_operations = {
854 .d_revalidate = nfs_lookup_revalidate,
855 .d_delete = nfs_dentry_delete,
856 .d_iput = nfs_dentry_iput,
857};
858
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400859/*
860 * Use intent information to check whether or not we're going to do
861 * an O_EXCL create using this path component.
862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static inline
864int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
865{
866 if (NFS_PROTO(dir)->version == 2)
867 return 0;
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400868 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
870 return (nd->intent.open.flags & O_EXCL) != 0;
871}
872
Trond Myklebust55a97592006-06-09 09:34:19 -0400873static inline int nfs_reval_fsid(struct inode *dir,
874 struct nfs_fh *fh, struct nfs_fattr *fattr)
875{
876 struct nfs_server *server = NFS_SERVER(dir);
877
878 if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
879 /* Revalidate fsid on root dir */
880 return __nfs_revalidate_inode(server, dir->i_sb->s_root->d_inode);
881 return 0;
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
885{
886 struct dentry *res;
887 struct inode *inode = NULL;
888 int error;
889 struct nfs_fh fhandle;
890 struct nfs_fattr fattr;
891
892 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
893 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -0500894 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 res = ERR_PTR(-ENAMETOOLONG);
897 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
898 goto out;
899
900 res = ERR_PTR(-ENOMEM);
901 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
902
903 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* If we're doing an exclusive create, optimize away the lookup */
906 if (nfs_is_exclusive_create(dir, nd))
907 goto no_entry;
908
909 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
910 if (error == -ENOENT)
911 goto no_entry;
912 if (error < 0) {
913 res = ERR_PTR(error);
914 goto out_unlock;
915 }
Trond Myklebust55a97592006-06-09 09:34:19 -0400916 error = nfs_reval_fsid(dir, &fhandle, &fattr);
917 if (error < 0) {
918 res = ERR_PTR(error);
919 goto out_unlock;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500922 res = (struct dentry *)inode;
923 if (IS_ERR(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto out_unlock;
925no_entry:
926 res = d_add_unique(dentry, inode);
927 if (res != NULL)
928 dentry = res;
929 nfs_renew_times(dentry);
930 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
931out_unlock:
932 unlock_kernel();
933out:
934 return res;
935}
936
937#ifdef CONFIG_NFS_V4
938static int nfs_open_revalidate(struct dentry *, struct nameidata *);
939
940struct dentry_operations nfs4_dentry_operations = {
941 .d_revalidate = nfs_open_revalidate,
942 .d_delete = nfs_dentry_delete,
943 .d_iput = nfs_dentry_iput,
944};
945
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400946/*
947 * Use intent information to determine whether we need to substitute
948 * the NFSv4-style stateful OPEN for the LOOKUP call
949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int is_atomic_open(struct inode *dir, struct nameidata *nd)
951{
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400952 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954 /* NFS does not (yet) have a stateful open for directories */
955 if (nd->flags & LOOKUP_DIRECTORY)
956 return 0;
957 /* Are we trying to write to a read only partition? */
958 if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
959 return 0;
960 return 1;
961}
962
963static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
964{
965 struct dentry *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int error;
967
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500968 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
969 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* Check that we are indeed trying to open this file */
972 if (!is_atomic_open(dir, nd))
973 goto no_open;
974
975 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
976 res = ERR_PTR(-ENAMETOOLONG);
977 goto out;
978 }
979 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
980
981 /* Let vfs_create() deal with O_EXCL */
Trond Myklebust02a913a2005-10-18 14:20:17 -0700982 if (nd->intent.open.flags & O_EXCL) {
983 d_add(dentry, NULL);
984 goto out;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Open the file on the server */
988 lock_kernel();
989 /* Revalidate parent directory attribute cache */
990 error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
991 if (error < 0) {
992 res = ERR_PTR(error);
Steve Dickson01c314a2005-08-19 17:57:48 -0700993 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto out;
995 }
996
997 if (nd->intent.open.flags & O_CREAT) {
998 nfs_begin_data_update(dir);
Trond Myklebust02a913a2005-10-18 14:20:17 -0700999 res = nfs4_atomic_open(dir, dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 nfs_end_data_update(dir);
1001 } else
Trond Myklebust02a913a2005-10-18 14:20:17 -07001002 res = nfs4_atomic_open(dir, dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unlock_kernel();
Trond Myklebust02a913a2005-10-18 14:20:17 -07001004 if (IS_ERR(res)) {
1005 error = PTR_ERR(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 switch (error) {
1007 /* Make a negative dentry */
1008 case -ENOENT:
Trond Myklebust02a913a2005-10-18 14:20:17 -07001009 res = NULL;
1010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* This turned out not to be a regular file */
Trond Myklebust6f926b52005-10-18 14:20:18 -07001012 case -EISDIR:
1013 case -ENOTDIR:
1014 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 case -ELOOP:
1016 if (!(nd->intent.open.flags & O_NOFOLLOW))
1017 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* case -EINVAL: */
1019 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto out;
1021 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001022 } else if (res != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 dentry = res;
1024 nfs_renew_times(dentry);
1025 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1026out:
1027 return res;
1028no_open:
1029 return nfs_lookup(dir, dentry, nd);
1030}
1031
1032static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1033{
1034 struct dentry *parent = NULL;
1035 struct inode *inode = dentry->d_inode;
1036 struct inode *dir;
1037 unsigned long verifier;
1038 int openflags, ret = 0;
1039
1040 parent = dget_parent(dentry);
1041 dir = parent->d_inode;
1042 if (!is_atomic_open(dir, nd))
1043 goto no_open;
1044 /* We can't create new files in nfs_open_revalidate(), so we
1045 * optimize away revalidation of negative dentries.
1046 */
1047 if (inode == NULL)
1048 goto out;
1049 /* NFS only supports OPEN on regular files */
1050 if (!S_ISREG(inode->i_mode))
1051 goto no_open;
1052 openflags = nd->intent.open.flags;
1053 /* We cannot do exclusive creation on a positive dentry */
1054 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1055 goto no_open;
1056 /* We can't create new files, or truncate existing ones here */
1057 openflags &= ~(O_CREAT|O_TRUNC);
1058
1059 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001060 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * operations that change the directory. We therefore save the
1062 * change attribute *before* we do the RPC call.
1063 */
1064 lock_kernel();
1065 verifier = nfs_save_change_attribute(dir);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001066 ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!ret)
1068 nfs_set_verifier(dentry, verifier);
1069 unlock_kernel();
1070out:
1071 dput(parent);
1072 if (!ret)
1073 d_drop(dentry);
1074 return ret;
1075no_open:
1076 dput(parent);
1077 if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
1078 return 1;
1079 return nfs_lookup_revalidate(dentry, nd);
1080}
1081#endif /* CONFIG_NFSV4 */
1082
1083static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
1084{
1085 struct dentry *parent = desc->file->f_dentry;
1086 struct inode *dir = parent->d_inode;
1087 struct nfs_entry *entry = desc->entry;
1088 struct dentry *dentry, *alias;
1089 struct qstr name = {
1090 .name = entry->name,
1091 .len = entry->len,
1092 };
1093 struct inode *inode;
1094
1095 switch (name.len) {
1096 case 2:
1097 if (name.name[0] == '.' && name.name[1] == '.')
1098 return dget_parent(parent);
1099 break;
1100 case 1:
1101 if (name.name[0] == '.')
1102 return dget(parent);
1103 }
1104 name.hash = full_name_hash(name.name, name.len);
1105 dentry = d_lookup(parent, &name);
1106 if (dentry != NULL)
1107 return dentry;
1108 if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
1109 return NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001110 /* Note: caller is already holding the dir->i_mutex! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dentry = d_alloc(parent, &name);
1112 if (dentry == NULL)
1113 return NULL;
1114 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1115 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001116 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 dput(dentry);
1118 return NULL;
1119 }
1120 alias = d_add_unique(dentry, inode);
1121 if (alias != NULL) {
1122 dput(dentry);
1123 dentry = alias;
1124 }
1125 nfs_renew_times(dentry);
1126 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1127 return dentry;
1128}
1129
1130/*
1131 * Code common to create, mkdir, and mknod.
1132 */
1133int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1134 struct nfs_fattr *fattr)
1135{
1136 struct inode *inode;
1137 int error = -EACCES;
1138
1139 /* We may have been initialized further down */
1140 if (dentry->d_inode)
1141 return 0;
1142 if (fhandle->size == 0) {
1143 struct inode *dir = dentry->d_parent->d_inode;
1144 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1145 if (error)
1146 goto out_err;
1147 }
1148 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1149 struct nfs_server *server = NFS_SB(dentry->d_sb);
1150 error = server->rpc_ops->getattr(server, fhandle, fattr);
1151 if (error < 0)
1152 goto out_err;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001155 error = PTR_ERR(inode);
1156 if (IS_ERR(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 goto out_err;
1158 d_instantiate(dentry, inode);
1159 return 0;
1160out_err:
1161 d_drop(dentry);
1162 return error;
1163}
1164
1165/*
1166 * Following a failed create operation, we drop the dentry rather
1167 * than retain a negative dentry. This avoids a problem in the event
1168 * that the operation succeeded on the server, but an error in the
1169 * reply path made it appear to have failed.
1170 */
1171static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1172 struct nameidata *nd)
1173{
1174 struct iattr attr;
1175 int error;
1176 int open_flags = 0;
1177
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001178 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1179 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 attr.ia_mode = mode;
1182 attr.ia_valid = ATTR_MODE;
1183
1184 if (nd && (nd->flags & LOOKUP_CREATE))
1185 open_flags = nd->intent.open.flags;
1186
1187 lock_kernel();
1188 nfs_begin_data_update(dir);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001189 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 nfs_end_data_update(dir);
1191 if (error != 0)
1192 goto out_err;
1193 nfs_renew_times(dentry);
1194 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1195 unlock_kernel();
1196 return 0;
1197out_err:
1198 unlock_kernel();
1199 d_drop(dentry);
1200 return error;
1201}
1202
1203/*
1204 * See comments for nfs_proc_create regarding failed operations.
1205 */
1206static int
1207nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1208{
1209 struct iattr attr;
1210 int status;
1211
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001212 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1213 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (!new_valid_dev(rdev))
1216 return -EINVAL;
1217
1218 attr.ia_mode = mode;
1219 attr.ia_valid = ATTR_MODE;
1220
1221 lock_kernel();
1222 nfs_begin_data_update(dir);
1223 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1224 nfs_end_data_update(dir);
1225 if (status != 0)
1226 goto out_err;
1227 nfs_renew_times(dentry);
1228 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1229 unlock_kernel();
1230 return 0;
1231out_err:
1232 unlock_kernel();
1233 d_drop(dentry);
1234 return status;
1235}
1236
1237/*
1238 * See comments for nfs_proc_create regarding failed operations.
1239 */
1240static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1241{
1242 struct iattr attr;
1243 int error;
1244
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001245 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1246 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 attr.ia_valid = ATTR_MODE;
1249 attr.ia_mode = mode | S_IFDIR;
1250
1251 lock_kernel();
1252 nfs_begin_data_update(dir);
1253 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1254 nfs_end_data_update(dir);
1255 if (error != 0)
1256 goto out_err;
1257 nfs_renew_times(dentry);
1258 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1259 unlock_kernel();
1260 return 0;
1261out_err:
1262 d_drop(dentry);
1263 unlock_kernel();
1264 return error;
1265}
1266
1267static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1268{
1269 int error;
1270
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001271 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1272 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 lock_kernel();
1275 nfs_begin_data_update(dir);
1276 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1277 /* Ensure the VFS deletes this inode */
1278 if (error == 0 && dentry->d_inode != NULL)
1279 dentry->d_inode->i_nlink = 0;
1280 nfs_end_data_update(dir);
1281 unlock_kernel();
1282
1283 return error;
1284}
1285
1286static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
1287{
1288 static unsigned int sillycounter;
1289 const int i_inosize = sizeof(dir->i_ino)*2;
1290 const int countersize = sizeof(sillycounter)*2;
1291 const int slen = sizeof(".nfs") + i_inosize + countersize - 1;
1292 char silly[slen+1];
1293 struct qstr qsilly;
1294 struct dentry *sdentry;
1295 int error = -EIO;
1296
1297 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
1298 dentry->d_parent->d_name.name, dentry->d_name.name,
1299 atomic_read(&dentry->d_count));
Chuck Lever91d5b472006-03-20 13:44:14 -05001300 nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302#ifdef NFS_PARANOIA
1303if (!dentry->d_inode)
1304printk("NFS: silly-renaming %s/%s, negative dentry??\n",
1305dentry->d_parent->d_name.name, dentry->d_name.name);
1306#endif
1307 /*
1308 * We don't allow a dentry to be silly-renamed twice.
1309 */
1310 error = -EBUSY;
1311 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1312 goto out;
1313
1314 sprintf(silly, ".nfs%*.*lx",
1315 i_inosize, i_inosize, dentry->d_inode->i_ino);
1316
Trond Myklebust34ea8182005-11-04 15:35:02 -05001317 /* Return delegation in anticipation of the rename */
1318 nfs_inode_return_delegation(dentry->d_inode);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sdentry = NULL;
1321 do {
1322 char *suffix = silly + slen - countersize;
1323
1324 dput(sdentry);
1325 sillycounter++;
1326 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
1327
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001328 dfprintk(VFS, "NFS: trying to rename %s to %s\n",
1329 dentry->d_name.name, silly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
1332 /*
1333 * N.B. Better to return EBUSY here ... it could be
1334 * dangerous to delete the file while it's in use.
1335 */
1336 if (IS_ERR(sdentry))
1337 goto out;
1338 } while(sdentry->d_inode != NULL); /* need negative lookup */
1339
1340 qsilly.name = silly;
1341 qsilly.len = strlen(silly);
1342 nfs_begin_data_update(dir);
1343 if (dentry->d_inode) {
1344 nfs_begin_data_update(dentry->d_inode);
1345 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1346 dir, &qsilly);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001347 nfs_mark_for_revalidate(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 nfs_end_data_update(dentry->d_inode);
1349 } else
1350 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1351 dir, &qsilly);
1352 nfs_end_data_update(dir);
1353 if (!error) {
1354 nfs_renew_times(dentry);
1355 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1356 d_move(dentry, sdentry);
1357 error = nfs_async_unlink(dentry);
1358 /* If we return 0 we don't unlink */
1359 }
1360 dput(sdentry);
1361out:
1362 return error;
1363}
1364
1365/*
1366 * Remove a file after making sure there are no pending writes,
1367 * and after checking that the file has only one user.
1368 *
1369 * We invalidate the attribute cache and free the inode prior to the operation
1370 * to avoid possible races if the server reuses the inode.
1371 */
1372static int nfs_safe_remove(struct dentry *dentry)
1373{
1374 struct inode *dir = dentry->d_parent->d_inode;
1375 struct inode *inode = dentry->d_inode;
1376 int error = -EBUSY;
1377
1378 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1379 dentry->d_parent->d_name.name, dentry->d_name.name);
1380
1381 /* If the dentry was sillyrenamed, we simply call d_delete() */
1382 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1383 error = 0;
1384 goto out;
1385 }
1386
1387 nfs_begin_data_update(dir);
1388 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001389 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 nfs_begin_data_update(inode);
1391 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1392 /* The VFS may want to delete this inode */
1393 if (error == 0)
1394 inode->i_nlink--;
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001395 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 nfs_end_data_update(inode);
1397 } else
1398 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1399 nfs_end_data_update(dir);
1400out:
1401 return error;
1402}
1403
1404/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1405 * belongs to an active ".nfs..." file and we return -EBUSY.
1406 *
1407 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1408 */
1409static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1410{
1411 int error;
1412 int need_rehash = 0;
1413
1414 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1415 dir->i_ino, dentry->d_name.name);
1416
1417 lock_kernel();
1418 spin_lock(&dcache_lock);
1419 spin_lock(&dentry->d_lock);
1420 if (atomic_read(&dentry->d_count) > 1) {
1421 spin_unlock(&dentry->d_lock);
1422 spin_unlock(&dcache_lock);
1423 error = nfs_sillyrename(dir, dentry);
1424 unlock_kernel();
1425 return error;
1426 }
1427 if (!d_unhashed(dentry)) {
1428 __d_drop(dentry);
1429 need_rehash = 1;
1430 }
1431 spin_unlock(&dentry->d_lock);
1432 spin_unlock(&dcache_lock);
1433 error = nfs_safe_remove(dentry);
1434 if (!error) {
1435 nfs_renew_times(dentry);
1436 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1437 } else if (need_rehash)
1438 d_rehash(dentry);
1439 unlock_kernel();
1440 return error;
1441}
1442
1443static int
1444nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1445{
1446 struct iattr attr;
1447 struct nfs_fattr sym_attr;
1448 struct nfs_fh sym_fh;
1449 struct qstr qsymname;
1450 int error;
1451
1452 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1453 dir->i_ino, dentry->d_name.name, symname);
1454
1455#ifdef NFS_PARANOIA
1456if (dentry->d_inode)
1457printk("nfs_proc_symlink: %s/%s not negative!\n",
1458dentry->d_parent->d_name.name, dentry->d_name.name);
1459#endif
1460 /*
1461 * Fill in the sattr for the call.
1462 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1463 */
1464 attr.ia_valid = ATTR_MODE;
1465 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1466
1467 qsymname.name = symname;
1468 qsymname.len = strlen(symname);
1469
1470 lock_kernel();
1471 nfs_begin_data_update(dir);
1472 error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
1473 &attr, &sym_fh, &sym_attr);
1474 nfs_end_data_update(dir);
1475 if (!error) {
1476 error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
1477 } else {
1478 if (error == -EEXIST)
1479 printk("nfs_proc_symlink: %s/%s already exists??\n",
1480 dentry->d_parent->d_name.name, dentry->d_name.name);
1481 d_drop(dentry);
1482 }
1483 unlock_kernel();
1484 return error;
1485}
1486
1487static int
1488nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1489{
1490 struct inode *inode = old_dentry->d_inode;
1491 int error;
1492
1493 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1494 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1495 dentry->d_parent->d_name.name, dentry->d_name.name);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 nfs_begin_data_update(dir);
1499 nfs_begin_data_update(inode);
1500 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001501 if (error == 0) {
1502 atomic_inc(&inode->i_count);
1503 d_instantiate(dentry, inode);
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 nfs_end_data_update(inode);
1506 nfs_end_data_update(dir);
1507 unlock_kernel();
1508 return error;
1509}
1510
1511/*
1512 * RENAME
1513 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1514 * different file handle for the same inode after a rename (e.g. when
1515 * moving to a different directory). A fail-safe method to do so would
1516 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1517 * rename the old file using the sillyrename stuff. This way, the original
1518 * file in old_dir will go away when the last process iput()s the inode.
1519 *
1520 * FIXED.
1521 *
1522 * It actually works quite well. One needs to have the possibility for
1523 * at least one ".nfs..." file in each directory the file ever gets
1524 * moved or linked to which happens automagically with the new
1525 * implementation that only depends on the dcache stuff instead of
1526 * using the inode layer
1527 *
1528 * Unfortunately, things are a little more complicated than indicated
1529 * above. For a cross-directory move, we want to make sure we can get
1530 * rid of the old inode after the operation. This means there must be
1531 * no pending writes (if it's a file), and the use count must be 1.
1532 * If these conditions are met, we can drop the dentries before doing
1533 * the rename.
1534 */
1535static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1536 struct inode *new_dir, struct dentry *new_dentry)
1537{
1538 struct inode *old_inode = old_dentry->d_inode;
1539 struct inode *new_inode = new_dentry->d_inode;
1540 struct dentry *dentry = NULL, *rehash = NULL;
1541 int error = -EBUSY;
1542
1543 /*
1544 * To prevent any new references to the target during the rename,
1545 * we unhash the dentry and free the inode in advance.
1546 */
1547 lock_kernel();
1548 if (!d_unhashed(new_dentry)) {
1549 d_drop(new_dentry);
1550 rehash = new_dentry;
1551 }
1552
1553 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1554 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1555 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1556 atomic_read(&new_dentry->d_count));
1557
1558 /*
1559 * First check whether the target is busy ... we can't
1560 * safely do _any_ rename if the target is in use.
1561 *
1562 * For files, make a copy of the dentry and then do a
1563 * silly-rename. If the silly-rename succeeds, the
1564 * copied dentry is hashed and becomes the new target.
1565 */
1566 if (!new_inode)
1567 goto go_ahead;
Trond Myklebust6fe43f92005-10-18 14:20:22 -07001568 if (S_ISDIR(new_inode->i_mode)) {
1569 error = -EISDIR;
1570 if (!S_ISDIR(old_inode->i_mode))
1571 goto out;
1572 } else if (atomic_read(&new_dentry->d_count) > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int err;
1574 /* copy the target dentry's name */
1575 dentry = d_alloc(new_dentry->d_parent,
1576 &new_dentry->d_name);
1577 if (!dentry)
1578 goto out;
1579
1580 /* silly-rename the existing target ... */
1581 err = nfs_sillyrename(new_dir, new_dentry);
1582 if (!err) {
1583 new_dentry = rehash = dentry;
1584 new_inode = NULL;
1585 /* instantiate the replacement target */
1586 d_instantiate(new_dentry, NULL);
1587 } else if (atomic_read(&new_dentry->d_count) > 1) {
1588 /* dentry still busy? */
1589#ifdef NFS_PARANOIA
1590 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1591 new_dentry->d_parent->d_name.name,
1592 new_dentry->d_name.name,
1593 atomic_read(&new_dentry->d_count));
1594#endif
1595 goto out;
1596 }
Trond Myklebust20509f12005-08-25 16:25:34 -07001597 } else
1598 new_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600go_ahead:
1601 /*
1602 * ... prune child dentries and writebacks if needed.
1603 */
1604 if (atomic_read(&old_dentry->d_count) > 1) {
1605 nfs_wb_all(old_inode);
1606 shrink_dcache_parent(old_dentry);
1607 }
Trond Myklebustcae7a072005-10-18 14:20:19 -07001608 nfs_inode_return_delegation(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Trond Myklebust24174112006-01-03 09:55:33 +01001610 if (new_inode != NULL) {
1611 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 d_delete(new_dentry);
Trond Myklebust24174112006-01-03 09:55:33 +01001613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 nfs_begin_data_update(old_dir);
1616 nfs_begin_data_update(new_dir);
1617 nfs_begin_data_update(old_inode);
1618 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1619 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001620 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 nfs_end_data_update(old_inode);
1622 nfs_end_data_update(new_dir);
1623 nfs_end_data_update(old_dir);
1624out:
1625 if (rehash)
1626 d_rehash(rehash);
1627 if (!error) {
1628 if (!S_ISDIR(old_inode->i_mode))
1629 d_move(old_dentry, new_dentry);
1630 nfs_renew_times(new_dentry);
1631 nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
1632 }
1633
1634 /* new dentry created? */
1635 if (dentry)
1636 dput(dentry);
1637 unlock_kernel();
1638 return error;
1639}
1640
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001641static DEFINE_SPINLOCK(nfs_access_lru_lock);
1642static LIST_HEAD(nfs_access_lru_list);
1643static atomic_long_t nfs_access_nr_entries;
1644
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001645static void nfs_access_free_entry(struct nfs_access_entry *entry)
1646{
1647 put_rpccred(entry->cred);
1648 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001649 smp_mb__before_atomic_dec();
1650 atomic_long_dec(&nfs_access_nr_entries);
1651 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001652}
1653
1654static void __nfs_access_zap_cache(struct inode *inode)
1655{
1656 struct nfs_inode *nfsi = NFS_I(inode);
1657 struct rb_root *root_node = &nfsi->access_cache;
1658 struct rb_node *n, *dispose = NULL;
1659 struct nfs_access_entry *entry;
1660
1661 /* Unhook entries from the cache */
1662 while ((n = rb_first(root_node)) != NULL) {
1663 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1664 rb_erase(n, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001665 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001666 n->rb_left = dispose;
1667 dispose = n;
1668 }
1669 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
1670 spin_unlock(&inode->i_lock);
1671
1672 /* Now kill them all! */
1673 while (dispose != NULL) {
1674 n = dispose;
1675 dispose = n->rb_left;
1676 nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
1677 }
1678}
1679
1680void nfs_access_zap_cache(struct inode *inode)
1681{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001682 /* Remove from global LRU init */
1683 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1684 spin_lock(&nfs_access_lru_lock);
1685 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1686 spin_unlock(&nfs_access_lru_lock);
1687 }
1688
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001689 spin_lock(&inode->i_lock);
1690 /* This will release the spinlock */
1691 __nfs_access_zap_cache(inode);
1692}
1693
1694static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
1695{
1696 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
1697 struct nfs_access_entry *entry;
1698
1699 while (n != NULL) {
1700 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1701
1702 if (cred < entry->cred)
1703 n = n->rb_left;
1704 else if (cred > entry->cred)
1705 n = n->rb_right;
1706 else
1707 return entry;
1708 }
1709 return NULL;
1710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
1713{
Chuck Lever55296802005-08-18 11:24:09 -07001714 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001715 struct nfs_access_entry *cache;
1716 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001718 spin_lock(&inode->i_lock);
1719 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
1720 goto out_zap;
1721 cache = nfs_access_search_rbtree(inode, cred);
1722 if (cache == NULL)
1723 goto out;
1724 if (time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
1725 goto out_stale;
1726 res->jiffies = cache->jiffies;
1727 res->cred = cache->cred;
1728 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001729 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001730 err = 0;
1731out:
1732 spin_unlock(&inode->i_lock);
1733 return err;
1734out_stale:
1735 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001736 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001737 spin_unlock(&inode->i_lock);
1738 nfs_access_free_entry(cache);
1739 return -ENOENT;
1740out_zap:
1741 /* This will release the spinlock */
1742 __nfs_access_zap_cache(inode);
1743 return -ENOENT;
1744}
1745
1746static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
1747{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001748 struct nfs_inode *nfsi = NFS_I(inode);
1749 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001750 struct rb_node **p = &root_node->rb_node;
1751 struct rb_node *parent = NULL;
1752 struct nfs_access_entry *entry;
1753
1754 spin_lock(&inode->i_lock);
1755 while (*p != NULL) {
1756 parent = *p;
1757 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
1758
1759 if (set->cred < entry->cred)
1760 p = &parent->rb_left;
1761 else if (set->cred > entry->cred)
1762 p = &parent->rb_right;
1763 else
1764 goto found;
1765 }
1766 rb_link_node(&set->rb_node, parent, p);
1767 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001768 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001769 spin_unlock(&inode->i_lock);
1770 return;
1771found:
1772 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001773 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
1774 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001775 spin_unlock(&inode->i_lock);
1776 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1780{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001781 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
1782 if (cache == NULL)
1783 return;
1784 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001786 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001788
1789 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001790
1791 /* Update accounting */
1792 smp_mb__before_atomic_inc();
1793 atomic_long_inc(&nfs_access_nr_entries);
1794 smp_mb__after_atomic_inc();
1795
1796 /* Add inode to global LRU list */
1797 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1798 spin_lock(&nfs_access_lru_lock);
1799 list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
1800 spin_unlock(&nfs_access_lru_lock);
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
1804static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
1805{
1806 struct nfs_access_entry cache;
1807 int status;
1808
1809 status = nfs_access_get_cached(inode, cred, &cache);
1810 if (status == 0)
1811 goto out;
1812
1813 /* Be clever: ask server to check for all possible rights */
1814 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
1815 cache.cred = cred;
1816 cache.jiffies = jiffies;
1817 status = NFS_PROTO(inode)->access(inode, &cache);
1818 if (status != 0)
1819 return status;
1820 nfs_access_add_cache(inode, &cache);
1821out:
1822 if ((cache.mask & mask) == mask)
1823 return 0;
1824 return -EACCES;
1825}
1826
1827int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1828{
1829 struct rpc_cred *cred;
1830 int res = 0;
1831
Chuck Lever91d5b472006-03-20 13:44:14 -05001832 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (mask == 0)
1835 goto out;
1836 /* Is this sys_access() ? */
1837 if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
1838 goto force_lookup;
1839
1840 switch (inode->i_mode & S_IFMT) {
1841 case S_IFLNK:
1842 goto out;
1843 case S_IFREG:
1844 /* NFSv4 has atomic_open... */
1845 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
1846 && nd != NULL
1847 && (nd->flags & LOOKUP_OPEN))
1848 goto out;
1849 break;
1850 case S_IFDIR:
1851 /*
1852 * Optimize away all write operations, since the server
1853 * will check permissions when we perform the op.
1854 */
1855 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
1856 goto out;
1857 }
1858
1859force_lookup:
1860 lock_kernel();
1861
1862 if (!NFS_PROTO(inode)->access)
1863 goto out_notsup;
1864
1865 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1866 if (!IS_ERR(cred)) {
1867 res = nfs_do_access(inode, cred, mask);
1868 put_rpccred(cred);
1869 } else
1870 res = PTR_ERR(cred);
1871 unlock_kernel();
1872out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001873 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
1874 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return res;
1876out_notsup:
1877 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1878 if (res == 0)
1879 res = generic_permission(inode, mask, NULL);
1880 unlock_kernel();
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884/*
1885 * Local variables:
1886 * version-control: t
1887 * kept-new-versions: 5
1888 * End:
1889 */