blob: e03537ff9350a6daae9d5233303775569577360c [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>
Catalin Marinas04e4bd12010-11-11 12:53:47 +000037#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050040#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080041#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040042#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* #define NFS_DEBUG_VERBOSE 1 */
45
46static int nfs_opendir(struct inode *, struct file *);
47static int nfs_readdir(struct file *, void *, filldir_t);
48static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
49static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
50static int nfs_mkdir(struct inode *, struct dentry *, int);
51static int nfs_rmdir(struct inode *, struct dentry *);
52static int nfs_unlink(struct inode *, struct dentry *);
53static int nfs_symlink(struct inode *, struct dentry *, const char *);
54static int nfs_link(struct dentry *, struct inode *, struct dentry *);
55static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
56static int nfs_rename(struct inode *, struct dentry *,
57 struct inode *, struct dentry *);
Christoph Hellwig7ea80852010-05-26 17:53:25 +020058static int nfs_fsync_dir(struct file *, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000059static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040060static int nfs_readdir_clear_array(struct page*, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080062const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000063 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .read = generic_read_dir,
65 .readdir = nfs_readdir,
66 .open = nfs_opendir,
67 .release = nfs_release,
68 .fsync = nfs_fsync_dir,
69};
70
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080071const struct inode_operations nfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .create = nfs_create,
73 .lookup = nfs_lookup,
74 .link = nfs_link,
75 .unlink = nfs_unlink,
76 .symlink = nfs_symlink,
77 .mkdir = nfs_mkdir,
78 .rmdir = nfs_rmdir,
79 .mknod = nfs_mknod,
80 .rename = nfs_rename,
81 .permission = nfs_permission,
82 .getattr = nfs_getattr,
83 .setattr = nfs_setattr,
84};
85
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040086const struct address_space_operations nfs_dir_addr_space_ops = {
87 .releasepage = nfs_readdir_clear_array,
88};
89
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000090#ifdef CONFIG_NFS_V3
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080091const struct inode_operations nfs3_dir_inode_operations = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000092 .create = nfs_create,
93 .lookup = nfs_lookup,
94 .link = nfs_link,
95 .unlink = nfs_unlink,
96 .symlink = nfs_symlink,
97 .mkdir = nfs_mkdir,
98 .rmdir = nfs_rmdir,
99 .mknod = nfs_mknod,
100 .rename = nfs_rename,
101 .permission = nfs_permission,
102 .getattr = nfs_getattr,
103 .setattr = nfs_setattr,
104 .listxattr = nfs3_listxattr,
105 .getxattr = nfs3_getxattr,
106 .setxattr = nfs3_setxattr,
107 .removexattr = nfs3_removexattr,
108};
109#endif /* CONFIG_NFS_V3 */
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#ifdef CONFIG_NFS_V4
112
113static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400114static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800115const struct inode_operations nfs4_dir_inode_operations = {
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400116 .create = nfs_open_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .lookup = nfs_atomic_lookup,
118 .link = nfs_link,
119 .unlink = nfs_unlink,
120 .symlink = nfs_symlink,
121 .mkdir = nfs_mkdir,
122 .rmdir = nfs_rmdir,
123 .mknod = nfs_mknod,
124 .rename = nfs_rename,
125 .permission = nfs_permission,
126 .getattr = nfs_getattr,
127 .setattr = nfs_setattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +0000128 .getxattr = nfs4_getxattr,
129 .setxattr = nfs4_setxattr,
130 .listxattr = nfs4_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133#endif /* CONFIG_NFS_V4 */
134
135/*
136 * Open file
137 */
138static int
139nfs_opendir(struct inode *inode, struct file *filp)
140{
Carsten Otte7451c4f2006-04-19 13:06:37 -0400141 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Chuck Lever6da24bc2008-06-11 17:55:58 -0400143 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400144 filp->f_path.dentry->d_parent->d_name.name,
145 filp->f_path.dentry->d_name.name);
146
147 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* Call generic open code in order to cache credentials */
Carsten Otte7451c4f2006-04-19 13:06:37 -0400150 res = nfs_open(inode, filp);
Neil Brownf5a73672010-08-10 10:20:05 -0400151 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
152 /* This is a mountpoint, so d_revalidate will never
153 * have been called, so we need to refresh the
154 * inode (for close-open consistency) ourselves.
155 */
156 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return res;
159}
160
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400161struct nfs_cache_array_entry {
162 u64 cookie;
163 u64 ino;
164 struct qstr string;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500165 unsigned char d_type;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400166};
167
168struct nfs_cache_array {
169 unsigned int size;
170 int eof_index;
171 u64 last_cookie;
172 struct nfs_cache_array_entry array[0];
173};
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;
Trond Myklebust0aded702010-11-30 21:56:32 -0500181 u64 last_cookie;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000182 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 decode_dirent_t decode;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400184
Neil Brown1f4eab72007-04-16 09:35:27 +1000185 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400186 unsigned long gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400187 unsigned int cache_entry_index;
188 unsigned int plus:1;
189 unsigned int eof:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190} nfs_readdir_descriptor_t;
191
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400192/*
193 * The caller is responsible for calling nfs_readdir_release_array(page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195static
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400196struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500198 void *ptr;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400199 if (page == NULL)
200 return ERR_PTR(-EIO);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500201 ptr = kmap(page);
202 if (ptr == NULL)
203 return ERR_PTR(-ENOMEM);
204 return ptr;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400205}
206
207static
208void nfs_readdir_release_array(struct page *page)
209{
210 kunmap(page);
211}
212
213/*
214 * we are freeing strings created by nfs_add_to_readdir_array()
215 */
216static
217int nfs_readdir_clear_array(struct page *page, gfp_t mask)
218{
219 struct nfs_cache_array *array = nfs_readdir_get_array(page);
220 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500221
222 if (IS_ERR(array))
223 return PTR_ERR(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400224 for (i = 0; i < array->size; i++)
225 kfree(array->array[i].string.name);
226 nfs_readdir_release_array(page);
227 return 0;
228}
229
230/*
231 * the caller is responsible for freeing qstr.name
232 * when called by nfs_readdir_add_to_array, the strings will be freed in
233 * nfs_clear_readdir_array()
234 */
235static
Trond Myklebust4a201d62010-10-23 14:53:23 -0400236int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400237{
238 string->len = len;
239 string->name = kmemdup(name, len, GFP_KERNEL);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400240 if (string->name == NULL)
241 return -ENOMEM;
Catalin Marinas04e4bd12010-11-11 12:53:47 +0000242 /*
243 * Avoid a kmemleak false positive. The pointer to the name is stored
244 * in a page cache page which kmemleak does not scan.
245 */
246 kmemleak_not_leak(string->name);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400247 string->hash = full_name_hash(name, len);
248 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400249}
250
251static
252int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
253{
254 struct nfs_cache_array *array = nfs_readdir_get_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400255 struct nfs_cache_array_entry *cache_entry;
256 int ret;
257
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400258 if (IS_ERR(array))
259 return PTR_ERR(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400260
Trond Myklebust4a201d62010-10-23 14:53:23 -0400261 cache_entry = &array->array[array->size];
Trond Myklebust30200932010-11-20 15:18:22 -0500262
263 /* Check that this entry lies within the page bounds */
264 ret = -ENOSPC;
265 if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
266 goto out;
267
Trond Myklebust4a201d62010-10-23 14:53:23 -0400268 cache_entry->cookie = entry->prev_cookie;
269 cache_entry->ino = entry->ino;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500270 cache_entry->d_type = entry->d_type;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400271 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
272 if (ret)
273 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400274 array->last_cookie = entry->cookie;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500275 array->size++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400276 if (entry->eof == 1)
277 array->eof_index = array->size;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400278out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400279 nfs_readdir_release_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400280 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400281}
282
283static
284int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
285{
286 loff_t diff = desc->file->f_pos - desc->current_index;
287 unsigned int index;
288
289 if (diff < 0)
290 goto out_eof;
291 if (diff >= array->size) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500292 if (array->eof_index >= 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400293 goto out_eof;
294 desc->current_index += array->size;
295 return -EAGAIN;
296 }
297
298 index = (unsigned int)diff;
299 *desc->dir_cookie = array->array[index].cookie;
300 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400301 return 0;
302out_eof:
303 desc->eof = 1;
304 return -EBADCOOKIE;
305}
306
307static
308int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
309{
310 int i;
311 int status = -EAGAIN;
312
313 for (i = 0; i < array->size; i++) {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400314 if (array->array[i].cookie == *desc->dir_cookie) {
315 desc->cache_entry_index = i;
316 status = 0;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500317 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400318 }
319 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500320 if (i == array->eof_index) {
321 desc->eof = 1;
322 status = -EBADCOOKIE;
323 }
324out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400325 return status;
326}
327
328static
329int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
330{
331 struct nfs_cache_array *array;
332 int status = -EBADCOOKIE;
333
334 if (desc->dir_cookie == NULL)
335 goto out;
336
337 array = nfs_readdir_get_array(desc->page);
338 if (IS_ERR(array)) {
339 status = PTR_ERR(array);
340 goto out;
341 }
342
343 if (*desc->dir_cookie == 0)
344 status = nfs_readdir_search_for_pos(array, desc);
345 else
346 status = nfs_readdir_search_for_cookie(array, desc);
347
Trond Myklebust0aded702010-11-30 21:56:32 -0500348 if (status == -EAGAIN)
349 desc->last_cookie = array->last_cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400350 nfs_readdir_release_array(desc->page);
351out:
352 return status;
353}
354
355/* Fill a page with xdr information before transferring to the cache page */
356static
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400357int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400358 struct nfs_entry *entry, struct file *file, struct inode *inode)
359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct rpc_cred *cred = nfs_file_cred(file);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400361 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int error;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 again:
365 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400366 gencount = nfs_inc_attr_generation_counter();
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400367 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 NFS_SERVER(inode)->dtsize, desc->plus);
369 if (error < 0) {
370 /* We requested READDIRPLUS, but the server doesn't grok it */
371 if (error == -ENOTSUPP && desc->plus) {
372 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200373 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 desc->plus = 0;
375 goto again;
376 }
377 goto error;
378 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000379 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400380 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400381error:
382 return error;
383}
384
385/* Fill in an entry based on the xdr code stored in desc->page */
386static
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400387int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400388{
Bryan Schumaker82f2e542010-10-21 16:33:18 -0400389 __be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400390 if (IS_ERR(p))
391 return PTR_ERR(p);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400392
393 entry->fattr->time_start = desc->timestamp;
394 entry->fattr->gencount = desc->gencount;
395 return 0;
396}
397
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400398static
399int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
400{
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400401 if (dentry->d_inode == NULL)
402 goto different;
Trond Myklebust37a09f02010-11-30 12:42:34 -0500403 if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400404 goto different;
405 return 1;
406different:
407 return 0;
408}
409
410static
411void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
412{
Trond Myklebust4a201d62010-10-23 14:53:23 -0400413 struct qstr filename = {
414 .len = entry->len,
415 .name = entry->name,
416 };
417 struct dentry *dentry;
418 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400419 struct inode *dir = parent->d_inode;
420 struct inode *inode;
421
Trond Myklebust4a201d62010-10-23 14:53:23 -0400422 if (filename.name[0] == '.') {
423 if (filename.len == 1)
424 return;
425 if (filename.len == 2 && filename.name[1] == '.')
426 return;
427 }
428 filename.hash = full_name_hash(filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400429
Trond Myklebust4a201d62010-10-23 14:53:23 -0400430 dentry = d_lookup(parent, &filename);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400431 if (dentry != NULL) {
432 if (nfs_same_file(dentry, entry)) {
433 nfs_refresh_inode(dentry->d_inode, entry->fattr);
434 goto out;
435 } else {
436 d_drop(dentry);
437 dput(dentry);
438 }
439 }
440
441 dentry = d_alloc(parent, &filename);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400442 if (dentry == NULL)
443 return;
444
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400445 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
446 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
447 if (IS_ERR(inode))
448 goto out;
449
450 alias = d_materialise_unique(dentry, inode);
451 if (IS_ERR(alias))
452 goto out;
453 else if (alias) {
454 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
455 dput(alias);
456 } else
457 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
458
459out:
460 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400461}
462
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400463/* Perform conversion from xdr to cache array */
464static
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500465int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400466 void *xdr_page, struct page *page, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400467{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400468 struct xdr_stream stream;
469 struct xdr_buf buf;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400470 __be32 *ptr = xdr_page;
Bryan Schumaker99424382010-10-21 16:33:16 -0400471 struct nfs_cache_array *array;
Trond Myklebust5c346852010-11-20 12:43:45 -0500472 unsigned int count = 0;
473 int status;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400474
475 buf.head->iov_base = xdr_page;
476 buf.head->iov_len = buflen;
477 buf.tail->iov_len = 0;
478 buf.page_base = 0;
479 buf.page_len = 0;
480 buf.buflen = buf.head->iov_len;
481 buf.len = buf.head->iov_len;
482
483 xdr_init_decode(&stream, &buf, ptr);
484
Bryan Schumaker99424382010-10-21 16:33:16 -0400485
486 do {
487 status = xdr_decode(desc, entry, &stream);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500488 if (status != 0) {
489 if (status == -EAGAIN)
490 status = 0;
Bryan Schumaker99424382010-10-21 16:33:16 -0400491 break;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500492 }
Bryan Schumaker99424382010-10-21 16:33:16 -0400493
Trond Myklebust5c346852010-11-20 12:43:45 -0500494 count++;
495
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400496 if (desc->plus == 1)
497 nfs_prime_dcache(desc->file->f_path.dentry, entry);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500498
499 status = nfs_readdir_add_to_array(entry, page);
500 if (status != 0)
501 break;
Bryan Schumaker99424382010-10-21 16:33:16 -0400502 } while (!entry->eof);
503
Trond Myklebust5c346852010-11-20 12:43:45 -0500504 if (count == 0 || (status == -EBADCOOKIE && entry->eof == 1)) {
Bryan Schumaker99424382010-10-21 16:33:16 -0400505 array = nfs_readdir_get_array(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500506 if (!IS_ERR(array)) {
507 array->eof_index = array->size;
508 status = 0;
509 nfs_readdir_release_array(page);
Trond Myklebust5c346852010-11-20 12:43:45 -0500510 } else
511 status = PTR_ERR(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400512 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500513 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400514}
515
516static
517void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
518{
519 unsigned int i;
520 for (i = 0; i < npages; i++)
521 put_page(pages[i]);
522}
523
524static
525void nfs_readdir_free_large_page(void *ptr, struct page **pages,
526 unsigned int npages)
527{
528 vm_unmap_ram(ptr, npages);
529 nfs_readdir_free_pagearray(pages, npages);
530}
531
532/*
533 * nfs_readdir_large_page will allocate pages that must be freed with a call
534 * to nfs_readdir_free_large_page
535 */
536static
537void *nfs_readdir_large_page(struct page **pages, unsigned int npages)
538{
539 void *ptr;
540 unsigned int i;
541
542 for (i = 0; i < npages; i++) {
543 struct page *page = alloc_page(GFP_KERNEL);
544 if (page == NULL)
545 goto out_freepages;
546 pages[i] = page;
547 }
548
Trond Myklebust4a201d62010-10-23 14:53:23 -0400549 ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400550 if (!IS_ERR_OR_NULL(ptr))
551 return ptr;
552out_freepages:
553 nfs_readdir_free_pagearray(pages, i);
554 return NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400555}
556
557static
558int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
559{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400560 struct page *pages[NFS_MAX_READDIR_PAGES];
561 void *pages_ptr = NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400562 struct nfs_entry entry;
563 struct file *file = desc->file;
564 struct nfs_cache_array *array;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500565 int status = -ENOMEM;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400566 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400567
568 entry.prev_cookie = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500569 entry.cookie = desc->last_cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400570 entry.eof = 0;
571 entry.fh = nfs_alloc_fhandle();
572 entry.fattr = nfs_alloc_fattr();
573 if (entry.fh == NULL || entry.fattr == NULL)
574 goto out;
575
576 array = nfs_readdir_get_array(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500577 if (IS_ERR(array)) {
578 status = PTR_ERR(array);
579 goto out;
580 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400581 memset(array, 0, sizeof(struct nfs_cache_array));
582 array->eof_index = -1;
583
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400584 pages_ptr = nfs_readdir_large_page(pages, array_size);
585 if (!pages_ptr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400586 goto out_release_array;
587 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500588 unsigned int pglen;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400589 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400590
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400591 if (status < 0)
592 break;
Trond Myklebustac396122010-11-15 20:26:22 -0500593 pglen = status;
594 status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, pglen);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500595 if (status < 0) {
596 if (status == -ENOSPC)
597 status = 0;
598 break;
599 }
600 } while (array->eof_index < 0);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400601
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400602 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400603out_release_array:
604 nfs_readdir_release_array(page);
605out:
606 nfs_free_fattr(entry.fattr);
607 nfs_free_fhandle(entry.fh);
608 return status;
609}
610
611/*
612 * Now we cache directories properly, by converting xdr information
613 * to an array that can be used for lookups later. This results in
614 * fewer cache pages, since we can store more information on each page.
615 * We only need to convert from xdr once so future lookups are much simpler
616 */
617static
618int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
619{
620 struct inode *inode = desc->file->f_path.dentry->d_inode;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500621 int ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400622
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500623 ret = nfs_readdir_xdr_to_array(desc, page, inode);
624 if (ret < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400625 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400627
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400628 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700629 /* Should never happen */
630 nfs_zap_mapping(inode, inode->i_mapping);
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unlock_page(page);
633 return 0;
634 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 unlock_page(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400639static
640void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 page_cache_release(desc->page);
643 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400646static
647struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500649 return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400650 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400654 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Olivier Galibert00a92642005-06-22 17:16:29 +0000655 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400656static
657int find_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000658{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400659 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000660
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400661 desc->page = get_cache_page(desc);
662 if (IS_ERR(desc->page))
663 return PTR_ERR(desc->page);
Olivier Galibert00a92642005-06-22 17:16:29 +0000664
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400665 res = nfs_readdir_search_array(desc);
666 if (res == 0)
667 return 0;
668 cache_page_release(desc);
669 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000670}
671
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400672/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static inline
674int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
675{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500676 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000677
Trond Myklebust0aded702010-11-30 21:56:32 -0500678 if (desc->page_index == 0) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500679 desc->current_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500680 desc->last_cookie = 0;
681 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400682 while (1) {
683 res = find_cache_page(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (res != -EAGAIN)
685 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400686 desc->page_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return res;
689}
690
691static inline unsigned int dt_type(struct inode *inode)
692{
693 return (inode->i_mode >> 12) & 15;
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/*
697 * Once we've found the start of the dirent within a page: fill 'er up...
698 */
699static
700int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
701 filldir_t filldir)
702{
703 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400704 int i = 0;
705 int res = 0;
706 struct nfs_cache_array *array = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400708 array = nfs_readdir_get_array(desc->page);
Trond Myklebuste7c58e92010-11-20 13:22:24 -0500709 if (IS_ERR(array)) {
710 res = PTR_ERR(array);
711 goto out;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400714 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500715 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Trond Myklebustece0b422010-11-20 13:55:33 -0500717 ent = &array->array[i];
718 if (filldir(dirent, ent->string.name, ent->string.len,
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500719 file->f_pos, nfs_compat_user_ino64(ent->ino),
720 ent->d_type) < 0) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500721 desc->eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500723 }
Olivier Galibert00a92642005-06-22 17:16:29 +0000724 file->f_pos++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400725 desc->cache_entry_index = i;
726 if (i < (array->size-1))
727 *desc->dir_cookie = array->array[i+1].cookie;
728 else
729 *desc->dir_cookie = array->last_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500731 if (i == array->eof_index)
732 desc->eof = 1;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400733
734 nfs_readdir_release_array(desc->page);
Trond Myklebuste7c58e92010-11-20 13:22:24 -0500735out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400736 cache_page_release(desc);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500737 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
738 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return res;
740}
741
742/*
743 * If we cannot find a cookie in our cache, we suspect that this is
744 * because it points to a deleted file, so we ask the server to return
745 * whatever it thinks is the next entry. We then feed this to filldir.
746 * If all goes well, we should then be able to find our way round the
747 * cache on the next call to readdir_search_pagecache();
748 *
749 * NOTE: we cannot add the anonymous page to the pagecache because
750 * the data it contains might not be page aligned. Besides,
751 * we should already have a complete representation of the
752 * directory in the page cache by the time we get here.
753 */
754static inline
755int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
756 filldir_t filldir)
757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct page *page = NULL;
759 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400760 struct inode *inode = desc->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500762 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
763 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 page = alloc_page(GFP_HIGHUSER);
766 if (!page) {
767 status = -ENOMEM;
768 goto out;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500771 desc->page_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500772 desc->last_cookie = *desc->dir_cookie;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500773 desc->page = page;
774
Trond Myklebust85f86072010-11-20 13:24:49 -0500775 status = nfs_readdir_xdr_to_array(desc, page, inode);
776 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400777 goto out_release;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 status = nfs_do_filldir(desc, dirent, filldir);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500782 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700783 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return status;
785 out_release:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400786 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto out;
788}
789
Olivier Galibert00a92642005-06-22 17:16:29 +0000790/* The file offset position represents the dirent entry number. A
791 last cookie cache takes care of the common case of reading the
792 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
794static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
795{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800796 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct inode *inode = dentry->d_inode;
798 nfs_readdir_descriptor_t my_desc,
799 *desc = &my_desc;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400800 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Chuck Lever6da24bc2008-06-11 17:55:58 -0400802 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500803 dentry->d_parent->d_name.name, dentry->d_name.name,
804 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500805 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000808 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000809 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000810 * to either find the entry with the appropriate number or
811 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
813 memset(desc, 0, sizeof(*desc));
814
815 desc->file = filp;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400816 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 desc->decode = NFS_PROTO(inode)->decode_dirent;
818 desc->plus = NFS_USE_READDIRPLUS(inode);
819
Trond Myklebust565277f2007-10-15 18:17:53 -0400820 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800821 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500822 if (res < 0)
823 goto out;
824
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400825 while (desc->eof != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500829 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* This means either end of directory */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400831 if (*desc->dir_cookie && desc->eof == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Or that the server has 'lost' a cookie */
833 res = uncached_readdir(desc, dirent, filldir);
Trond Myklebustece0b422010-11-20 13:55:33 -0500834 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 continue;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 }
839 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200840 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400842 desc->page_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 desc->plus = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400844 desc->eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 continue;
846 }
847 if (res < 0)
848 break;
849
850 res = nfs_do_filldir(desc, dirent, filldir);
Trond Myklebustece0b422010-11-20 13:55:33 -0500851 if (res < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500854out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400855 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500856 if (res > 0)
857 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400858 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500859 dentry->d_parent->d_name.name, dentry->d_name.name,
860 res);
861 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Trond Myklebust10afec902007-05-14 17:16:04 -0400864static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000865{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400866 struct dentry *dentry = filp->f_path.dentry;
867 struct inode *inode = dentry->d_inode;
868
Chuck Lever6da24bc2008-06-11 17:55:58 -0400869 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400870 dentry->d_parent->d_name.name,
871 dentry->d_name.name,
872 offset, origin);
873
874 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000875 switch (origin) {
876 case 1:
877 offset += filp->f_pos;
878 case 0:
879 if (offset >= 0)
880 break;
881 default:
882 offset = -EINVAL;
883 goto out;
884 }
885 if (offset != filp->f_pos) {
886 filp->f_pos = offset;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400887 nfs_file_open_context(filp)->dir_cookie = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000888 }
889out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400890 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000891 return offset;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/*
895 * All directory operations under NFS are synchronous, so fsync()
896 * is a dummy operation.
897 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200898static int nfs_fsync_dir(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200900 struct dentry *dentry = filp->f_path.dentry;
901
Chuck Lever6da24bc2008-06-11 17:55:58 -0400902 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500903 dentry->d_parent->d_name.name, dentry->d_name.name,
904 datasync);
905
Chuck Lever54917782008-05-27 16:29:07 -0400906 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
909
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400910/**
911 * nfs_force_lookup_revalidate - Mark the directory as having changed
912 * @dir - pointer to directory inode
913 *
914 * This forces the revalidation code in nfs_lookup_revalidate() to do a
915 * full lookup on all child dentries of 'dir' whenever a change occurs
916 * on the server that might have invalidated our dcache.
917 *
918 * The caller should be holding dir->i_lock
919 */
920void nfs_force_lookup_revalidate(struct inode *dir)
921{
Trond Myklebust011935a2008-10-14 19:24:50 -0400922 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * A check for whether or not the parent directory has changed.
927 * In the case it has, we assume that the dentries are untrustworthy
928 * and may need to be looked up again.
929 */
Trond Myklebustc79ba782007-01-31 08:16:24 -0500930static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 if (IS_ROOT(dentry))
933 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400934 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
935 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -0400936 if (!nfs_verify_change_attribute(dir, dentry->d_time))
937 return 0;
938 /* Revalidate nfsi->cache_change_attribute before we declare a match */
939 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
940 return 0;
941 if (!nfs_verify_change_attribute(dir, dentry->d_time))
942 return 0;
943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400947 * Return the intent data that applies to this particular path component
948 *
949 * Note that the current set of intents only apply to the very last
950 * component of the path.
951 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
952 */
953static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
954{
955 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
956 return 0;
957 return nd->flags & mask;
958}
959
960/*
Trond Myklebusta12802c2007-10-02 19:13:04 -0400961 * Use intent information to check whether or not we're going to do
962 * an O_EXCL create using this path component.
963 */
964static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
965{
966 if (NFS_PROTO(dir)->version == 2)
967 return 0;
Al Viro35165862008-08-05 03:00:49 -0400968 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -0400969}
970
971/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400972 * Inode and filehandle revalidation for lookups.
973 *
974 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
975 * or if the intent information indicates that we're about to open this
976 * particular file and the "nocto" mount flag is not set.
977 *
978 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static inline
980int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
981{
982 struct nfs_server *server = NFS_SERVER(inode);
983
Trond Myklebust4e99a1f2008-03-06 12:34:59 -0500984 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400988 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto out_force;
990 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400991 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400992 !(server->flags & NFS_MOUNT_NOCTO) &&
993 (S_ISREG(inode->i_mode) ||
994 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -0400996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 return nfs_revalidate_inode(server, inode);
999out_force:
1000 return __nfs_revalidate_inode(server, inode);
1001}
1002
1003/*
1004 * We judge how long we want to trust negative
1005 * dentries by looking at the parent inode mtime.
1006 *
1007 * If parent mtime has changed, we revalidate, else we wait for a
1008 * period corresponding to the parent's attribute cache timeout value.
1009 */
1010static inline
1011int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1012 struct nameidata *nd)
1013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001015 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001017 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1018 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return !nfs_check_verifier(dir, dentry);
1020}
1021
1022/*
1023 * This is called every time the dcache has a lookup hit,
1024 * and we should check whether we can really trust that
1025 * lookup.
1026 *
1027 * NOTE! The hit can be a negative hit too, don't assume
1028 * we have an inode!
1029 *
1030 * If the parent directory is seen to have changed, we throw out the
1031 * cached dentry and do a new lookup.
1032 */
1033static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
1034{
1035 struct inode *dir;
1036 struct inode *inode;
1037 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001038 struct nfs_fh *fhandle = NULL;
1039 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -05001044 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 inode = dentry->d_inode;
1046
1047 if (!inode) {
1048 if (nfs_neg_need_reval(dir, dentry, nd))
1049 goto out_bad;
1050 goto out_valid;
1051 }
1052
1053 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001054 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001055 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001056 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto out_bad;
1058 }
1059
Trond Myklebust15860ab2008-12-23 15:21:54 -05001060 if (nfs_have_delegation(inode, FMODE_READ))
1061 goto out_set_verifier;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -04001064 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (nfs_lookup_verify_inode(inode, nd))
1066 goto out_zap_parent;
1067 goto out_valid;
1068 }
1069
1070 if (NFS_STALE(inode))
1071 goto out_bad;
1072
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001073 error = -ENOMEM;
1074 fhandle = nfs_alloc_fhandle();
1075 fattr = nfs_alloc_fattr();
1076 if (fhandle == NULL || fattr == NULL)
1077 goto out_error;
1078
1079 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (error)
1081 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001082 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001084 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto out_bad;
1086
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001087 nfs_free_fattr(fattr);
1088 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001089out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -04001090 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001093 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001094 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001095 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 1;
1097out_zap_parent:
1098 nfs_zap_caches(dir);
1099 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -04001100 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (inode && S_ISDIR(inode->i_mode)) {
1102 /* Purge readdir caches. */
1103 nfs_zap_caches(inode);
1104 /* If we have submounts, don't unhash ! */
1105 if (have_submounts(dentry))
1106 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +01001107 if (dentry->d_flags & DCACHE_DISCONNECTED)
1108 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 shrink_dcache_parent(dentry);
1110 }
1111 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001112 nfs_free_fattr(fattr);
1113 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001115 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001116 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001117 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001119out_error:
1120 nfs_free_fattr(fattr);
1121 nfs_free_fhandle(fhandle);
1122 dput(parent);
1123 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1124 __func__, dentry->d_parent->d_name.name,
1125 dentry->d_name.name, error);
1126 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129/*
1130 * This is called from dput() when d_count is going to 0.
1131 */
1132static int nfs_dentry_delete(struct dentry *dentry)
1133{
1134 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1135 dentry->d_parent->d_name.name, dentry->d_name.name,
1136 dentry->d_flags);
1137
Trond Myklebust77f11192008-01-28 19:43:19 -05001138 /* Unhash any dentry with a stale inode */
1139 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1140 return 1;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1143 /* Unhash it, so that ->d_iput() would be called */
1144 return 1;
1145 }
1146 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1147 /* Unhash it, so that ancestors of killed async unlink
1148 * files will be cleaned up during umount */
1149 return 1;
1150 }
1151 return 0;
1152
1153}
1154
Trond Myklebust1b83d702008-06-11 15:44:04 -04001155static void nfs_drop_nlink(struct inode *inode)
1156{
1157 spin_lock(&inode->i_lock);
1158 if (inode->i_nlink > 0)
1159 drop_nlink(inode);
1160 spin_unlock(&inode->i_lock);
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * Called when the dentry loses inode.
1165 * We use it to clean up silly-renamed files.
1166 */
1167static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1168{
Neil Brown83672d32007-02-26 12:48:25 +11001169 if (S_ISDIR(inode->i_mode))
1170 /* drop any readdir cache as it could easily be old */
1171 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001174 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001175 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 iput(inode);
1178}
1179
Al Virof786aa92009-02-20 05:51:22 +00001180const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 .d_revalidate = nfs_lookup_revalidate,
1182 .d_delete = nfs_dentry_delete,
1183 .d_iput = nfs_dentry_iput,
1184};
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1187{
1188 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -04001189 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001191 struct nfs_fh *fhandle = NULL;
1192 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1196 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -05001197 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 res = ERR_PTR(-ENAMETOOLONG);
1200 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1201 goto out;
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1204
Trond Myklebustfd684072006-09-05 12:27:44 -04001205 /*
1206 * If we're doing an exclusive create, optimize away the lookup
1207 * but don't hash the dentry.
1208 */
1209 if (nfs_is_exclusive_create(dir, nd)) {
1210 d_instantiate(dentry, NULL);
1211 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -04001212 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -04001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001215 res = ERR_PTR(-ENOMEM);
1216 fhandle = nfs_alloc_fhandle();
1217 fattr = nfs_alloc_fattr();
1218 if (fhandle == NULL || fattr == NULL)
1219 goto out;
1220
Trond Myklebust565277f2007-10-15 18:17:53 -04001221 parent = dentry->d_parent;
1222 /* Protect against concurrent sillydeletes */
1223 nfs_block_sillyrename(parent);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001224 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (error == -ENOENT)
1226 goto no_entry;
1227 if (error < 0) {
1228 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -04001229 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001231 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001232 res = (struct dentry *)inode;
1233 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001234 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -04001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236no_entry:
David Howells54ceac42006-08-22 20:06:13 -04001237 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001238 if (res != NULL) {
1239 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001240 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001244out_unblock_sillyrename:
1245 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001247 nfs_free_fattr(fattr);
1248 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return res;
1250}
1251
1252#ifdef CONFIG_NFS_V4
1253static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1254
Al Virof786aa92009-02-20 05:51:22 +00001255const struct dentry_operations nfs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 .d_revalidate = nfs_open_revalidate,
1257 .d_delete = nfs_dentry_delete,
1258 .d_iput = nfs_dentry_iput,
1259};
1260
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001261/*
1262 * Use intent information to determine whether we need to substitute
1263 * the NFSv4-style stateful OPEN for the LOOKUP call
1264 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001265static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001267 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return 0;
1269 /* NFS does not (yet) have a stateful open for directories */
1270 if (nd->flags & LOOKUP_DIRECTORY)
1271 return 0;
1272 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001273 if (__mnt_is_readonly(nd->path.mnt) &&
1274 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276 return 1;
1277}
1278
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001279static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1280{
1281 struct path path = {
1282 .mnt = nd->path.mnt,
1283 .dentry = dentry,
1284 };
1285 struct nfs_open_context *ctx;
1286 struct rpc_cred *cred;
1287 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1288
1289 cred = rpc_lookup_cred();
1290 if (IS_ERR(cred))
1291 return ERR_CAST(cred);
1292 ctx = alloc_nfs_open_context(&path, cred, fmode);
1293 put_rpccred(cred);
1294 if (ctx == NULL)
1295 return ERR_PTR(-ENOMEM);
1296 return ctx;
1297}
1298
1299static int do_open(struct inode *inode, struct file *filp)
1300{
1301 nfs_fscache_set_inode_cookie(inode, filp);
1302 return 0;
1303}
1304
1305static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1306{
1307 struct file *filp;
1308 int ret = 0;
1309
1310 /* If the open_intent is for execute, we have an extra check to make */
1311 if (ctx->mode & FMODE_EXEC) {
1312 ret = nfs_may_open(ctx->path.dentry->d_inode,
1313 ctx->cred,
1314 nd->intent.open.flags);
1315 if (ret < 0)
1316 goto out;
1317 }
1318 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1319 if (IS_ERR(filp))
1320 ret = PTR_ERR(filp);
1321 else
1322 nfs_file_set_open_context(filp, ctx);
1323out:
1324 put_nfs_open_context(ctx);
1325 return ret;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1329{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001330 struct nfs_open_context *ctx;
1331 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct dentry *res = NULL;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001333 struct inode *inode;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001334 int open_flags;
Trond Myklebust898f6352010-10-23 11:24:25 -04001335 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001337 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1338 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /* Check that we are indeed trying to open this file */
Trond Myklebust5584c302008-12-23 15:21:54 -05001341 if (!is_atomic_open(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 goto no_open;
1343
1344 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1345 res = ERR_PTR(-ENAMETOOLONG);
1346 goto out;
1347 }
1348 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1349
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001350 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1351 * the dentry. */
Al Viro35165862008-08-05 03:00:49 -04001352 if (nd->flags & LOOKUP_EXCL) {
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001353 d_instantiate(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001354 goto out;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001357 ctx = nameidata_to_nfs_open_context(dentry, nd);
1358 res = ERR_CAST(ctx);
1359 if (IS_ERR(ctx))
1360 goto out;
1361
1362 open_flags = nd->intent.open.flags;
1363 if (nd->flags & LOOKUP_CREATE) {
1364 attr.ia_mode = nd->intent.open.create_mode;
1365 attr.ia_valid = ATTR_MODE;
1366 if (!IS_POSIXACL(dir))
1367 attr.ia_mode &= ~current_umask();
1368 } else {
Trond Myklebust898f6352010-10-23 11:24:25 -04001369 open_flags &= ~(O_EXCL | O_CREAT);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001370 attr.ia_valid = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001371 }
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* Open the file on the server */
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001374 nfs_block_sillyrename(dentry->d_parent);
Trond Myklebust2b484292010-09-17 10:56:51 -04001375 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001376 if (IS_ERR(inode)) {
1377 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001378 put_nfs_open_context(ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001379 switch (PTR_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* Make a negative dentry */
1381 case -ENOENT:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001382 d_add(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001383 res = NULL;
1384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* This turned out not to be a regular file */
Trond Myklebust6f926b52005-10-18 14:20:18 -07001386 case -ENOTDIR:
1387 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 case -ELOOP:
1389 if (!(nd->intent.open.flags & O_NOFOLLOW))
1390 goto no_open;
Trond Myklebust23ebbd92010-11-03 10:24:16 -04001391 /* case -EISDIR: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* case -EINVAL: */
1393 default:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001394 res = ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 goto out;
1396 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001397 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001398 res = d_add_unique(dentry, inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001399 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001400 if (res != NULL) {
1401 dput(ctx->path.dentry);
1402 ctx->path.dentry = dget(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 dentry = res;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001404 }
Trond Myklebust898f6352010-10-23 11:24:25 -04001405 err = nfs_intent_set_file(nd, ctx);
1406 if (err < 0) {
1407 if (res != NULL)
1408 dput(res);
1409 return ERR_PTR(err);
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411out:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001412 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return res;
1414no_open:
1415 return nfs_lookup(dir, dentry, nd);
1416}
1417
1418static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1419{
1420 struct dentry *parent = NULL;
1421 struct inode *inode = dentry->d_inode;
1422 struct inode *dir;
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001423 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int openflags, ret = 0;
1425
Trond Myklebust1f063d22010-04-22 15:35:55 -04001426 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001427 goto no_open;
Trond Myklebust2b484292010-09-17 10:56:51 -04001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 parent = dget_parent(dentry);
1430 dir = parent->d_inode;
Trond Myklebust2b484292010-09-17 10:56:51 -04001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* We can't create new files in nfs_open_revalidate(), so we
1433 * optimize away revalidation of negative dentries.
1434 */
Trond Myklebust216d5d062007-10-01 20:10:12 -04001435 if (inode == NULL) {
1436 if (!nfs_neg_need_reval(dir, dentry, nd))
1437 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto out;
Trond Myklebust216d5d062007-10-01 20:10:12 -04001439 }
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* NFS only supports OPEN on regular files */
1442 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001443 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 openflags = nd->intent.open.flags;
1445 /* We cannot do exclusive creation on a positive dentry */
1446 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001447 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* We can't create new files, or truncate existing ones here */
Trond Myklebust0a377cf2010-08-18 09:25:42 -04001449 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001451 ctx = nameidata_to_nfs_open_context(dentry, nd);
1452 ret = PTR_ERR(ctx);
1453 if (IS_ERR(ctx))
1454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001456 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 * operations that change the directory. We therefore save the
1458 * change attribute *before* we do the RPC call.
1459 */
Trond Myklebust2b484292010-09-17 10:56:51 -04001460 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
Trond Myklebust535918f2010-09-17 10:56:51 -04001461 if (IS_ERR(inode)) {
1462 ret = PTR_ERR(inode);
1463 switch (ret) {
1464 case -EPERM:
1465 case -EACCES:
1466 case -EDQUOT:
1467 case -ENOSPC:
1468 case -EROFS:
1469 goto out_put_ctx;
1470 default:
1471 goto out_drop;
1472 }
1473 }
1474 iput(inode);
Trond Myklebust898f6352010-10-23 11:24:25 -04001475 if (inode != dentry->d_inode)
Trond Myklebust535918f2010-09-17 10:56:51 -04001476 goto out_drop;
Trond Myklebust898f6352010-10-23 11:24:25 -04001477
1478 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1479 ret = nfs_intent_set_file(nd, ctx);
1480 if (ret >= 0)
1481 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482out:
1483 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001485out_drop:
1486 d_drop(dentry);
1487 ret = 0;
1488out_put_ctx:
1489 put_nfs_open_context(ctx);
1490 goto out;
1491
Trond Myklebust5584c302008-12-23 15:21:54 -05001492no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001494no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return nfs_lookup_revalidate(dentry, nd);
1496}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001497
1498static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1499 struct nameidata *nd)
1500{
1501 struct nfs_open_context *ctx = NULL;
1502 struct iattr attr;
1503 int error;
1504 int open_flags = 0;
1505
1506 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1507 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1508
1509 attr.ia_mode = mode;
1510 attr.ia_valid = ATTR_MODE;
1511
1512 if ((nd->flags & LOOKUP_CREATE) != 0) {
1513 open_flags = nd->intent.open.flags;
1514
1515 ctx = nameidata_to_nfs_open_context(dentry, nd);
1516 error = PTR_ERR(ctx);
1517 if (IS_ERR(ctx))
Trond Myklebust898f6352010-10-23 11:24:25 -04001518 goto out_err_drop;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001519 }
1520
1521 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1522 if (error != 0)
1523 goto out_put_ctx;
Trond Myklebust898f6352010-10-23 11:24:25 -04001524 if (ctx != NULL) {
1525 error = nfs_intent_set_file(nd, ctx);
1526 if (error < 0)
1527 goto out_err;
1528 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001529 return 0;
1530out_put_ctx:
1531 if (ctx != NULL)
1532 put_nfs_open_context(ctx);
Trond Myklebust898f6352010-10-23 11:24:25 -04001533out_err_drop:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001534 d_drop(dentry);
Trond Myklebust898f6352010-10-23 11:24:25 -04001535out_err:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001536 return error;
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539#endif /* CONFIG_NFSV4 */
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/*
1542 * Code common to create, mkdir, and mknod.
1543 */
1544int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1545 struct nfs_fattr *fattr)
1546{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001547 struct dentry *parent = dget_parent(dentry);
1548 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct inode *inode;
1550 int error = -EACCES;
1551
Trond Myklebustfab728e2007-09-29 17:41:33 -04001552 d_drop(dentry);
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* We may have been initialized further down */
1555 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (fhandle->size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1559 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001560 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001562 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1564 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001565 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001567 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001570 error = PTR_ERR(inode);
1571 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001572 goto out_error;
1573 d_add(dentry, inode);
1574out:
1575 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001577out_error:
1578 nfs_mark_for_revalidate(dir);
1579 dput(parent);
1580 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/*
1584 * Following a failed create operation, we drop the dentry rather
1585 * than retain a negative dentry. This avoids a problem in the event
1586 * that the operation succeeded on the server, but an error in the
1587 * reply path made it appear to have failed.
1588 */
1589static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1590 struct nameidata *nd)
1591{
1592 struct iattr attr;
1593 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001595 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1596 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 attr.ia_mode = mode;
1599 attr.ia_valid = ATTR_MODE;
1600
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001601 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (error != 0)
1603 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return 0;
1605out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 d_drop(dentry);
1607 return error;
1608}
1609
1610/*
1611 * See comments for nfs_proc_create regarding failed operations.
1612 */
1613static int
1614nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1615{
1616 struct iattr attr;
1617 int status;
1618
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001619 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1620 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (!new_valid_dev(rdev))
1623 return -EINVAL;
1624
1625 attr.ia_mode = mode;
1626 attr.ia_valid = ATTR_MODE;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (status != 0)
1630 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 d_drop(dentry);
1634 return status;
1635}
1636
1637/*
1638 * See comments for nfs_proc_create regarding failed operations.
1639 */
1640static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1641{
1642 struct iattr attr;
1643 int error;
1644
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001645 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1646 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 attr.ia_valid = ATTR_MODE;
1649 attr.ia_mode = mode | S_IFDIR;
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (error != 0)
1653 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
1655out_err:
1656 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return error;
1658}
1659
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001660static void nfs_dentry_handle_enoent(struct dentry *dentry)
1661{
1662 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1663 d_delete(dentry);
1664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1667{
1668 int error;
1669
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001670 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1671 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1674 /* Ensure the VFS deletes this inode */
1675 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001676 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001677 else if (error == -ENOENT)
1678 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 return error;
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/*
1684 * Remove a file after making sure there are no pending writes,
1685 * and after checking that the file has only one user.
1686 *
1687 * We invalidate the attribute cache and free the inode prior to the operation
1688 * to avoid possible races if the server reuses the inode.
1689 */
1690static int nfs_safe_remove(struct dentry *dentry)
1691{
1692 struct inode *dir = dentry->d_parent->d_inode;
1693 struct inode *inode = dentry->d_inode;
1694 int error = -EBUSY;
1695
1696 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1697 dentry->d_parent->d_name.name, dentry->d_name.name);
1698
1699 /* If the dentry was sillyrenamed, we simply call d_delete() */
1700 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1701 error = 0;
1702 goto out;
1703 }
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001706 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1708 /* The VFS may want to delete this inode */
1709 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001710 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001711 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 } else
1713 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001714 if (error == -ENOENT)
1715 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716out:
1717 return error;
1718}
1719
1720/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1721 * belongs to an active ".nfs..." file and we return -EBUSY.
1722 *
1723 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1724 */
1725static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1726{
1727 int error;
1728 int need_rehash = 0;
1729
1730 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1731 dir->i_ino, dentry->d_name.name);
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 spin_lock(&dcache_lock);
1734 spin_lock(&dentry->d_lock);
1735 if (atomic_read(&dentry->d_count) > 1) {
1736 spin_unlock(&dentry->d_lock);
1737 spin_unlock(&dcache_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001738 /* Start asynchronous writeout of the inode */
1739 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return error;
1742 }
1743 if (!d_unhashed(dentry)) {
1744 __d_drop(dentry);
1745 need_rehash = 1;
1746 }
1747 spin_unlock(&dentry->d_lock);
1748 spin_unlock(&dcache_lock);
1749 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001750 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1752 } else if (need_rehash)
1753 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return error;
1755}
1756
Chuck Lever873101b2006-08-22 20:06:23 -04001757/*
1758 * To create a symbolic link, most file systems instantiate a new inode,
1759 * add a page to it containing the path, then write it out to the disk
1760 * using prepare_write/commit_write.
1761 *
1762 * Unfortunately the NFS client can't create the in-core inode first
1763 * because it needs a file handle to create an in-core inode (see
1764 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1765 * symlink request has completed on the server.
1766 *
1767 * So instead we allocate a raw page, copy the symname into it, then do
1768 * the SYMLINK request with the page as the buffer. If it succeeds, we
1769 * now have a new file handle and can instantiate an in-core NFS inode
1770 * and move the raw page into its mapping.
1771 */
1772static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Chuck Lever873101b2006-08-22 20:06:23 -04001774 struct pagevec lru_pvec;
1775 struct page *page;
1776 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001778 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int error;
1780
1781 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1782 dir->i_ino, dentry->d_name.name, symname);
1783
Chuck Lever873101b2006-08-22 20:06:23 -04001784 if (pathlen > PAGE_SIZE)
1785 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Chuck Lever873101b2006-08-22 20:06:23 -04001787 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1788 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Jeff Layton83d93f22007-06-07 09:58:08 -04001790 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001791 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001792 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001793
1794 kaddr = kmap_atomic(page, KM_USER0);
1795 memcpy(kaddr, symname, pathlen);
1796 if (pathlen < PAGE_SIZE)
1797 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1798 kunmap_atomic(kaddr, KM_USER0);
1799
Chuck Lever94a6d752006-08-22 20:06:23 -04001800 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001801 if (error != 0) {
1802 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1803 dir->i_sb->s_id, dir->i_ino,
1804 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001806 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001807 return error;
1808 }
1809
1810 /*
1811 * No big deal if we can't add this page to the page cache here.
1812 * READLINK will get the missing page from the server if needed.
1813 */
1814 pagevec_init(&lru_pvec, 0);
1815 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1816 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001817 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001818 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001819 SetPageUptodate(page);
1820 unlock_page(page);
1821 } else
1822 __free_page(page);
1823
Chuck Lever873101b2006-08-22 20:06:23 -04001824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827static int
1828nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1829{
1830 struct inode *inode = old_dentry->d_inode;
1831 int error;
1832
1833 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1834 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1835 dentry->d_parent->d_name.name, dentry->d_name.name);
1836
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001837 nfs_inode_return_delegation(inode);
1838
Trond Myklebust9697d232007-10-02 21:58:05 -04001839 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001841 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04001842 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04001843 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return error;
1846}
1847
1848/*
1849 * RENAME
1850 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1851 * different file handle for the same inode after a rename (e.g. when
1852 * moving to a different directory). A fail-safe method to do so would
1853 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1854 * rename the old file using the sillyrename stuff. This way, the original
1855 * file in old_dir will go away when the last process iput()s the inode.
1856 *
1857 * FIXED.
1858 *
1859 * It actually works quite well. One needs to have the possibility for
1860 * at least one ".nfs..." file in each directory the file ever gets
1861 * moved or linked to which happens automagically with the new
1862 * implementation that only depends on the dcache stuff instead of
1863 * using the inode layer
1864 *
1865 * Unfortunately, things are a little more complicated than indicated
1866 * above. For a cross-directory move, we want to make sure we can get
1867 * rid of the old inode after the operation. This means there must be
1868 * no pending writes (if it's a file), and the use count must be 1.
1869 * If these conditions are met, we can drop the dentries before doing
1870 * the rename.
1871 */
1872static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1873 struct inode *new_dir, struct dentry *new_dentry)
1874{
1875 struct inode *old_inode = old_dentry->d_inode;
1876 struct inode *new_inode = new_dentry->d_inode;
1877 struct dentry *dentry = NULL, *rehash = NULL;
1878 int error = -EBUSY;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1881 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1882 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1883 atomic_read(&new_dentry->d_count));
1884
1885 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001886 * For non-directories, check whether the target is busy and if so,
1887 * make a copy of the dentry and then do a silly-rename. If the
1888 * silly-rename succeeds, the copied dentry is hashed and becomes
1889 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001891 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1892 /*
1893 * To prevent any new references to the target during the
1894 * rename, we unhash the dentry in advance.
1895 */
1896 if (!d_unhashed(new_dentry)) {
1897 d_drop(new_dentry);
1898 rehash = new_dentry;
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Miklos Szeredi27226102009-12-03 15:58:56 -05001901 if (atomic_read(&new_dentry->d_count) > 2) {
1902 int err;
1903
1904 /* copy the target dentry's name */
1905 dentry = d_alloc(new_dentry->d_parent,
1906 &new_dentry->d_name);
1907 if (!dentry)
1908 goto out;
1909
1910 /* silly-rename the existing target ... */
1911 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001912 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001913 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001914
1915 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001916 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001917 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05001918 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Trond Myklebustcae7a072005-10-18 14:20:19 -07001921 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001922 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01001923 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1926 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001927 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928out:
1929 if (rehash)
1930 d_rehash(rehash);
1931 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001932 if (new_inode != NULL)
1933 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07001934 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04001935 nfs_set_verifier(new_dentry,
1936 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001937 } else if (error == -ENOENT)
1938 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 /* new dentry created? */
1941 if (dentry)
1942 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return error;
1944}
1945
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001946static DEFINE_SPINLOCK(nfs_access_lru_lock);
1947static LIST_HEAD(nfs_access_lru_list);
1948static atomic_long_t nfs_access_nr_entries;
1949
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001950static void nfs_access_free_entry(struct nfs_access_entry *entry)
1951{
1952 put_rpccred(entry->cred);
1953 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001954 smp_mb__before_atomic_dec();
1955 atomic_long_dec(&nfs_access_nr_entries);
1956 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001957}
1958
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001959static void nfs_access_free_list(struct list_head *head)
1960{
1961 struct nfs_access_entry *cache;
1962
1963 while (!list_empty(head)) {
1964 cache = list_entry(head->next, struct nfs_access_entry, lru);
1965 list_del(&cache->lru);
1966 nfs_access_free_entry(cache);
1967 }
1968}
1969
Dave Chinner7f8275d2010-07-19 14:56:17 +10001970int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebust979df722006-07-25 11:28:19 -04001971{
1972 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001973 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04001974 struct nfs_access_entry *cache;
1975
Trond Myklebust61d5eb22010-05-13 12:51:06 -04001976 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1977 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001978
Trond Myklebusta50f7952007-06-05 19:23:43 -04001979 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04001980 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04001981 struct inode *inode;
1982
1983 if (nr_to_scan-- == 0)
1984 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001985 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04001986 spin_lock(&inode->i_lock);
1987 if (list_empty(&nfsi->access_cache_entry_lru))
1988 goto remove_lru_entry;
1989 cache = list_entry(nfsi->access_cache_entry_lru.next,
1990 struct nfs_access_entry, lru);
1991 list_move(&cache->lru, &head);
1992 rb_erase(&cache->rb_node, &nfsi->access_cache);
1993 if (!list_empty(&nfsi->access_cache_entry_lru))
1994 list_move_tail(&nfsi->access_cache_inode_lru,
1995 &nfs_access_lru_list);
1996 else {
1997remove_lru_entry:
1998 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001999 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04002000 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002001 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04002002 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002003 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002004 }
2005 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002006 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04002007 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2008}
2009
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002010static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002011{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002012 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002013 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002014 struct nfs_access_entry *entry;
2015
2016 /* Unhook entries from the cache */
2017 while ((n = rb_first(root_node)) != NULL) {
2018 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2019 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002020 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002021 }
2022 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002023}
2024
2025void nfs_access_zap_cache(struct inode *inode)
2026{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002027 LIST_HEAD(head);
2028
2029 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2030 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002031 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002032 spin_lock(&nfs_access_lru_lock);
2033 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002034 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002035
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002036 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002037 __nfs_access_zap_cache(NFS_I(inode), &head);
2038 spin_unlock(&inode->i_lock);
2039 spin_unlock(&nfs_access_lru_lock);
2040 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002041}
2042
2043static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2044{
2045 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2046 struct nfs_access_entry *entry;
2047
2048 while (n != NULL) {
2049 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2050
2051 if (cred < entry->cred)
2052 n = n->rb_left;
2053 else if (cred > entry->cred)
2054 n = n->rb_right;
2055 else
2056 return entry;
2057 }
2058 return NULL;
2059}
2060
Trond Myklebustaf22f942007-08-10 17:45:10 -04002061static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Chuck Lever55296802005-08-18 11:24:09 -07002063 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002064 struct nfs_access_entry *cache;
2065 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002067 spin_lock(&inode->i_lock);
2068 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2069 goto out_zap;
2070 cache = nfs_access_search_rbtree(inode, cred);
2071 if (cache == NULL)
2072 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05002073 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05002074 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002075 goto out_stale;
2076 res->jiffies = cache->jiffies;
2077 res->cred = cache->cred;
2078 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002079 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002080 err = 0;
2081out:
2082 spin_unlock(&inode->i_lock);
2083 return err;
2084out_stale:
2085 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002086 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002087 spin_unlock(&inode->i_lock);
2088 nfs_access_free_entry(cache);
2089 return -ENOENT;
2090out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002091 spin_unlock(&inode->i_lock);
2092 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002093 return -ENOENT;
2094}
2095
2096static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2097{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002098 struct nfs_inode *nfsi = NFS_I(inode);
2099 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002100 struct rb_node **p = &root_node->rb_node;
2101 struct rb_node *parent = NULL;
2102 struct nfs_access_entry *entry;
2103
2104 spin_lock(&inode->i_lock);
2105 while (*p != NULL) {
2106 parent = *p;
2107 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2108
2109 if (set->cred < entry->cred)
2110 p = &parent->rb_left;
2111 else if (set->cred > entry->cred)
2112 p = &parent->rb_right;
2113 else
2114 goto found;
2115 }
2116 rb_link_node(&set->rb_node, parent, p);
2117 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002118 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002119 spin_unlock(&inode->i_lock);
2120 return;
2121found:
2122 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002123 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2124 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002125 spin_unlock(&inode->i_lock);
2126 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Trond Myklebustaf22f942007-08-10 17:45:10 -04002129static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002131 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2132 if (cache == NULL)
2133 return;
2134 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002136 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002138
2139 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002140
2141 /* Update accounting */
2142 smp_mb__before_atomic_inc();
2143 atomic_long_inc(&nfs_access_nr_entries);
2144 smp_mb__after_atomic_inc();
2145
2146 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002147 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002148 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002149 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2150 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2151 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002152 spin_unlock(&nfs_access_lru_lock);
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2157{
2158 struct nfs_access_entry cache;
2159 int status;
2160
2161 status = nfs_access_get_cached(inode, cred, &cache);
2162 if (status == 0)
2163 goto out;
2164
2165 /* Be clever: ask server to check for all possible rights */
2166 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2167 cache.cred = cred;
2168 cache.jiffies = jiffies;
2169 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002170 if (status != 0) {
2171 if (status == -ESTALE) {
2172 nfs_zap_caches(inode);
2173 if (!S_ISDIR(inode->i_mode))
2174 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 nfs_access_add_cache(inode, &cache);
2179out:
Al Viroe6305c42008-07-15 21:03:57 -04002180 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
2182 return -EACCES;
2183}
2184
Trond Myklebustaf22f942007-08-10 17:45:10 -04002185static int nfs_open_permission_mask(int openflags)
2186{
2187 int mask = 0;
2188
2189 if (openflags & FMODE_READ)
2190 mask |= MAY_READ;
2191 if (openflags & FMODE_WRITE)
2192 mask |= MAY_WRITE;
2193 if (openflags & FMODE_EXEC)
2194 mask |= MAY_EXEC;
2195 return mask;
2196}
2197
2198int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2199{
2200 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2201}
2202
Al Viroe6305c42008-07-15 21:03:57 -04002203int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 struct rpc_cred *cred;
2206 int res = 0;
2207
Chuck Lever91d5b472006-03-20 13:44:14 -05002208 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2209
Al Viroe6305c42008-07-15 21:03:57 -04002210 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 goto out;
2212 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002213 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 goto force_lookup;
2215
2216 switch (inode->i_mode & S_IFMT) {
2217 case S_IFLNK:
2218 goto out;
2219 case S_IFREG:
2220 /* NFSv4 has atomic_open... */
2221 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002222 && (mask & MAY_OPEN)
2223 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 goto out;
2225 break;
2226 case S_IFDIR:
2227 /*
2228 * Optimize away all write operations, since the server
2229 * will check permissions when we perform the op.
2230 */
2231 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2232 goto out;
2233 }
2234
2235force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (!NFS_PROTO(inode)->access)
2237 goto out_notsup;
2238
Trond Myklebust98a8e322008-03-12 12:25:28 -04002239 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (!IS_ERR(cred)) {
2241 res = nfs_do_access(inode, cred, mask);
2242 put_rpccred(cred);
2243 } else
2244 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002246 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2247 res = -EACCES;
2248
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002249 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2250 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return res;
2252out_notsup:
2253 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2254 if (res == 0)
2255 res = generic_permission(inode, mask, NULL);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
2259/*
2260 * Local variables:
2261 * version-control: t
2262 * kept-new-versions: 5
2263 * End:
2264 */