blob: c6ce8af266ed891df0e41502285dec34b9e87db3 [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 {
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400576 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400577
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400578 if (status < 0)
579 break;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500580 status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, array_size * PAGE_SIZE);
581 if (status < 0) {
582 if (status == -ENOSPC)
583 status = 0;
584 break;
585 }
586 } while (array->eof_index < 0);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400587
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400588 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400589out_release_array:
590 nfs_readdir_release_array(page);
591out:
592 nfs_free_fattr(entry.fattr);
593 nfs_free_fhandle(entry.fh);
594 return status;
595}
596
597/*
598 * Now we cache directories properly, by converting xdr information
599 * to an array that can be used for lookups later. This results in
600 * fewer cache pages, since we can store more information on each page.
601 * We only need to convert from xdr once so future lookups are much simpler
602 */
603static
604int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
605{
606 struct inode *inode = desc->file->f_path.dentry->d_inode;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500607 int ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400608
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500609 ret = nfs_readdir_xdr_to_array(desc, page, inode);
610 if (ret < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400611 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400613
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400614 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700615 /* Should never happen */
616 nfs_zap_mapping(inode, inode->i_mapping);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unlock_page(page);
619 return 0;
620 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unlock_page(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400625static
626void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 page_cache_release(desc->page);
629 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400632static
633struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500635 return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400636 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400640 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Olivier Galibert00a92642005-06-22 17:16:29 +0000641 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400642static
643int find_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000644{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400645 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000646
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400647 desc->page = get_cache_page(desc);
648 if (IS_ERR(desc->page))
649 return PTR_ERR(desc->page);
Olivier Galibert00a92642005-06-22 17:16:29 +0000650
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400651 res = nfs_readdir_search_array(desc);
652 if (res == 0)
653 return 0;
654 cache_page_release(desc);
655 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000656}
657
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400658/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static inline
660int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
661{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500662 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000663
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500664 if (desc->page_index == 0)
665 desc->current_index = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400666 while (1) {
667 res = find_cache_page(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (res != -EAGAIN)
669 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400670 desc->page_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return res;
673}
674
675static inline unsigned int dt_type(struct inode *inode)
676{
677 return (inode->i_mode >> 12) & 15;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * Once we've found the start of the dirent within a page: fill 'er up...
682 */
683static
684int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
685 filldir_t filldir)
686{
687 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400688 int i = 0;
689 int res = 0;
690 struct nfs_cache_array *array = NULL;
691 unsigned int d_type = DT_UNKNOWN;
692 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400694 array = nfs_readdir_get_array(desc->page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500695 if (IS_ERR(array))
696 return PTR_ERR(array);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400698 for (i = desc->cache_entry_index; i < array->size; i++) {
699 d_type = DT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400701 res = filldir(dirent, array->array[i].string.name,
702 array->array[i].string.len, file->f_pos,
703 nfs_compat_user_ino64(array->array[i].ino), d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (res < 0)
705 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000706 file->f_pos++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400707 desc->cache_entry_index = i;
708 if (i < (array->size-1))
709 *desc->dir_cookie = array->array[i+1].cookie;
710 else
711 *desc->dir_cookie = array->last_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500713 if (i == array->eof_index)
714 desc->eof = 1;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400715
716 nfs_readdir_release_array(desc->page);
717 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (dentry != NULL)
719 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500720 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
721 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return res;
723}
724
725/*
726 * If we cannot find a cookie in our cache, we suspect that this is
727 * because it points to a deleted file, so we ask the server to return
728 * whatever it thinks is the next entry. We then feed this to filldir.
729 * If all goes well, we should then be able to find our way round the
730 * cache on the next call to readdir_search_pagecache();
731 *
732 * NOTE: we cannot add the anonymous page to the pagecache because
733 * the data it contains might not be page aligned. Besides,
734 * we should already have a complete representation of the
735 * directory in the page cache by the time we get here.
736 */
737static inline
738int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
739 filldir_t filldir)
740{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct page *page = NULL;
742 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400743 struct inode *inode = desc->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500745 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
746 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 page = alloc_page(GFP_HIGHUSER);
749 if (!page) {
750 status = -ENOMEM;
751 goto out;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400754 if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
755 status = -EIO;
756 goto out_release;
757 }
758
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400759 desc->page_index = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400760 desc->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 status = nfs_do_filldir(desc, dirent, filldir);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500764 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700765 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return status;
767 out_release:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400768 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto out;
770}
771
Olivier Galibert00a92642005-06-22 17:16:29 +0000772/* The file offset position represents the dirent entry number. A
773 last cookie cache takes care of the common case of reading the
774 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
776static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
777{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800778 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct inode *inode = dentry->d_inode;
780 nfs_readdir_descriptor_t my_desc,
781 *desc = &my_desc;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400782 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Chuck Lever6da24bc2008-06-11 17:55:58 -0400784 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500785 dentry->d_parent->d_name.name, dentry->d_name.name,
786 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500787 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000790 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000791 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000792 * to either find the entry with the appropriate number or
793 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
795 memset(desc, 0, sizeof(*desc));
796
797 desc->file = filp;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400798 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 desc->decode = NFS_PROTO(inode)->decode_dirent;
800 desc->plus = NFS_USE_READDIRPLUS(inode);
801
Trond Myklebust565277f2007-10-15 18:17:53 -0400802 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800803 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500804 if (res < 0)
805 goto out;
806
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400807 while (desc->eof != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (res == -EBADCOOKIE) {
811 /* This means either end of directory */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400812 if (*desc->dir_cookie && desc->eof == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Or that the server has 'lost' a cookie */
814 res = uncached_readdir(desc, dirent, filldir);
815 if (res >= 0)
816 continue;
817 }
818 res = 0;
819 break;
820 }
821 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200822 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400824 desc->page_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 desc->plus = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400826 desc->eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 continue;
828 }
829 if (res < 0)
830 break;
831
832 res = nfs_do_filldir(desc, dirent, filldir);
833 if (res < 0) {
834 res = 0;
835 break;
836 }
837 }
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500838out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400839 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500840 if (res > 0)
841 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400842 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500843 dentry->d_parent->d_name.name, dentry->d_name.name,
844 res);
845 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Trond Myklebust10afec902007-05-14 17:16:04 -0400848static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000849{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400850 struct dentry *dentry = filp->f_path.dentry;
851 struct inode *inode = dentry->d_inode;
852
Chuck Lever6da24bc2008-06-11 17:55:58 -0400853 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400854 dentry->d_parent->d_name.name,
855 dentry->d_name.name,
856 offset, origin);
857
858 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000859 switch (origin) {
860 case 1:
861 offset += filp->f_pos;
862 case 0:
863 if (offset >= 0)
864 break;
865 default:
866 offset = -EINVAL;
867 goto out;
868 }
869 if (offset != filp->f_pos) {
870 filp->f_pos = offset;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400871 nfs_file_open_context(filp)->dir_cookie = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000872 }
873out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400874 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000875 return offset;
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/*
879 * All directory operations under NFS are synchronous, so fsync()
880 * is a dummy operation.
881 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200882static int nfs_fsync_dir(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200884 struct dentry *dentry = filp->f_path.dentry;
885
Chuck Lever6da24bc2008-06-11 17:55:58 -0400886 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500887 dentry->d_parent->d_name.name, dentry->d_name.name,
888 datasync);
889
Chuck Lever54917782008-05-27 16:29:07 -0400890 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
892}
893
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400894/**
895 * nfs_force_lookup_revalidate - Mark the directory as having changed
896 * @dir - pointer to directory inode
897 *
898 * This forces the revalidation code in nfs_lookup_revalidate() to do a
899 * full lookup on all child dentries of 'dir' whenever a change occurs
900 * on the server that might have invalidated our dcache.
901 *
902 * The caller should be holding dir->i_lock
903 */
904void nfs_force_lookup_revalidate(struct inode *dir)
905{
Trond Myklebust011935a2008-10-14 19:24:50 -0400906 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/*
910 * A check for whether or not the parent directory has changed.
911 * In the case it has, we assume that the dentries are untrustworthy
912 * and may need to be looked up again.
913 */
Trond Myklebustc79ba782007-01-31 08:16:24 -0500914static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 if (IS_ROOT(dentry))
917 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400918 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
919 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -0400920 if (!nfs_verify_change_attribute(dir, dentry->d_time))
921 return 0;
922 /* Revalidate nfsi->cache_change_attribute before we declare a match */
923 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
924 return 0;
925 if (!nfs_verify_change_attribute(dir, dentry->d_time))
926 return 0;
927 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400931 * Return the intent data that applies to this particular path component
932 *
933 * Note that the current set of intents only apply to the very last
934 * component of the path.
935 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
936 */
937static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
938{
939 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
940 return 0;
941 return nd->flags & mask;
942}
943
944/*
Trond Myklebusta12802c2007-10-02 19:13:04 -0400945 * Use intent information to check whether or not we're going to do
946 * an O_EXCL create using this path component.
947 */
948static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
949{
950 if (NFS_PROTO(dir)->version == 2)
951 return 0;
Al Viro35165862008-08-05 03:00:49 -0400952 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -0400953}
954
955/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400956 * Inode and filehandle revalidation for lookups.
957 *
958 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
959 * or if the intent information indicates that we're about to open this
960 * particular file and the "nocto" mount flag is not set.
961 *
962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963static inline
964int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
965{
966 struct nfs_server *server = NFS_SERVER(inode);
967
Trond Myklebust4e99a1f2008-03-06 12:34:59 -0500968 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400972 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 goto out_force;
974 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400975 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400976 !(server->flags & NFS_MOUNT_NOCTO) &&
977 (S_ISREG(inode->i_mode) ||
978 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -0400980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 return nfs_revalidate_inode(server, inode);
983out_force:
984 return __nfs_revalidate_inode(server, inode);
985}
986
987/*
988 * We judge how long we want to trust negative
989 * dentries by looking at the parent inode mtime.
990 *
991 * If parent mtime has changed, we revalidate, else we wait for a
992 * period corresponding to the parent's attribute cache timeout value.
993 */
994static inline
995int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
996 struct nameidata *nd)
997{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400999 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001001 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1002 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return !nfs_check_verifier(dir, dentry);
1004}
1005
1006/*
1007 * This is called every time the dcache has a lookup hit,
1008 * and we should check whether we can really trust that
1009 * lookup.
1010 *
1011 * NOTE! The hit can be a negative hit too, don't assume
1012 * we have an inode!
1013 *
1014 * If the parent directory is seen to have changed, we throw out the
1015 * cached dentry and do a new lookup.
1016 */
1017static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
1018{
1019 struct inode *dir;
1020 struct inode *inode;
1021 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001022 struct nfs_fh *fhandle = NULL;
1023 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -05001028 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 inode = dentry->d_inode;
1030
1031 if (!inode) {
1032 if (nfs_neg_need_reval(dir, dentry, nd))
1033 goto out_bad;
1034 goto out_valid;
1035 }
1036
1037 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001038 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001039 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001040 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto out_bad;
1042 }
1043
Trond Myklebust15860ab2008-12-23 15:21:54 -05001044 if (nfs_have_delegation(inode, FMODE_READ))
1045 goto out_set_verifier;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -04001048 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (nfs_lookup_verify_inode(inode, nd))
1050 goto out_zap_parent;
1051 goto out_valid;
1052 }
1053
1054 if (NFS_STALE(inode))
1055 goto out_bad;
1056
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001057 error = -ENOMEM;
1058 fhandle = nfs_alloc_fhandle();
1059 fattr = nfs_alloc_fattr();
1060 if (fhandle == NULL || fattr == NULL)
1061 goto out_error;
1062
1063 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (error)
1065 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001066 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001068 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto out_bad;
1070
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001071 nfs_free_fattr(fattr);
1072 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001073out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -04001074 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001077 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001078 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001079 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 1;
1081out_zap_parent:
1082 nfs_zap_caches(dir);
1083 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -04001084 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (inode && S_ISDIR(inode->i_mode)) {
1086 /* Purge readdir caches. */
1087 nfs_zap_caches(inode);
1088 /* If we have submounts, don't unhash ! */
1089 if (have_submounts(dentry))
1090 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +01001091 if (dentry->d_flags & DCACHE_DISCONNECTED)
1092 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 shrink_dcache_parent(dentry);
1094 }
1095 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001096 nfs_free_fattr(fattr);
1097 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001099 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001100 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001101 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001103out_error:
1104 nfs_free_fattr(fattr);
1105 nfs_free_fhandle(fhandle);
1106 dput(parent);
1107 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1108 __func__, dentry->d_parent->d_name.name,
1109 dentry->d_name.name, error);
1110 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113/*
1114 * This is called from dput() when d_count is going to 0.
1115 */
1116static int nfs_dentry_delete(struct dentry *dentry)
1117{
1118 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1119 dentry->d_parent->d_name.name, dentry->d_name.name,
1120 dentry->d_flags);
1121
Trond Myklebust77f11192008-01-28 19:43:19 -05001122 /* Unhash any dentry with a stale inode */
1123 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1124 return 1;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1127 /* Unhash it, so that ->d_iput() would be called */
1128 return 1;
1129 }
1130 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1131 /* Unhash it, so that ancestors of killed async unlink
1132 * files will be cleaned up during umount */
1133 return 1;
1134 }
1135 return 0;
1136
1137}
1138
Trond Myklebust1b83d702008-06-11 15:44:04 -04001139static void nfs_drop_nlink(struct inode *inode)
1140{
1141 spin_lock(&inode->i_lock);
1142 if (inode->i_nlink > 0)
1143 drop_nlink(inode);
1144 spin_unlock(&inode->i_lock);
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147/*
1148 * Called when the dentry loses inode.
1149 * We use it to clean up silly-renamed files.
1150 */
1151static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1152{
Neil Brown83672d32007-02-26 12:48:25 +11001153 if (S_ISDIR(inode->i_mode))
1154 /* drop any readdir cache as it could easily be old */
1155 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001158 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001159 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 iput(inode);
1162}
1163
Al Virof786aa92009-02-20 05:51:22 +00001164const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 .d_revalidate = nfs_lookup_revalidate,
1166 .d_delete = nfs_dentry_delete,
1167 .d_iput = nfs_dentry_iput,
1168};
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1171{
1172 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -04001173 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001175 struct nfs_fh *fhandle = NULL;
1176 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1180 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -05001181 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 res = ERR_PTR(-ENAMETOOLONG);
1184 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1185 goto out;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1188
Trond Myklebustfd684072006-09-05 12:27:44 -04001189 /*
1190 * If we're doing an exclusive create, optimize away the lookup
1191 * but don't hash the dentry.
1192 */
1193 if (nfs_is_exclusive_create(dir, nd)) {
1194 d_instantiate(dentry, NULL);
1195 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -04001196 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -04001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001199 res = ERR_PTR(-ENOMEM);
1200 fhandle = nfs_alloc_fhandle();
1201 fattr = nfs_alloc_fattr();
1202 if (fhandle == NULL || fattr == NULL)
1203 goto out;
1204
Trond Myklebust565277f2007-10-15 18:17:53 -04001205 parent = dentry->d_parent;
1206 /* Protect against concurrent sillydeletes */
1207 nfs_block_sillyrename(parent);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001208 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (error == -ENOENT)
1210 goto no_entry;
1211 if (error < 0) {
1212 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -04001213 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001215 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001216 res = (struct dentry *)inode;
1217 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001218 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -04001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220no_entry:
David Howells54ceac42006-08-22 20:06:13 -04001221 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001222 if (res != NULL) {
1223 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001224 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001228out_unblock_sillyrename:
1229 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001231 nfs_free_fattr(fattr);
1232 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return res;
1234}
1235
1236#ifdef CONFIG_NFS_V4
1237static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1238
Al Virof786aa92009-02-20 05:51:22 +00001239const struct dentry_operations nfs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 .d_revalidate = nfs_open_revalidate,
1241 .d_delete = nfs_dentry_delete,
1242 .d_iput = nfs_dentry_iput,
1243};
1244
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001245/*
1246 * Use intent information to determine whether we need to substitute
1247 * the NFSv4-style stateful OPEN for the LOOKUP call
1248 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001249static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001251 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253 /* NFS does not (yet) have a stateful open for directories */
1254 if (nd->flags & LOOKUP_DIRECTORY)
1255 return 0;
1256 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001257 if (__mnt_is_readonly(nd->path.mnt) &&
1258 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260 return 1;
1261}
1262
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001263static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1264{
1265 struct path path = {
1266 .mnt = nd->path.mnt,
1267 .dentry = dentry,
1268 };
1269 struct nfs_open_context *ctx;
1270 struct rpc_cred *cred;
1271 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1272
1273 cred = rpc_lookup_cred();
1274 if (IS_ERR(cred))
1275 return ERR_CAST(cred);
1276 ctx = alloc_nfs_open_context(&path, cred, fmode);
1277 put_rpccred(cred);
1278 if (ctx == NULL)
1279 return ERR_PTR(-ENOMEM);
1280 return ctx;
1281}
1282
1283static int do_open(struct inode *inode, struct file *filp)
1284{
1285 nfs_fscache_set_inode_cookie(inode, filp);
1286 return 0;
1287}
1288
1289static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1290{
1291 struct file *filp;
1292 int ret = 0;
1293
1294 /* If the open_intent is for execute, we have an extra check to make */
1295 if (ctx->mode & FMODE_EXEC) {
1296 ret = nfs_may_open(ctx->path.dentry->d_inode,
1297 ctx->cred,
1298 nd->intent.open.flags);
1299 if (ret < 0)
1300 goto out;
1301 }
1302 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1303 if (IS_ERR(filp))
1304 ret = PTR_ERR(filp);
1305 else
1306 nfs_file_set_open_context(filp, ctx);
1307out:
1308 put_nfs_open_context(ctx);
1309 return ret;
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1313{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001314 struct nfs_open_context *ctx;
1315 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct dentry *res = NULL;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001317 struct inode *inode;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001318 int open_flags;
Trond Myklebust898f6352010-10-23 11:24:25 -04001319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001321 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1322 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /* Check that we are indeed trying to open this file */
Trond Myklebust5584c302008-12-23 15:21:54 -05001325 if (!is_atomic_open(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 goto no_open;
1327
1328 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1329 res = ERR_PTR(-ENAMETOOLONG);
1330 goto out;
1331 }
1332 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1333
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001334 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1335 * the dentry. */
Al Viro35165862008-08-05 03:00:49 -04001336 if (nd->flags & LOOKUP_EXCL) {
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001337 d_instantiate(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001338 goto out;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001341 ctx = nameidata_to_nfs_open_context(dentry, nd);
1342 res = ERR_CAST(ctx);
1343 if (IS_ERR(ctx))
1344 goto out;
1345
1346 open_flags = nd->intent.open.flags;
1347 if (nd->flags & LOOKUP_CREATE) {
1348 attr.ia_mode = nd->intent.open.create_mode;
1349 attr.ia_valid = ATTR_MODE;
1350 if (!IS_POSIXACL(dir))
1351 attr.ia_mode &= ~current_umask();
1352 } else {
Trond Myklebust898f6352010-10-23 11:24:25 -04001353 open_flags &= ~(O_EXCL | O_CREAT);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001354 attr.ia_valid = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001355 }
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* Open the file on the server */
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001358 nfs_block_sillyrename(dentry->d_parent);
Trond Myklebust2b484292010-09-17 10:56:51 -04001359 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001360 if (IS_ERR(inode)) {
1361 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001362 put_nfs_open_context(ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001363 switch (PTR_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* Make a negative dentry */
1365 case -ENOENT:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001366 d_add(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001367 res = NULL;
1368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* This turned out not to be a regular file */
Trond Myklebust6f926b52005-10-18 14:20:18 -07001370 case -ENOTDIR:
1371 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 case -ELOOP:
1373 if (!(nd->intent.open.flags & O_NOFOLLOW))
1374 goto no_open;
Trond Myklebust23ebbd92010-11-03 10:24:16 -04001375 /* case -EISDIR: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* case -EINVAL: */
1377 default:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001378 res = ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto out;
1380 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001381 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001382 res = d_add_unique(dentry, inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001383 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001384 if (res != NULL) {
1385 dput(ctx->path.dentry);
1386 ctx->path.dentry = dget(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 dentry = res;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001388 }
Trond Myklebust898f6352010-10-23 11:24:25 -04001389 err = nfs_intent_set_file(nd, ctx);
1390 if (err < 0) {
1391 if (res != NULL)
1392 dput(res);
1393 return ERR_PTR(err);
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395out:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001396 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return res;
1398no_open:
1399 return nfs_lookup(dir, dentry, nd);
1400}
1401
1402static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1403{
1404 struct dentry *parent = NULL;
1405 struct inode *inode = dentry->d_inode;
1406 struct inode *dir;
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001407 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int openflags, ret = 0;
1409
Trond Myklebust1f063d22010-04-22 15:35:55 -04001410 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001411 goto no_open;
Trond Myklebust2b484292010-09-17 10:56:51 -04001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 parent = dget_parent(dentry);
1414 dir = parent->d_inode;
Trond Myklebust2b484292010-09-17 10:56:51 -04001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /* We can't create new files in nfs_open_revalidate(), so we
1417 * optimize away revalidation of negative dentries.
1418 */
Trond Myklebust216d5d062007-10-01 20:10:12 -04001419 if (inode == NULL) {
1420 if (!nfs_neg_need_reval(dir, dentry, nd))
1421 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto out;
Trond Myklebust216d5d062007-10-01 20:10:12 -04001423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* NFS only supports OPEN on regular files */
1426 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001427 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 openflags = nd->intent.open.flags;
1429 /* We cannot do exclusive creation on a positive dentry */
1430 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001431 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* We can't create new files, or truncate existing ones here */
Trond Myklebust0a377cf2010-08-18 09:25:42 -04001433 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001435 ctx = nameidata_to_nfs_open_context(dentry, nd);
1436 ret = PTR_ERR(ctx);
1437 if (IS_ERR(ctx))
1438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001440 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * operations that change the directory. We therefore save the
1442 * change attribute *before* we do the RPC call.
1443 */
Trond Myklebust2b484292010-09-17 10:56:51 -04001444 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
Trond Myklebust535918f2010-09-17 10:56:51 -04001445 if (IS_ERR(inode)) {
1446 ret = PTR_ERR(inode);
1447 switch (ret) {
1448 case -EPERM:
1449 case -EACCES:
1450 case -EDQUOT:
1451 case -ENOSPC:
1452 case -EROFS:
1453 goto out_put_ctx;
1454 default:
1455 goto out_drop;
1456 }
1457 }
1458 iput(inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001459 if (inode != dentry->d_inode)
Trond Myklebust535918f2010-09-17 10:56:51 -04001460 goto out_drop;
Trond Myklebust898f6352010-10-23 11:24:25 -04001461
1462 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1463 ret = nfs_intent_set_file(nd, ctx);
1464 if (ret >= 0)
1465 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466out:
1467 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001469out_drop:
1470 d_drop(dentry);
1471 ret = 0;
1472out_put_ctx:
1473 put_nfs_open_context(ctx);
1474 goto out;
1475
Trond Myklebust5584c302008-12-23 15:21:54 -05001476no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001478no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return nfs_lookup_revalidate(dentry, nd);
1480}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001481
1482static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1483 struct nameidata *nd)
1484{
1485 struct nfs_open_context *ctx = NULL;
1486 struct iattr attr;
1487 int error;
1488 int open_flags = 0;
1489
1490 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1491 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1492
1493 attr.ia_mode = mode;
1494 attr.ia_valid = ATTR_MODE;
1495
1496 if ((nd->flags & LOOKUP_CREATE) != 0) {
1497 open_flags = nd->intent.open.flags;
1498
1499 ctx = nameidata_to_nfs_open_context(dentry, nd);
1500 error = PTR_ERR(ctx);
1501 if (IS_ERR(ctx))
Trond Myklebust898f6352010-10-23 11:24:25 -04001502 goto out_err_drop;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001503 }
1504
1505 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1506 if (error != 0)
1507 goto out_put_ctx;
Trond Myklebust898f6352010-10-23 11:24:25 -04001508 if (ctx != NULL) {
1509 error = nfs_intent_set_file(nd, ctx);
1510 if (error < 0)
1511 goto out_err;
1512 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001513 return 0;
1514out_put_ctx:
1515 if (ctx != NULL)
1516 put_nfs_open_context(ctx);
Trond Myklebust898f6352010-10-23 11:24:25 -04001517out_err_drop:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001518 d_drop(dentry);
Trond Myklebust898f6352010-10-23 11:24:25 -04001519out_err:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001520 return error;
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523#endif /* CONFIG_NFSV4 */
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*
1526 * Code common to create, mkdir, and mknod.
1527 */
1528int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1529 struct nfs_fattr *fattr)
1530{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001531 struct dentry *parent = dget_parent(dentry);
1532 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct inode *inode;
1534 int error = -EACCES;
1535
Trond Myklebustfab728e2007-09-29 17:41:33 -04001536 d_drop(dentry);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* We may have been initialized further down */
1539 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (fhandle->size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1543 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001544 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001546 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1548 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001549 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001551 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001554 error = PTR_ERR(inode);
1555 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001556 goto out_error;
1557 d_add(dentry, inode);
1558out:
1559 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001561out_error:
1562 nfs_mark_for_revalidate(dir);
1563 dput(parent);
1564 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
1568 * Following a failed create operation, we drop the dentry rather
1569 * than retain a negative dentry. This avoids a problem in the event
1570 * that the operation succeeded on the server, but an error in the
1571 * reply path made it appear to have failed.
1572 */
1573static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1574 struct nameidata *nd)
1575{
1576 struct iattr attr;
1577 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001579 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1580 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 attr.ia_mode = mode;
1583 attr.ia_valid = ATTR_MODE;
1584
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001585 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (error != 0)
1587 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return 0;
1589out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 d_drop(dentry);
1591 return error;
1592}
1593
1594/*
1595 * See comments for nfs_proc_create regarding failed operations.
1596 */
1597static int
1598nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1599{
1600 struct iattr attr;
1601 int status;
1602
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001603 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1604 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 if (!new_valid_dev(rdev))
1607 return -EINVAL;
1608
1609 attr.ia_mode = mode;
1610 attr.ia_valid = ATTR_MODE;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (status != 0)
1614 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 d_drop(dentry);
1618 return status;
1619}
1620
1621/*
1622 * See comments for nfs_proc_create regarding failed operations.
1623 */
1624static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1625{
1626 struct iattr attr;
1627 int error;
1628
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001629 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1630 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 attr.ia_valid = ATTR_MODE;
1633 attr.ia_mode = mode | S_IFDIR;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (error != 0)
1637 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 0;
1639out_err:
1640 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return error;
1642}
1643
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001644static void nfs_dentry_handle_enoent(struct dentry *dentry)
1645{
1646 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1647 d_delete(dentry);
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1651{
1652 int error;
1653
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001654 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1655 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1658 /* Ensure the VFS deletes this inode */
1659 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001660 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001661 else if (error == -ENOENT)
1662 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 return error;
1665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/*
1668 * Remove a file after making sure there are no pending writes,
1669 * and after checking that the file has only one user.
1670 *
1671 * We invalidate the attribute cache and free the inode prior to the operation
1672 * to avoid possible races if the server reuses the inode.
1673 */
1674static int nfs_safe_remove(struct dentry *dentry)
1675{
1676 struct inode *dir = dentry->d_parent->d_inode;
1677 struct inode *inode = dentry->d_inode;
1678 int error = -EBUSY;
1679
1680 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1681 dentry->d_parent->d_name.name, dentry->d_name.name);
1682
1683 /* If the dentry was sillyrenamed, we simply call d_delete() */
1684 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1685 error = 0;
1686 goto out;
1687 }
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001690 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1692 /* The VFS may want to delete this inode */
1693 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001694 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001695 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 } else
1697 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001698 if (error == -ENOENT)
1699 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700out:
1701 return error;
1702}
1703
1704/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1705 * belongs to an active ".nfs..." file and we return -EBUSY.
1706 *
1707 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1708 */
1709static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1710{
1711 int error;
1712 int need_rehash = 0;
1713
1714 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1715 dir->i_ino, dentry->d_name.name);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 spin_lock(&dcache_lock);
1718 spin_lock(&dentry->d_lock);
1719 if (atomic_read(&dentry->d_count) > 1) {
1720 spin_unlock(&dentry->d_lock);
1721 spin_unlock(&dcache_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001722 /* Start asynchronous writeout of the inode */
1723 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return error;
1726 }
1727 if (!d_unhashed(dentry)) {
1728 __d_drop(dentry);
1729 need_rehash = 1;
1730 }
1731 spin_unlock(&dentry->d_lock);
1732 spin_unlock(&dcache_lock);
1733 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001734 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1736 } else if (need_rehash)
1737 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return error;
1739}
1740
Chuck Lever873101b2006-08-22 20:06:23 -04001741/*
1742 * To create a symbolic link, most file systems instantiate a new inode,
1743 * add a page to it containing the path, then write it out to the disk
1744 * using prepare_write/commit_write.
1745 *
1746 * Unfortunately the NFS client can't create the in-core inode first
1747 * because it needs a file handle to create an in-core inode (see
1748 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1749 * symlink request has completed on the server.
1750 *
1751 * So instead we allocate a raw page, copy the symname into it, then do
1752 * the SYMLINK request with the page as the buffer. If it succeeds, we
1753 * now have a new file handle and can instantiate an in-core NFS inode
1754 * and move the raw page into its mapping.
1755 */
1756static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Chuck Lever873101b2006-08-22 20:06:23 -04001758 struct pagevec lru_pvec;
1759 struct page *page;
1760 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001762 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int error;
1764
1765 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1766 dir->i_ino, dentry->d_name.name, symname);
1767
Chuck Lever873101b2006-08-22 20:06:23 -04001768 if (pathlen > PAGE_SIZE)
1769 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Chuck Lever873101b2006-08-22 20:06:23 -04001771 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1772 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Jeff Layton83d93f22007-06-07 09:58:08 -04001774 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001775 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001776 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001777
1778 kaddr = kmap_atomic(page, KM_USER0);
1779 memcpy(kaddr, symname, pathlen);
1780 if (pathlen < PAGE_SIZE)
1781 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1782 kunmap_atomic(kaddr, KM_USER0);
1783
Chuck Lever94a6d752006-08-22 20:06:23 -04001784 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001785 if (error != 0) {
1786 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1787 dir->i_sb->s_id, dir->i_ino,
1788 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001790 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001791 return error;
1792 }
1793
1794 /*
1795 * No big deal if we can't add this page to the page cache here.
1796 * READLINK will get the missing page from the server if needed.
1797 */
1798 pagevec_init(&lru_pvec, 0);
1799 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1800 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001801 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001802 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001803 SetPageUptodate(page);
1804 unlock_page(page);
1805 } else
1806 __free_page(page);
1807
Chuck Lever873101b2006-08-22 20:06:23 -04001808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811static int
1812nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1813{
1814 struct inode *inode = old_dentry->d_inode;
1815 int error;
1816
1817 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1818 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1819 dentry->d_parent->d_name.name, dentry->d_name.name);
1820
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001821 nfs_inode_return_delegation(inode);
1822
Trond Myklebust9697d232007-10-02 21:58:05 -04001823 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001825 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04001826 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04001827 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return error;
1830}
1831
1832/*
1833 * RENAME
1834 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1835 * different file handle for the same inode after a rename (e.g. when
1836 * moving to a different directory). A fail-safe method to do so would
1837 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1838 * rename the old file using the sillyrename stuff. This way, the original
1839 * file in old_dir will go away when the last process iput()s the inode.
1840 *
1841 * FIXED.
1842 *
1843 * It actually works quite well. One needs to have the possibility for
1844 * at least one ".nfs..." file in each directory the file ever gets
1845 * moved or linked to which happens automagically with the new
1846 * implementation that only depends on the dcache stuff instead of
1847 * using the inode layer
1848 *
1849 * Unfortunately, things are a little more complicated than indicated
1850 * above. For a cross-directory move, we want to make sure we can get
1851 * rid of the old inode after the operation. This means there must be
1852 * no pending writes (if it's a file), and the use count must be 1.
1853 * If these conditions are met, we can drop the dentries before doing
1854 * the rename.
1855 */
1856static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1857 struct inode *new_dir, struct dentry *new_dentry)
1858{
1859 struct inode *old_inode = old_dentry->d_inode;
1860 struct inode *new_inode = new_dentry->d_inode;
1861 struct dentry *dentry = NULL, *rehash = NULL;
1862 int error = -EBUSY;
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1865 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1866 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1867 atomic_read(&new_dentry->d_count));
1868
1869 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001870 * For non-directories, check whether the target is busy and if so,
1871 * make a copy of the dentry and then do a silly-rename. If the
1872 * silly-rename succeeds, the copied dentry is hashed and becomes
1873 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001875 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1876 /*
1877 * To prevent any new references to the target during the
1878 * rename, we unhash the dentry in advance.
1879 */
1880 if (!d_unhashed(new_dentry)) {
1881 d_drop(new_dentry);
1882 rehash = new_dentry;
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Miklos Szeredi27226102009-12-03 15:58:56 -05001885 if (atomic_read(&new_dentry->d_count) > 2) {
1886 int err;
1887
1888 /* copy the target dentry's name */
1889 dentry = d_alloc(new_dentry->d_parent,
1890 &new_dentry->d_name);
1891 if (!dentry)
1892 goto out;
1893
1894 /* silly-rename the existing target ... */
1895 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001896 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001897 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001898
1899 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001900 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001901 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05001902 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Trond Myklebustcae7a072005-10-18 14:20:19 -07001905 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001906 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01001907 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1910 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001911 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912out:
1913 if (rehash)
1914 d_rehash(rehash);
1915 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001916 if (new_inode != NULL)
1917 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07001918 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04001919 nfs_set_verifier(new_dentry,
1920 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001921 } else if (error == -ENOENT)
1922 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /* new dentry created? */
1925 if (dentry)
1926 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return error;
1928}
1929
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001930static DEFINE_SPINLOCK(nfs_access_lru_lock);
1931static LIST_HEAD(nfs_access_lru_list);
1932static atomic_long_t nfs_access_nr_entries;
1933
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001934static void nfs_access_free_entry(struct nfs_access_entry *entry)
1935{
1936 put_rpccred(entry->cred);
1937 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001938 smp_mb__before_atomic_dec();
1939 atomic_long_dec(&nfs_access_nr_entries);
1940 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001941}
1942
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001943static void nfs_access_free_list(struct list_head *head)
1944{
1945 struct nfs_access_entry *cache;
1946
1947 while (!list_empty(head)) {
1948 cache = list_entry(head->next, struct nfs_access_entry, lru);
1949 list_del(&cache->lru);
1950 nfs_access_free_entry(cache);
1951 }
1952}
1953
Dave Chinner7f8275d2010-07-19 14:56:17 +10001954int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebust979df722006-07-25 11:28:19 -04001955{
1956 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001957 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04001958 struct nfs_access_entry *cache;
1959
Trond Myklebust61d5eb22010-05-13 12:51:06 -04001960 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1961 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001962
Trond Myklebusta50f7952007-06-05 19:23:43 -04001963 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001964 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04001965 struct inode *inode;
1966
1967 if (nr_to_scan-- == 0)
1968 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001969 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04001970 spin_lock(&inode->i_lock);
1971 if (list_empty(&nfsi->access_cache_entry_lru))
1972 goto remove_lru_entry;
1973 cache = list_entry(nfsi->access_cache_entry_lru.next,
1974 struct nfs_access_entry, lru);
1975 list_move(&cache->lru, &head);
1976 rb_erase(&cache->rb_node, &nfsi->access_cache);
1977 if (!list_empty(&nfsi->access_cache_entry_lru))
1978 list_move_tail(&nfsi->access_cache_inode_lru,
1979 &nfs_access_lru_list);
1980 else {
1981remove_lru_entry:
1982 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001983 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001984 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001985 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001986 }
Trond Myklebust59844a92010-05-26 08:42:24 -04001987 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04001988 }
1989 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001990 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04001991 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1992}
1993
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001994static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001995{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001996 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001997 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001998 struct nfs_access_entry *entry;
1999
2000 /* Unhook entries from the cache */
2001 while ((n = rb_first(root_node)) != NULL) {
2002 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2003 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002004 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002005 }
2006 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002007}
2008
2009void nfs_access_zap_cache(struct inode *inode)
2010{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002011 LIST_HEAD(head);
2012
2013 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2014 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002015 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002016 spin_lock(&nfs_access_lru_lock);
2017 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002018 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002019
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002020 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002021 __nfs_access_zap_cache(NFS_I(inode), &head);
2022 spin_unlock(&inode->i_lock);
2023 spin_unlock(&nfs_access_lru_lock);
2024 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002025}
2026
2027static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2028{
2029 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2030 struct nfs_access_entry *entry;
2031
2032 while (n != NULL) {
2033 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2034
2035 if (cred < entry->cred)
2036 n = n->rb_left;
2037 else if (cred > entry->cred)
2038 n = n->rb_right;
2039 else
2040 return entry;
2041 }
2042 return NULL;
2043}
2044
Trond Myklebustaf22f942007-08-10 17:45:10 -04002045static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Chuck Lever55296802005-08-18 11:24:09 -07002047 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002048 struct nfs_access_entry *cache;
2049 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002051 spin_lock(&inode->i_lock);
2052 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2053 goto out_zap;
2054 cache = nfs_access_search_rbtree(inode, cred);
2055 if (cache == NULL)
2056 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05002057 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05002058 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002059 goto out_stale;
2060 res->jiffies = cache->jiffies;
2061 res->cred = cache->cred;
2062 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002063 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002064 err = 0;
2065out:
2066 spin_unlock(&inode->i_lock);
2067 return err;
2068out_stale:
2069 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002070 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002071 spin_unlock(&inode->i_lock);
2072 nfs_access_free_entry(cache);
2073 return -ENOENT;
2074out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002075 spin_unlock(&inode->i_lock);
2076 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002077 return -ENOENT;
2078}
2079
2080static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2081{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002082 struct nfs_inode *nfsi = NFS_I(inode);
2083 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002084 struct rb_node **p = &root_node->rb_node;
2085 struct rb_node *parent = NULL;
2086 struct nfs_access_entry *entry;
2087
2088 spin_lock(&inode->i_lock);
2089 while (*p != NULL) {
2090 parent = *p;
2091 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2092
2093 if (set->cred < entry->cred)
2094 p = &parent->rb_left;
2095 else if (set->cred > entry->cred)
2096 p = &parent->rb_right;
2097 else
2098 goto found;
2099 }
2100 rb_link_node(&set->rb_node, parent, p);
2101 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002102 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002103 spin_unlock(&inode->i_lock);
2104 return;
2105found:
2106 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002107 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2108 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002109 spin_unlock(&inode->i_lock);
2110 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Trond Myklebustaf22f942007-08-10 17:45:10 -04002113static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002115 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2116 if (cache == NULL)
2117 return;
2118 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002120 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002122
2123 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002124
2125 /* Update accounting */
2126 smp_mb__before_atomic_inc();
2127 atomic_long_inc(&nfs_access_nr_entries);
2128 smp_mb__after_atomic_inc();
2129
2130 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002131 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002132 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002133 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2134 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2135 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002136 spin_unlock(&nfs_access_lru_lock);
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2141{
2142 struct nfs_access_entry cache;
2143 int status;
2144
2145 status = nfs_access_get_cached(inode, cred, &cache);
2146 if (status == 0)
2147 goto out;
2148
2149 /* Be clever: ask server to check for all possible rights */
2150 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2151 cache.cred = cred;
2152 cache.jiffies = jiffies;
2153 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002154 if (status != 0) {
2155 if (status == -ESTALE) {
2156 nfs_zap_caches(inode);
2157 if (!S_ISDIR(inode->i_mode))
2158 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 nfs_access_add_cache(inode, &cache);
2163out:
Al Viroe6305c42008-07-15 21:03:57 -04002164 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return 0;
2166 return -EACCES;
2167}
2168
Trond Myklebustaf22f942007-08-10 17:45:10 -04002169static int nfs_open_permission_mask(int openflags)
2170{
2171 int mask = 0;
2172
2173 if (openflags & FMODE_READ)
2174 mask |= MAY_READ;
2175 if (openflags & FMODE_WRITE)
2176 mask |= MAY_WRITE;
2177 if (openflags & FMODE_EXEC)
2178 mask |= MAY_EXEC;
2179 return mask;
2180}
2181
2182int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2183{
2184 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2185}
2186
Al Viroe6305c42008-07-15 21:03:57 -04002187int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 struct rpc_cred *cred;
2190 int res = 0;
2191
Chuck Lever91d5b472006-03-20 13:44:14 -05002192 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2193
Al Viroe6305c42008-07-15 21:03:57 -04002194 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto out;
2196 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002197 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto force_lookup;
2199
2200 switch (inode->i_mode & S_IFMT) {
2201 case S_IFLNK:
2202 goto out;
2203 case S_IFREG:
2204 /* NFSv4 has atomic_open... */
2205 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002206 && (mask & MAY_OPEN)
2207 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto out;
2209 break;
2210 case S_IFDIR:
2211 /*
2212 * Optimize away all write operations, since the server
2213 * will check permissions when we perform the op.
2214 */
2215 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2216 goto out;
2217 }
2218
2219force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (!NFS_PROTO(inode)->access)
2221 goto out_notsup;
2222
Trond Myklebust98a8e322008-03-12 12:25:28 -04002223 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (!IS_ERR(cred)) {
2225 res = nfs_do_access(inode, cred, mask);
2226 put_rpccred(cred);
2227 } else
2228 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002230 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2231 res = -EACCES;
2232
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002233 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2234 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return res;
2236out_notsup:
2237 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2238 if (res == 0)
2239 res = generic_permission(inode, mask, NULL);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243/*
2244 * Local variables:
2245 * version-control: t
2246 * kept-new-versions: 5
2247 * End:
2248 */