blob: c9196c9cf5a64d1520b85a92b0f4f3ba1f843cfc [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{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500197 void *ptr;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400198 if (page == NULL)
199 return ERR_PTR(-EIO);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500200 ptr = kmap(page);
201 if (ptr == NULL)
202 return ERR_PTR(-ENOMEM);
203 return ptr;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400204}
205
206static
207void nfs_readdir_release_array(struct page *page)
208{
209 kunmap(page);
210}
211
212/*
213 * we are freeing strings created by nfs_add_to_readdir_array()
214 */
215static
216int nfs_readdir_clear_array(struct page *page, gfp_t mask)
217{
218 struct nfs_cache_array *array = nfs_readdir_get_array(page);
219 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500220
221 if (IS_ERR(array))
222 return PTR_ERR(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400223 for (i = 0; i < array->size; i++)
224 kfree(array->array[i].string.name);
225 nfs_readdir_release_array(page);
226 return 0;
227}
228
229/*
230 * the caller is responsible for freeing qstr.name
231 * when called by nfs_readdir_add_to_array, the strings will be freed in
232 * nfs_clear_readdir_array()
233 */
234static
Trond Myklebust4a201d62010-10-23 14:53:23 -0400235int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400236{
237 string->len = len;
238 string->name = kmemdup(name, len, GFP_KERNEL);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400239 if (string->name == NULL)
240 return -ENOMEM;
241 string->hash = full_name_hash(name, len);
242 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400243}
244
245static
246int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
247{
248 struct nfs_cache_array *array = nfs_readdir_get_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400249 struct nfs_cache_array_entry *cache_entry;
250 int ret;
251
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400252 if (IS_ERR(array))
253 return PTR_ERR(array);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500254 ret = -ENOSPC;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400255 if (array->size >= MAX_READDIR_ARRAY)
256 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400257
Trond Myklebust4a201d62010-10-23 14:53:23 -0400258 cache_entry = &array->array[array->size];
259 cache_entry->cookie = entry->prev_cookie;
260 cache_entry->ino = entry->ino;
261 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
262 if (ret)
263 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400264 array->last_cookie = entry->cookie;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500265 array->size++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400266 if (entry->eof == 1)
267 array->eof_index = array->size;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400268out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400269 nfs_readdir_release_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400270 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400271}
272
273static
274int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
275{
276 loff_t diff = desc->file->f_pos - desc->current_index;
277 unsigned int index;
278
279 if (diff < 0)
280 goto out_eof;
281 if (diff >= array->size) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500282 if (array->eof_index >= 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400283 goto out_eof;
284 desc->current_index += array->size;
285 return -EAGAIN;
286 }
287
288 index = (unsigned int)diff;
289 *desc->dir_cookie = array->array[index].cookie;
290 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400291 return 0;
292out_eof:
293 desc->eof = 1;
294 return -EBADCOOKIE;
295}
296
297static
298int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
299{
300 int i;
301 int status = -EAGAIN;
302
303 for (i = 0; i < array->size; i++) {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400304 if (array->array[i].cookie == *desc->dir_cookie) {
305 desc->cache_entry_index = i;
306 status = 0;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500307 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400308 }
309 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500310 if (i == array->eof_index) {
311 desc->eof = 1;
312 status = -EBADCOOKIE;
313 }
314out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400315 return status;
316}
317
318static
319int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
320{
321 struct nfs_cache_array *array;
322 int status = -EBADCOOKIE;
323
324 if (desc->dir_cookie == NULL)
325 goto out;
326
327 array = nfs_readdir_get_array(desc->page);
328 if (IS_ERR(array)) {
329 status = PTR_ERR(array);
330 goto out;
331 }
332
333 if (*desc->dir_cookie == 0)
334 status = nfs_readdir_search_for_pos(array, desc);
335 else
336 status = nfs_readdir_search_for_cookie(array, desc);
337
338 nfs_readdir_release_array(desc->page);
339out:
340 return status;
341}
342
343/* Fill a page with xdr information before transferring to the cache page */
344static
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400345int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400346 struct nfs_entry *entry, struct file *file, struct inode *inode)
347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct rpc_cred *cred = nfs_file_cred(file);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400349 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int error;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 again:
353 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400354 gencount = nfs_inc_attr_generation_counter();
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400355 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 NFS_SERVER(inode)->dtsize, desc->plus);
357 if (error < 0) {
358 /* We requested READDIRPLUS, but the server doesn't grok it */
359 if (error == -ENOTSUPP && desc->plus) {
360 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200361 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 desc->plus = 0;
363 goto again;
364 }
365 goto error;
366 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000367 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400368 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400369error:
370 return error;
371}
372
373/* Fill in an entry based on the xdr code stored in desc->page */
374static
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400375int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400376{
Bryan Schumaker82f2e542010-10-21 16:33:18 -0400377 __be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400378 if (IS_ERR(p))
379 return PTR_ERR(p);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400380
381 entry->fattr->time_start = desc->timestamp;
382 entry->fattr->gencount = desc->gencount;
383 return 0;
384}
385
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400386static
387int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
388{
389 struct nfs_inode *node;
390 if (dentry->d_inode == NULL)
391 goto different;
392 node = NFS_I(dentry->d_inode);
393 if (node->fh.size != entry->fh->size)
394 goto different;
395 if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
396 goto different;
397 return 1;
398different:
399 return 0;
400}
401
402static
403void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
404{
Trond Myklebust4a201d62010-10-23 14:53:23 -0400405 struct qstr filename = {
406 .len = entry->len,
407 .name = entry->name,
408 };
409 struct dentry *dentry;
410 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400411 struct inode *dir = parent->d_inode;
412 struct inode *inode;
413
Trond Myklebust4a201d62010-10-23 14:53:23 -0400414 if (filename.name[0] == '.') {
415 if (filename.len == 1)
416 return;
417 if (filename.len == 2 && filename.name[1] == '.')
418 return;
419 }
420 filename.hash = full_name_hash(filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400421
Trond Myklebust4a201d62010-10-23 14:53:23 -0400422 dentry = d_lookup(parent, &filename);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400423 if (dentry != NULL) {
424 if (nfs_same_file(dentry, entry)) {
425 nfs_refresh_inode(dentry->d_inode, entry->fattr);
426 goto out;
427 } else {
428 d_drop(dentry);
429 dput(dentry);
430 }
431 }
432
433 dentry = d_alloc(parent, &filename);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400434 if (dentry == NULL)
435 return;
436
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400437 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
438 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
439 if (IS_ERR(inode))
440 goto out;
441
442 alias = d_materialise_unique(dentry, inode);
443 if (IS_ERR(alias))
444 goto out;
445 else if (alias) {
446 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
447 dput(alias);
448 } else
449 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
450
451out:
452 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400453}
454
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400455/* Perform conversion from xdr to cache array */
456static
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500457int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400458 void *xdr_page, struct page *page, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400459{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400460 struct xdr_stream stream;
461 struct xdr_buf buf;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400462 __be32 *ptr = xdr_page;
Bryan Schumaker99424382010-10-21 16:33:16 -0400463 int status;
464 struct nfs_cache_array *array;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400465
466 buf.head->iov_base = xdr_page;
467 buf.head->iov_len = buflen;
468 buf.tail->iov_len = 0;
469 buf.page_base = 0;
470 buf.page_len = 0;
471 buf.buflen = buf.head->iov_len;
472 buf.len = buf.head->iov_len;
473
474 xdr_init_decode(&stream, &buf, ptr);
475
Bryan Schumaker99424382010-10-21 16:33:16 -0400476
477 do {
478 status = xdr_decode(desc, entry, &stream);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500479 if (status != 0) {
480 if (status == -EAGAIN)
481 status = 0;
Bryan Schumaker99424382010-10-21 16:33:16 -0400482 break;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500483 }
Bryan Schumaker99424382010-10-21 16:33:16 -0400484
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400485 if (desc->plus == 1)
486 nfs_prime_dcache(desc->file->f_path.dentry, entry);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500487
488 status = nfs_readdir_add_to_array(entry, page);
489 if (status != 0)
490 break;
Bryan Schumaker99424382010-10-21 16:33:16 -0400491 } while (!entry->eof);
492
493 if (status == -EBADCOOKIE && entry->eof) {
494 array = nfs_readdir_get_array(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500495 if (!IS_ERR(array)) {
496 array->eof_index = array->size;
497 status = 0;
498 nfs_readdir_release_array(page);
499 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400500 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500501 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400502}
503
504static
505void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
506{
507 unsigned int i;
508 for (i = 0; i < npages; i++)
509 put_page(pages[i]);
510}
511
512static
513void nfs_readdir_free_large_page(void *ptr, struct page **pages,
514 unsigned int npages)
515{
516 vm_unmap_ram(ptr, npages);
517 nfs_readdir_free_pagearray(pages, npages);
518}
519
520/*
521 * nfs_readdir_large_page will allocate pages that must be freed with a call
522 * to nfs_readdir_free_large_page
523 */
524static
525void *nfs_readdir_large_page(struct page **pages, unsigned int npages)
526{
527 void *ptr;
528 unsigned int i;
529
530 for (i = 0; i < npages; i++) {
531 struct page *page = alloc_page(GFP_KERNEL);
532 if (page == NULL)
533 goto out_freepages;
534 pages[i] = page;
535 }
536
Trond Myklebust4a201d62010-10-23 14:53:23 -0400537 ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400538 if (!IS_ERR_OR_NULL(ptr))
539 return ptr;
540out_freepages:
541 nfs_readdir_free_pagearray(pages, i);
542 return NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400543}
544
545static
546int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
547{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400548 struct page *pages[NFS_MAX_READDIR_PAGES];
549 void *pages_ptr = NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400550 struct nfs_entry entry;
551 struct file *file = desc->file;
552 struct nfs_cache_array *array;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500553 int status = -ENOMEM;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400554 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400555
556 entry.prev_cookie = 0;
557 entry.cookie = *desc->dir_cookie;
558 entry.eof = 0;
559 entry.fh = nfs_alloc_fhandle();
560 entry.fattr = nfs_alloc_fattr();
561 if (entry.fh == NULL || entry.fattr == NULL)
562 goto out;
563
564 array = nfs_readdir_get_array(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500565 if (IS_ERR(array)) {
566 status = PTR_ERR(array);
567 goto out;
568 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400569 memset(array, 0, sizeof(struct nfs_cache_array));
570 array->eof_index = -1;
571
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400572 pages_ptr = nfs_readdir_large_page(pages, array_size);
573 if (!pages_ptr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400574 goto out_release_array;
575 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500576 unsigned int pglen;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400577 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400578
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400579 if (status < 0)
580 break;
Trond Myklebustac396122010-11-15 20:26:22 -0500581 pglen = status;
582 status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, pglen);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500583 if (status < 0) {
584 if (status == -ENOSPC)
585 status = 0;
586 break;
587 }
588 } while (array->eof_index < 0);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400589
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400590 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400591out_release_array:
592 nfs_readdir_release_array(page);
593out:
594 nfs_free_fattr(entry.fattr);
595 nfs_free_fhandle(entry.fh);
596 return status;
597}
598
599/*
600 * Now we cache directories properly, by converting xdr information
601 * to an array that can be used for lookups later. This results in
602 * fewer cache pages, since we can store more information on each page.
603 * We only need to convert from xdr once so future lookups are much simpler
604 */
605static
606int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
607{
608 struct inode *inode = desc->file->f_path.dentry->d_inode;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500609 int ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400610
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500611 ret = nfs_readdir_xdr_to_array(desc, page, inode);
612 if (ret < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400613 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400615
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400616 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700617 /* Should never happen */
618 nfs_zap_mapping(inode, inode->i_mapping);
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 unlock_page(page);
621 return 0;
622 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unlock_page(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400627static
628void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 page_cache_release(desc->page);
631 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400634static
635struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500637 return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400638 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400642 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Olivier Galibert00a92642005-06-22 17:16:29 +0000643 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400644static
645int find_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000646{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400647 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000648
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400649 desc->page = get_cache_page(desc);
650 if (IS_ERR(desc->page))
651 return PTR_ERR(desc->page);
Olivier Galibert00a92642005-06-22 17:16:29 +0000652
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400653 res = nfs_readdir_search_array(desc);
654 if (res == 0)
655 return 0;
656 cache_page_release(desc);
657 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000658}
659
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400660/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static inline
662int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
663{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500664 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000665
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500666 if (desc->page_index == 0)
667 desc->current_index = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400668 while (1) {
669 res = find_cache_page(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (res != -EAGAIN)
671 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400672 desc->page_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return res;
675}
676
677static inline unsigned int dt_type(struct inode *inode)
678{
679 return (inode->i_mode >> 12) & 15;
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/*
683 * Once we've found the start of the dirent within a page: fill 'er up...
684 */
685static
686int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
687 filldir_t filldir)
688{
689 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400690 int i = 0;
691 int res = 0;
692 struct nfs_cache_array *array = NULL;
693 unsigned int d_type = DT_UNKNOWN;
694 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400696 array = nfs_readdir_get_array(desc->page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500697 if (IS_ERR(array))
698 return PTR_ERR(array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400700 for (i = desc->cache_entry_index; i < array->size; i++) {
701 d_type = DT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400703 res = filldir(dirent, array->array[i].string.name,
704 array->array[i].string.len, file->f_pos,
705 nfs_compat_user_ino64(array->array[i].ino), d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (res < 0)
707 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000708 file->f_pos++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400709 desc->cache_entry_index = i;
710 if (i < (array->size-1))
711 *desc->dir_cookie = array->array[i+1].cookie;
712 else
713 *desc->dir_cookie = array->last_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500715 if (i == array->eof_index)
716 desc->eof = 1;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400717
718 nfs_readdir_release_array(desc->page);
719 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (dentry != NULL)
721 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500722 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
723 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return res;
725}
726
727/*
728 * If we cannot find a cookie in our cache, we suspect that this is
729 * because it points to a deleted file, so we ask the server to return
730 * whatever it thinks is the next entry. We then feed this to filldir.
731 * If all goes well, we should then be able to find our way round the
732 * cache on the next call to readdir_search_pagecache();
733 *
734 * NOTE: we cannot add the anonymous page to the pagecache because
735 * the data it contains might not be page aligned. Besides,
736 * we should already have a complete representation of the
737 * directory in the page cache by the time we get here.
738 */
739static inline
740int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
741 filldir_t filldir)
742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct page *page = NULL;
744 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400745 struct inode *inode = desc->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500747 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
748 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 page = alloc_page(GFP_HIGHUSER);
751 if (!page) {
752 status = -ENOMEM;
753 goto out;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400756 if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
757 status = -EIO;
758 goto out_release;
759 }
760
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400761 desc->page_index = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400762 desc->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 status = nfs_do_filldir(desc, dirent, filldir);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500766 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700767 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return status;
769 out_release:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400770 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto out;
772}
773
Olivier Galibert00a92642005-06-22 17:16:29 +0000774/* The file offset position represents the dirent entry number. A
775 last cookie cache takes care of the common case of reading the
776 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
779{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800780 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct inode *inode = dentry->d_inode;
782 nfs_readdir_descriptor_t my_desc,
783 *desc = &my_desc;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400784 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Chuck Lever6da24bc2008-06-11 17:55:58 -0400786 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500787 dentry->d_parent->d_name.name, dentry->d_name.name,
788 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500789 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000792 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000793 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000794 * to either find the entry with the appropriate number or
795 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
797 memset(desc, 0, sizeof(*desc));
798
799 desc->file = filp;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400800 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 desc->decode = NFS_PROTO(inode)->decode_dirent;
802 desc->plus = NFS_USE_READDIRPLUS(inode);
803
Trond Myklebust565277f2007-10-15 18:17:53 -0400804 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800805 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500806 if (res < 0)
807 goto out;
808
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400809 while (desc->eof != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (res == -EBADCOOKIE) {
813 /* This means either end of directory */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400814 if (*desc->dir_cookie && desc->eof == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* Or that the server has 'lost' a cookie */
816 res = uncached_readdir(desc, dirent, filldir);
817 if (res >= 0)
818 continue;
819 }
820 res = 0;
821 break;
822 }
823 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200824 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400826 desc->page_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 desc->plus = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400828 desc->eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 continue;
830 }
831 if (res < 0)
832 break;
833
834 res = nfs_do_filldir(desc, dirent, filldir);
835 if (res < 0) {
836 res = 0;
837 break;
838 }
839 }
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500840out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400841 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500842 if (res > 0)
843 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400844 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500845 dentry->d_parent->d_name.name, dentry->d_name.name,
846 res);
847 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Trond Myklebust10afec902007-05-14 17:16:04 -0400850static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000851{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400852 struct dentry *dentry = filp->f_path.dentry;
853 struct inode *inode = dentry->d_inode;
854
Chuck Lever6da24bc2008-06-11 17:55:58 -0400855 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400856 dentry->d_parent->d_name.name,
857 dentry->d_name.name,
858 offset, origin);
859
860 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000861 switch (origin) {
862 case 1:
863 offset += filp->f_pos;
864 case 0:
865 if (offset >= 0)
866 break;
867 default:
868 offset = -EINVAL;
869 goto out;
870 }
871 if (offset != filp->f_pos) {
872 filp->f_pos = offset;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400873 nfs_file_open_context(filp)->dir_cookie = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000874 }
875out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400876 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000877 return offset;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * All directory operations under NFS are synchronous, so fsync()
882 * is a dummy operation.
883 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200884static int nfs_fsync_dir(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200886 struct dentry *dentry = filp->f_path.dentry;
887
Chuck Lever6da24bc2008-06-11 17:55:58 -0400888 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500889 dentry->d_parent->d_name.name, dentry->d_name.name,
890 datasync);
891
Chuck Lever54917782008-05-27 16:29:07 -0400892 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return 0;
894}
895
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400896/**
897 * nfs_force_lookup_revalidate - Mark the directory as having changed
898 * @dir - pointer to directory inode
899 *
900 * This forces the revalidation code in nfs_lookup_revalidate() to do a
901 * full lookup on all child dentries of 'dir' whenever a change occurs
902 * on the server that might have invalidated our dcache.
903 *
904 * The caller should be holding dir->i_lock
905 */
906void nfs_force_lookup_revalidate(struct inode *dir)
907{
Trond Myklebust011935a2008-10-14 19:24:50 -0400908 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/*
912 * A check for whether or not the parent directory has changed.
913 * In the case it has, we assume that the dentries are untrustworthy
914 * and may need to be looked up again.
915 */
Trond Myklebustc79ba782007-01-31 08:16:24 -0500916static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 if (IS_ROOT(dentry))
919 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400920 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
921 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -0400922 if (!nfs_verify_change_attribute(dir, dentry->d_time))
923 return 0;
924 /* Revalidate nfsi->cache_change_attribute before we declare a match */
925 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
926 return 0;
927 if (!nfs_verify_change_attribute(dir, dentry->d_time))
928 return 0;
929 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400933 * Return the intent data that applies to this particular path component
934 *
935 * Note that the current set of intents only apply to the very last
936 * component of the path.
937 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
938 */
939static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
940{
941 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
942 return 0;
943 return nd->flags & mask;
944}
945
946/*
Trond Myklebusta12802c2007-10-02 19:13:04 -0400947 * Use intent information to check whether or not we're going to do
948 * an O_EXCL create using this path component.
949 */
950static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
951{
952 if (NFS_PROTO(dir)->version == 2)
953 return 0;
Al Viro35165862008-08-05 03:00:49 -0400954 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -0400955}
956
957/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400958 * Inode and filehandle revalidation for lookups.
959 *
960 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
961 * or if the intent information indicates that we're about to open this
962 * particular file and the "nocto" mount flag is not set.
963 *
964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static inline
966int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
967{
968 struct nfs_server *server = NFS_SERVER(inode);
969
Trond Myklebust4e99a1f2008-03-06 12:34:59 -0500970 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400974 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto out_force;
976 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400977 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400978 !(server->flags & NFS_MOUNT_NOCTO) &&
979 (S_ISREG(inode->i_mode) ||
980 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -0400982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 return nfs_revalidate_inode(server, inode);
985out_force:
986 return __nfs_revalidate_inode(server, inode);
987}
988
989/*
990 * We judge how long we want to trust negative
991 * dentries by looking at the parent inode mtime.
992 *
993 * If parent mtime has changed, we revalidate, else we wait for a
994 * period corresponding to the parent's attribute cache timeout value.
995 */
996static inline
997int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
998 struct nameidata *nd)
999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001001 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001003 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1004 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return !nfs_check_verifier(dir, dentry);
1006}
1007
1008/*
1009 * This is called every time the dcache has a lookup hit,
1010 * and we should check whether we can really trust that
1011 * lookup.
1012 *
1013 * NOTE! The hit can be a negative hit too, don't assume
1014 * we have an inode!
1015 *
1016 * If the parent directory is seen to have changed, we throw out the
1017 * cached dentry and do a new lookup.
1018 */
1019static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
1020{
1021 struct inode *dir;
1022 struct inode *inode;
1023 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001024 struct nfs_fh *fhandle = NULL;
1025 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -05001030 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 inode = dentry->d_inode;
1032
1033 if (!inode) {
1034 if (nfs_neg_need_reval(dir, dentry, nd))
1035 goto out_bad;
1036 goto out_valid;
1037 }
1038
1039 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001040 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001041 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001042 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto out_bad;
1044 }
1045
Trond Myklebust15860ab2008-12-23 15:21:54 -05001046 if (nfs_have_delegation(inode, FMODE_READ))
1047 goto out_set_verifier;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -04001050 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (nfs_lookup_verify_inode(inode, nd))
1052 goto out_zap_parent;
1053 goto out_valid;
1054 }
1055
1056 if (NFS_STALE(inode))
1057 goto out_bad;
1058
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001059 error = -ENOMEM;
1060 fhandle = nfs_alloc_fhandle();
1061 fattr = nfs_alloc_fattr();
1062 if (fhandle == NULL || fattr == NULL)
1063 goto out_error;
1064
1065 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (error)
1067 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001068 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001070 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto out_bad;
1072
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001073 nfs_free_fattr(fattr);
1074 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001075out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -04001076 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001079 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001080 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001081 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 1;
1083out_zap_parent:
1084 nfs_zap_caches(dir);
1085 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -04001086 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (inode && S_ISDIR(inode->i_mode)) {
1088 /* Purge readdir caches. */
1089 nfs_zap_caches(inode);
1090 /* If we have submounts, don't unhash ! */
1091 if (have_submounts(dentry))
1092 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +01001093 if (dentry->d_flags & DCACHE_DISCONNECTED)
1094 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 shrink_dcache_parent(dentry);
1096 }
1097 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001098 nfs_free_fattr(fattr);
1099 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001101 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001102 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001103 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001105out_error:
1106 nfs_free_fattr(fattr);
1107 nfs_free_fhandle(fhandle);
1108 dput(parent);
1109 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1110 __func__, dentry->d_parent->d_name.name,
1111 dentry->d_name.name, error);
1112 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115/*
1116 * This is called from dput() when d_count is going to 0.
1117 */
1118static int nfs_dentry_delete(struct dentry *dentry)
1119{
1120 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1121 dentry->d_parent->d_name.name, dentry->d_name.name,
1122 dentry->d_flags);
1123
Trond Myklebust77f11192008-01-28 19:43:19 -05001124 /* Unhash any dentry with a stale inode */
1125 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1126 return 1;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1129 /* Unhash it, so that ->d_iput() would be called */
1130 return 1;
1131 }
1132 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1133 /* Unhash it, so that ancestors of killed async unlink
1134 * files will be cleaned up during umount */
1135 return 1;
1136 }
1137 return 0;
1138
1139}
1140
Trond Myklebust1b83d702008-06-11 15:44:04 -04001141static void nfs_drop_nlink(struct inode *inode)
1142{
1143 spin_lock(&inode->i_lock);
1144 if (inode->i_nlink > 0)
1145 drop_nlink(inode);
1146 spin_unlock(&inode->i_lock);
1147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/*
1150 * Called when the dentry loses inode.
1151 * We use it to clean up silly-renamed files.
1152 */
1153static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1154{
Neil Brown83672d32007-02-26 12:48:25 +11001155 if (S_ISDIR(inode->i_mode))
1156 /* drop any readdir cache as it could easily be old */
1157 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001160 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001161 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 iput(inode);
1164}
1165
Al Virof786aa92009-02-20 05:51:22 +00001166const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 .d_revalidate = nfs_lookup_revalidate,
1168 .d_delete = nfs_dentry_delete,
1169 .d_iput = nfs_dentry_iput,
1170};
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1173{
1174 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -04001175 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001177 struct nfs_fh *fhandle = NULL;
1178 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1182 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -05001183 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 res = ERR_PTR(-ENAMETOOLONG);
1186 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1187 goto out;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1190
Trond Myklebustfd684072006-09-05 12:27:44 -04001191 /*
1192 * If we're doing an exclusive create, optimize away the lookup
1193 * but don't hash the dentry.
1194 */
1195 if (nfs_is_exclusive_create(dir, nd)) {
1196 d_instantiate(dentry, NULL);
1197 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -04001198 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -04001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001201 res = ERR_PTR(-ENOMEM);
1202 fhandle = nfs_alloc_fhandle();
1203 fattr = nfs_alloc_fattr();
1204 if (fhandle == NULL || fattr == NULL)
1205 goto out;
1206
Trond Myklebust565277f2007-10-15 18:17:53 -04001207 parent = dentry->d_parent;
1208 /* Protect against concurrent sillydeletes */
1209 nfs_block_sillyrename(parent);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001210 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (error == -ENOENT)
1212 goto no_entry;
1213 if (error < 0) {
1214 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -04001215 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001217 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001218 res = (struct dentry *)inode;
1219 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001220 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -04001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222no_entry:
David Howells54ceac42006-08-22 20:06:13 -04001223 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001224 if (res != NULL) {
1225 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001226 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001230out_unblock_sillyrename:
1231 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001233 nfs_free_fattr(fattr);
1234 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return res;
1236}
1237
1238#ifdef CONFIG_NFS_V4
1239static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1240
Al Virof786aa92009-02-20 05:51:22 +00001241const struct dentry_operations nfs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .d_revalidate = nfs_open_revalidate,
1243 .d_delete = nfs_dentry_delete,
1244 .d_iput = nfs_dentry_iput,
1245};
1246
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001247/*
1248 * Use intent information to determine whether we need to substitute
1249 * the NFSv4-style stateful OPEN for the LOOKUP call
1250 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001251static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001253 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255 /* NFS does not (yet) have a stateful open for directories */
1256 if (nd->flags & LOOKUP_DIRECTORY)
1257 return 0;
1258 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001259 if (__mnt_is_readonly(nd->path.mnt) &&
1260 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262 return 1;
1263}
1264
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001265static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1266{
1267 struct path path = {
1268 .mnt = nd->path.mnt,
1269 .dentry = dentry,
1270 };
1271 struct nfs_open_context *ctx;
1272 struct rpc_cred *cred;
1273 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1274
1275 cred = rpc_lookup_cred();
1276 if (IS_ERR(cred))
1277 return ERR_CAST(cred);
1278 ctx = alloc_nfs_open_context(&path, cred, fmode);
1279 put_rpccred(cred);
1280 if (ctx == NULL)
1281 return ERR_PTR(-ENOMEM);
1282 return ctx;
1283}
1284
1285static int do_open(struct inode *inode, struct file *filp)
1286{
1287 nfs_fscache_set_inode_cookie(inode, filp);
1288 return 0;
1289}
1290
1291static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1292{
1293 struct file *filp;
1294 int ret = 0;
1295
1296 /* If the open_intent is for execute, we have an extra check to make */
1297 if (ctx->mode & FMODE_EXEC) {
1298 ret = nfs_may_open(ctx->path.dentry->d_inode,
1299 ctx->cred,
1300 nd->intent.open.flags);
1301 if (ret < 0)
1302 goto out;
1303 }
1304 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1305 if (IS_ERR(filp))
1306 ret = PTR_ERR(filp);
1307 else
1308 nfs_file_set_open_context(filp, ctx);
1309out:
1310 put_nfs_open_context(ctx);
1311 return ret;
1312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1315{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001316 struct nfs_open_context *ctx;
1317 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct dentry *res = NULL;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001319 struct inode *inode;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001320 int open_flags;
Trond Myklebust898f6352010-10-23 11:24:25 -04001321 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001323 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1324 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* Check that we are indeed trying to open this file */
Trond Myklebust5584c302008-12-23 15:21:54 -05001327 if (!is_atomic_open(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto no_open;
1329
1330 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1331 res = ERR_PTR(-ENAMETOOLONG);
1332 goto out;
1333 }
1334 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1335
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001336 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1337 * the dentry. */
Al Viro35165862008-08-05 03:00:49 -04001338 if (nd->flags & LOOKUP_EXCL) {
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001339 d_instantiate(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001340 goto out;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001343 ctx = nameidata_to_nfs_open_context(dentry, nd);
1344 res = ERR_CAST(ctx);
1345 if (IS_ERR(ctx))
1346 goto out;
1347
1348 open_flags = nd->intent.open.flags;
1349 if (nd->flags & LOOKUP_CREATE) {
1350 attr.ia_mode = nd->intent.open.create_mode;
1351 attr.ia_valid = ATTR_MODE;
1352 if (!IS_POSIXACL(dir))
1353 attr.ia_mode &= ~current_umask();
1354 } else {
Trond Myklebust898f6352010-10-23 11:24:25 -04001355 open_flags &= ~(O_EXCL | O_CREAT);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001356 attr.ia_valid = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001357 }
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* Open the file on the server */
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001360 nfs_block_sillyrename(dentry->d_parent);
Trond Myklebust2b484292010-09-17 10:56:51 -04001361 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001362 if (IS_ERR(inode)) {
1363 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001364 put_nfs_open_context(ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001365 switch (PTR_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* Make a negative dentry */
1367 case -ENOENT:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001368 d_add(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001369 res = NULL;
1370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* This turned out not to be a regular file */
Trond Myklebust6f926b52005-10-18 14:20:18 -07001372 case -ENOTDIR:
1373 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 case -ELOOP:
1375 if (!(nd->intent.open.flags & O_NOFOLLOW))
1376 goto no_open;
Trond Myklebust23ebbd92010-11-03 10:24:16 -04001377 /* case -EISDIR: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* case -EINVAL: */
1379 default:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001380 res = ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 goto out;
1382 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001383 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001384 res = d_add_unique(dentry, inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001385 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001386 if (res != NULL) {
1387 dput(ctx->path.dentry);
1388 ctx->path.dentry = dget(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 dentry = res;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001390 }
Trond Myklebust898f6352010-10-23 11:24:25 -04001391 err = nfs_intent_set_file(nd, ctx);
1392 if (err < 0) {
1393 if (res != NULL)
1394 dput(res);
1395 return ERR_PTR(err);
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397out:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001398 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return res;
1400no_open:
1401 return nfs_lookup(dir, dentry, nd);
1402}
1403
1404static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1405{
1406 struct dentry *parent = NULL;
1407 struct inode *inode = dentry->d_inode;
1408 struct inode *dir;
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001409 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int openflags, ret = 0;
1411
Trond Myklebust1f063d22010-04-22 15:35:55 -04001412 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001413 goto no_open;
Trond Myklebust2b484292010-09-17 10:56:51 -04001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 parent = dget_parent(dentry);
1416 dir = parent->d_inode;
Trond Myklebust2b484292010-09-17 10:56:51 -04001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* We can't create new files in nfs_open_revalidate(), so we
1419 * optimize away revalidation of negative dentries.
1420 */
Trond Myklebust216d5d062007-10-01 20:10:12 -04001421 if (inode == NULL) {
1422 if (!nfs_neg_need_reval(dir, dentry, nd))
1423 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 goto out;
Trond Myklebust216d5d062007-10-01 20:10:12 -04001425 }
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* NFS only supports OPEN on regular files */
1428 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001429 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 openflags = nd->intent.open.flags;
1431 /* We cannot do exclusive creation on a positive dentry */
1432 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001433 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* We can't create new files, or truncate existing ones here */
Trond Myklebust0a377cf2010-08-18 09:25:42 -04001435 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001437 ctx = nameidata_to_nfs_open_context(dentry, nd);
1438 ret = PTR_ERR(ctx);
1439 if (IS_ERR(ctx))
1440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001442 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * operations that change the directory. We therefore save the
1444 * change attribute *before* we do the RPC call.
1445 */
Trond Myklebust2b484292010-09-17 10:56:51 -04001446 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
Trond Myklebust535918f2010-09-17 10:56:51 -04001447 if (IS_ERR(inode)) {
1448 ret = PTR_ERR(inode);
1449 switch (ret) {
1450 case -EPERM:
1451 case -EACCES:
1452 case -EDQUOT:
1453 case -ENOSPC:
1454 case -EROFS:
1455 goto out_put_ctx;
1456 default:
1457 goto out_drop;
1458 }
1459 }
1460 iput(inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001461 if (inode != dentry->d_inode)
Trond Myklebust535918f2010-09-17 10:56:51 -04001462 goto out_drop;
Trond Myklebust898f6352010-10-23 11:24:25 -04001463
1464 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1465 ret = nfs_intent_set_file(nd, ctx);
1466 if (ret >= 0)
1467 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468out:
1469 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001471out_drop:
1472 d_drop(dentry);
1473 ret = 0;
1474out_put_ctx:
1475 put_nfs_open_context(ctx);
1476 goto out;
1477
Trond Myklebust5584c302008-12-23 15:21:54 -05001478no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001480no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return nfs_lookup_revalidate(dentry, nd);
1482}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001483
1484static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1485 struct nameidata *nd)
1486{
1487 struct nfs_open_context *ctx = NULL;
1488 struct iattr attr;
1489 int error;
1490 int open_flags = 0;
1491
1492 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1493 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1494
1495 attr.ia_mode = mode;
1496 attr.ia_valid = ATTR_MODE;
1497
1498 if ((nd->flags & LOOKUP_CREATE) != 0) {
1499 open_flags = nd->intent.open.flags;
1500
1501 ctx = nameidata_to_nfs_open_context(dentry, nd);
1502 error = PTR_ERR(ctx);
1503 if (IS_ERR(ctx))
Trond Myklebust898f6352010-10-23 11:24:25 -04001504 goto out_err_drop;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001505 }
1506
1507 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1508 if (error != 0)
1509 goto out_put_ctx;
Trond Myklebust898f6352010-10-23 11:24:25 -04001510 if (ctx != NULL) {
1511 error = nfs_intent_set_file(nd, ctx);
1512 if (error < 0)
1513 goto out_err;
1514 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001515 return 0;
1516out_put_ctx:
1517 if (ctx != NULL)
1518 put_nfs_open_context(ctx);
Trond Myklebust898f6352010-10-23 11:24:25 -04001519out_err_drop:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001520 d_drop(dentry);
Trond Myklebust898f6352010-10-23 11:24:25 -04001521out_err:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001522 return error;
1523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525#endif /* CONFIG_NFSV4 */
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/*
1528 * Code common to create, mkdir, and mknod.
1529 */
1530int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1531 struct nfs_fattr *fattr)
1532{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001533 struct dentry *parent = dget_parent(dentry);
1534 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct inode *inode;
1536 int error = -EACCES;
1537
Trond Myklebustfab728e2007-09-29 17:41:33 -04001538 d_drop(dentry);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* We may have been initialized further down */
1541 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (fhandle->size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1545 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001546 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001548 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1550 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001551 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001553 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001556 error = PTR_ERR(inode);
1557 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001558 goto out_error;
1559 d_add(dentry, inode);
1560out:
1561 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001563out_error:
1564 nfs_mark_for_revalidate(dir);
1565 dput(parent);
1566 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
1569/*
1570 * Following a failed create operation, we drop the dentry rather
1571 * than retain a negative dentry. This avoids a problem in the event
1572 * that the operation succeeded on the server, but an error in the
1573 * reply path made it appear to have failed.
1574 */
1575static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1576 struct nameidata *nd)
1577{
1578 struct iattr attr;
1579 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001581 dfprintk(VFS, "NFS: create(%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 attr.ia_mode = mode;
1585 attr.ia_valid = ATTR_MODE;
1586
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001587 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (error != 0)
1589 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return 0;
1591out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 d_drop(dentry);
1593 return error;
1594}
1595
1596/*
1597 * See comments for nfs_proc_create regarding failed operations.
1598 */
1599static int
1600nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1601{
1602 struct iattr attr;
1603 int status;
1604
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001605 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1606 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (!new_valid_dev(rdev))
1609 return -EINVAL;
1610
1611 attr.ia_mode = mode;
1612 attr.ia_valid = ATTR_MODE;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (status != 0)
1616 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
1618out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 d_drop(dentry);
1620 return status;
1621}
1622
1623/*
1624 * See comments for nfs_proc_create regarding failed operations.
1625 */
1626static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1627{
1628 struct iattr attr;
1629 int error;
1630
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001631 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1632 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 attr.ia_valid = ATTR_MODE;
1635 attr.ia_mode = mode | S_IFDIR;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (error != 0)
1639 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return 0;
1641out_err:
1642 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return error;
1644}
1645
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001646static void nfs_dentry_handle_enoent(struct dentry *dentry)
1647{
1648 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1649 d_delete(dentry);
1650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1653{
1654 int error;
1655
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001656 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1657 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1660 /* Ensure the VFS deletes this inode */
1661 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001662 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001663 else if (error == -ENOENT)
1664 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 return error;
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/*
1670 * Remove a file after making sure there are no pending writes,
1671 * and after checking that the file has only one user.
1672 *
1673 * We invalidate the attribute cache and free the inode prior to the operation
1674 * to avoid possible races if the server reuses the inode.
1675 */
1676static int nfs_safe_remove(struct dentry *dentry)
1677{
1678 struct inode *dir = dentry->d_parent->d_inode;
1679 struct inode *inode = dentry->d_inode;
1680 int error = -EBUSY;
1681
1682 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1683 dentry->d_parent->d_name.name, dentry->d_name.name);
1684
1685 /* If the dentry was sillyrenamed, we simply call d_delete() */
1686 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1687 error = 0;
1688 goto out;
1689 }
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001692 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1694 /* The VFS may want to delete this inode */
1695 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001696 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001697 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 } else
1699 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001700 if (error == -ENOENT)
1701 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702out:
1703 return error;
1704}
1705
1706/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1707 * belongs to an active ".nfs..." file and we return -EBUSY.
1708 *
1709 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1710 */
1711static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1712{
1713 int error;
1714 int need_rehash = 0;
1715
1716 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1717 dir->i_ino, dentry->d_name.name);
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 spin_lock(&dcache_lock);
1720 spin_lock(&dentry->d_lock);
1721 if (atomic_read(&dentry->d_count) > 1) {
1722 spin_unlock(&dentry->d_lock);
1723 spin_unlock(&dcache_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001724 /* Start asynchronous writeout of the inode */
1725 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return error;
1728 }
1729 if (!d_unhashed(dentry)) {
1730 __d_drop(dentry);
1731 need_rehash = 1;
1732 }
1733 spin_unlock(&dentry->d_lock);
1734 spin_unlock(&dcache_lock);
1735 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001736 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1738 } else if (need_rehash)
1739 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return error;
1741}
1742
Chuck Lever873101b2006-08-22 20:06:23 -04001743/*
1744 * To create a symbolic link, most file systems instantiate a new inode,
1745 * add a page to it containing the path, then write it out to the disk
1746 * using prepare_write/commit_write.
1747 *
1748 * Unfortunately the NFS client can't create the in-core inode first
1749 * because it needs a file handle to create an in-core inode (see
1750 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1751 * symlink request has completed on the server.
1752 *
1753 * So instead we allocate a raw page, copy the symname into it, then do
1754 * the SYMLINK request with the page as the buffer. If it succeeds, we
1755 * now have a new file handle and can instantiate an in-core NFS inode
1756 * and move the raw page into its mapping.
1757 */
1758static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Chuck Lever873101b2006-08-22 20:06:23 -04001760 struct pagevec lru_pvec;
1761 struct page *page;
1762 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001764 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int error;
1766
1767 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1768 dir->i_ino, dentry->d_name.name, symname);
1769
Chuck Lever873101b2006-08-22 20:06:23 -04001770 if (pathlen > PAGE_SIZE)
1771 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Chuck Lever873101b2006-08-22 20:06:23 -04001773 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1774 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Jeff Layton83d93f22007-06-07 09:58:08 -04001776 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001777 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001778 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001779
1780 kaddr = kmap_atomic(page, KM_USER0);
1781 memcpy(kaddr, symname, pathlen);
1782 if (pathlen < PAGE_SIZE)
1783 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1784 kunmap_atomic(kaddr, KM_USER0);
1785
Chuck Lever94a6d752006-08-22 20:06:23 -04001786 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001787 if (error != 0) {
1788 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1789 dir->i_sb->s_id, dir->i_ino,
1790 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001792 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001793 return error;
1794 }
1795
1796 /*
1797 * No big deal if we can't add this page to the page cache here.
1798 * READLINK will get the missing page from the server if needed.
1799 */
1800 pagevec_init(&lru_pvec, 0);
1801 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1802 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001803 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001804 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001805 SetPageUptodate(page);
1806 unlock_page(page);
1807 } else
1808 __free_page(page);
1809
Chuck Lever873101b2006-08-22 20:06:23 -04001810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813static int
1814nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1815{
1816 struct inode *inode = old_dentry->d_inode;
1817 int error;
1818
1819 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1820 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1821 dentry->d_parent->d_name.name, dentry->d_name.name);
1822
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001823 nfs_inode_return_delegation(inode);
1824
Trond Myklebust9697d232007-10-02 21:58:05 -04001825 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001827 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04001828 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04001829 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return error;
1832}
1833
1834/*
1835 * RENAME
1836 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1837 * different file handle for the same inode after a rename (e.g. when
1838 * moving to a different directory). A fail-safe method to do so would
1839 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1840 * rename the old file using the sillyrename stuff. This way, the original
1841 * file in old_dir will go away when the last process iput()s the inode.
1842 *
1843 * FIXED.
1844 *
1845 * It actually works quite well. One needs to have the possibility for
1846 * at least one ".nfs..." file in each directory the file ever gets
1847 * moved or linked to which happens automagically with the new
1848 * implementation that only depends on the dcache stuff instead of
1849 * using the inode layer
1850 *
1851 * Unfortunately, things are a little more complicated than indicated
1852 * above. For a cross-directory move, we want to make sure we can get
1853 * rid of the old inode after the operation. This means there must be
1854 * no pending writes (if it's a file), and the use count must be 1.
1855 * If these conditions are met, we can drop the dentries before doing
1856 * the rename.
1857 */
1858static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1859 struct inode *new_dir, struct dentry *new_dentry)
1860{
1861 struct inode *old_inode = old_dentry->d_inode;
1862 struct inode *new_inode = new_dentry->d_inode;
1863 struct dentry *dentry = NULL, *rehash = NULL;
1864 int error = -EBUSY;
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1867 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1868 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1869 atomic_read(&new_dentry->d_count));
1870
1871 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001872 * For non-directories, check whether the target is busy and if so,
1873 * make a copy of the dentry and then do a silly-rename. If the
1874 * silly-rename succeeds, the copied dentry is hashed and becomes
1875 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001877 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1878 /*
1879 * To prevent any new references to the target during the
1880 * rename, we unhash the dentry in advance.
1881 */
1882 if (!d_unhashed(new_dentry)) {
1883 d_drop(new_dentry);
1884 rehash = new_dentry;
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Miklos Szeredi27226102009-12-03 15:58:56 -05001887 if (atomic_read(&new_dentry->d_count) > 2) {
1888 int err;
1889
1890 /* copy the target dentry's name */
1891 dentry = d_alloc(new_dentry->d_parent,
1892 &new_dentry->d_name);
1893 if (!dentry)
1894 goto out;
1895
1896 /* silly-rename the existing target ... */
1897 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001898 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001899 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001900
1901 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001902 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001903 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05001904 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Trond Myklebustcae7a072005-10-18 14:20:19 -07001907 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001908 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01001909 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1912 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001913 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914out:
1915 if (rehash)
1916 d_rehash(rehash);
1917 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001918 if (new_inode != NULL)
1919 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07001920 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04001921 nfs_set_verifier(new_dentry,
1922 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001923 } else if (error == -ENOENT)
1924 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /* new dentry created? */
1927 if (dentry)
1928 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return error;
1930}
1931
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001932static DEFINE_SPINLOCK(nfs_access_lru_lock);
1933static LIST_HEAD(nfs_access_lru_list);
1934static atomic_long_t nfs_access_nr_entries;
1935
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001936static void nfs_access_free_entry(struct nfs_access_entry *entry)
1937{
1938 put_rpccred(entry->cred);
1939 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001940 smp_mb__before_atomic_dec();
1941 atomic_long_dec(&nfs_access_nr_entries);
1942 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001943}
1944
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001945static void nfs_access_free_list(struct list_head *head)
1946{
1947 struct nfs_access_entry *cache;
1948
1949 while (!list_empty(head)) {
1950 cache = list_entry(head->next, struct nfs_access_entry, lru);
1951 list_del(&cache->lru);
1952 nfs_access_free_entry(cache);
1953 }
1954}
1955
Dave Chinner7f8275d2010-07-19 14:56:17 +10001956int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebust979df722006-07-25 11:28:19 -04001957{
1958 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001959 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04001960 struct nfs_access_entry *cache;
1961
Trond Myklebust61d5eb22010-05-13 12:51:06 -04001962 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1963 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001964
Trond Myklebusta50f7952007-06-05 19:23:43 -04001965 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001966 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04001967 struct inode *inode;
1968
1969 if (nr_to_scan-- == 0)
1970 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001971 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04001972 spin_lock(&inode->i_lock);
1973 if (list_empty(&nfsi->access_cache_entry_lru))
1974 goto remove_lru_entry;
1975 cache = list_entry(nfsi->access_cache_entry_lru.next,
1976 struct nfs_access_entry, lru);
1977 list_move(&cache->lru, &head);
1978 rb_erase(&cache->rb_node, &nfsi->access_cache);
1979 if (!list_empty(&nfsi->access_cache_entry_lru))
1980 list_move_tail(&nfsi->access_cache_inode_lru,
1981 &nfs_access_lru_list);
1982 else {
1983remove_lru_entry:
1984 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001985 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001986 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001987 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001988 }
Trond Myklebust59844a92010-05-26 08:42:24 -04001989 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04001990 }
1991 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001992 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04001993 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1994}
1995
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001996static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001997{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001998 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001999 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002000 struct nfs_access_entry *entry;
2001
2002 /* Unhook entries from the cache */
2003 while ((n = rb_first(root_node)) != NULL) {
2004 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2005 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002006 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002007 }
2008 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002009}
2010
2011void nfs_access_zap_cache(struct inode *inode)
2012{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002013 LIST_HEAD(head);
2014
2015 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2016 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002017 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002018 spin_lock(&nfs_access_lru_lock);
2019 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002020 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002021
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002022 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002023 __nfs_access_zap_cache(NFS_I(inode), &head);
2024 spin_unlock(&inode->i_lock);
2025 spin_unlock(&nfs_access_lru_lock);
2026 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002027}
2028
2029static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2030{
2031 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2032 struct nfs_access_entry *entry;
2033
2034 while (n != NULL) {
2035 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2036
2037 if (cred < entry->cred)
2038 n = n->rb_left;
2039 else if (cred > entry->cred)
2040 n = n->rb_right;
2041 else
2042 return entry;
2043 }
2044 return NULL;
2045}
2046
Trond Myklebustaf22f942007-08-10 17:45:10 -04002047static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Chuck Lever55296802005-08-18 11:24:09 -07002049 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002050 struct nfs_access_entry *cache;
2051 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002053 spin_lock(&inode->i_lock);
2054 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2055 goto out_zap;
2056 cache = nfs_access_search_rbtree(inode, cred);
2057 if (cache == NULL)
2058 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05002059 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05002060 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002061 goto out_stale;
2062 res->jiffies = cache->jiffies;
2063 res->cred = cache->cred;
2064 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002065 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002066 err = 0;
2067out:
2068 spin_unlock(&inode->i_lock);
2069 return err;
2070out_stale:
2071 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002072 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002073 spin_unlock(&inode->i_lock);
2074 nfs_access_free_entry(cache);
2075 return -ENOENT;
2076out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002077 spin_unlock(&inode->i_lock);
2078 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002079 return -ENOENT;
2080}
2081
2082static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2083{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002084 struct nfs_inode *nfsi = NFS_I(inode);
2085 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002086 struct rb_node **p = &root_node->rb_node;
2087 struct rb_node *parent = NULL;
2088 struct nfs_access_entry *entry;
2089
2090 spin_lock(&inode->i_lock);
2091 while (*p != NULL) {
2092 parent = *p;
2093 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2094
2095 if (set->cred < entry->cred)
2096 p = &parent->rb_left;
2097 else if (set->cred > entry->cred)
2098 p = &parent->rb_right;
2099 else
2100 goto found;
2101 }
2102 rb_link_node(&set->rb_node, parent, p);
2103 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002104 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002105 spin_unlock(&inode->i_lock);
2106 return;
2107found:
2108 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002109 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2110 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002111 spin_unlock(&inode->i_lock);
2112 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Trond Myklebustaf22f942007-08-10 17:45:10 -04002115static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002117 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2118 if (cache == NULL)
2119 return;
2120 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002122 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002124
2125 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002126
2127 /* Update accounting */
2128 smp_mb__before_atomic_inc();
2129 atomic_long_inc(&nfs_access_nr_entries);
2130 smp_mb__after_atomic_inc();
2131
2132 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002133 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002134 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002135 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2136 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2137 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002138 spin_unlock(&nfs_access_lru_lock);
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
2142static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2143{
2144 struct nfs_access_entry cache;
2145 int status;
2146
2147 status = nfs_access_get_cached(inode, cred, &cache);
2148 if (status == 0)
2149 goto out;
2150
2151 /* Be clever: ask server to check for all possible rights */
2152 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2153 cache.cred = cred;
2154 cache.jiffies = jiffies;
2155 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002156 if (status != 0) {
2157 if (status == -ESTALE) {
2158 nfs_zap_caches(inode);
2159 if (!S_ISDIR(inode->i_mode))
2160 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 nfs_access_add_cache(inode, &cache);
2165out:
Al Viroe6305c42008-07-15 21:03:57 -04002166 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return 0;
2168 return -EACCES;
2169}
2170
Trond Myklebustaf22f942007-08-10 17:45:10 -04002171static int nfs_open_permission_mask(int openflags)
2172{
2173 int mask = 0;
2174
2175 if (openflags & FMODE_READ)
2176 mask |= MAY_READ;
2177 if (openflags & FMODE_WRITE)
2178 mask |= MAY_WRITE;
2179 if (openflags & FMODE_EXEC)
2180 mask |= MAY_EXEC;
2181 return mask;
2182}
2183
2184int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2185{
2186 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2187}
2188
Al Viroe6305c42008-07-15 21:03:57 -04002189int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct rpc_cred *cred;
2192 int res = 0;
2193
Chuck Lever91d5b472006-03-20 13:44:14 -05002194 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2195
Al Viroe6305c42008-07-15 21:03:57 -04002196 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto out;
2198 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002199 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 goto force_lookup;
2201
2202 switch (inode->i_mode & S_IFMT) {
2203 case S_IFLNK:
2204 goto out;
2205 case S_IFREG:
2206 /* NFSv4 has atomic_open... */
2207 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002208 && (mask & MAY_OPEN)
2209 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto out;
2211 break;
2212 case S_IFDIR:
2213 /*
2214 * Optimize away all write operations, since the server
2215 * will check permissions when we perform the op.
2216 */
2217 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2218 goto out;
2219 }
2220
2221force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (!NFS_PROTO(inode)->access)
2223 goto out_notsup;
2224
Trond Myklebust98a8e322008-03-12 12:25:28 -04002225 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (!IS_ERR(cred)) {
2227 res = nfs_do_access(inode, cred, mask);
2228 put_rpccred(cred);
2229 } else
2230 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002232 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2233 res = -EACCES;
2234
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002235 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2236 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return res;
2238out_notsup:
2239 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2240 if (res == 0)
2241 res = generic_permission(inode, mask, NULL);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245/*
2246 * Local variables:
2247 * version-control: t
2248 * kept-new-versions: 5
2249 * End:
2250 */