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