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