blob: 635ff65d3092fa49c841d38d6492f73b970617d9 [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>
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -040036#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050039#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080040#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040041#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* #define NFS_DEBUG_VERBOSE 1 */
44
45static int nfs_opendir(struct inode *, struct file *);
46static int nfs_readdir(struct file *, void *, filldir_t);
47static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
48static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
49static int nfs_mkdir(struct inode *, struct dentry *, int);
50static int nfs_rmdir(struct inode *, struct dentry *);
51static int nfs_unlink(struct inode *, struct dentry *);
52static int nfs_symlink(struct inode *, struct dentry *, const char *);
53static int nfs_link(struct dentry *, struct inode *, struct dentry *);
54static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
55static int nfs_rename(struct inode *, struct dentry *,
56 struct inode *, struct dentry *);
Christoph Hellwig7ea80852010-05-26 17:53:25 +020057static int nfs_fsync_dir(struct file *, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000058static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040059static int nfs_readdir_clear_array(struct page*, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080061const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000062 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .read = generic_read_dir,
64 .readdir = nfs_readdir,
65 .open = nfs_opendir,
66 .release = nfs_release,
67 .fsync = nfs_fsync_dir,
68};
69
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080070const struct inode_operations nfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .create = nfs_create,
72 .lookup = nfs_lookup,
73 .link = nfs_link,
74 .unlink = nfs_unlink,
75 .symlink = nfs_symlink,
76 .mkdir = nfs_mkdir,
77 .rmdir = nfs_rmdir,
78 .mknod = nfs_mknod,
79 .rename = nfs_rename,
80 .permission = nfs_permission,
81 .getattr = nfs_getattr,
82 .setattr = nfs_setattr,
83};
84
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040085const struct address_space_operations nfs_dir_addr_space_ops = {
86 .releasepage = nfs_readdir_clear_array,
87};
88
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000089#ifdef CONFIG_NFS_V3
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080090const struct inode_operations nfs3_dir_inode_operations = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000091 .create = nfs_create,
92 .lookup = nfs_lookup,
93 .link = nfs_link,
94 .unlink = nfs_unlink,
95 .symlink = nfs_symlink,
96 .mkdir = nfs_mkdir,
97 .rmdir = nfs_rmdir,
98 .mknod = nfs_mknod,
99 .rename = nfs_rename,
100 .permission = nfs_permission,
101 .getattr = nfs_getattr,
102 .setattr = nfs_setattr,
103 .listxattr = nfs3_listxattr,
104 .getxattr = nfs3_getxattr,
105 .setxattr = nfs3_setxattr,
106 .removexattr = nfs3_removexattr,
107};
108#endif /* CONFIG_NFS_V3 */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifdef CONFIG_NFS_V4
111
112static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400113static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800114const struct inode_operations nfs4_dir_inode_operations = {
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400115 .create = nfs_open_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .lookup = nfs_atomic_lookup,
117 .link = nfs_link,
118 .unlink = nfs_unlink,
119 .symlink = nfs_symlink,
120 .mkdir = nfs_mkdir,
121 .rmdir = nfs_rmdir,
122 .mknod = nfs_mknod,
123 .rename = nfs_rename,
124 .permission = nfs_permission,
125 .getattr = nfs_getattr,
126 .setattr = nfs_setattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +0000127 .getxattr = nfs4_getxattr,
128 .setxattr = nfs4_setxattr,
129 .listxattr = nfs4_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132#endif /* CONFIG_NFS_V4 */
133
134/*
135 * Open file
136 */
137static int
138nfs_opendir(struct inode *inode, struct file *filp)
139{
Carsten Otte7451c4f2006-04-19 13:06:37 -0400140 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Chuck Lever6da24bc2008-06-11 17:55:58 -0400142 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400143 filp->f_path.dentry->d_parent->d_name.name,
144 filp->f_path.dentry->d_name.name);
145
146 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Call generic open code in order to cache credentials */
Carsten Otte7451c4f2006-04-19 13:06:37 -0400149 res = nfs_open(inode, filp);
Neil Brownf5a73672010-08-10 10:20:05 -0400150 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
151 /* This is a mountpoint, so d_revalidate will never
152 * have been called, so we need to refresh the
153 * inode (for close-open consistency) ourselves.
154 */
155 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return res;
158}
159
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400160struct nfs_cache_array_entry {
161 u64 cookie;
162 u64 ino;
163 struct qstr string;
164};
165
166struct nfs_cache_array {
167 unsigned int size;
168 int eof_index;
169 u64 last_cookie;
170 struct nfs_cache_array_entry array[0];
171};
172
173#define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
174
Bryan Schumaker82f2e542010-10-21 16:33:18 -0400175typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, struct nfs_server *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176typedef struct {
177 struct file *file;
178 struct page *page;
179 unsigned long page_index;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000180 u64 *dir_cookie;
181 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 decode_dirent_t decode;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400183
Neil Brown1f4eab72007-04-16 09:35:27 +1000184 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400185 unsigned long gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400186 unsigned int cache_entry_index;
187 unsigned int plus:1;
188 unsigned int eof:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189} nfs_readdir_descriptor_t;
190
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400191/*
192 * The caller is responsible for calling nfs_readdir_release_array(page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
194static
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400195struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400197 if (page == NULL)
198 return ERR_PTR(-EIO);
199 return (struct nfs_cache_array *)kmap(page);
200}
201
202static
203void nfs_readdir_release_array(struct page *page)
204{
205 kunmap(page);
206}
207
208/*
209 * we are freeing strings created by nfs_add_to_readdir_array()
210 */
211static
212int nfs_readdir_clear_array(struct page *page, gfp_t mask)
213{
214 struct nfs_cache_array *array = nfs_readdir_get_array(page);
215 int i;
216 for (i = 0; i < array->size; i++)
217 kfree(array->array[i].string.name);
218 nfs_readdir_release_array(page);
219 return 0;
220}
221
222/*
223 * the caller is responsible for freeing qstr.name
224 * when called by nfs_readdir_add_to_array, the strings will be freed in
225 * nfs_clear_readdir_array()
226 */
227static
Trond Myklebust4a201d62010-10-23 14:53:23 -0400228int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400229{
230 string->len = len;
231 string->name = kmemdup(name, len, GFP_KERNEL);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400232 if (string->name == NULL)
233 return -ENOMEM;
234 string->hash = full_name_hash(name, len);
235 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400236}
237
238static
239int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
240{
241 struct nfs_cache_array *array = nfs_readdir_get_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400242 struct nfs_cache_array_entry *cache_entry;
243 int ret;
244
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400245 if (IS_ERR(array))
246 return PTR_ERR(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400247 ret = -EIO;
248 if (array->size >= MAX_READDIR_ARRAY)
249 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400250
Trond Myklebust4a201d62010-10-23 14:53:23 -0400251 cache_entry = &array->array[array->size];
252 cache_entry->cookie = entry->prev_cookie;
253 cache_entry->ino = entry->ino;
254 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
255 if (ret)
256 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400257 array->last_cookie = entry->cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400258 if (entry->eof == 1)
259 array->eof_index = array->size;
260 array->size++;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400261out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400262 nfs_readdir_release_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400263 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400264}
265
266static
267int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
268{
269 loff_t diff = desc->file->f_pos - desc->current_index;
270 unsigned int index;
271
272 if (diff < 0)
273 goto out_eof;
274 if (diff >= array->size) {
275 if (array->eof_index > 0)
276 goto out_eof;
277 desc->current_index += array->size;
278 return -EAGAIN;
279 }
280
281 index = (unsigned int)diff;
282 *desc->dir_cookie = array->array[index].cookie;
283 desc->cache_entry_index = index;
284 if (index == array->eof_index)
285 desc->eof = 1;
286 return 0;
287out_eof:
288 desc->eof = 1;
289 return -EBADCOOKIE;
290}
291
292static
293int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
294{
295 int i;
296 int status = -EAGAIN;
297
298 for (i = 0; i < array->size; i++) {
299 if (i == array->eof_index) {
300 desc->eof = 1;
301 status = -EBADCOOKIE;
302 }
303 if (array->array[i].cookie == *desc->dir_cookie) {
304 desc->cache_entry_index = i;
305 status = 0;
306 break;
307 }
308 }
309
310 return status;
311}
312
313static
314int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
315{
316 struct nfs_cache_array *array;
317 int status = -EBADCOOKIE;
318
319 if (desc->dir_cookie == NULL)
320 goto out;
321
322 array = nfs_readdir_get_array(desc->page);
323 if (IS_ERR(array)) {
324 status = PTR_ERR(array);
325 goto out;
326 }
327
328 if (*desc->dir_cookie == 0)
329 status = nfs_readdir_search_for_pos(array, desc);
330 else
331 status = nfs_readdir_search_for_cookie(array, desc);
332
333 nfs_readdir_release_array(desc->page);
334out:
335 return status;
336}
337
338/* Fill a page with xdr information before transferring to the cache page */
339static
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400340int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400341 struct nfs_entry *entry, struct file *file, struct inode *inode)
342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct rpc_cred *cred = nfs_file_cred(file);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400344 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int error;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 again:
348 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400349 gencount = nfs_inc_attr_generation_counter();
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400350 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 NFS_SERVER(inode)->dtsize, desc->plus);
352 if (error < 0) {
353 /* We requested READDIRPLUS, but the server doesn't grok it */
354 if (error == -ENOTSUPP && desc->plus) {
355 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200356 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 desc->plus = 0;
358 goto again;
359 }
360 goto error;
361 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000362 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400363 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400364error:
365 return error;
366}
367
368/* Fill in an entry based on the xdr code stored in desc->page */
369static
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400370int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400371{
Bryan Schumaker82f2e542010-10-21 16:33:18 -0400372 __be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400373 if (IS_ERR(p))
374 return PTR_ERR(p);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400375
376 entry->fattr->time_start = desc->timestamp;
377 entry->fattr->gencount = desc->gencount;
378 return 0;
379}
380
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400381static
382int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
383{
384 struct nfs_inode *node;
385 if (dentry->d_inode == NULL)
386 goto different;
387 node = NFS_I(dentry->d_inode);
388 if (node->fh.size != entry->fh->size)
389 goto different;
390 if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
391 goto different;
392 return 1;
393different:
394 return 0;
395}
396
397static
398void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
399{
Trond Myklebust4a201d62010-10-23 14:53:23 -0400400 struct qstr filename = {
401 .len = entry->len,
402 .name = entry->name,
403 };
404 struct dentry *dentry;
405 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400406 struct inode *dir = parent->d_inode;
407 struct inode *inode;
408
Trond Myklebust4a201d62010-10-23 14:53:23 -0400409 if (filename.name[0] == '.') {
410 if (filename.len == 1)
411 return;
412 if (filename.len == 2 && filename.name[1] == '.')
413 return;
414 }
415 filename.hash = full_name_hash(filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400416
Trond Myklebust4a201d62010-10-23 14:53:23 -0400417 dentry = d_lookup(parent, &filename);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400418 if (dentry != NULL) {
419 if (nfs_same_file(dentry, entry)) {
420 nfs_refresh_inode(dentry->d_inode, entry->fattr);
421 goto out;
422 } else {
423 d_drop(dentry);
424 dput(dentry);
425 }
426 }
427
428 dentry = d_alloc(parent, &filename);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400429 if (dentry == NULL)
430 return;
431
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400432 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
433 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
434 if (IS_ERR(inode))
435 goto out;
436
437 alias = d_materialise_unique(dentry, inode);
438 if (IS_ERR(alias))
439 goto out;
440 else if (alias) {
441 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
442 dput(alias);
443 } else
444 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
445
446out:
447 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400448}
449
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400450/* Perform conversion from xdr to cache array */
451static
452void nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400453 void *xdr_page, struct page *page, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400454{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400455 struct xdr_stream stream;
456 struct xdr_buf buf;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400457 __be32 *ptr = xdr_page;
Bryan Schumaker99424382010-10-21 16:33:16 -0400458 int status;
459 struct nfs_cache_array *array;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400460
461 buf.head->iov_base = xdr_page;
462 buf.head->iov_len = buflen;
463 buf.tail->iov_len = 0;
464 buf.page_base = 0;
465 buf.page_len = 0;
466 buf.buflen = buf.head->iov_len;
467 buf.len = buf.head->iov_len;
468
469 xdr_init_decode(&stream, &buf, ptr);
470
Bryan Schumaker99424382010-10-21 16:33:16 -0400471
472 do {
473 status = xdr_decode(desc, entry, &stream);
474 if (status != 0)
475 break;
476
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400477 if (nfs_readdir_add_to_array(entry, page) == -1)
478 break;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400479 if (desc->plus == 1)
480 nfs_prime_dcache(desc->file->f_path.dentry, entry);
Bryan Schumaker99424382010-10-21 16:33:16 -0400481 } while (!entry->eof);
482
483 if (status == -EBADCOOKIE && entry->eof) {
484 array = nfs_readdir_get_array(page);
485 array->eof_index = array->size - 1;
486 status = 0;
487 nfs_readdir_release_array(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400488 }
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400489}
490
491static
492void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
493{
494 unsigned int i;
495 for (i = 0; i < npages; i++)
496 put_page(pages[i]);
497}
498
499static
500void nfs_readdir_free_large_page(void *ptr, struct page **pages,
501 unsigned int npages)
502{
503 vm_unmap_ram(ptr, npages);
504 nfs_readdir_free_pagearray(pages, npages);
505}
506
507/*
508 * nfs_readdir_large_page will allocate pages that must be freed with a call
509 * to nfs_readdir_free_large_page
510 */
511static
512void *nfs_readdir_large_page(struct page **pages, unsigned int npages)
513{
514 void *ptr;
515 unsigned int i;
516
517 for (i = 0; i < npages; i++) {
518 struct page *page = alloc_page(GFP_KERNEL);
519 if (page == NULL)
520 goto out_freepages;
521 pages[i] = page;
522 }
523
Trond Myklebust4a201d62010-10-23 14:53:23 -0400524 ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400525 if (!IS_ERR_OR_NULL(ptr))
526 return ptr;
527out_freepages:
528 nfs_readdir_free_pagearray(pages, i);
529 return NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400530}
531
532static
533int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
534{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400535 struct page *pages[NFS_MAX_READDIR_PAGES];
536 void *pages_ptr = NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400537 struct nfs_entry entry;
538 struct file *file = desc->file;
539 struct nfs_cache_array *array;
540 int status = 0;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400541 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400542
543 entry.prev_cookie = 0;
544 entry.cookie = *desc->dir_cookie;
545 entry.eof = 0;
546 entry.fh = nfs_alloc_fhandle();
547 entry.fattr = nfs_alloc_fattr();
548 if (entry.fh == NULL || entry.fattr == NULL)
549 goto out;
550
551 array = nfs_readdir_get_array(page);
552 memset(array, 0, sizeof(struct nfs_cache_array));
553 array->eof_index = -1;
554
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400555 pages_ptr = nfs_readdir_large_page(pages, array_size);
556 if (!pages_ptr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400557 goto out_release_array;
558 do {
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400559 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400560
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400561 if (status < 0)
562 break;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400563 nfs_readdir_page_filler(desc, &entry, pages_ptr, page, array_size * PAGE_SIZE);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400564 } while (array->eof_index < 0 && array->size < MAX_READDIR_ARRAY);
565
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400566 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400567out_release_array:
568 nfs_readdir_release_array(page);
569out:
570 nfs_free_fattr(entry.fattr);
571 nfs_free_fhandle(entry.fh);
572 return status;
573}
574
575/*
576 * Now we cache directories properly, by converting xdr information
577 * to an array that can be used for lookups later. This results in
578 * fewer cache pages, since we can store more information on each page.
579 * We only need to convert from xdr once so future lookups are much simpler
580 */
581static
582int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
583{
584 struct inode *inode = desc->file->f_path.dentry->d_inode;
585
Bryan Schumaker3c8a1ae2010-10-21 16:33:16 -0400586 if (nfs_readdir_xdr_to_array(desc, page, inode) < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400587 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400589
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400590 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700591 /* Should never happen */
592 nfs_zap_mapping(inode, inode->i_mapping);
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unlock_page(page);
595 return 0;
596 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -EIO;
599}
600
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400601static
602void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 page_cache_release(desc->page);
605 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400608static
609struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400611 struct page *page;
612 page = read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
613 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
614 if (IS_ERR(page))
615 desc->eof = 1;
616 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400620 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Olivier Galibert00a92642005-06-22 17:16:29 +0000621 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400622static
623int find_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000624{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400625 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000626
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400627 desc->page = get_cache_page(desc);
628 if (IS_ERR(desc->page))
629 return PTR_ERR(desc->page);
Olivier Galibert00a92642005-06-22 17:16:29 +0000630
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400631 res = nfs_readdir_search_array(desc);
632 if (res == 0)
633 return 0;
634 cache_page_release(desc);
635 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000636}
637
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400638/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static inline
640int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
641{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400642 int res = -EAGAIN;
Olivier Galibert00a92642005-06-22 17:16:29 +0000643
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400644 while (1) {
645 res = find_cache_page(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (res != -EAGAIN)
647 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400648 desc->page_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return res;
651}
652
653static inline unsigned int dt_type(struct inode *inode)
654{
655 return (inode->i_mode >> 12) & 15;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * Once we've found the start of the dirent within a page: fill 'er up...
660 */
661static
662int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
663 filldir_t filldir)
664{
665 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400666 int i = 0;
667 int res = 0;
668 struct nfs_cache_array *array = NULL;
669 unsigned int d_type = DT_UNKNOWN;
670 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400672 array = nfs_readdir_get_array(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400674 for (i = desc->cache_entry_index; i < array->size; i++) {
675 d_type = DT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400677 res = filldir(dirent, array->array[i].string.name,
678 array->array[i].string.len, file->f_pos,
679 nfs_compat_user_ino64(array->array[i].ino), d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (res < 0)
681 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000682 file->f_pos++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400683 desc->cache_entry_index = i;
684 if (i < (array->size-1))
685 *desc->dir_cookie = array->array[i+1].cookie;
686 else
687 *desc->dir_cookie = array->last_cookie;
688 if (i == array->eof_index) {
689 desc->eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400693
694 nfs_readdir_release_array(desc->page);
695 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (dentry != NULL)
697 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500698 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
699 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return res;
701}
702
703/*
704 * If we cannot find a cookie in our cache, we suspect that this is
705 * because it points to a deleted file, so we ask the server to return
706 * whatever it thinks is the next entry. We then feed this to filldir.
707 * If all goes well, we should then be able to find our way round the
708 * cache on the next call to readdir_search_pagecache();
709 *
710 * NOTE: we cannot add the anonymous page to the pagecache because
711 * the data it contains might not be page aligned. Besides,
712 * we should already have a complete representation of the
713 * directory in the page cache by the time we get here.
714 */
715static inline
716int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
717 filldir_t filldir)
718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct page *page = NULL;
720 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400721 struct inode *inode = desc->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500723 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
724 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 page = alloc_page(GFP_HIGHUSER);
727 if (!page) {
728 status = -ENOMEM;
729 goto out;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400732 if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
733 status = -EIO;
734 goto out_release;
735 }
736
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400737 desc->page_index = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400738 desc->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 status = nfs_do_filldir(desc, dirent, filldir);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500742 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700743 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return status;
745 out_release:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400746 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto out;
748}
749
Olivier Galibert00a92642005-06-22 17:16:29 +0000750/* The file offset position represents the dirent entry number. A
751 last cookie cache takes care of the common case of reading the
752 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
755{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800756 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct inode *inode = dentry->d_inode;
758 nfs_readdir_descriptor_t my_desc,
759 *desc = &my_desc;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400760 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Chuck Lever6da24bc2008-06-11 17:55:58 -0400762 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500763 dentry->d_parent->d_name.name, dentry->d_name.name,
764 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500765 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000768 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000769 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000770 * to either find the entry with the appropriate number or
771 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
773 memset(desc, 0, sizeof(*desc));
774
775 desc->file = filp;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400776 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 desc->decode = NFS_PROTO(inode)->decode_dirent;
778 desc->plus = NFS_USE_READDIRPLUS(inode);
779
Trond Myklebust565277f2007-10-15 18:17:53 -0400780 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800781 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500782 if (res < 0)
783 goto out;
784
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400785 while (desc->eof != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (res == -EBADCOOKIE) {
789 /* This means either end of directory */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400790 if (*desc->dir_cookie && desc->eof == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Or that the server has 'lost' a cookie */
792 res = uncached_readdir(desc, dirent, filldir);
793 if (res >= 0)
794 continue;
795 }
796 res = 0;
797 break;
798 }
799 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200800 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400802 desc->page_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 desc->plus = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400804 desc->eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 continue;
806 }
807 if (res < 0)
808 break;
809
810 res = nfs_do_filldir(desc, dirent, filldir);
811 if (res < 0) {
812 res = 0;
813 break;
814 }
815 }
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500816out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400817 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500818 if (res > 0)
819 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400820 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500821 dentry->d_parent->d_name.name, dentry->d_name.name,
822 res);
823 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Trond Myklebust10afec92007-05-14 17:16:04 -0400826static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000827{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400828 struct dentry *dentry = filp->f_path.dentry;
829 struct inode *inode = dentry->d_inode;
830
Chuck Lever6da24bc2008-06-11 17:55:58 -0400831 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400832 dentry->d_parent->d_name.name,
833 dentry->d_name.name,
834 offset, origin);
835
836 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000837 switch (origin) {
838 case 1:
839 offset += filp->f_pos;
840 case 0:
841 if (offset >= 0)
842 break;
843 default:
844 offset = -EINVAL;
845 goto out;
846 }
847 if (offset != filp->f_pos) {
848 filp->f_pos = offset;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400849 nfs_file_open_context(filp)->dir_cookie = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000850 }
851out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400852 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000853 return offset;
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * All directory operations under NFS are synchronous, so fsync()
858 * is a dummy operation.
859 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200860static int nfs_fsync_dir(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200862 struct dentry *dentry = filp->f_path.dentry;
863
Chuck Lever6da24bc2008-06-11 17:55:58 -0400864 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500865 dentry->d_parent->d_name.name, dentry->d_name.name,
866 datasync);
867
Chuck Lever54917782008-05-27 16:29:07 -0400868 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
870}
871
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400872/**
873 * nfs_force_lookup_revalidate - Mark the directory as having changed
874 * @dir - pointer to directory inode
875 *
876 * This forces the revalidation code in nfs_lookup_revalidate() to do a
877 * full lookup on all child dentries of 'dir' whenever a change occurs
878 * on the server that might have invalidated our dcache.
879 *
880 * The caller should be holding dir->i_lock
881 */
882void nfs_force_lookup_revalidate(struct inode *dir)
883{
Trond Myklebust011935a2008-10-14 19:24:50 -0400884 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * A check for whether or not the parent directory has changed.
889 * In the case it has, we assume that the dentries are untrustworthy
890 * and may need to be looked up again.
891 */
Trond Myklebustc79ba782007-01-31 08:16:24 -0500892static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 if (IS_ROOT(dentry))
895 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400896 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
897 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -0400898 if (!nfs_verify_change_attribute(dir, dentry->d_time))
899 return 0;
900 /* Revalidate nfsi->cache_change_attribute before we declare a match */
901 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
902 return 0;
903 if (!nfs_verify_change_attribute(dir, dentry->d_time))
904 return 0;
905 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400909 * Return the intent data that applies to this particular path component
910 *
911 * Note that the current set of intents only apply to the very last
912 * component of the path.
913 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
914 */
915static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
916{
917 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
918 return 0;
919 return nd->flags & mask;
920}
921
922/*
Trond Myklebusta12802c2007-10-02 19:13:04 -0400923 * Use intent information to check whether or not we're going to do
924 * an O_EXCL create using this path component.
925 */
926static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
927{
928 if (NFS_PROTO(dir)->version == 2)
929 return 0;
Al Viro35165862008-08-05 03:00:49 -0400930 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -0400931}
932
933/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400934 * Inode and filehandle revalidation for lookups.
935 *
936 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
937 * or if the intent information indicates that we're about to open this
938 * particular file and the "nocto" mount flag is not set.
939 *
940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static inline
942int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
943{
944 struct nfs_server *server = NFS_SERVER(inode);
945
Trond Myklebust4e99a1f2008-03-06 12:34:59 -0500946 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400950 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto out_force;
952 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400953 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400954 !(server->flags & NFS_MOUNT_NOCTO) &&
955 (S_ISREG(inode->i_mode) ||
956 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -0400958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 return nfs_revalidate_inode(server, inode);
961out_force:
962 return __nfs_revalidate_inode(server, inode);
963}
964
965/*
966 * We judge how long we want to trust negative
967 * dentries by looking at the parent inode mtime.
968 *
969 * If parent mtime has changed, we revalidate, else we wait for a
970 * period corresponding to the parent's attribute cache timeout value.
971 */
972static inline
973int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
974 struct nameidata *nd)
975{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400977 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400979 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
980 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return !nfs_check_verifier(dir, dentry);
982}
983
984/*
985 * This is called every time the dcache has a lookup hit,
986 * and we should check whether we can really trust that
987 * lookup.
988 *
989 * NOTE! The hit can be a negative hit too, don't assume
990 * we have an inode!
991 *
992 * If the parent directory is seen to have changed, we throw out the
993 * cached dentry and do a new lookup.
994 */
995static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
996{
997 struct inode *dir;
998 struct inode *inode;
999 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001000 struct nfs_fh *fhandle = NULL;
1001 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -05001006 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 inode = dentry->d_inode;
1008
1009 if (!inode) {
1010 if (nfs_neg_need_reval(dir, dentry, nd))
1011 goto out_bad;
1012 goto out_valid;
1013 }
1014
1015 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001016 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001017 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001018 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 goto out_bad;
1020 }
1021
Trond Myklebust15860ab2008-12-23 15:21:54 -05001022 if (nfs_have_delegation(inode, FMODE_READ))
1023 goto out_set_verifier;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -04001026 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (nfs_lookup_verify_inode(inode, nd))
1028 goto out_zap_parent;
1029 goto out_valid;
1030 }
1031
1032 if (NFS_STALE(inode))
1033 goto out_bad;
1034
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001035 error = -ENOMEM;
1036 fhandle = nfs_alloc_fhandle();
1037 fattr = nfs_alloc_fattr();
1038 if (fhandle == NULL || fattr == NULL)
1039 goto out_error;
1040
1041 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (error)
1043 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001044 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001046 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out_bad;
1048
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001049 nfs_free_fattr(fattr);
1050 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001051out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -04001052 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001055 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001056 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001057 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 1;
1059out_zap_parent:
1060 nfs_zap_caches(dir);
1061 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -04001062 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (inode && S_ISDIR(inode->i_mode)) {
1064 /* Purge readdir caches. */
1065 nfs_zap_caches(inode);
1066 /* If we have submounts, don't unhash ! */
1067 if (have_submounts(dentry))
1068 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +01001069 if (dentry->d_flags & DCACHE_DISCONNECTED)
1070 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 shrink_dcache_parent(dentry);
1072 }
1073 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001074 nfs_free_fattr(fattr);
1075 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001077 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001078 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001079 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001081out_error:
1082 nfs_free_fattr(fattr);
1083 nfs_free_fhandle(fhandle);
1084 dput(parent);
1085 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1086 __func__, dentry->d_parent->d_name.name,
1087 dentry->d_name.name, error);
1088 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091/*
1092 * This is called from dput() when d_count is going to 0.
1093 */
1094static int nfs_dentry_delete(struct dentry *dentry)
1095{
1096 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1097 dentry->d_parent->d_name.name, dentry->d_name.name,
1098 dentry->d_flags);
1099
Trond Myklebust77f11192008-01-28 19:43:19 -05001100 /* Unhash any dentry with a stale inode */
1101 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1102 return 1;
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1105 /* Unhash it, so that ->d_iput() would be called */
1106 return 1;
1107 }
1108 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1109 /* Unhash it, so that ancestors of killed async unlink
1110 * files will be cleaned up during umount */
1111 return 1;
1112 }
1113 return 0;
1114
1115}
1116
Trond Myklebust1b83d702008-06-11 15:44:04 -04001117static void nfs_drop_nlink(struct inode *inode)
1118{
1119 spin_lock(&inode->i_lock);
1120 if (inode->i_nlink > 0)
1121 drop_nlink(inode);
1122 spin_unlock(&inode->i_lock);
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
1126 * Called when the dentry loses inode.
1127 * We use it to clean up silly-renamed files.
1128 */
1129static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1130{
Neil Brown83672d32007-02-26 12:48:25 +11001131 if (S_ISDIR(inode->i_mode))
1132 /* drop any readdir cache as it could easily be old */
1133 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001136 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001137 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 iput(inode);
1140}
1141
Al Virof786aa92009-02-20 05:51:22 +00001142const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 .d_revalidate = nfs_lookup_revalidate,
1144 .d_delete = nfs_dentry_delete,
1145 .d_iput = nfs_dentry_iput,
1146};
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1149{
1150 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -04001151 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001153 struct nfs_fh *fhandle = NULL;
1154 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1158 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -05001159 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 res = ERR_PTR(-ENAMETOOLONG);
1162 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1163 goto out;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1166
Trond Myklebustfd684072006-09-05 12:27:44 -04001167 /*
1168 * If we're doing an exclusive create, optimize away the lookup
1169 * but don't hash the dentry.
1170 */
1171 if (nfs_is_exclusive_create(dir, nd)) {
1172 d_instantiate(dentry, NULL);
1173 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -04001174 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -04001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001177 res = ERR_PTR(-ENOMEM);
1178 fhandle = nfs_alloc_fhandle();
1179 fattr = nfs_alloc_fattr();
1180 if (fhandle == NULL || fattr == NULL)
1181 goto out;
1182
Trond Myklebust565277f2007-10-15 18:17:53 -04001183 parent = dentry->d_parent;
1184 /* Protect against concurrent sillydeletes */
1185 nfs_block_sillyrename(parent);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001186 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (error == -ENOENT)
1188 goto no_entry;
1189 if (error < 0) {
1190 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -04001191 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001193 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001194 res = (struct dentry *)inode;
1195 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001196 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -04001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198no_entry:
David Howells54ceac42006-08-22 20:06:13 -04001199 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001200 if (res != NULL) {
1201 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001202 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001206out_unblock_sillyrename:
1207 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001209 nfs_free_fattr(fattr);
1210 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return res;
1212}
1213
1214#ifdef CONFIG_NFS_V4
1215static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1216
Al Virof786aa92009-02-20 05:51:22 +00001217const struct dentry_operations nfs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 .d_revalidate = nfs_open_revalidate,
1219 .d_delete = nfs_dentry_delete,
1220 .d_iput = nfs_dentry_iput,
1221};
1222
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001223/*
1224 * Use intent information to determine whether we need to substitute
1225 * the NFSv4-style stateful OPEN for the LOOKUP call
1226 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001227static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001229 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return 0;
1231 /* NFS does not (yet) have a stateful open for directories */
1232 if (nd->flags & LOOKUP_DIRECTORY)
1233 return 0;
1234 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001235 if (__mnt_is_readonly(nd->path.mnt) &&
1236 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 0;
1238 return 1;
1239}
1240
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001241static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1242{
1243 struct path path = {
1244 .mnt = nd->path.mnt,
1245 .dentry = dentry,
1246 };
1247 struct nfs_open_context *ctx;
1248 struct rpc_cred *cred;
1249 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1250
1251 cred = rpc_lookup_cred();
1252 if (IS_ERR(cred))
1253 return ERR_CAST(cred);
1254 ctx = alloc_nfs_open_context(&path, cred, fmode);
1255 put_rpccred(cred);
1256 if (ctx == NULL)
1257 return ERR_PTR(-ENOMEM);
1258 return ctx;
1259}
1260
1261static int do_open(struct inode *inode, struct file *filp)
1262{
1263 nfs_fscache_set_inode_cookie(inode, filp);
1264 return 0;
1265}
1266
1267static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1268{
1269 struct file *filp;
1270 int ret = 0;
1271
1272 /* If the open_intent is for execute, we have an extra check to make */
1273 if (ctx->mode & FMODE_EXEC) {
1274 ret = nfs_may_open(ctx->path.dentry->d_inode,
1275 ctx->cred,
1276 nd->intent.open.flags);
1277 if (ret < 0)
1278 goto out;
1279 }
1280 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1281 if (IS_ERR(filp))
1282 ret = PTR_ERR(filp);
1283 else
1284 nfs_file_set_open_context(filp, ctx);
1285out:
1286 put_nfs_open_context(ctx);
1287 return ret;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1291{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001292 struct nfs_open_context *ctx;
1293 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct dentry *res = NULL;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001295 struct inode *inode;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001296 int open_flags;
Trond Myklebust898f6352010-10-23 11:24:25 -04001297 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001299 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1300 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* Check that we are indeed trying to open this file */
Trond Myklebust5584c302008-12-23 15:21:54 -05001303 if (!is_atomic_open(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto no_open;
1305
1306 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1307 res = ERR_PTR(-ENAMETOOLONG);
1308 goto out;
1309 }
1310 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1311
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001312 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1313 * the dentry. */
Al Viro35165862008-08-05 03:00:49 -04001314 if (nd->flags & LOOKUP_EXCL) {
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001315 d_instantiate(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001316 goto out;
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001319 ctx = nameidata_to_nfs_open_context(dentry, nd);
1320 res = ERR_CAST(ctx);
1321 if (IS_ERR(ctx))
1322 goto out;
1323
1324 open_flags = nd->intent.open.flags;
1325 if (nd->flags & LOOKUP_CREATE) {
1326 attr.ia_mode = nd->intent.open.create_mode;
1327 attr.ia_valid = ATTR_MODE;
1328 if (!IS_POSIXACL(dir))
1329 attr.ia_mode &= ~current_umask();
1330 } else {
Trond Myklebust898f6352010-10-23 11:24:25 -04001331 open_flags &= ~(O_EXCL | O_CREAT);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001332 attr.ia_valid = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001333 }
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /* Open the file on the server */
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001336 nfs_block_sillyrename(dentry->d_parent);
Trond Myklebust2b484292010-09-17 10:56:51 -04001337 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001338 if (IS_ERR(inode)) {
1339 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001340 put_nfs_open_context(ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001341 switch (PTR_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /* Make a negative dentry */
1343 case -ENOENT:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001344 d_add(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001345 res = NULL;
1346 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* This turned out not to be a regular file */
Trond Myklebust6f926b52005-10-18 14:20:18 -07001348 case -ENOTDIR:
1349 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 case -ELOOP:
1351 if (!(nd->intent.open.flags & O_NOFOLLOW))
1352 goto no_open;
Trond Myklebust23ebbd92010-11-03 10:24:16 -04001353 /* case -EISDIR: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* case -EINVAL: */
1355 default:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001356 res = ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto out;
1358 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001359 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001360 res = d_add_unique(dentry, inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001361 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001362 if (res != NULL) {
1363 dput(ctx->path.dentry);
1364 ctx->path.dentry = dget(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 dentry = res;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001366 }
Trond Myklebust898f6352010-10-23 11:24:25 -04001367 err = nfs_intent_set_file(nd, ctx);
1368 if (err < 0) {
1369 if (res != NULL)
1370 dput(res);
1371 return ERR_PTR(err);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373out:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001374 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return res;
1376no_open:
1377 return nfs_lookup(dir, dentry, nd);
1378}
1379
1380static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1381{
1382 struct dentry *parent = NULL;
1383 struct inode *inode = dentry->d_inode;
1384 struct inode *dir;
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001385 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int openflags, ret = 0;
1387
Trond Myklebust1f063d22010-04-22 15:35:55 -04001388 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001389 goto no_open;
Trond Myklebust2b484292010-09-17 10:56:51 -04001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 parent = dget_parent(dentry);
1392 dir = parent->d_inode;
Trond Myklebust2b484292010-09-17 10:56:51 -04001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* We can't create new files in nfs_open_revalidate(), so we
1395 * optimize away revalidation of negative dentries.
1396 */
Trond Myklebust216d5d02007-10-01 20:10:12 -04001397 if (inode == NULL) {
1398 if (!nfs_neg_need_reval(dir, dentry, nd))
1399 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto out;
Trond Myklebust216d5d02007-10-01 20:10:12 -04001401 }
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* NFS only supports OPEN on regular files */
1404 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001405 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 openflags = nd->intent.open.flags;
1407 /* We cannot do exclusive creation on a positive dentry */
1408 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001409 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /* We can't create new files, or truncate existing ones here */
Trond Myklebust0a377cf2010-08-18 09:25:42 -04001411 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001413 ctx = nameidata_to_nfs_open_context(dentry, nd);
1414 ret = PTR_ERR(ctx);
1415 if (IS_ERR(ctx))
1416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001418 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * operations that change the directory. We therefore save the
1420 * change attribute *before* we do the RPC call.
1421 */
Trond Myklebust2b484292010-09-17 10:56:51 -04001422 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
Trond Myklebust535918f2010-09-17 10:56:51 -04001423 if (IS_ERR(inode)) {
1424 ret = PTR_ERR(inode);
1425 switch (ret) {
1426 case -EPERM:
1427 case -EACCES:
1428 case -EDQUOT:
1429 case -ENOSPC:
1430 case -EROFS:
1431 goto out_put_ctx;
1432 default:
1433 goto out_drop;
1434 }
1435 }
1436 iput(inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001437 if (inode != dentry->d_inode)
Trond Myklebust535918f2010-09-17 10:56:51 -04001438 goto out_drop;
Trond Myklebust898f6352010-10-23 11:24:25 -04001439
1440 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1441 ret = nfs_intent_set_file(nd, ctx);
1442 if (ret >= 0)
1443 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444out:
1445 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001447out_drop:
1448 d_drop(dentry);
1449 ret = 0;
1450out_put_ctx:
1451 put_nfs_open_context(ctx);
1452 goto out;
1453
Trond Myklebust5584c302008-12-23 15:21:54 -05001454no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001456no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return nfs_lookup_revalidate(dentry, nd);
1458}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001459
1460static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1461 struct nameidata *nd)
1462{
1463 struct nfs_open_context *ctx = NULL;
1464 struct iattr attr;
1465 int error;
1466 int open_flags = 0;
1467
1468 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1469 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1470
1471 attr.ia_mode = mode;
1472 attr.ia_valid = ATTR_MODE;
1473
1474 if ((nd->flags & LOOKUP_CREATE) != 0) {
1475 open_flags = nd->intent.open.flags;
1476
1477 ctx = nameidata_to_nfs_open_context(dentry, nd);
1478 error = PTR_ERR(ctx);
1479 if (IS_ERR(ctx))
Trond Myklebust898f6352010-10-23 11:24:25 -04001480 goto out_err_drop;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001481 }
1482
1483 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1484 if (error != 0)
1485 goto out_put_ctx;
Trond Myklebust898f6352010-10-23 11:24:25 -04001486 if (ctx != NULL) {
1487 error = nfs_intent_set_file(nd, ctx);
1488 if (error < 0)
1489 goto out_err;
1490 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001491 return 0;
1492out_put_ctx:
1493 if (ctx != NULL)
1494 put_nfs_open_context(ctx);
Trond Myklebust898f6352010-10-23 11:24:25 -04001495out_err_drop:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001496 d_drop(dentry);
Trond Myklebust898f6352010-10-23 11:24:25 -04001497out_err:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001498 return error;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501#endif /* CONFIG_NFSV4 */
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/*
1504 * Code common to create, mkdir, and mknod.
1505 */
1506int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1507 struct nfs_fattr *fattr)
1508{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001509 struct dentry *parent = dget_parent(dentry);
1510 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 struct inode *inode;
1512 int error = -EACCES;
1513
Trond Myklebustfab728e2007-09-29 17:41:33 -04001514 d_drop(dentry);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /* We may have been initialized further down */
1517 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (fhandle->size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1521 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001522 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001524 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1526 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001527 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001529 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001532 error = PTR_ERR(inode);
1533 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001534 goto out_error;
1535 d_add(dentry, inode);
1536out:
1537 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001539out_error:
1540 nfs_mark_for_revalidate(dir);
1541 dput(parent);
1542 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545/*
1546 * Following a failed create operation, we drop the dentry rather
1547 * than retain a negative dentry. This avoids a problem in the event
1548 * that the operation succeeded on the server, but an error in the
1549 * reply path made it appear to have failed.
1550 */
1551static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1552 struct nameidata *nd)
1553{
1554 struct iattr attr;
1555 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001557 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1558 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 attr.ia_mode = mode;
1561 attr.ia_valid = ATTR_MODE;
1562
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001563 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (error != 0)
1565 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 0;
1567out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 d_drop(dentry);
1569 return error;
1570}
1571
1572/*
1573 * See comments for nfs_proc_create regarding failed operations.
1574 */
1575static int
1576nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1577{
1578 struct iattr attr;
1579 int status;
1580
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001581 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1582 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (!new_valid_dev(rdev))
1585 return -EINVAL;
1586
1587 attr.ia_mode = mode;
1588 attr.ia_valid = ATTR_MODE;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (status != 0)
1592 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return 0;
1594out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 d_drop(dentry);
1596 return status;
1597}
1598
1599/*
1600 * See comments for nfs_proc_create regarding failed operations.
1601 */
1602static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1603{
1604 struct iattr attr;
1605 int error;
1606
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001607 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1608 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 attr.ia_valid = ATTR_MODE;
1611 attr.ia_mode = mode | S_IFDIR;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (error != 0)
1615 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
1617out_err:
1618 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return error;
1620}
1621
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001622static void nfs_dentry_handle_enoent(struct dentry *dentry)
1623{
1624 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1625 d_delete(dentry);
1626}
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1629{
1630 int error;
1631
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001632 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1633 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1636 /* Ensure the VFS deletes this inode */
1637 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001638 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001639 else if (error == -ENOENT)
1640 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 return error;
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645/*
1646 * Remove a file after making sure there are no pending writes,
1647 * and after checking that the file has only one user.
1648 *
1649 * We invalidate the attribute cache and free the inode prior to the operation
1650 * to avoid possible races if the server reuses the inode.
1651 */
1652static int nfs_safe_remove(struct dentry *dentry)
1653{
1654 struct inode *dir = dentry->d_parent->d_inode;
1655 struct inode *inode = dentry->d_inode;
1656 int error = -EBUSY;
1657
1658 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1659 dentry->d_parent->d_name.name, dentry->d_name.name);
1660
1661 /* If the dentry was sillyrenamed, we simply call d_delete() */
1662 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1663 error = 0;
1664 goto out;
1665 }
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001668 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1670 /* The VFS may want to delete this inode */
1671 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001672 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001673 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 } else
1675 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001676 if (error == -ENOENT)
1677 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678out:
1679 return error;
1680}
1681
1682/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1683 * belongs to an active ".nfs..." file and we return -EBUSY.
1684 *
1685 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1686 */
1687static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1688{
1689 int error;
1690 int need_rehash = 0;
1691
1692 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1693 dir->i_ino, dentry->d_name.name);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 spin_lock(&dcache_lock);
1696 spin_lock(&dentry->d_lock);
1697 if (atomic_read(&dentry->d_count) > 1) {
1698 spin_unlock(&dentry->d_lock);
1699 spin_unlock(&dcache_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001700 /* Start asynchronous writeout of the inode */
1701 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return error;
1704 }
1705 if (!d_unhashed(dentry)) {
1706 __d_drop(dentry);
1707 need_rehash = 1;
1708 }
1709 spin_unlock(&dentry->d_lock);
1710 spin_unlock(&dcache_lock);
1711 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001712 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1714 } else if (need_rehash)
1715 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return error;
1717}
1718
Chuck Lever873101b2006-08-22 20:06:23 -04001719/*
1720 * To create a symbolic link, most file systems instantiate a new inode,
1721 * add a page to it containing the path, then write it out to the disk
1722 * using prepare_write/commit_write.
1723 *
1724 * Unfortunately the NFS client can't create the in-core inode first
1725 * because it needs a file handle to create an in-core inode (see
1726 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1727 * symlink request has completed on the server.
1728 *
1729 * So instead we allocate a raw page, copy the symname into it, then do
1730 * the SYMLINK request with the page as the buffer. If it succeeds, we
1731 * now have a new file handle and can instantiate an in-core NFS inode
1732 * and move the raw page into its mapping.
1733 */
1734static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Chuck Lever873101b2006-08-22 20:06:23 -04001736 struct pagevec lru_pvec;
1737 struct page *page;
1738 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001740 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int error;
1742
1743 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1744 dir->i_ino, dentry->d_name.name, symname);
1745
Chuck Lever873101b2006-08-22 20:06:23 -04001746 if (pathlen > PAGE_SIZE)
1747 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Chuck Lever873101b2006-08-22 20:06:23 -04001749 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1750 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jeff Layton83d93f22007-06-07 09:58:08 -04001752 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001753 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001754 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001755
1756 kaddr = kmap_atomic(page, KM_USER0);
1757 memcpy(kaddr, symname, pathlen);
1758 if (pathlen < PAGE_SIZE)
1759 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1760 kunmap_atomic(kaddr, KM_USER0);
1761
Chuck Lever94a6d752006-08-22 20:06:23 -04001762 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001763 if (error != 0) {
1764 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1765 dir->i_sb->s_id, dir->i_ino,
1766 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001768 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001769 return error;
1770 }
1771
1772 /*
1773 * No big deal if we can't add this page to the page cache here.
1774 * READLINK will get the missing page from the server if needed.
1775 */
1776 pagevec_init(&lru_pvec, 0);
1777 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1778 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001779 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001780 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001781 SetPageUptodate(page);
1782 unlock_page(page);
1783 } else
1784 __free_page(page);
1785
Chuck Lever873101b2006-08-22 20:06:23 -04001786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789static int
1790nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1791{
1792 struct inode *inode = old_dentry->d_inode;
1793 int error;
1794
1795 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1796 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1797 dentry->d_parent->d_name.name, dentry->d_name.name);
1798
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001799 nfs_inode_return_delegation(inode);
1800
Trond Myklebust9697d232007-10-02 21:58:05 -04001801 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001803 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04001804 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04001805 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return error;
1808}
1809
1810/*
1811 * RENAME
1812 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1813 * different file handle for the same inode after a rename (e.g. when
1814 * moving to a different directory). A fail-safe method to do so would
1815 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1816 * rename the old file using the sillyrename stuff. This way, the original
1817 * file in old_dir will go away when the last process iput()s the inode.
1818 *
1819 * FIXED.
1820 *
1821 * It actually works quite well. One needs to have the possibility for
1822 * at least one ".nfs..." file in each directory the file ever gets
1823 * moved or linked to which happens automagically with the new
1824 * implementation that only depends on the dcache stuff instead of
1825 * using the inode layer
1826 *
1827 * Unfortunately, things are a little more complicated than indicated
1828 * above. For a cross-directory move, we want to make sure we can get
1829 * rid of the old inode after the operation. This means there must be
1830 * no pending writes (if it's a file), and the use count must be 1.
1831 * If these conditions are met, we can drop the dentries before doing
1832 * the rename.
1833 */
1834static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1835 struct inode *new_dir, struct dentry *new_dentry)
1836{
1837 struct inode *old_inode = old_dentry->d_inode;
1838 struct inode *new_inode = new_dentry->d_inode;
1839 struct dentry *dentry = NULL, *rehash = NULL;
1840 int error = -EBUSY;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1843 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1844 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1845 atomic_read(&new_dentry->d_count));
1846
1847 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001848 * For non-directories, check whether the target is busy and if so,
1849 * make a copy of the dentry and then do a silly-rename. If the
1850 * silly-rename succeeds, the copied dentry is hashed and becomes
1851 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001853 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1854 /*
1855 * To prevent any new references to the target during the
1856 * rename, we unhash the dentry in advance.
1857 */
1858 if (!d_unhashed(new_dentry)) {
1859 d_drop(new_dentry);
1860 rehash = new_dentry;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Miklos Szeredi27226102009-12-03 15:58:56 -05001863 if (atomic_read(&new_dentry->d_count) > 2) {
1864 int err;
1865
1866 /* copy the target dentry's name */
1867 dentry = d_alloc(new_dentry->d_parent,
1868 &new_dentry->d_name);
1869 if (!dentry)
1870 goto out;
1871
1872 /* silly-rename the existing target ... */
1873 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001874 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001875 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001876
1877 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001878 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001879 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05001880 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Trond Myklebustcae7a072005-10-18 14:20:19 -07001883 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001884 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01001885 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1888 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001889 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890out:
1891 if (rehash)
1892 d_rehash(rehash);
1893 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001894 if (new_inode != NULL)
1895 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07001896 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04001897 nfs_set_verifier(new_dentry,
1898 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001899 } else if (error == -ENOENT)
1900 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 /* new dentry created? */
1903 if (dentry)
1904 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return error;
1906}
1907
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001908static DEFINE_SPINLOCK(nfs_access_lru_lock);
1909static LIST_HEAD(nfs_access_lru_list);
1910static atomic_long_t nfs_access_nr_entries;
1911
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001912static void nfs_access_free_entry(struct nfs_access_entry *entry)
1913{
1914 put_rpccred(entry->cred);
1915 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001916 smp_mb__before_atomic_dec();
1917 atomic_long_dec(&nfs_access_nr_entries);
1918 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001919}
1920
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001921static void nfs_access_free_list(struct list_head *head)
1922{
1923 struct nfs_access_entry *cache;
1924
1925 while (!list_empty(head)) {
1926 cache = list_entry(head->next, struct nfs_access_entry, lru);
1927 list_del(&cache->lru);
1928 nfs_access_free_entry(cache);
1929 }
1930}
1931
Dave Chinner7f8275d2010-07-19 14:56:17 +10001932int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebust979df722006-07-25 11:28:19 -04001933{
1934 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001935 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04001936 struct nfs_access_entry *cache;
1937
Trond Myklebust61d5eb22010-05-13 12:51:06 -04001938 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1939 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001940
Trond Myklebusta50f7952007-06-05 19:23:43 -04001941 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001942 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04001943 struct inode *inode;
1944
1945 if (nr_to_scan-- == 0)
1946 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001947 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04001948 spin_lock(&inode->i_lock);
1949 if (list_empty(&nfsi->access_cache_entry_lru))
1950 goto remove_lru_entry;
1951 cache = list_entry(nfsi->access_cache_entry_lru.next,
1952 struct nfs_access_entry, lru);
1953 list_move(&cache->lru, &head);
1954 rb_erase(&cache->rb_node, &nfsi->access_cache);
1955 if (!list_empty(&nfsi->access_cache_entry_lru))
1956 list_move_tail(&nfsi->access_cache_inode_lru,
1957 &nfs_access_lru_list);
1958 else {
1959remove_lru_entry:
1960 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001961 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001962 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001963 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001964 }
Trond Myklebust59844a92010-05-26 08:42:24 -04001965 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04001966 }
1967 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001968 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04001969 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1970}
1971
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001972static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001973{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001974 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001975 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001976 struct nfs_access_entry *entry;
1977
1978 /* Unhook entries from the cache */
1979 while ((n = rb_first(root_node)) != NULL) {
1980 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1981 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001982 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001983 }
1984 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001985}
1986
1987void nfs_access_zap_cache(struct inode *inode)
1988{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001989 LIST_HEAD(head);
1990
1991 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
1992 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001993 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001994 spin_lock(&nfs_access_lru_lock);
1995 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001996 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001997
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001998 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001999 __nfs_access_zap_cache(NFS_I(inode), &head);
2000 spin_unlock(&inode->i_lock);
2001 spin_unlock(&nfs_access_lru_lock);
2002 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002003}
2004
2005static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2006{
2007 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2008 struct nfs_access_entry *entry;
2009
2010 while (n != NULL) {
2011 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2012
2013 if (cred < entry->cred)
2014 n = n->rb_left;
2015 else if (cred > entry->cred)
2016 n = n->rb_right;
2017 else
2018 return entry;
2019 }
2020 return NULL;
2021}
2022
Trond Myklebustaf22f942007-08-10 17:45:10 -04002023static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Chuck Lever55296802005-08-18 11:24:09 -07002025 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002026 struct nfs_access_entry *cache;
2027 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002029 spin_lock(&inode->i_lock);
2030 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2031 goto out_zap;
2032 cache = nfs_access_search_rbtree(inode, cred);
2033 if (cache == NULL)
2034 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05002035 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05002036 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002037 goto out_stale;
2038 res->jiffies = cache->jiffies;
2039 res->cred = cache->cred;
2040 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002041 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002042 err = 0;
2043out:
2044 spin_unlock(&inode->i_lock);
2045 return err;
2046out_stale:
2047 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002048 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002049 spin_unlock(&inode->i_lock);
2050 nfs_access_free_entry(cache);
2051 return -ENOENT;
2052out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002053 spin_unlock(&inode->i_lock);
2054 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002055 return -ENOENT;
2056}
2057
2058static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2059{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002060 struct nfs_inode *nfsi = NFS_I(inode);
2061 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002062 struct rb_node **p = &root_node->rb_node;
2063 struct rb_node *parent = NULL;
2064 struct nfs_access_entry *entry;
2065
2066 spin_lock(&inode->i_lock);
2067 while (*p != NULL) {
2068 parent = *p;
2069 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2070
2071 if (set->cred < entry->cred)
2072 p = &parent->rb_left;
2073 else if (set->cred > entry->cred)
2074 p = &parent->rb_right;
2075 else
2076 goto found;
2077 }
2078 rb_link_node(&set->rb_node, parent, p);
2079 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002080 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002081 spin_unlock(&inode->i_lock);
2082 return;
2083found:
2084 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002085 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2086 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002087 spin_unlock(&inode->i_lock);
2088 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Trond Myklebustaf22f942007-08-10 17:45:10 -04002091static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002093 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2094 if (cache == NULL)
2095 return;
2096 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002098 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002100
2101 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002102
2103 /* Update accounting */
2104 smp_mb__before_atomic_inc();
2105 atomic_long_inc(&nfs_access_nr_entries);
2106 smp_mb__after_atomic_inc();
2107
2108 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002109 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002110 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002111 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2112 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2113 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002114 spin_unlock(&nfs_access_lru_lock);
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2119{
2120 struct nfs_access_entry cache;
2121 int status;
2122
2123 status = nfs_access_get_cached(inode, cred, &cache);
2124 if (status == 0)
2125 goto out;
2126
2127 /* Be clever: ask server to check for all possible rights */
2128 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2129 cache.cred = cred;
2130 cache.jiffies = jiffies;
2131 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002132 if (status != 0) {
2133 if (status == -ESTALE) {
2134 nfs_zap_caches(inode);
2135 if (!S_ISDIR(inode->i_mode))
2136 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 nfs_access_add_cache(inode, &cache);
2141out:
Al Viroe6305c42008-07-15 21:03:57 -04002142 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144 return -EACCES;
2145}
2146
Trond Myklebustaf22f942007-08-10 17:45:10 -04002147static int nfs_open_permission_mask(int openflags)
2148{
2149 int mask = 0;
2150
2151 if (openflags & FMODE_READ)
2152 mask |= MAY_READ;
2153 if (openflags & FMODE_WRITE)
2154 mask |= MAY_WRITE;
2155 if (openflags & FMODE_EXEC)
2156 mask |= MAY_EXEC;
2157 return mask;
2158}
2159
2160int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2161{
2162 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2163}
2164
Al Viroe6305c42008-07-15 21:03:57 -04002165int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct rpc_cred *cred;
2168 int res = 0;
2169
Chuck Lever91d5b472006-03-20 13:44:14 -05002170 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2171
Al Viroe6305c42008-07-15 21:03:57 -04002172 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto out;
2174 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002175 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto force_lookup;
2177
2178 switch (inode->i_mode & S_IFMT) {
2179 case S_IFLNK:
2180 goto out;
2181 case S_IFREG:
2182 /* NFSv4 has atomic_open... */
2183 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002184 && (mask & MAY_OPEN)
2185 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto out;
2187 break;
2188 case S_IFDIR:
2189 /*
2190 * Optimize away all write operations, since the server
2191 * will check permissions when we perform the op.
2192 */
2193 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2194 goto out;
2195 }
2196
2197force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (!NFS_PROTO(inode)->access)
2199 goto out_notsup;
2200
Trond Myklebust98a8e322008-03-12 12:25:28 -04002201 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!IS_ERR(cred)) {
2203 res = nfs_do_access(inode, cred, mask);
2204 put_rpccred(cred);
2205 } else
2206 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002208 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2209 res = -EACCES;
2210
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002211 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2212 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return res;
2214out_notsup:
2215 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2216 if (res == 0)
2217 res = generic_permission(inode, mask, NULL);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002218 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
2221/*
2222 * Local variables:
2223 * version-control: t
2224 * kept-new-versions: 5
2225 * End:
2226 */