blob: 88cbcda76856a779c5342dfedb55937c296e58a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/dir.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs directory handling functions
7 *
8 * 10 Apr 1996 Added silly rename for unlink --okir
9 * 28 Sep 1996 Improved directory cache --okir
10 * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
11 * Re-implemented silly rename for unlink, newly implemented
12 * silly rename for nfs_rename() following the suggestions
13 * of Olaf Kirch (okir) found in this file.
14 * Following Linus comments on my original hack, this version
15 * depends only on the dcache stuff and doesn't touch the inode
16 * layer (iput() and friends).
17 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
18 */
19
20#include <linux/time.h>
21#include <linux/errno.h>
22#include <linux/stat.h>
23#include <linux/fcntl.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/sunrpc/clnt.h>
29#include <linux/nfs_fs.h>
30#include <linux/nfs_mount.h>
31#include <linux/pagemap.h>
Chuck Lever873101b2006-08-22 20:06:23 -040032#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/namei.h>
David Howells54ceac42006-08-22 20:06:13 -040034#include <linux/mount.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050038#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080039#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040040#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* #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 *);
Christoph Hellwig7ea80852010-05-26 17:53:25 +020056static int nfs_fsync_dir(struct file *, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000057static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040058static int nfs_readdir_clear_array(struct page*, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080060const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000061 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .read = generic_read_dir,
63 .readdir = nfs_readdir,
64 .open = nfs_opendir,
65 .release = nfs_release,
66 .fsync = nfs_fsync_dir,
67};
68
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080069const struct inode_operations nfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .create = nfs_create,
71 .lookup = nfs_lookup,
72 .link = nfs_link,
73 .unlink = nfs_unlink,
74 .symlink = nfs_symlink,
75 .mkdir = nfs_mkdir,
76 .rmdir = nfs_rmdir,
77 .mknod = nfs_mknod,
78 .rename = nfs_rename,
79 .permission = nfs_permission,
80 .getattr = nfs_getattr,
81 .setattr = nfs_setattr,
82};
83
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040084const struct address_space_operations nfs_dir_addr_space_ops = {
85 .releasepage = nfs_readdir_clear_array,
86};
87
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000088#ifdef CONFIG_NFS_V3
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080089const struct inode_operations nfs3_dir_inode_operations = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000090 .create = nfs_create,
91 .lookup = nfs_lookup,
92 .link = nfs_link,
93 .unlink = nfs_unlink,
94 .symlink = nfs_symlink,
95 .mkdir = nfs_mkdir,
96 .rmdir = nfs_rmdir,
97 .mknod = nfs_mknod,
98 .rename = nfs_rename,
99 .permission = nfs_permission,
100 .getattr = nfs_getattr,
101 .setattr = nfs_setattr,
102 .listxattr = nfs3_listxattr,
103 .getxattr = nfs3_getxattr,
104 .setxattr = nfs3_setxattr,
105 .removexattr = nfs3_removexattr,
106};
107#endif /* CONFIG_NFS_V3 */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#ifdef CONFIG_NFS_V4
110
111static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400112static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800113const struct inode_operations nfs4_dir_inode_operations = {
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400114 .create = nfs_open_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .lookup = nfs_atomic_lookup,
116 .link = nfs_link,
117 .unlink = nfs_unlink,
118 .symlink = nfs_symlink,
119 .mkdir = nfs_mkdir,
120 .rmdir = nfs_rmdir,
121 .mknod = nfs_mknod,
122 .rename = nfs_rename,
123 .permission = nfs_permission,
124 .getattr = nfs_getattr,
125 .setattr = nfs_setattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +0000126 .getxattr = nfs4_getxattr,
127 .setxattr = nfs4_setxattr,
128 .listxattr = nfs4_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131#endif /* CONFIG_NFS_V4 */
132
133/*
134 * Open file
135 */
136static int
137nfs_opendir(struct inode *inode, struct file *filp)
138{
Carsten Otte7451c4f2006-04-19 13:06:37 -0400139 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Chuck Lever6da24bc2008-06-11 17:55:58 -0400141 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400142 filp->f_path.dentry->d_parent->d_name.name,
143 filp->f_path.dentry->d_name.name);
144
145 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* Call generic open code in order to cache credentials */
Carsten Otte7451c4f2006-04-19 13:06:37 -0400148 res = nfs_open(inode, filp);
Neil Brownf5a73672010-08-10 10:20:05 -0400149 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
150 /* This is a mountpoint, so d_revalidate will never
151 * have been called, so we need to refresh the
152 * inode (for close-open consistency) ourselves.
153 */
154 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return res;
157}
158
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400159struct nfs_cache_array_entry {
160 u64 cookie;
161 u64 ino;
162 struct qstr string;
163};
164
165struct nfs_cache_array {
166 unsigned int size;
167 int eof_index;
168 u64 last_cookie;
169 struct nfs_cache_array_entry array[0];
170};
171
172#define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
173
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400174typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175typedef struct {
176 struct file *file;
177 struct page *page;
178 unsigned long page_index;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000179 u64 *dir_cookie;
180 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 decode_dirent_t decode;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400182
Neil Brown1f4eab72007-04-16 09:35:27 +1000183 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400184 unsigned long gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400185 unsigned int cache_entry_index;
186 unsigned int plus:1;
187 unsigned int eof:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188} nfs_readdir_descriptor_t;
189
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400190/*
191 * The caller is responsible for calling nfs_readdir_release_array(page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193static
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400194struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400196 if (page == NULL)
197 return ERR_PTR(-EIO);
198 return (struct nfs_cache_array *)kmap(page);
199}
200
201static
202void nfs_readdir_release_array(struct page *page)
203{
204 kunmap(page);
205}
206
207/*
208 * we are freeing strings created by nfs_add_to_readdir_array()
209 */
210static
211int nfs_readdir_clear_array(struct page *page, gfp_t mask)
212{
213 struct nfs_cache_array *array = nfs_readdir_get_array(page);
214 int i;
215 for (i = 0; i < array->size; i++)
216 kfree(array->array[i].string.name);
217 nfs_readdir_release_array(page);
218 return 0;
219}
220
221/*
222 * the caller is responsible for freeing qstr.name
223 * when called by nfs_readdir_add_to_array, the strings will be freed in
224 * nfs_clear_readdir_array()
225 */
226static
227void nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
228{
229 string->len = len;
230 string->name = kmemdup(name, len, GFP_KERNEL);
231 string->hash = full_name_hash(string->name, string->len);
232}
233
234static
235int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
236{
237 struct nfs_cache_array *array = nfs_readdir_get_array(page);
238 if (IS_ERR(array))
239 return PTR_ERR(array);
240 if (array->size >= MAX_READDIR_ARRAY) {
241 nfs_readdir_release_array(page);
242 return -EIO;
243 }
244
245 array->array[array->size].cookie = entry->prev_cookie;
246 array->last_cookie = entry->cookie;
247 array->array[array->size].ino = entry->ino;
248 nfs_readdir_make_qstr(&array->array[array->size].string, entry->name, entry->len);
249 if (entry->eof == 1)
250 array->eof_index = array->size;
251 array->size++;
252 nfs_readdir_release_array(page);
253 return 0;
254}
255
256static
257int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
258{
259 loff_t diff = desc->file->f_pos - desc->current_index;
260 unsigned int index;
261
262 if (diff < 0)
263 goto out_eof;
264 if (diff >= array->size) {
265 if (array->eof_index > 0)
266 goto out_eof;
267 desc->current_index += array->size;
268 return -EAGAIN;
269 }
270
271 index = (unsigned int)diff;
272 *desc->dir_cookie = array->array[index].cookie;
273 desc->cache_entry_index = index;
274 if (index == array->eof_index)
275 desc->eof = 1;
276 return 0;
277out_eof:
278 desc->eof = 1;
279 return -EBADCOOKIE;
280}
281
282static
283int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
284{
285 int i;
286 int status = -EAGAIN;
287
288 for (i = 0; i < array->size; i++) {
289 if (i == array->eof_index) {
290 desc->eof = 1;
291 status = -EBADCOOKIE;
292 }
293 if (array->array[i].cookie == *desc->dir_cookie) {
294 desc->cache_entry_index = i;
295 status = 0;
296 break;
297 }
298 }
299
300 return status;
301}
302
303static
304int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
305{
306 struct nfs_cache_array *array;
307 int status = -EBADCOOKIE;
308
309 if (desc->dir_cookie == NULL)
310 goto out;
311
312 array = nfs_readdir_get_array(desc->page);
313 if (IS_ERR(array)) {
314 status = PTR_ERR(array);
315 goto out;
316 }
317
318 if (*desc->dir_cookie == 0)
319 status = nfs_readdir_search_for_pos(array, desc);
320 else
321 status = nfs_readdir_search_for_cookie(array, desc);
322
323 nfs_readdir_release_array(desc->page);
324out:
325 return status;
326}
327
328/* Fill a page with xdr information before transferring to the cache page */
329static
330int nfs_readdir_xdr_filler(struct page *xdr_page, nfs_readdir_descriptor_t *desc,
331 struct nfs_entry *entry, struct file *file, struct inode *inode)
332{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct rpc_cred *cred = nfs_file_cred(file);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400334 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int error;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 again:
338 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400339 gencount = nfs_inc_attr_generation_counter();
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400340 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, xdr_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 NFS_SERVER(inode)->dtsize, desc->plus);
342 if (error < 0) {
343 /* We requested READDIRPLUS, but the server doesn't grok it */
344 if (error == -ENOTSUPP && desc->plus) {
345 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200346 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 desc->plus = 0;
348 goto again;
349 }
350 goto error;
351 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000352 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400353 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400354error:
355 return error;
356}
357
358/* Fill in an entry based on the xdr code stored in desc->page */
359static
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400360int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400361{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400362 __be32 *p = desc->decode(stream, entry, desc->plus);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400363 if (IS_ERR(p))
364 return PTR_ERR(p);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400365
366 entry->fattr->time_start = desc->timestamp;
367 entry->fattr->gencount = desc->gencount;
368 return 0;
369}
370
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400371static
372int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
373{
374 struct nfs_inode *node;
375 if (dentry->d_inode == NULL)
376 goto different;
377 node = NFS_I(dentry->d_inode);
378 if (node->fh.size != entry->fh->size)
379 goto different;
380 if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
381 goto different;
382 return 1;
383different:
384 return 0;
385}
386
387static
388void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
389{
390 struct qstr filename;
391 struct dentry *dentry = NULL;
392 struct dentry *alias = NULL;
393 struct inode *dir = parent->d_inode;
394 struct inode *inode;
395
396 nfs_readdir_make_qstr(&filename, entry->name, entry->len);
397 if (filename.len == 1 && filename.name[0] == '.')
398 dentry = dget(parent);
399 else if (filename.len == 2 && filename.name[0] == '.'
400 && filename.name[1] == '.')
401 dentry = dget_parent(parent);
402 else
403 dentry = d_lookup(parent, &filename);
404
405 if (dentry != NULL) {
406 if (nfs_same_file(dentry, entry)) {
407 nfs_refresh_inode(dentry->d_inode, entry->fattr);
408 goto out;
409 } else {
410 d_drop(dentry);
411 dput(dentry);
412 }
413 }
414
415 dentry = d_alloc(parent, &filename);
416 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
417 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
418 if (IS_ERR(inode))
419 goto out;
420
421 alias = d_materialise_unique(dentry, inode);
422 if (IS_ERR(alias))
423 goto out;
424 else if (alias) {
425 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
426 dput(alias);
427 } else
428 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
429
430out:
431 dput(dentry);
432 kfree(filename.name);
433 return;
434}
435
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400436/* Perform conversion from xdr to cache array */
437static
438void nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400439 struct page *xdr_page, struct page *page, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400440{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400441 struct xdr_stream stream;
442 struct xdr_buf buf;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400443 __be32 *ptr = kmap(xdr_page);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400444
445 buf.head->iov_base = xdr_page;
446 buf.head->iov_len = buflen;
447 buf.tail->iov_len = 0;
448 buf.page_base = 0;
449 buf.page_len = 0;
450 buf.buflen = buf.head->iov_len;
451 buf.len = buf.head->iov_len;
452
453 xdr_init_decode(&stream, &buf, ptr);
454
455 while (xdr_decode(desc, entry, &stream) == 0) {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400456 if (nfs_readdir_add_to_array(entry, page) == -1)
457 break;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400458 if (desc->plus == 1)
459 nfs_prime_dcache(desc->file->f_path.dentry, entry);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400460 }
461 kunmap(xdr_page);
462}
463
464static
465int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
466{
467 struct page *xdr_page;
468 struct nfs_entry entry;
469 struct file *file = desc->file;
470 struct nfs_cache_array *array;
471 int status = 0;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400472 unsigned int array_size = 1;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400473
474 entry.prev_cookie = 0;
475 entry.cookie = *desc->dir_cookie;
476 entry.eof = 0;
477 entry.fh = nfs_alloc_fhandle();
478 entry.fattr = nfs_alloc_fattr();
479 if (entry.fh == NULL || entry.fattr == NULL)
480 goto out;
481
482 array = nfs_readdir_get_array(page);
483 memset(array, 0, sizeof(struct nfs_cache_array));
484 array->eof_index = -1;
485
486 xdr_page = alloc_page(GFP_KERNEL);
487 if (!xdr_page)
488 goto out_release_array;
489 do {
490 status = nfs_readdir_xdr_filler(xdr_page, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400491
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400492 if (status < 0)
493 break;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400494 nfs_readdir_page_filler(desc, &entry, xdr_page, page, array_size * PAGE_SIZE);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400495 } while (array->eof_index < 0 && array->size < MAX_READDIR_ARRAY);
496
497 put_page(xdr_page);
498out_release_array:
499 nfs_readdir_release_array(page);
500out:
501 nfs_free_fattr(entry.fattr);
502 nfs_free_fhandle(entry.fh);
503 return status;
504}
505
506/*
507 * Now we cache directories properly, by converting xdr information
508 * to an array that can be used for lookups later. This results in
509 * fewer cache pages, since we can store more information on each page.
510 * We only need to convert from xdr once so future lookups are much simpler
511 */
512static
513int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
514{
515 struct inode *inode = desc->file->f_path.dentry->d_inode;
516
517 if (nfs_readdir_xdr_to_array(desc, page, inode) == -1)
518 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400520
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400521 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700522 /* Should never happen */
523 nfs_zap_mapping(inode, inode->i_mapping);
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unlock_page(page);
526 return 0;
527 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -EIO;
530}
531
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400532static
533void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 page_cache_release(desc->page);
536 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400539static
540struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400542 struct page *page;
543 page = read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
544 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
545 if (IS_ERR(page))
546 desc->eof = 1;
547 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400551 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Olivier Galibert00a92642005-06-22 17:16:29 +0000552 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400553static
554int find_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000555{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400556 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000557
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400558 desc->page = get_cache_page(desc);
559 if (IS_ERR(desc->page))
560 return PTR_ERR(desc->page);
Olivier Galibert00a92642005-06-22 17:16:29 +0000561
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400562 res = nfs_readdir_search_array(desc);
563 if (res == 0)
564 return 0;
565 cache_page_release(desc);
566 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000567}
568
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400569/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static inline
571int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
572{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400573 int res = -EAGAIN;
Olivier Galibert00a92642005-06-22 17:16:29 +0000574
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400575 while (1) {
576 res = find_cache_page(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (res != -EAGAIN)
578 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400579 desc->page_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return res;
582}
583
584static inline unsigned int dt_type(struct inode *inode)
585{
586 return (inode->i_mode >> 12) & 15;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
590 * Once we've found the start of the dirent within a page: fill 'er up...
591 */
592static
593int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
594 filldir_t filldir)
595{
596 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400597 int i = 0;
598 int res = 0;
599 struct nfs_cache_array *array = NULL;
600 unsigned int d_type = DT_UNKNOWN;
601 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400603 array = nfs_readdir_get_array(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400605 for (i = desc->cache_entry_index; i < array->size; i++) {
606 d_type = DT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400608 res = filldir(dirent, array->array[i].string.name,
609 array->array[i].string.len, file->f_pos,
610 nfs_compat_user_ino64(array->array[i].ino), d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (res < 0)
612 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000613 file->f_pos++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400614 desc->cache_entry_index = i;
615 if (i < (array->size-1))
616 *desc->dir_cookie = array->array[i+1].cookie;
617 else
618 *desc->dir_cookie = array->last_cookie;
619 if (i == array->eof_index) {
620 desc->eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400624
625 nfs_readdir_release_array(desc->page);
626 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (dentry != NULL)
628 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500629 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
630 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return res;
632}
633
634/*
635 * If we cannot find a cookie in our cache, we suspect that this is
636 * because it points to a deleted file, so we ask the server to return
637 * whatever it thinks is the next entry. We then feed this to filldir.
638 * If all goes well, we should then be able to find our way round the
639 * cache on the next call to readdir_search_pagecache();
640 *
641 * NOTE: we cannot add the anonymous page to the pagecache because
642 * the data it contains might not be page aligned. Besides,
643 * we should already have a complete representation of the
644 * directory in the page cache by the time we get here.
645 */
646static inline
647int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
648 filldir_t filldir)
649{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct page *page = NULL;
651 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400652 struct inode *inode = desc->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500654 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
655 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 page = alloc_page(GFP_HIGHUSER);
658 if (!page) {
659 status = -ENOMEM;
660 goto out;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400663 if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
664 status = -EIO;
665 goto out_release;
666 }
667
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400668 desc->page_index = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400669 desc->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 status = nfs_do_filldir(desc, dirent, filldir);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500673 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700674 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return status;
676 out_release:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400677 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto out;
679}
680
Olivier Galibert00a92642005-06-22 17:16:29 +0000681/* The file offset position represents the dirent entry number. A
682 last cookie cache takes care of the common case of reading the
683 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
685static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
686{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800687 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct inode *inode = dentry->d_inode;
689 nfs_readdir_descriptor_t my_desc,
690 *desc = &my_desc;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400691 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Chuck Lever6da24bc2008-06-11 17:55:58 -0400693 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500694 dentry->d_parent->d_name.name, dentry->d_name.name,
695 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500696 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000699 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000700 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000701 * to either find the entry with the appropriate number or
702 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704 memset(desc, 0, sizeof(*desc));
705
706 desc->file = filp;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400707 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 desc->decode = NFS_PROTO(inode)->decode_dirent;
709 desc->plus = NFS_USE_READDIRPLUS(inode);
710
Trond Myklebust565277f2007-10-15 18:17:53 -0400711 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800712 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500713 if (res < 0)
714 goto out;
715
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400716 while (desc->eof != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (res == -EBADCOOKIE) {
720 /* This means either end of directory */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400721 if (*desc->dir_cookie && desc->eof == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Or that the server has 'lost' a cookie */
723 res = uncached_readdir(desc, dirent, filldir);
724 if (res >= 0)
725 continue;
726 }
727 res = 0;
728 break;
729 }
730 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200731 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400733 desc->page_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 desc->plus = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400735 desc->eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 continue;
737 }
738 if (res < 0)
739 break;
740
741 res = nfs_do_filldir(desc, dirent, filldir);
742 if (res < 0) {
743 res = 0;
744 break;
745 }
746 }
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500747out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400748 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500749 if (res > 0)
750 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400751 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500752 dentry->d_parent->d_name.name, dentry->d_name.name,
753 res);
754 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Trond Myklebust10afec902007-05-14 17:16:04 -0400757static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000758{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400759 struct dentry *dentry = filp->f_path.dentry;
760 struct inode *inode = dentry->d_inode;
761
Chuck Lever6da24bc2008-06-11 17:55:58 -0400762 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400763 dentry->d_parent->d_name.name,
764 dentry->d_name.name,
765 offset, origin);
766
767 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000768 switch (origin) {
769 case 1:
770 offset += filp->f_pos;
771 case 0:
772 if (offset >= 0)
773 break;
774 default:
775 offset = -EINVAL;
776 goto out;
777 }
778 if (offset != filp->f_pos) {
779 filp->f_pos = offset;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400780 nfs_file_open_context(filp)->dir_cookie = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000781 }
782out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400783 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000784 return offset;
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
788 * All directory operations under NFS are synchronous, so fsync()
789 * is a dummy operation.
790 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200791static int nfs_fsync_dir(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200793 struct dentry *dentry = filp->f_path.dentry;
794
Chuck Lever6da24bc2008-06-11 17:55:58 -0400795 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500796 dentry->d_parent->d_name.name, dentry->d_name.name,
797 datasync);
798
Chuck Lever54917782008-05-27 16:29:07 -0400799 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801}
802
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400803/**
804 * nfs_force_lookup_revalidate - Mark the directory as having changed
805 * @dir - pointer to directory inode
806 *
807 * This forces the revalidation code in nfs_lookup_revalidate() to do a
808 * full lookup on all child dentries of 'dir' whenever a change occurs
809 * on the server that might have invalidated our dcache.
810 *
811 * The caller should be holding dir->i_lock
812 */
813void nfs_force_lookup_revalidate(struct inode *dir)
814{
Trond Myklebust011935a2008-10-14 19:24:50 -0400815 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * A check for whether or not the parent directory has changed.
820 * In the case it has, we assume that the dentries are untrustworthy
821 * and may need to be looked up again.
822 */
Trond Myklebustc79ba782007-01-31 08:16:24 -0500823static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 if (IS_ROOT(dentry))
826 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400827 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
828 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -0400829 if (!nfs_verify_change_attribute(dir, dentry->d_time))
830 return 0;
831 /* Revalidate nfsi->cache_change_attribute before we declare a match */
832 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
833 return 0;
834 if (!nfs_verify_change_attribute(dir, dentry->d_time))
835 return 0;
836 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400840 * Return the intent data that applies to this particular path component
841 *
842 * Note that the current set of intents only apply to the very last
843 * component of the path.
844 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
845 */
846static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
847{
848 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
849 return 0;
850 return nd->flags & mask;
851}
852
853/*
Trond Myklebusta12802c2007-10-02 19:13:04 -0400854 * Use intent information to check whether or not we're going to do
855 * an O_EXCL create using this path component.
856 */
857static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
858{
859 if (NFS_PROTO(dir)->version == 2)
860 return 0;
Al Viro35165862008-08-05 03:00:49 -0400861 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -0400862}
863
864/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400865 * Inode and filehandle revalidation for lookups.
866 *
867 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
868 * or if the intent information indicates that we're about to open this
869 * particular file and the "nocto" mount flag is not set.
870 *
871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872static inline
873int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
874{
875 struct nfs_server *server = NFS_SERVER(inode);
876
Trond Myklebust4e99a1f2008-03-06 12:34:59 -0500877 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400881 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto out_force;
883 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400884 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400885 !(server->flags & NFS_MOUNT_NOCTO) &&
886 (S_ISREG(inode->i_mode) ||
887 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -0400889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 return nfs_revalidate_inode(server, inode);
892out_force:
893 return __nfs_revalidate_inode(server, inode);
894}
895
896/*
897 * We judge how long we want to trust negative
898 * dentries by looking at the parent inode mtime.
899 *
900 * If parent mtime has changed, we revalidate, else we wait for a
901 * period corresponding to the parent's attribute cache timeout value.
902 */
903static inline
904int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
905 struct nameidata *nd)
906{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400908 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400910 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
911 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return !nfs_check_verifier(dir, dentry);
913}
914
915/*
916 * This is called every time the dcache has a lookup hit,
917 * and we should check whether we can really trust that
918 * lookup.
919 *
920 * NOTE! The hit can be a negative hit too, don't assume
921 * we have an inode!
922 *
923 * If the parent directory is seen to have changed, we throw out the
924 * cached dentry and do a new lookup.
925 */
926static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
927{
928 struct inode *dir;
929 struct inode *inode;
930 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400931 struct nfs_fh *fhandle = NULL;
932 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -0500937 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 inode = dentry->d_inode;
939
940 if (!inode) {
941 if (nfs_neg_need_reval(dir, dentry, nd))
942 goto out_bad;
943 goto out_valid;
944 }
945
946 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500947 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700948 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500949 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto out_bad;
951 }
952
Trond Myklebust15860ab2008-12-23 15:21:54 -0500953 if (nfs_have_delegation(inode, FMODE_READ))
954 goto out_set_verifier;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -0400957 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (nfs_lookup_verify_inode(inode, nd))
959 goto out_zap_parent;
960 goto out_valid;
961 }
962
963 if (NFS_STALE(inode))
964 goto out_bad;
965
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400966 error = -ENOMEM;
967 fhandle = nfs_alloc_fhandle();
968 fattr = nfs_alloc_fattr();
969 if (fhandle == NULL || fattr == NULL)
970 goto out_error;
971
972 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (error)
974 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400975 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400977 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto out_bad;
979
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400980 nfs_free_fattr(fattr);
981 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -0500982out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -0400983 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500986 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700987 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500988 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 1;
990out_zap_parent:
991 nfs_zap_caches(dir);
992 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -0400993 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (inode && S_ISDIR(inode->i_mode)) {
995 /* Purge readdir caches. */
996 nfs_zap_caches(inode);
997 /* If we have submounts, don't unhash ! */
998 if (have_submounts(dentry))
999 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +01001000 if (dentry->d_flags & DCACHE_DISCONNECTED)
1001 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 shrink_dcache_parent(dentry);
1003 }
1004 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001005 nfs_free_fattr(fattr);
1006 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001008 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001009 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001010 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001012out_error:
1013 nfs_free_fattr(fattr);
1014 nfs_free_fhandle(fhandle);
1015 dput(parent);
1016 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1017 __func__, dentry->d_parent->d_name.name,
1018 dentry->d_name.name, error);
1019 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/*
1023 * This is called from dput() when d_count is going to 0.
1024 */
1025static int nfs_dentry_delete(struct dentry *dentry)
1026{
1027 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1028 dentry->d_parent->d_name.name, dentry->d_name.name,
1029 dentry->d_flags);
1030
Trond Myklebust77f11192008-01-28 19:43:19 -05001031 /* Unhash any dentry with a stale inode */
1032 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1033 return 1;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1036 /* Unhash it, so that ->d_iput() would be called */
1037 return 1;
1038 }
1039 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1040 /* Unhash it, so that ancestors of killed async unlink
1041 * files will be cleaned up during umount */
1042 return 1;
1043 }
1044 return 0;
1045
1046}
1047
Trond Myklebust1b83d702008-06-11 15:44:04 -04001048static void nfs_drop_nlink(struct inode *inode)
1049{
1050 spin_lock(&inode->i_lock);
1051 if (inode->i_nlink > 0)
1052 drop_nlink(inode);
1053 spin_unlock(&inode->i_lock);
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * Called when the dentry loses inode.
1058 * We use it to clean up silly-renamed files.
1059 */
1060static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1061{
Neil Brown83672d32007-02-26 12:48:25 +11001062 if (S_ISDIR(inode->i_mode))
1063 /* drop any readdir cache as it could easily be old */
1064 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001067 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001068 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 iput(inode);
1071}
1072
Al Virof786aa92009-02-20 05:51:22 +00001073const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 .d_revalidate = nfs_lookup_revalidate,
1075 .d_delete = nfs_dentry_delete,
1076 .d_iput = nfs_dentry_iput,
1077};
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1080{
1081 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -04001082 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001084 struct nfs_fh *fhandle = NULL;
1085 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1089 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -05001090 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 res = ERR_PTR(-ENAMETOOLONG);
1093 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1094 goto out;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1097
Trond Myklebustfd684072006-09-05 12:27:44 -04001098 /*
1099 * If we're doing an exclusive create, optimize away the lookup
1100 * but don't hash the dentry.
1101 */
1102 if (nfs_is_exclusive_create(dir, nd)) {
1103 d_instantiate(dentry, NULL);
1104 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -04001105 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -04001106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001108 res = ERR_PTR(-ENOMEM);
1109 fhandle = nfs_alloc_fhandle();
1110 fattr = nfs_alloc_fattr();
1111 if (fhandle == NULL || fattr == NULL)
1112 goto out;
1113
Trond Myklebust565277f2007-10-15 18:17:53 -04001114 parent = dentry->d_parent;
1115 /* Protect against concurrent sillydeletes */
1116 nfs_block_sillyrename(parent);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001117 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (error == -ENOENT)
1119 goto no_entry;
1120 if (error < 0) {
1121 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -04001122 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001124 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001125 res = (struct dentry *)inode;
1126 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001127 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -04001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129no_entry:
David Howells54ceac42006-08-22 20:06:13 -04001130 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001131 if (res != NULL) {
1132 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001133 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001137out_unblock_sillyrename:
1138 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001140 nfs_free_fattr(fattr);
1141 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return res;
1143}
1144
1145#ifdef CONFIG_NFS_V4
1146static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1147
Al Virof786aa92009-02-20 05:51:22 +00001148const struct dentry_operations nfs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 .d_revalidate = nfs_open_revalidate,
1150 .d_delete = nfs_dentry_delete,
1151 .d_iput = nfs_dentry_iput,
1152};
1153
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001154/*
1155 * Use intent information to determine whether we need to substitute
1156 * the NFSv4-style stateful OPEN for the LOOKUP call
1157 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001158static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001160 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162 /* NFS does not (yet) have a stateful open for directories */
1163 if (nd->flags & LOOKUP_DIRECTORY)
1164 return 0;
1165 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001166 if (__mnt_is_readonly(nd->path.mnt) &&
1167 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 0;
1169 return 1;
1170}
1171
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001172static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1173{
1174 struct path path = {
1175 .mnt = nd->path.mnt,
1176 .dentry = dentry,
1177 };
1178 struct nfs_open_context *ctx;
1179 struct rpc_cred *cred;
1180 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1181
1182 cred = rpc_lookup_cred();
1183 if (IS_ERR(cred))
1184 return ERR_CAST(cred);
1185 ctx = alloc_nfs_open_context(&path, cred, fmode);
1186 put_rpccred(cred);
1187 if (ctx == NULL)
1188 return ERR_PTR(-ENOMEM);
1189 return ctx;
1190}
1191
1192static int do_open(struct inode *inode, struct file *filp)
1193{
1194 nfs_fscache_set_inode_cookie(inode, filp);
1195 return 0;
1196}
1197
1198static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1199{
1200 struct file *filp;
1201 int ret = 0;
1202
1203 /* If the open_intent is for execute, we have an extra check to make */
1204 if (ctx->mode & FMODE_EXEC) {
1205 ret = nfs_may_open(ctx->path.dentry->d_inode,
1206 ctx->cred,
1207 nd->intent.open.flags);
1208 if (ret < 0)
1209 goto out;
1210 }
1211 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1212 if (IS_ERR(filp))
1213 ret = PTR_ERR(filp);
1214 else
1215 nfs_file_set_open_context(filp, ctx);
1216out:
1217 put_nfs_open_context(ctx);
1218 return ret;
1219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1222{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001223 struct nfs_open_context *ctx;
1224 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct dentry *res = NULL;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001226 struct inode *inode;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001227 int open_flags;
Trond Myklebust898f6352010-10-23 11:24:25 -04001228 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001230 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1231 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* Check that we are indeed trying to open this file */
Trond Myklebust5584c302008-12-23 15:21:54 -05001234 if (!is_atomic_open(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto no_open;
1236
1237 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1238 res = ERR_PTR(-ENAMETOOLONG);
1239 goto out;
1240 }
1241 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1242
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001243 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1244 * the dentry. */
Al Viro35165862008-08-05 03:00:49 -04001245 if (nd->flags & LOOKUP_EXCL) {
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001246 d_instantiate(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001247 goto out;
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001250 ctx = nameidata_to_nfs_open_context(dentry, nd);
1251 res = ERR_CAST(ctx);
1252 if (IS_ERR(ctx))
1253 goto out;
1254
1255 open_flags = nd->intent.open.flags;
1256 if (nd->flags & LOOKUP_CREATE) {
1257 attr.ia_mode = nd->intent.open.create_mode;
1258 attr.ia_valid = ATTR_MODE;
1259 if (!IS_POSIXACL(dir))
1260 attr.ia_mode &= ~current_umask();
1261 } else {
Trond Myklebust898f6352010-10-23 11:24:25 -04001262 open_flags &= ~(O_EXCL | O_CREAT);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001263 attr.ia_valid = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001264 }
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Open the file on the server */
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001267 nfs_block_sillyrename(dentry->d_parent);
Trond Myklebust2b484292010-09-17 10:56:51 -04001268 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001269 if (IS_ERR(inode)) {
1270 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001271 put_nfs_open_context(ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001272 switch (PTR_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* Make a negative dentry */
1274 case -ENOENT:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001275 d_add(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001276 res = NULL;
1277 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* This turned out not to be a regular file */
Trond Myklebust80e60632010-03-25 13:51:05 -04001279 case -EISDIR:
Trond Myklebust6f926b52005-10-18 14:20:18 -07001280 case -ENOTDIR:
1281 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 case -ELOOP:
1283 if (!(nd->intent.open.flags & O_NOFOLLOW))
1284 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* case -EINVAL: */
1286 default:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001287 res = ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto out;
1289 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001290 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001291 res = d_add_unique(dentry, inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001292 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001293 if (res != NULL) {
1294 dput(ctx->path.dentry);
1295 ctx->path.dentry = dget(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 dentry = res;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001297 }
Trond Myklebust898f6352010-10-23 11:24:25 -04001298 err = nfs_intent_set_file(nd, ctx);
1299 if (err < 0) {
1300 if (res != NULL)
1301 dput(res);
1302 return ERR_PTR(err);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304out:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001305 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return res;
1307no_open:
1308 return nfs_lookup(dir, dentry, nd);
1309}
1310
1311static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1312{
1313 struct dentry *parent = NULL;
1314 struct inode *inode = dentry->d_inode;
1315 struct inode *dir;
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001316 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int openflags, ret = 0;
1318
Trond Myklebust1f063d22010-04-22 15:35:55 -04001319 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001320 goto no_open;
Trond Myklebust2b484292010-09-17 10:56:51 -04001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 parent = dget_parent(dentry);
1323 dir = parent->d_inode;
Trond Myklebust2b484292010-09-17 10:56:51 -04001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* We can't create new files in nfs_open_revalidate(), so we
1326 * optimize away revalidation of negative dentries.
1327 */
Trond Myklebust216d5d062007-10-01 20:10:12 -04001328 if (inode == NULL) {
1329 if (!nfs_neg_need_reval(dir, dentry, nd))
1330 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto out;
Trond Myklebust216d5d062007-10-01 20:10:12 -04001332 }
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* NFS only supports OPEN on regular files */
1335 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001336 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 openflags = nd->intent.open.flags;
1338 /* We cannot do exclusive creation on a positive dentry */
1339 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001340 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* We can't create new files, or truncate existing ones here */
Trond Myklebust0a377cf2010-08-18 09:25:42 -04001342 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001344 ctx = nameidata_to_nfs_open_context(dentry, nd);
1345 ret = PTR_ERR(ctx);
1346 if (IS_ERR(ctx))
1347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001349 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * operations that change the directory. We therefore save the
1351 * change attribute *before* we do the RPC call.
1352 */
Trond Myklebust2b484292010-09-17 10:56:51 -04001353 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
Trond Myklebust535918f2010-09-17 10:56:51 -04001354 if (IS_ERR(inode)) {
1355 ret = PTR_ERR(inode);
1356 switch (ret) {
1357 case -EPERM:
1358 case -EACCES:
1359 case -EDQUOT:
1360 case -ENOSPC:
1361 case -EROFS:
1362 goto out_put_ctx;
1363 default:
1364 goto out_drop;
1365 }
1366 }
1367 iput(inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001368 if (inode != dentry->d_inode)
Trond Myklebust535918f2010-09-17 10:56:51 -04001369 goto out_drop;
Trond Myklebust898f6352010-10-23 11:24:25 -04001370
1371 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1372 ret = nfs_intent_set_file(nd, ctx);
1373 if (ret >= 0)
1374 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375out:
1376 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001378out_drop:
1379 d_drop(dentry);
1380 ret = 0;
1381out_put_ctx:
1382 put_nfs_open_context(ctx);
1383 goto out;
1384
Trond Myklebust5584c302008-12-23 15:21:54 -05001385no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001387no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return nfs_lookup_revalidate(dentry, nd);
1389}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001390
1391static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1392 struct nameidata *nd)
1393{
1394 struct nfs_open_context *ctx = NULL;
1395 struct iattr attr;
1396 int error;
1397 int open_flags = 0;
1398
1399 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1400 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1401
1402 attr.ia_mode = mode;
1403 attr.ia_valid = ATTR_MODE;
1404
1405 if ((nd->flags & LOOKUP_CREATE) != 0) {
1406 open_flags = nd->intent.open.flags;
1407
1408 ctx = nameidata_to_nfs_open_context(dentry, nd);
1409 error = PTR_ERR(ctx);
1410 if (IS_ERR(ctx))
Trond Myklebust898f6352010-10-23 11:24:25 -04001411 goto out_err_drop;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001412 }
1413
1414 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1415 if (error != 0)
1416 goto out_put_ctx;
Trond Myklebust898f6352010-10-23 11:24:25 -04001417 if (ctx != NULL) {
1418 error = nfs_intent_set_file(nd, ctx);
1419 if (error < 0)
1420 goto out_err;
1421 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001422 return 0;
1423out_put_ctx:
1424 if (ctx != NULL)
1425 put_nfs_open_context(ctx);
Trond Myklebust898f6352010-10-23 11:24:25 -04001426out_err_drop:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001427 d_drop(dentry);
Trond Myklebust898f6352010-10-23 11:24:25 -04001428out_err:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001429 return error;
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#endif /* CONFIG_NFSV4 */
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434/*
1435 * Code common to create, mkdir, and mknod.
1436 */
1437int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1438 struct nfs_fattr *fattr)
1439{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001440 struct dentry *parent = dget_parent(dentry);
1441 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct inode *inode;
1443 int error = -EACCES;
1444
Trond Myklebustfab728e2007-09-29 17:41:33 -04001445 d_drop(dentry);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* We may have been initialized further down */
1448 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (fhandle->size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1452 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001453 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001455 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1457 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001458 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001460 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001463 error = PTR_ERR(inode);
1464 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001465 goto out_error;
1466 d_add(dentry, inode);
1467out:
1468 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001470out_error:
1471 nfs_mark_for_revalidate(dir);
1472 dput(parent);
1473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476/*
1477 * Following a failed create operation, we drop the dentry rather
1478 * than retain a negative dentry. This avoids a problem in the event
1479 * that the operation succeeded on the server, but an error in the
1480 * reply path made it appear to have failed.
1481 */
1482static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1483 struct nameidata *nd)
1484{
1485 struct iattr attr;
1486 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001488 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1489 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 attr.ia_mode = mode;
1492 attr.ia_valid = ATTR_MODE;
1493
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001494 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (error != 0)
1496 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 d_drop(dentry);
1500 return error;
1501}
1502
1503/*
1504 * See comments for nfs_proc_create regarding failed operations.
1505 */
1506static int
1507nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1508{
1509 struct iattr attr;
1510 int status;
1511
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001512 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1513 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 if (!new_valid_dev(rdev))
1516 return -EINVAL;
1517
1518 attr.ia_mode = mode;
1519 attr.ia_valid = ATTR_MODE;
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (status != 0)
1523 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
1525out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 d_drop(dentry);
1527 return status;
1528}
1529
1530/*
1531 * See comments for nfs_proc_create regarding failed operations.
1532 */
1533static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1534{
1535 struct iattr attr;
1536 int error;
1537
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001538 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1539 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 attr.ia_valid = ATTR_MODE;
1542 attr.ia_mode = mode | S_IFDIR;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (error != 0)
1546 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 0;
1548out_err:
1549 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return error;
1551}
1552
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001553static void nfs_dentry_handle_enoent(struct dentry *dentry)
1554{
1555 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1556 d_delete(dentry);
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1560{
1561 int error;
1562
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001563 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1564 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1567 /* Ensure the VFS deletes this inode */
1568 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001569 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001570 else if (error == -ENOENT)
1571 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 return error;
1574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
1577 * Remove a file after making sure there are no pending writes,
1578 * and after checking that the file has only one user.
1579 *
1580 * We invalidate the attribute cache and free the inode prior to the operation
1581 * to avoid possible races if the server reuses the inode.
1582 */
1583static int nfs_safe_remove(struct dentry *dentry)
1584{
1585 struct inode *dir = dentry->d_parent->d_inode;
1586 struct inode *inode = dentry->d_inode;
1587 int error = -EBUSY;
1588
1589 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1590 dentry->d_parent->d_name.name, dentry->d_name.name);
1591
1592 /* If the dentry was sillyrenamed, we simply call d_delete() */
1593 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1594 error = 0;
1595 goto out;
1596 }
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001599 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1601 /* The VFS may want to delete this inode */
1602 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001603 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001604 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 } else
1606 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001607 if (error == -ENOENT)
1608 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609out:
1610 return error;
1611}
1612
1613/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1614 * belongs to an active ".nfs..." file and we return -EBUSY.
1615 *
1616 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1617 */
1618static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1619{
1620 int error;
1621 int need_rehash = 0;
1622
1623 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1624 dir->i_ino, dentry->d_name.name);
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 spin_lock(&dcache_lock);
1627 spin_lock(&dentry->d_lock);
1628 if (atomic_read(&dentry->d_count) > 1) {
1629 spin_unlock(&dentry->d_lock);
1630 spin_unlock(&dcache_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001631 /* Start asynchronous writeout of the inode */
1632 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return error;
1635 }
1636 if (!d_unhashed(dentry)) {
1637 __d_drop(dentry);
1638 need_rehash = 1;
1639 }
1640 spin_unlock(&dentry->d_lock);
1641 spin_unlock(&dcache_lock);
1642 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001643 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1645 } else if (need_rehash)
1646 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return error;
1648}
1649
Chuck Lever873101b2006-08-22 20:06:23 -04001650/*
1651 * To create a symbolic link, most file systems instantiate a new inode,
1652 * add a page to it containing the path, then write it out to the disk
1653 * using prepare_write/commit_write.
1654 *
1655 * Unfortunately the NFS client can't create the in-core inode first
1656 * because it needs a file handle to create an in-core inode (see
1657 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1658 * symlink request has completed on the server.
1659 *
1660 * So instead we allocate a raw page, copy the symname into it, then do
1661 * the SYMLINK request with the page as the buffer. If it succeeds, we
1662 * now have a new file handle and can instantiate an in-core NFS inode
1663 * and move the raw page into its mapping.
1664 */
1665static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Chuck Lever873101b2006-08-22 20:06:23 -04001667 struct pagevec lru_pvec;
1668 struct page *page;
1669 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001671 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int error;
1673
1674 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1675 dir->i_ino, dentry->d_name.name, symname);
1676
Chuck Lever873101b2006-08-22 20:06:23 -04001677 if (pathlen > PAGE_SIZE)
1678 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Chuck Lever873101b2006-08-22 20:06:23 -04001680 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1681 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jeff Layton83d93f22007-06-07 09:58:08 -04001683 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001684 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001685 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001686
1687 kaddr = kmap_atomic(page, KM_USER0);
1688 memcpy(kaddr, symname, pathlen);
1689 if (pathlen < PAGE_SIZE)
1690 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1691 kunmap_atomic(kaddr, KM_USER0);
1692
Chuck Lever94a6d752006-08-22 20:06:23 -04001693 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001694 if (error != 0) {
1695 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1696 dir->i_sb->s_id, dir->i_ino,
1697 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001699 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001700 return error;
1701 }
1702
1703 /*
1704 * No big deal if we can't add this page to the page cache here.
1705 * READLINK will get the missing page from the server if needed.
1706 */
1707 pagevec_init(&lru_pvec, 0);
1708 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1709 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001710 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001711 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001712 SetPageUptodate(page);
1713 unlock_page(page);
1714 } else
1715 __free_page(page);
1716
Chuck Lever873101b2006-08-22 20:06:23 -04001717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720static int
1721nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1722{
1723 struct inode *inode = old_dentry->d_inode;
1724 int error;
1725
1726 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1727 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1728 dentry->d_parent->d_name.name, dentry->d_name.name);
1729
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001730 nfs_inode_return_delegation(inode);
1731
Trond Myklebust9697d232007-10-02 21:58:05 -04001732 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001734 if (error == 0) {
1735 atomic_inc(&inode->i_count);
Trond Myklebust9697d232007-10-02 21:58:05 -04001736 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return error;
1739}
1740
1741/*
1742 * RENAME
1743 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1744 * different file handle for the same inode after a rename (e.g. when
1745 * moving to a different directory). A fail-safe method to do so would
1746 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1747 * rename the old file using the sillyrename stuff. This way, the original
1748 * file in old_dir will go away when the last process iput()s the inode.
1749 *
1750 * FIXED.
1751 *
1752 * It actually works quite well. One needs to have the possibility for
1753 * at least one ".nfs..." file in each directory the file ever gets
1754 * moved or linked to which happens automagically with the new
1755 * implementation that only depends on the dcache stuff instead of
1756 * using the inode layer
1757 *
1758 * Unfortunately, things are a little more complicated than indicated
1759 * above. For a cross-directory move, we want to make sure we can get
1760 * rid of the old inode after the operation. This means there must be
1761 * no pending writes (if it's a file), and the use count must be 1.
1762 * If these conditions are met, we can drop the dentries before doing
1763 * the rename.
1764 */
1765static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1766 struct inode *new_dir, struct dentry *new_dentry)
1767{
1768 struct inode *old_inode = old_dentry->d_inode;
1769 struct inode *new_inode = new_dentry->d_inode;
1770 struct dentry *dentry = NULL, *rehash = NULL;
1771 int error = -EBUSY;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1774 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1775 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1776 atomic_read(&new_dentry->d_count));
1777
1778 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001779 * For non-directories, check whether the target is busy and if so,
1780 * make a copy of the dentry and then do a silly-rename. If the
1781 * silly-rename succeeds, the copied dentry is hashed and becomes
1782 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001784 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1785 /*
1786 * To prevent any new references to the target during the
1787 * rename, we unhash the dentry in advance.
1788 */
1789 if (!d_unhashed(new_dentry)) {
1790 d_drop(new_dentry);
1791 rehash = new_dentry;
1792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Miklos Szeredi27226102009-12-03 15:58:56 -05001794 if (atomic_read(&new_dentry->d_count) > 2) {
1795 int err;
1796
1797 /* copy the target dentry's name */
1798 dentry = d_alloc(new_dentry->d_parent,
1799 &new_dentry->d_name);
1800 if (!dentry)
1801 goto out;
1802
1803 /* silly-rename the existing target ... */
1804 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001805 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001806 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001807
1808 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001809 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001810 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05001811 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Trond Myklebustcae7a072005-10-18 14:20:19 -07001814 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001815 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01001816 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1819 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001820 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821out:
1822 if (rehash)
1823 d_rehash(rehash);
1824 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001825 if (new_inode != NULL)
1826 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07001827 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04001828 nfs_set_verifier(new_dentry,
1829 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001830 } else if (error == -ENOENT)
1831 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /* new dentry created? */
1834 if (dentry)
1835 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return error;
1837}
1838
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001839static DEFINE_SPINLOCK(nfs_access_lru_lock);
1840static LIST_HEAD(nfs_access_lru_list);
1841static atomic_long_t nfs_access_nr_entries;
1842
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001843static void nfs_access_free_entry(struct nfs_access_entry *entry)
1844{
1845 put_rpccred(entry->cred);
1846 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001847 smp_mb__before_atomic_dec();
1848 atomic_long_dec(&nfs_access_nr_entries);
1849 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001850}
1851
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001852static void nfs_access_free_list(struct list_head *head)
1853{
1854 struct nfs_access_entry *cache;
1855
1856 while (!list_empty(head)) {
1857 cache = list_entry(head->next, struct nfs_access_entry, lru);
1858 list_del(&cache->lru);
1859 nfs_access_free_entry(cache);
1860 }
1861}
1862
Dave Chinner7f8275d2010-07-19 14:56:17 +10001863int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebust979df722006-07-25 11:28:19 -04001864{
1865 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001866 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04001867 struct nfs_access_entry *cache;
1868
Trond Myklebust61d5eb22010-05-13 12:51:06 -04001869 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1870 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001871
Trond Myklebusta50f7952007-06-05 19:23:43 -04001872 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001873 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04001874 struct inode *inode;
1875
1876 if (nr_to_scan-- == 0)
1877 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001878 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04001879 spin_lock(&inode->i_lock);
1880 if (list_empty(&nfsi->access_cache_entry_lru))
1881 goto remove_lru_entry;
1882 cache = list_entry(nfsi->access_cache_entry_lru.next,
1883 struct nfs_access_entry, lru);
1884 list_move(&cache->lru, &head);
1885 rb_erase(&cache->rb_node, &nfsi->access_cache);
1886 if (!list_empty(&nfsi->access_cache_entry_lru))
1887 list_move_tail(&nfsi->access_cache_inode_lru,
1888 &nfs_access_lru_list);
1889 else {
1890remove_lru_entry:
1891 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001892 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001893 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001894 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001895 }
Trond Myklebust59844a92010-05-26 08:42:24 -04001896 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04001897 }
1898 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001899 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04001900 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1901}
1902
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001903static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001904{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001905 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001906 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001907 struct nfs_access_entry *entry;
1908
1909 /* Unhook entries from the cache */
1910 while ((n = rb_first(root_node)) != NULL) {
1911 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1912 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001913 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001914 }
1915 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001916}
1917
1918void nfs_access_zap_cache(struct inode *inode)
1919{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001920 LIST_HEAD(head);
1921
1922 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
1923 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001924 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001925 spin_lock(&nfs_access_lru_lock);
1926 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001927 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001928
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001929 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001930 __nfs_access_zap_cache(NFS_I(inode), &head);
1931 spin_unlock(&inode->i_lock);
1932 spin_unlock(&nfs_access_lru_lock);
1933 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001934}
1935
1936static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
1937{
1938 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
1939 struct nfs_access_entry *entry;
1940
1941 while (n != NULL) {
1942 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1943
1944 if (cred < entry->cred)
1945 n = n->rb_left;
1946 else if (cred > entry->cred)
1947 n = n->rb_right;
1948 else
1949 return entry;
1950 }
1951 return NULL;
1952}
1953
Trond Myklebustaf22f942007-08-10 17:45:10 -04001954static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Chuck Lever55296802005-08-18 11:24:09 -07001956 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001957 struct nfs_access_entry *cache;
1958 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001960 spin_lock(&inode->i_lock);
1961 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
1962 goto out_zap;
1963 cache = nfs_access_search_rbtree(inode, cred);
1964 if (cache == NULL)
1965 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05001966 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05001967 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001968 goto out_stale;
1969 res->jiffies = cache->jiffies;
1970 res->cred = cache->cred;
1971 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001972 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001973 err = 0;
1974out:
1975 spin_unlock(&inode->i_lock);
1976 return err;
1977out_stale:
1978 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001979 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001980 spin_unlock(&inode->i_lock);
1981 nfs_access_free_entry(cache);
1982 return -ENOENT;
1983out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001984 spin_unlock(&inode->i_lock);
1985 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001986 return -ENOENT;
1987}
1988
1989static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
1990{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001991 struct nfs_inode *nfsi = NFS_I(inode);
1992 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001993 struct rb_node **p = &root_node->rb_node;
1994 struct rb_node *parent = NULL;
1995 struct nfs_access_entry *entry;
1996
1997 spin_lock(&inode->i_lock);
1998 while (*p != NULL) {
1999 parent = *p;
2000 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2001
2002 if (set->cred < entry->cred)
2003 p = &parent->rb_left;
2004 else if (set->cred > entry->cred)
2005 p = &parent->rb_right;
2006 else
2007 goto found;
2008 }
2009 rb_link_node(&set->rb_node, parent, p);
2010 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002011 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002012 spin_unlock(&inode->i_lock);
2013 return;
2014found:
2015 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002016 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2017 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002018 spin_unlock(&inode->i_lock);
2019 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Trond Myklebustaf22f942007-08-10 17:45:10 -04002022static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002024 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2025 if (cache == NULL)
2026 return;
2027 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002029 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002031
2032 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002033
2034 /* Update accounting */
2035 smp_mb__before_atomic_inc();
2036 atomic_long_inc(&nfs_access_nr_entries);
2037 smp_mb__after_atomic_inc();
2038
2039 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002040 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002041 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002042 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2043 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2044 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002045 spin_unlock(&nfs_access_lru_lock);
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2050{
2051 struct nfs_access_entry cache;
2052 int status;
2053
2054 status = nfs_access_get_cached(inode, cred, &cache);
2055 if (status == 0)
2056 goto out;
2057
2058 /* Be clever: ask server to check for all possible rights */
2059 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2060 cache.cred = cred;
2061 cache.jiffies = jiffies;
2062 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002063 if (status != 0) {
2064 if (status == -ESTALE) {
2065 nfs_zap_caches(inode);
2066 if (!S_ISDIR(inode->i_mode))
2067 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 nfs_access_add_cache(inode, &cache);
2072out:
Al Viroe6305c42008-07-15 21:03:57 -04002073 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075 return -EACCES;
2076}
2077
Trond Myklebustaf22f942007-08-10 17:45:10 -04002078static int nfs_open_permission_mask(int openflags)
2079{
2080 int mask = 0;
2081
2082 if (openflags & FMODE_READ)
2083 mask |= MAY_READ;
2084 if (openflags & FMODE_WRITE)
2085 mask |= MAY_WRITE;
2086 if (openflags & FMODE_EXEC)
2087 mask |= MAY_EXEC;
2088 return mask;
2089}
2090
2091int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2092{
2093 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2094}
2095
Al Viroe6305c42008-07-15 21:03:57 -04002096int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 struct rpc_cred *cred;
2099 int res = 0;
2100
Chuck Lever91d5b472006-03-20 13:44:14 -05002101 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2102
Al Viroe6305c42008-07-15 21:03:57 -04002103 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto out;
2105 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002106 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto force_lookup;
2108
2109 switch (inode->i_mode & S_IFMT) {
2110 case S_IFLNK:
2111 goto out;
2112 case S_IFREG:
2113 /* NFSv4 has atomic_open... */
2114 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002115 && (mask & MAY_OPEN)
2116 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 goto out;
2118 break;
2119 case S_IFDIR:
2120 /*
2121 * Optimize away all write operations, since the server
2122 * will check permissions when we perform the op.
2123 */
2124 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2125 goto out;
2126 }
2127
2128force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!NFS_PROTO(inode)->access)
2130 goto out_notsup;
2131
Trond Myklebust98a8e322008-03-12 12:25:28 -04002132 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (!IS_ERR(cred)) {
2134 res = nfs_do_access(inode, cred, mask);
2135 put_rpccred(cred);
2136 } else
2137 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002139 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2140 res = -EACCES;
2141
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002142 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2143 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return res;
2145out_notsup:
2146 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2147 if (res == 0)
2148 res = generic_permission(inode, mask, NULL);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152/*
2153 * Local variables:
2154 * version-control: t
2155 * kept-new-versions: 5
2156 * End:
2157 */