blob: 0d8c71271d1a8c44b02545cd40bdf421013985c2 [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>
Catalin Marinas04e4bd12010-11-11 12:53:47 +000036#include <linux/kmemleak.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000037#include <linux/xattr.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 *);
Bryan Schumaker480c2002011-03-23 14:48:29 -040047static int nfs_closedir(struct inode *, struct file *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int nfs_readdir(struct file *, void *, filldir_t);
49static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
Al Viro4acdaf22011-07-26 01:42:34 -040050static int nfs_create(struct inode *, struct dentry *, umode_t, struct nameidata *);
Al Viro18bb1db2011-07-26 01:41:39 -040051static int nfs_mkdir(struct inode *, struct dentry *, umode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int nfs_rmdir(struct inode *, struct dentry *);
53static int nfs_unlink(struct inode *, struct dentry *);
54static int nfs_symlink(struct inode *, struct dentry *, const char *);
55static int nfs_link(struct dentry *, struct inode *, struct dentry *);
Al Viro1a67aaf2011-07-26 01:52:52 -040056static int nfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int nfs_rename(struct inode *, struct dentry *,
58 struct inode *, struct dentry *);
Josef Bacik02c24a82011-07-16 20:44:56 -040059static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000060static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Trond Myklebust11de3b12010-12-01 14:17:06 -050061static void nfs_readdir_clear_array(struct page*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080063const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000064 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .read = generic_read_dir,
66 .readdir = nfs_readdir,
67 .open = nfs_opendir,
Bryan Schumaker480c2002011-03-23 14:48:29 -040068 .release = nfs_closedir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .fsync = nfs_fsync_dir,
70};
71
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080072const struct inode_operations nfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .create = nfs_create,
74 .lookup = nfs_lookup,
75 .link = nfs_link,
76 .unlink = nfs_unlink,
77 .symlink = nfs_symlink,
78 .mkdir = nfs_mkdir,
79 .rmdir = nfs_rmdir,
80 .mknod = nfs_mknod,
81 .rename = nfs_rename,
82 .permission = nfs_permission,
83 .getattr = nfs_getattr,
84 .setattr = nfs_setattr,
85};
86
Trond Myklebust11de3b12010-12-01 14:17:06 -050087const struct address_space_operations nfs_dir_aops = {
88 .freepage = nfs_readdir_clear_array,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040089};
90
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000091#ifdef CONFIG_NFS_V3
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080092const struct inode_operations nfs3_dir_inode_operations = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000093 .create = nfs_create,
94 .lookup = nfs_lookup,
95 .link = nfs_link,
96 .unlink = nfs_unlink,
97 .symlink = nfs_symlink,
98 .mkdir = nfs_mkdir,
99 .rmdir = nfs_rmdir,
100 .mknod = nfs_mknod,
101 .rename = nfs_rename,
102 .permission = nfs_permission,
103 .getattr = nfs_getattr,
104 .setattr = nfs_setattr,
105 .listxattr = nfs3_listxattr,
106 .getxattr = nfs3_getxattr,
107 .setxattr = nfs3_setxattr,
108 .removexattr = nfs3_removexattr,
109};
110#endif /* CONFIG_NFS_V3 */
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#ifdef CONFIG_NFS_V4
113
Miklos Szeredi0dd2b472012-06-05 15:10:18 +0200114static struct file *nfs_atomic_open(struct inode *, struct dentry *,
115 struct opendata *, unsigned, umode_t,
116 bool *);
117static int nfs4_create(struct inode *dir, struct dentry *dentry,
118 umode_t mode, struct nameidata *nd);
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800119const struct inode_operations nfs4_dir_inode_operations = {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +0200120 .create = nfs4_create,
121 .lookup = nfs_lookup,
122 .atomic_open = nfs_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .link = nfs_link,
124 .unlink = nfs_unlink,
125 .symlink = nfs_symlink,
126 .mkdir = nfs_mkdir,
127 .rmdir = nfs_rmdir,
128 .mknod = nfs_mknod,
129 .rename = nfs_rename,
130 .permission = nfs_permission,
131 .getattr = nfs_getattr,
132 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +0000133 .getxattr = generic_getxattr,
134 .setxattr = generic_setxattr,
135 .listxattr = generic_listxattr,
136 .removexattr = generic_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139#endif /* CONFIG_NFS_V4 */
140
Trond Myklebust0c030802011-07-30 12:45:35 -0400141static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
Bryan Schumaker480c2002011-03-23 14:48:29 -0400142{
143 struct nfs_open_dir_context *ctx;
144 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
145 if (ctx != NULL) {
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400146 ctx->duped = 0;
Trond Myklebust0c030802011-07-30 12:45:35 -0400147 ctx->attr_gencount = NFS_I(dir)->attr_gencount;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400148 ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400149 ctx->dup_cookie = 0;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400150 ctx->cred = get_rpccred(cred);
Trond Myklebust0c030802011-07-30 12:45:35 -0400151 return ctx;
152 }
153 return ERR_PTR(-ENOMEM);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400154}
155
156static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
157{
158 put_rpccred(ctx->cred);
159 kfree(ctx);
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
163 * Open file
164 */
165static int
166nfs_opendir(struct inode *inode, struct file *filp)
167{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400168 int res = 0;
169 struct nfs_open_dir_context *ctx;
170 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Chuck Lever6da24bc2008-06-11 17:55:58 -0400172 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400173 filp->f_path.dentry->d_parent->d_name.name,
174 filp->f_path.dentry->d_name.name);
175
176 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500177
Bryan Schumaker480c2002011-03-23 14:48:29 -0400178 cred = rpc_lookup_cred();
179 if (IS_ERR(cred))
180 return PTR_ERR(cred);
Trond Myklebust0c030802011-07-30 12:45:35 -0400181 ctx = alloc_nfs_open_dir_context(inode, cred);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400182 if (IS_ERR(ctx)) {
183 res = PTR_ERR(ctx);
184 goto out;
185 }
186 filp->private_data = ctx;
Neil Brownf5a73672010-08-10 10:20:05 -0400187 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
188 /* This is a mountpoint, so d_revalidate will never
189 * have been called, so we need to refresh the
190 * inode (for close-open consistency) ourselves.
191 */
192 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
193 }
Bryan Schumaker480c2002011-03-23 14:48:29 -0400194out:
195 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return res;
197}
198
Bryan Schumaker480c2002011-03-23 14:48:29 -0400199static int
200nfs_closedir(struct inode *inode, struct file *filp)
201{
202 put_nfs_open_dir_context(filp->private_data);
203 return 0;
204}
205
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400206struct nfs_cache_array_entry {
207 u64 cookie;
208 u64 ino;
209 struct qstr string;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500210 unsigned char d_type;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400211};
212
213struct nfs_cache_array {
Chuck Lever88b8e132012-03-01 17:00:23 -0500214 int size;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400215 int eof_index;
216 u64 last_cookie;
217 struct nfs_cache_array_entry array[0];
218};
219
Chuck Lever573c4e12010-12-14 14:58:11 +0000220typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221typedef struct {
222 struct file *file;
223 struct page *page;
224 unsigned long page_index;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000225 u64 *dir_cookie;
Trond Myklebust0aded702010-11-30 21:56:32 -0500226 u64 last_cookie;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000227 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 decode_dirent_t decode;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400229
Neil Brown1f4eab72007-04-16 09:35:27 +1000230 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400231 unsigned long gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400232 unsigned int cache_entry_index;
233 unsigned int plus:1;
234 unsigned int eof:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235} nfs_readdir_descriptor_t;
236
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400237/*
238 * The caller is responsible for calling nfs_readdir_release_array(page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240static
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400241struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500243 void *ptr;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400244 if (page == NULL)
245 return ERR_PTR(-EIO);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500246 ptr = kmap(page);
247 if (ptr == NULL)
248 return ERR_PTR(-ENOMEM);
249 return ptr;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400250}
251
252static
253void nfs_readdir_release_array(struct page *page)
254{
255 kunmap(page);
256}
257
258/*
259 * we are freeing strings created by nfs_add_to_readdir_array()
260 */
261static
Trond Myklebust11de3b12010-12-01 14:17:06 -0500262void nfs_readdir_clear_array(struct page *page)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400263{
Trond Myklebust11de3b12010-12-01 14:17:06 -0500264 struct nfs_cache_array *array;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400265 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500266
Cong Wang2b86ce22011-11-25 23:14:33 +0800267 array = kmap_atomic(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400268 for (i = 0; i < array->size; i++)
269 kfree(array->array[i].string.name);
Cong Wang2b86ce22011-11-25 23:14:33 +0800270 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400271}
272
273/*
274 * the caller is responsible for freeing qstr.name
275 * when called by nfs_readdir_add_to_array, the strings will be freed in
276 * nfs_clear_readdir_array()
277 */
278static
Trond Myklebust4a201d62010-10-23 14:53:23 -0400279int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400280{
281 string->len = len;
282 string->name = kmemdup(name, len, GFP_KERNEL);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400283 if (string->name == NULL)
284 return -ENOMEM;
Catalin Marinas04e4bd12010-11-11 12:53:47 +0000285 /*
286 * Avoid a kmemleak false positive. The pointer to the name is stored
287 * in a page cache page which kmemleak does not scan.
288 */
289 kmemleak_not_leak(string->name);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400290 string->hash = full_name_hash(name, len);
291 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400292}
293
294static
295int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
296{
297 struct nfs_cache_array *array = nfs_readdir_get_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400298 struct nfs_cache_array_entry *cache_entry;
299 int ret;
300
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400301 if (IS_ERR(array))
302 return PTR_ERR(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400303
Trond Myklebust4a201d62010-10-23 14:53:23 -0400304 cache_entry = &array->array[array->size];
Trond Myklebust30200932010-11-20 15:18:22 -0500305
306 /* Check that this entry lies within the page bounds */
307 ret = -ENOSPC;
308 if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
309 goto out;
310
Trond Myklebust4a201d62010-10-23 14:53:23 -0400311 cache_entry->cookie = entry->prev_cookie;
312 cache_entry->ino = entry->ino;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500313 cache_entry->d_type = entry->d_type;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400314 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
315 if (ret)
316 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400317 array->last_cookie = entry->cookie;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500318 array->size++;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500319 if (entry->eof != 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400320 array->eof_index = array->size;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400321out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400322 nfs_readdir_release_array(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400323 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400324}
325
326static
327int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
328{
329 loff_t diff = desc->file->f_pos - desc->current_index;
330 unsigned int index;
331
332 if (diff < 0)
333 goto out_eof;
334 if (diff >= array->size) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500335 if (array->eof_index >= 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400336 goto out_eof;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400337 return -EAGAIN;
338 }
339
340 index = (unsigned int)diff;
341 *desc->dir_cookie = array->array[index].cookie;
342 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400343 return 0;
344out_eof:
345 desc->eof = 1;
346 return -EBADCOOKIE;
347}
348
349static
350int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
351{
352 int i;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400353 loff_t new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400354 int status = -EAGAIN;
355
356 for (i = 0; i < array->size; i++) {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400357 if (array->array[i].cookie == *desc->dir_cookie) {
Trond Myklebust0c030802011-07-30 12:45:35 -0400358 struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode);
359 struct nfs_open_dir_context *ctx = desc->file->private_data;
360
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400361 new_pos = desc->current_index + i;
Trond Myklebust0c030802011-07-30 12:45:35 -0400362 if (ctx->attr_gencount != nfsi->attr_gencount
363 || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
364 ctx->duped = 0;
365 ctx->attr_gencount = nfsi->attr_gencount;
366 } else if (new_pos < desc->file->f_pos) {
367 if (ctx->duped > 0
368 && ctx->dup_cookie == *desc->dir_cookie) {
369 if (printk_ratelimit()) {
370 pr_notice("NFS: directory %s/%s contains a readdir loop."
371 "Please contact your server vendor. "
Bryan Schumaker374e4e32011-07-29 11:49:06 -0400372 "The file: %s has duplicate cookie %llu\n",
Trond Myklebust0c030802011-07-30 12:45:35 -0400373 desc->file->f_dentry->d_parent->d_name.name,
374 desc->file->f_dentry->d_name.name,
Bryan Schumaker374e4e32011-07-29 11:49:06 -0400375 array->array[i].string.name,
Trond Myklebust0c030802011-07-30 12:45:35 -0400376 *desc->dir_cookie);
377 }
378 status = -ELOOP;
379 goto out;
380 }
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400381 ctx->dup_cookie = *desc->dir_cookie;
Trond Myklebust0c030802011-07-30 12:45:35 -0400382 ctx->duped = -1;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400383 }
384 desc->file->f_pos = new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400385 desc->cache_entry_index = i;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500386 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400387 }
388 }
Trond Myklebust47c716c2010-12-07 12:44:56 -0500389 if (array->eof_index >= 0) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500390 status = -EBADCOOKIE;
Trond Myklebust18fb5fe2010-12-07 12:41:58 -0500391 if (*desc->dir_cookie == array->last_cookie)
392 desc->eof = 1;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500393 }
Trond Myklebust0c030802011-07-30 12:45:35 -0400394out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400395 return status;
396}
397
398static
399int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
400{
401 struct nfs_cache_array *array;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500402 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400403
404 array = nfs_readdir_get_array(desc->page);
405 if (IS_ERR(array)) {
406 status = PTR_ERR(array);
407 goto out;
408 }
409
410 if (*desc->dir_cookie == 0)
411 status = nfs_readdir_search_for_pos(array, desc);
412 else
413 status = nfs_readdir_search_for_cookie(array, desc);
414
Trond Myklebust47c716c2010-12-07 12:44:56 -0500415 if (status == -EAGAIN) {
Trond Myklebust0aded702010-11-30 21:56:32 -0500416 desc->last_cookie = array->last_cookie;
Trond Myklebuste47c0852011-03-23 08:43:09 -0400417 desc->current_index += array->size;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500418 desc->page_index++;
419 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400420 nfs_readdir_release_array(desc->page);
421out:
422 return status;
423}
424
425/* Fill a page with xdr information before transferring to the cache page */
426static
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400427int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400428 struct nfs_entry *entry, struct file *file, struct inode *inode)
429{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400430 struct nfs_open_dir_context *ctx = file->private_data;
431 struct rpc_cred *cred = ctx->cred;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400432 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int error;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 again:
436 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400437 gencount = nfs_inc_attr_generation_counter();
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400438 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 NFS_SERVER(inode)->dtsize, desc->plus);
440 if (error < 0) {
441 /* We requested READDIRPLUS, but the server doesn't grok it */
442 if (error == -ENOTSUPP && desc->plus) {
443 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200444 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 desc->plus = 0;
446 goto again;
447 }
448 goto error;
449 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000450 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400451 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400452error:
453 return error;
454}
455
Chuck Lever573c4e12010-12-14 14:58:11 +0000456static int xdr_decode(nfs_readdir_descriptor_t *desc,
457 struct nfs_entry *entry, struct xdr_stream *xdr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400458{
Chuck Lever573c4e12010-12-14 14:58:11 +0000459 int error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400460
Chuck Lever573c4e12010-12-14 14:58:11 +0000461 error = desc->decode(xdr, entry, desc->plus);
462 if (error)
463 return error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400464 entry->fattr->time_start = desc->timestamp;
465 entry->fattr->gencount = desc->gencount;
466 return 0;
467}
468
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400469static
470int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
471{
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400472 if (dentry->d_inode == NULL)
473 goto different;
Trond Myklebust37a09f02010-11-30 12:42:34 -0500474 if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400475 goto different;
476 return 1;
477different:
478 return 0;
479}
480
481static
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400482bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
483{
484 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
485 return false;
486 if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
487 return true;
488 if (filp->f_pos == 0)
489 return true;
490 return false;
491}
492
493/*
494 * This function is called by the lookup code to request the use of
495 * readdirplus to accelerate any future lookups in the same
496 * directory.
497 */
498static
499void nfs_advise_use_readdirplus(struct inode *dir)
500{
501 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
502}
503
504static
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400505void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
506{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700507 struct qstr filename = QSTR_INIT(entry->name, entry->len);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400508 struct dentry *dentry;
509 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400510 struct inode *dir = parent->d_inode;
511 struct inode *inode;
512
Trond Myklebust4a201d62010-10-23 14:53:23 -0400513 if (filename.name[0] == '.') {
514 if (filename.len == 1)
515 return;
516 if (filename.len == 2 && filename.name[1] == '.')
517 return;
518 }
519 filename.hash = full_name_hash(filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400520
Trond Myklebust4a201d62010-10-23 14:53:23 -0400521 dentry = d_lookup(parent, &filename);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400522 if (dentry != NULL) {
523 if (nfs_same_file(dentry, entry)) {
524 nfs_refresh_inode(dentry->d_inode, entry->fattr);
525 goto out;
526 } else {
527 d_drop(dentry);
528 dput(dentry);
529 }
530 }
531
532 dentry = d_alloc(parent, &filename);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400533 if (dentry == NULL)
534 return;
535
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400536 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
537 if (IS_ERR(inode))
538 goto out;
539
540 alias = d_materialise_unique(dentry, inode);
541 if (IS_ERR(alias))
542 goto out;
543 else if (alias) {
544 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
545 dput(alias);
546 } else
547 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
548
549out:
550 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400551}
552
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400553/* Perform conversion from xdr to cache array */
554static
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500555int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
Trond Myklebust66502392011-01-08 17:45:38 -0500556 struct page **xdr_pages, struct page *page, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400557{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400558 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400559 struct xdr_buf buf;
Trond Myklebust66502392011-01-08 17:45:38 -0500560 struct page *scratch;
Bryan Schumaker99424382010-10-21 16:33:16 -0400561 struct nfs_cache_array *array;
Trond Myklebust5c346852010-11-20 12:43:45 -0500562 unsigned int count = 0;
563 int status;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400564
Trond Myklebust66502392011-01-08 17:45:38 -0500565 scratch = alloc_page(GFP_KERNEL);
566 if (scratch == NULL)
567 return -ENOMEM;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400568
Benny Halevyf7da7a12011-05-19 14:16:47 -0400569 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
Trond Myklebust66502392011-01-08 17:45:38 -0500570 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Bryan Schumaker99424382010-10-21 16:33:16 -0400571
572 do {
573 status = xdr_decode(desc, entry, &stream);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500574 if (status != 0) {
575 if (status == -EAGAIN)
576 status = 0;
Bryan Schumaker99424382010-10-21 16:33:16 -0400577 break;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500578 }
Bryan Schumaker99424382010-10-21 16:33:16 -0400579
Trond Myklebust5c346852010-11-20 12:43:45 -0500580 count++;
581
Trond Myklebust47c716c2010-12-07 12:44:56 -0500582 if (desc->plus != 0)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400583 nfs_prime_dcache(desc->file->f_path.dentry, entry);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500584
585 status = nfs_readdir_add_to_array(entry, page);
586 if (status != 0)
587 break;
Bryan Schumaker99424382010-10-21 16:33:16 -0400588 } while (!entry->eof);
589
Trond Myklebust47c716c2010-12-07 12:44:56 -0500590 if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
Bryan Schumaker99424382010-10-21 16:33:16 -0400591 array = nfs_readdir_get_array(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500592 if (!IS_ERR(array)) {
593 array->eof_index = array->size;
594 status = 0;
595 nfs_readdir_release_array(page);
Trond Myklebust5c346852010-11-20 12:43:45 -0500596 } else
597 status = PTR_ERR(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400598 }
Trond Myklebust66502392011-01-08 17:45:38 -0500599
600 put_page(scratch);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500601 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400602}
603
604static
605void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
606{
607 unsigned int i;
608 for (i = 0; i < npages; i++)
609 put_page(pages[i]);
610}
611
612static
613void nfs_readdir_free_large_page(void *ptr, struct page **pages,
614 unsigned int npages)
615{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400616 nfs_readdir_free_pagearray(pages, npages);
617}
618
619/*
620 * nfs_readdir_large_page will allocate pages that must be freed with a call
621 * to nfs_readdir_free_large_page
622 */
623static
Trond Myklebust66502392011-01-08 17:45:38 -0500624int nfs_readdir_large_page(struct page **pages, unsigned int npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400625{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400626 unsigned int i;
627
628 for (i = 0; i < npages; i++) {
629 struct page *page = alloc_page(GFP_KERNEL);
630 if (page == NULL)
631 goto out_freepages;
632 pages[i] = page;
633 }
Trond Myklebust66502392011-01-08 17:45:38 -0500634 return 0;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400635
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400636out_freepages:
637 nfs_readdir_free_pagearray(pages, i);
Trond Myklebust66502392011-01-08 17:45:38 -0500638 return -ENOMEM;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400639}
640
641static
642int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
643{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400644 struct page *pages[NFS_MAX_READDIR_PAGES];
645 void *pages_ptr = NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400646 struct nfs_entry entry;
647 struct file *file = desc->file;
648 struct nfs_cache_array *array;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500649 int status = -ENOMEM;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400650 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400651
652 entry.prev_cookie = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500653 entry.cookie = desc->last_cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400654 entry.eof = 0;
655 entry.fh = nfs_alloc_fhandle();
656 entry.fattr = nfs_alloc_fattr();
Chuck Lever573c4e12010-12-14 14:58:11 +0000657 entry.server = NFS_SERVER(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400658 if (entry.fh == NULL || entry.fattr == NULL)
659 goto out;
660
661 array = nfs_readdir_get_array(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500662 if (IS_ERR(array)) {
663 status = PTR_ERR(array);
664 goto out;
665 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400666 memset(array, 0, sizeof(struct nfs_cache_array));
667 array->eof_index = -1;
668
Trond Myklebust66502392011-01-08 17:45:38 -0500669 status = nfs_readdir_large_page(pages, array_size);
670 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400671 goto out_release_array;
672 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500673 unsigned int pglen;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400674 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400675
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400676 if (status < 0)
677 break;
Trond Myklebustac396122010-11-15 20:26:22 -0500678 pglen = status;
Trond Myklebust66502392011-01-08 17:45:38 -0500679 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500680 if (status < 0) {
681 if (status == -ENOSPC)
682 status = 0;
683 break;
684 }
685 } while (array->eof_index < 0);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400686
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400687 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400688out_release_array:
689 nfs_readdir_release_array(page);
690out:
691 nfs_free_fattr(entry.fattr);
692 nfs_free_fhandle(entry.fh);
693 return status;
694}
695
696/*
697 * Now we cache directories properly, by converting xdr information
698 * to an array that can be used for lookups later. This results in
699 * fewer cache pages, since we can store more information on each page.
700 * We only need to convert from xdr once so future lookups are much simpler
701 */
702static
703int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
704{
705 struct inode *inode = desc->file->f_path.dentry->d_inode;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500706 int ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400707
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500708 ret = nfs_readdir_xdr_to_array(desc, page, inode);
709 if (ret < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400710 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400712
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400713 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700714 /* Should never happen */
715 nfs_zap_mapping(inode, inode->i_mapping);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unlock_page(page);
718 return 0;
719 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unlock_page(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400724static
725void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Trond Myklebust11de3b12010-12-01 14:17:06 -0500727 if (!desc->page->mapping)
728 nfs_readdir_clear_array(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 page_cache_release(desc->page);
730 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400733static
734struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500736 return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400737 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400741 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Olivier Galibert00a92642005-06-22 17:16:29 +0000742 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400743static
744int find_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000745{
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400746 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000747
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400748 desc->page = get_cache_page(desc);
749 if (IS_ERR(desc->page))
750 return PTR_ERR(desc->page);
Olivier Galibert00a92642005-06-22 17:16:29 +0000751
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400752 res = nfs_readdir_search_array(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500753 if (res != 0)
754 cache_page_release(desc);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400755 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000756}
757
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400758/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static inline
760int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
761{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500762 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000763
Trond Myklebust0aded702010-11-30 21:56:32 -0500764 if (desc->page_index == 0) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500765 desc->current_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500766 desc->last_cookie = 0;
767 }
Trond Myklebust47c716c2010-12-07 12:44:56 -0500768 do {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400769 res = find_cache_page(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500770 } while (res == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return res;
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*
775 * Once we've found the start of the dirent within a page: fill 'er up...
776 */
777static
778int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
779 filldir_t filldir)
780{
781 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400782 int i = 0;
783 int res = 0;
784 struct nfs_cache_array *array = NULL;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400785 struct nfs_open_dir_context *ctx = file->private_data;
786
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400787 array = nfs_readdir_get_array(desc->page);
Trond Myklebuste7c58e92010-11-20 13:22:24 -0500788 if (IS_ERR(array)) {
789 res = PTR_ERR(array);
790 goto out;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400793 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500794 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Trond Myklebustece0b422010-11-20 13:55:33 -0500796 ent = &array->array[i];
797 if (filldir(dirent, ent->string.name, ent->string.len,
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500798 file->f_pos, nfs_compat_user_ino64(ent->ino),
799 ent->d_type) < 0) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500800 desc->eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500802 }
Olivier Galibert00a92642005-06-22 17:16:29 +0000803 file->f_pos++;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400804 if (i < (array->size-1))
805 *desc->dir_cookie = array->array[i+1].cookie;
806 else
807 *desc->dir_cookie = array->last_cookie;
Trond Myklebust0c030802011-07-30 12:45:35 -0400808 if (ctx->duped != 0)
809 ctx->duped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Trond Myklebust47c716c2010-12-07 12:44:56 -0500811 if (array->eof_index >= 0)
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500812 desc->eof = 1;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400813
814 nfs_readdir_release_array(desc->page);
Trond Myklebuste7c58e92010-11-20 13:22:24 -0500815out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400816 cache_page_release(desc);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500817 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
818 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return res;
820}
821
822/*
823 * If we cannot find a cookie in our cache, we suspect that this is
824 * because it points to a deleted file, so we ask the server to return
825 * whatever it thinks is the next entry. We then feed this to filldir.
826 * If all goes well, we should then be able to find our way round the
827 * cache on the next call to readdir_search_pagecache();
828 *
829 * NOTE: we cannot add the anonymous page to the pagecache because
830 * the data it contains might not be page aligned. Besides,
831 * we should already have a complete representation of the
832 * directory in the page cache by the time we get here.
833 */
834static inline
835int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
836 filldir_t filldir)
837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct page *page = NULL;
839 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400840 struct inode *inode = desc->file->f_path.dentry->d_inode;
Trond Myklebust0c030802011-07-30 12:45:35 -0400841 struct nfs_open_dir_context *ctx = desc->file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500843 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
844 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 page = alloc_page(GFP_HIGHUSER);
847 if (!page) {
848 status = -ENOMEM;
849 goto out;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500852 desc->page_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500853 desc->last_cookie = *desc->dir_cookie;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500854 desc->page = page;
Trond Myklebust0c030802011-07-30 12:45:35 -0400855 ctx->duped = 0;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500856
Trond Myklebust85f86072010-11-20 13:24:49 -0500857 status = nfs_readdir_xdr_to_array(desc, page, inode);
858 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400859 goto out_release;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 status = nfs_do_filldir(desc, dirent, filldir);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500864 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700865 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return status;
867 out_release:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400868 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto out;
870}
871
Olivier Galibert00a92642005-06-22 17:16:29 +0000872/* The file offset position represents the dirent entry number. A
873 last cookie cache takes care of the common case of reading the
874 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
876static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
877{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800878 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct inode *inode = dentry->d_inode;
880 nfs_readdir_descriptor_t my_desc,
881 *desc = &my_desc;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400882 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500883 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Chuck Lever6da24bc2008-06-11 17:55:58 -0400885 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500886 dentry->d_parent->d_name.name, dentry->d_name.name,
887 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500888 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000891 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000892 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000893 * to either find the entry with the appropriate number or
894 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
896 memset(desc, 0, sizeof(*desc));
897
898 desc->file = filp;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400899 desc->dir_cookie = &dir_ctx->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 desc->decode = NFS_PROTO(inode)->decode_dirent;
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400901 desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Trond Myklebust565277f2007-10-15 18:17:53 -0400903 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800904 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500905 if (res < 0)
906 goto out;
907
Trond Myklebust47c716c2010-12-07 12:44:56 -0500908 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500912 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* This means either end of directory */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400914 if (*desc->dir_cookie && desc->eof == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* Or that the server has 'lost' a cookie */
916 res = uncached_readdir(desc, dirent, filldir);
Trond Myklebustece0b422010-11-20 13:55:33 -0500917 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 continue;
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 break;
921 }
922 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200923 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400925 desc->page_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 desc->plus = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400927 desc->eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 continue;
929 }
930 if (res < 0)
931 break;
932
933 res = nfs_do_filldir(desc, dirent, filldir);
Trond Myklebustece0b422010-11-20 13:55:33 -0500934 if (res < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500936 } while (!desc->eof);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500937out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400938 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500939 if (res > 0)
940 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400941 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500942 dentry->d_parent->d_name.name, dentry->d_name.name,
943 res);
944 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Trond Myklebust10afec902007-05-14 17:16:04 -0400947static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000948{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400949 struct dentry *dentry = filp->f_path.dentry;
950 struct inode *inode = dentry->d_inode;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400951 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Chuck Leverb84e06c2008-06-11 17:55:34 -0400952
Chuck Lever6da24bc2008-06-11 17:55:58 -0400953 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400954 dentry->d_parent->d_name.name,
955 dentry->d_name.name,
956 offset, origin);
957
958 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000959 switch (origin) {
960 case 1:
961 offset += filp->f_pos;
962 case 0:
963 if (offset >= 0)
964 break;
965 default:
966 offset = -EINVAL;
967 goto out;
968 }
969 if (offset != filp->f_pos) {
970 filp->f_pos = offset;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400971 dir_ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400972 dir_ctx->duped = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000973 }
974out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400975 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000976 return offset;
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * All directory operations under NFS are synchronous, so fsync()
981 * is a dummy operation.
982 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400983static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
984 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200986 struct dentry *dentry = filp->f_path.dentry;
Josef Bacik02c24a82011-07-16 20:44:56 -0400987 struct inode *inode = dentry->d_inode;
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200988
Chuck Lever6da24bc2008-06-11 17:55:58 -0400989 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500990 dentry->d_parent->d_name.name, dentry->d_name.name,
991 datasync);
992
Josef Bacik02c24a82011-07-16 20:44:56 -0400993 mutex_lock(&inode->i_mutex);
Chuck Lever54917782008-05-27 16:29:07 -0400994 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Josef Bacik02c24a82011-07-16 20:44:56 -0400995 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return 0;
997}
998
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400999/**
1000 * nfs_force_lookup_revalidate - Mark the directory as having changed
1001 * @dir - pointer to directory inode
1002 *
1003 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1004 * full lookup on all child dentries of 'dir' whenever a change occurs
1005 * on the server that might have invalidated our dcache.
1006 *
1007 * The caller should be holding dir->i_lock
1008 */
1009void nfs_force_lookup_revalidate(struct inode *dir)
1010{
Trond Myklebust011935a2008-10-14 19:24:50 -04001011 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
1015 * A check for whether or not the parent directory has changed.
1016 * In the case it has, we assume that the dentries are untrustworthy
1017 * and may need to be looked up again.
1018 */
Trond Myklebustc79ba782007-01-31 08:16:24 -05001019static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 if (IS_ROOT(dentry))
1022 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001023 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1024 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001025 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1026 return 0;
1027 /* Revalidate nfsi->cache_change_attribute before we declare a match */
1028 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1029 return 0;
1030 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1031 return 0;
1032 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001036 * Return the intent data that applies to this particular path component
1037 *
1038 * Note that the current set of intents only apply to the very last
Al Viro8aeb3762011-06-25 21:48:43 -04001039 * component of the path and none of them is set before that last
1040 * component.
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001041 */
Nick Piggin34286d62011-01-07 17:49:57 +11001042static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
1043 unsigned int mask)
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001044{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001045 return nd->flags & mask;
1046}
1047
1048/*
Trond Myklebusta12802c2007-10-02 19:13:04 -04001049 * Use intent information to check whether or not we're going to do
1050 * an O_EXCL create using this path component.
1051 */
1052static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
1053{
1054 if (NFS_PROTO(dir)->version == 2)
1055 return 0;
Al Viro35165862008-08-05 03:00:49 -04001056 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -04001057}
1058
1059/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001060 * Inode and filehandle revalidation for lookups.
1061 *
1062 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1063 * or if the intent information indicates that we're about to open this
1064 * particular file and the "nocto" mount flag is not set.
1065 *
1066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static inline
1068int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
1069{
1070 struct nfs_server *server = NFS_SERVER(inode);
1071
David Howells36d43a42011-01-14 18:45:42 +00001072 if (IS_AUTOMOUNT(inode))
Trond Myklebust4e99a1f2008-03-06 12:34:59 -05001073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001076 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto out_force;
1078 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001079 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -04001080 !(server->flags & NFS_MOUNT_NOCTO) &&
1081 (S_ISREG(inode->i_mode) ||
1082 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -04001084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 return nfs_revalidate_inode(server, inode);
1087out_force:
1088 return __nfs_revalidate_inode(server, inode);
1089}
1090
1091/*
1092 * We judge how long we want to trust negative
1093 * dentries by looking at the parent inode mtime.
1094 *
1095 * If parent mtime has changed, we revalidate, else we wait for a
1096 * period corresponding to the parent's attribute cache timeout value.
1097 */
1098static inline
1099int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1100 struct nameidata *nd)
1101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001103 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001105 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1106 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return !nfs_check_verifier(dir, dentry);
1108}
1109
1110/*
1111 * This is called every time the dcache has a lookup hit,
1112 * and we should check whether we can really trust that
1113 * lookup.
1114 *
1115 * NOTE! The hit can be a negative hit too, don't assume
1116 * we have an inode!
1117 *
1118 * If the parent directory is seen to have changed, we throw out the
1119 * cached dentry and do a new lookup.
1120 */
Nick Piggin34286d62011-01-07 17:49:57 +11001121static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct inode *dir;
1124 struct inode *inode;
1125 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001126 struct nfs_fh *fhandle = NULL;
1127 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Nick Piggin34286d62011-01-07 17:49:57 +11001130 if (nd->flags & LOOKUP_RCU)
1131 return -ECHILD;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -05001135 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 inode = dentry->d_inode;
1137
1138 if (!inode) {
1139 if (nfs_neg_need_reval(dir, dentry, nd))
1140 goto out_bad;
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001141 goto out_valid_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001145 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001146 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001147 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 goto out_bad;
1149 }
1150
Trond Myklebust15860ab2008-12-23 15:21:54 -05001151 if (nfs_have_delegation(inode, FMODE_READ))
1152 goto out_set_verifier;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -04001155 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (nfs_lookup_verify_inode(inode, nd))
1157 goto out_zap_parent;
1158 goto out_valid;
1159 }
1160
1161 if (NFS_STALE(inode))
1162 goto out_bad;
1163
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001164 error = -ENOMEM;
1165 fhandle = nfs_alloc_fhandle();
1166 fattr = nfs_alloc_fattr();
1167 if (fhandle == NULL || fattr == NULL)
1168 goto out_error;
1169
Bryan Schumaker80a16b22012-04-27 13:27:46 -04001170 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (error)
1172 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001173 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001175 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto out_bad;
1177
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001178 nfs_free_fattr(fattr);
1179 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001180out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -04001181 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 out_valid:
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001183 /* Success: notify readdir to use READDIRPLUS */
1184 nfs_advise_use_readdirplus(dir);
1185 out_valid_noent:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001187 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001188 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001189 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return 1;
1191out_zap_parent:
1192 nfs_zap_caches(dir);
1193 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -04001194 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (inode && S_ISDIR(inode->i_mode)) {
1196 /* Purge readdir caches. */
1197 nfs_zap_caches(inode);
1198 /* If we have submounts, don't unhash ! */
1199 if (have_submounts(dentry))
1200 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +01001201 if (dentry->d_flags & DCACHE_DISCONNECTED)
1202 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 shrink_dcache_parent(dentry);
1204 }
1205 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001206 nfs_free_fattr(fattr);
1207 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001209 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001210 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001211 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001213out_error:
1214 nfs_free_fattr(fattr);
1215 nfs_free_fhandle(fhandle);
1216 dput(parent);
1217 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1218 __func__, dentry->d_parent->d_name.name,
1219 dentry->d_name.name, error);
1220 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*
1224 * This is called from dput() when d_count is going to 0.
1225 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001226static int nfs_dentry_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1229 dentry->d_parent->d_name.name, dentry->d_name.name,
1230 dentry->d_flags);
1231
Trond Myklebust77f11192008-01-28 19:43:19 -05001232 /* Unhash any dentry with a stale inode */
1233 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1234 return 1;
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1237 /* Unhash it, so that ->d_iput() would be called */
1238 return 1;
1239 }
1240 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1241 /* Unhash it, so that ancestors of killed async unlink
1242 * files will be cleaned up during umount */
1243 return 1;
1244 }
1245 return 0;
1246
1247}
1248
Trond Myklebust1b83d702008-06-11 15:44:04 -04001249static void nfs_drop_nlink(struct inode *inode)
1250{
1251 spin_lock(&inode->i_lock);
1252 if (inode->i_nlink > 0)
1253 drop_nlink(inode);
1254 spin_unlock(&inode->i_lock);
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/*
1258 * Called when the dentry loses inode.
1259 * We use it to clean up silly-renamed files.
1260 */
1261static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1262{
Neil Brown83672d32007-02-26 12:48:25 +11001263 if (S_ISDIR(inode->i_mode))
1264 /* drop any readdir cache as it could easily be old */
1265 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001268 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001269 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 iput(inode);
1272}
1273
Al Virob1942c52011-03-16 05:44:14 -04001274static void nfs_d_release(struct dentry *dentry)
1275{
1276 /* free cached devname value, if it survived that far */
1277 if (unlikely(dentry->d_fsdata)) {
1278 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1279 WARN_ON(1);
1280 else
1281 kfree(dentry->d_fsdata);
1282 }
1283}
1284
Al Virof786aa92009-02-20 05:51:22 +00001285const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 .d_revalidate = nfs_lookup_revalidate,
1287 .d_delete = nfs_dentry_delete,
1288 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001289 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001290 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291};
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1294{
1295 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -04001296 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001298 struct nfs_fh *fhandle = NULL;
1299 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1303 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -05001304 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 res = ERR_PTR(-ENAMETOOLONG);
1307 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1308 goto out;
1309
Trond Myklebustfd684072006-09-05 12:27:44 -04001310 /*
1311 * If we're doing an exclusive create, optimize away the lookup
1312 * but don't hash the dentry.
1313 */
1314 if (nfs_is_exclusive_create(dir, nd)) {
1315 d_instantiate(dentry, NULL);
1316 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -04001317 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -04001318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001320 res = ERR_PTR(-ENOMEM);
1321 fhandle = nfs_alloc_fhandle();
1322 fattr = nfs_alloc_fattr();
1323 if (fhandle == NULL || fattr == NULL)
1324 goto out;
1325
Trond Myklebust565277f2007-10-15 18:17:53 -04001326 parent = dentry->d_parent;
1327 /* Protect against concurrent sillydeletes */
1328 nfs_block_sillyrename(parent);
Bryan Schumaker80a16b22012-04-27 13:27:46 -04001329 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (error == -ENOENT)
1331 goto no_entry;
1332 if (error < 0) {
1333 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -04001334 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001336 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Namhyung Kimbf0c84f2010-12-28 17:02:46 +00001337 res = ERR_CAST(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001338 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001339 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -04001340
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001341 /* Success: notify readdir to use READDIRPLUS */
1342 nfs_advise_use_readdirplus(dir);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344no_entry:
David Howells54ceac42006-08-22 20:06:13 -04001345 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001346 if (res != NULL) {
1347 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -04001348 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001352out_unblock_sillyrename:
1353 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001355 nfs_free_fattr(fattr);
1356 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return res;
1358}
1359
1360#ifdef CONFIG_NFS_V4
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001361static int nfs4_lookup_revalidate(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Al Virof786aa92009-02-20 05:51:22 +00001363const struct dentry_operations nfs4_dentry_operations = {
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001364 .d_revalidate = nfs4_lookup_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 .d_delete = nfs_dentry_delete,
1366 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001367 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001368 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369};
1370
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001371/*
1372 * Use intent information to determine whether we need to substitute
1373 * the NFSv4-style stateful OPEN for the LOOKUP call
1374 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001375static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001377 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 0;
1379 /* NFS does not (yet) have a stateful open for directories */
1380 if (nd->flags & LOOKUP_DIRECTORY)
1381 return 0;
1382 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001383 if (__mnt_is_readonly(nd->path.mnt) &&
Al Viro8a5e9292011-06-25 19:15:54 -04001384 (nd->intent.open.flags & (O_CREAT|O_TRUNC|O_ACCMODE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386 return 1;
1387}
1388
Al Viro8a5e9292011-06-25 19:15:54 -04001389static fmode_t flags_to_mode(int flags)
1390{
1391 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1392 if ((flags & O_ACCMODE) != O_WRONLY)
1393 res |= FMODE_READ;
1394 if ((flags & O_ACCMODE) != O_RDONLY)
1395 res |= FMODE_WRITE;
1396 return res;
1397}
1398
Al Viro51141592011-06-22 18:47:28 -04001399static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001400{
Al Viro5ede7b12011-10-23 18:49:54 -04001401 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001402}
1403
1404static int do_open(struct inode *inode, struct file *filp)
1405{
1406 nfs_fscache_set_inode_cookie(inode, filp);
1407 return 0;
1408}
1409
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001410static struct file *nfs_finish_open(struct nfs_open_context *ctx,
1411 struct dentry *dentry,
1412 struct opendata *od, unsigned open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001413{
1414 struct file *filp;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001415 int err;
1416
1417 if (ctx->dentry != dentry) {
1418 dput(ctx->dentry);
1419 ctx->dentry = dget(dentry);
1420 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001421
1422 /* If the open_intent is for execute, we have an extra check to make */
1423 if (ctx->mode & FMODE_EXEC) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001424 err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1425 if (err < 0) {
1426 filp = ERR_PTR(err);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001427 goto out;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001428 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001429 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001430
1431 filp = finish_open(od, dentry, do_open);
1432 if (!IS_ERR(filp))
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001433 nfs_file_set_open_context(filp, ctx);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001434
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001435out:
1436 put_nfs_open_context(ctx);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001437 return filp;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001438}
1439
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001440static struct file *nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1441 struct opendata *od, unsigned open_flags,
1442 umode_t mode, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001444 struct nfs_open_context *ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001445 struct dentry *res;
1446 struct iattr attr = { .ia_valid = ATTR_OPEN };
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001447 struct inode *inode;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001448 struct file *filp;
Trond Myklebust898f6352010-10-23 11:24:25 -04001449 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001451 /* Expect a negative dentry */
1452 BUG_ON(dentry->d_inode);
1453
1454 dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001455 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1456
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001457 /* NFS only supports OPEN on regular files */
1458 if ((open_flags & O_DIRECTORY)) {
1459 err = -ENOENT;
1460 if (!d_unhashed(dentry)) {
1461 /*
1462 * Hashed negative dentry with O_DIRECTORY: dentry was
1463 * revalidated and is fine, no need to perform lookup
1464 * again
1465 */
1466 goto out_err;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001471 err = -ENAMETOOLONG;
1472 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1473 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001475 if (open_flags & O_CREAT) {
Trond Myklebust536e43d2012-01-17 22:04:26 -05001476 attr.ia_valid |= ATTR_MODE;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001477 attr.ia_mode = mode & ~current_umask();
1478 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001479 if (open_flags & O_TRUNC) {
1480 attr.ia_valid |= ATTR_SIZE;
1481 attr.ia_size = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001482 }
1483
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001484 ctx = create_nfs_open_context(dentry, open_flags);
1485 err = PTR_ERR(ctx);
1486 if (IS_ERR(ctx))
1487 goto out_err;
1488
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001489 nfs_block_sillyrename(dentry->d_parent);
Trond Myklebust2b484292010-09-17 10:56:51 -04001490 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001491 d_drop(dentry);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001492 if (IS_ERR(inode)) {
1493 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001494 put_nfs_open_context(ctx);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001495 err = PTR_ERR(inode);
1496 switch (err) {
1497 case -ENOENT:
1498 d_add(dentry, NULL);
1499 break;
1500 case -EISDIR:
1501 case -ENOTDIR:
1502 goto no_open;
1503 case -ELOOP:
1504 if (!(open_flags & O_NOFOLLOW))
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 goto no_open;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* case -EINVAL: */
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001508 default:
1509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001511 goto out_err;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001512 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001513 res = d_add_unique(dentry, inode);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001514 if (res != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dentry = res;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001516
1517 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001518 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001519
1520 filp = nfs_finish_open(ctx, dentry, od, open_flags);
1521
1522 dput(res);
1523 return filp;
1524
1525out_err:
1526 return ERR_PTR(err);
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528no_open:
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001529 res = nfs_lookup(dir, dentry, NULL);
1530 err = PTR_ERR(res);
1531 if (IS_ERR(res))
1532 goto out_err;
1533
1534 finish_no_open(od, res);
1535 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001538static int nfs4_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct dentry *parent = NULL;
Nick Piggin657e94b2011-01-14 02:48:39 +00001541 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 struct inode *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int openflags, ret = 0;
1544
Nick Piggin657e94b2011-01-14 02:48:39 +00001545 if (nd->flags & LOOKUP_RCU)
1546 return -ECHILD;
1547
1548 inode = dentry->d_inode;
Trond Myklebust1f063d22010-04-22 15:35:55 -04001549 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001550 goto no_open;
Trond Myklebust2b484292010-09-17 10:56:51 -04001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 parent = dget_parent(dentry);
1553 dir = parent->d_inode;
Trond Myklebust2b484292010-09-17 10:56:51 -04001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* We can't create new files in nfs_open_revalidate(), so we
1556 * optimize away revalidation of negative dentries.
1557 */
Trond Myklebust216d5d062007-10-01 20:10:12 -04001558 if (inode == NULL) {
1559 if (!nfs_neg_need_reval(dir, dentry, nd))
1560 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 goto out;
Trond Myklebust216d5d062007-10-01 20:10:12 -04001562 }
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* NFS only supports OPEN on regular files */
1565 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001566 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 openflags = nd->intent.open.flags;
1568 /* We cannot do exclusive creation on a positive dentry */
1569 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001570 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001572 /* Let f_op->open() actually open (and revalidate) the file */
1573 ret = 1;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575out:
1576 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001578
Trond Myklebust5584c302008-12-23 15:21:54 -05001579no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001581no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return nfs_lookup_revalidate(dentry, nd);
1583}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001584
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001585static int nfs4_create(struct inode *dir, struct dentry *dentry,
1586 umode_t mode, struct nameidata *nd)
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001587{
1588 struct nfs_open_context *ctx = NULL;
1589 struct iattr attr;
1590 int error;
Al Viro8a5e9292011-06-25 19:15:54 -04001591 int open_flags = O_CREAT|O_EXCL;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001592
1593 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1594 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1595
1596 attr.ia_mode = mode;
1597 attr.ia_valid = ATTR_MODE;
1598
Al Virodd7dd5562011-06-25 21:17:17 -04001599 if (nd)
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001600 open_flags = nd->intent.open.flags;
1601
Al Virof7c85862011-06-22 18:53:18 -04001602 ctx = create_nfs_open_context(dentry, open_flags);
1603 error = PTR_ERR(ctx);
1604 if (IS_ERR(ctx))
1605 goto out_err_drop;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001606
1607 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1608 if (error != 0)
1609 goto out_put_ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001610
1611 put_nfs_open_context(ctx);
1612
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001613 return 0;
1614out_put_ctx:
Al Virof7c85862011-06-22 18:53:18 -04001615 put_nfs_open_context(ctx);
Trond Myklebust898f6352010-10-23 11:24:25 -04001616out_err_drop:
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001617 d_drop(dentry);
1618 return error;
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621#endif /* CONFIG_NFSV4 */
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623/*
1624 * Code common to create, mkdir, and mknod.
1625 */
1626int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1627 struct nfs_fattr *fattr)
1628{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001629 struct dentry *parent = dget_parent(dentry);
1630 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct inode *inode;
1632 int error = -EACCES;
1633
Trond Myklebustfab728e2007-09-29 17:41:33 -04001634 d_drop(dentry);
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* We may have been initialized further down */
1637 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001638 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (fhandle->size == 0) {
Bryan Schumaker80a16b22012-04-27 13:27:46 -04001640 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001642 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001644 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1646 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001647 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001649 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001652 error = PTR_ERR(inode);
1653 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001654 goto out_error;
1655 d_add(dentry, inode);
1656out:
1657 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001659out_error:
1660 nfs_mark_for_revalidate(dir);
1661 dput(parent);
1662 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665/*
1666 * Following a failed create operation, we drop the dentry rather
1667 * than retain a negative dentry. This avoids a problem in the event
1668 * that the operation succeeded on the server, but an error in the
1669 * reply path made it appear to have failed.
1670 */
Al Viro4acdaf22011-07-26 01:42:34 -04001671static int nfs_create(struct inode *dir, struct dentry *dentry,
1672 umode_t mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct iattr attr;
1675 int error;
Al Viro8a5e9292011-06-25 19:15:54 -04001676 int open_flags = O_CREAT|O_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001678 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1679 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 attr.ia_mode = mode;
1682 attr.ia_valid = ATTR_MODE;
1683
Al Virodd7dd5562011-06-25 21:17:17 -04001684 if (nd)
Trond Myklebust8a0eebf2011-01-13 14:15:50 -05001685 open_flags = nd->intent.open.flags;
1686
1687 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (error != 0)
1689 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 d_drop(dentry);
1693 return error;
1694}
1695
1696/*
1697 * See comments for nfs_proc_create regarding failed operations.
1698 */
1699static int
Al Viro1a67aaf2011-07-26 01:52:52 -04001700nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 struct iattr attr;
1703 int status;
1704
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001705 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1706 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (!new_valid_dev(rdev))
1709 return -EINVAL;
1710
1711 attr.ia_mode = mode;
1712 attr.ia_valid = ATTR_MODE;
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (status != 0)
1716 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 d_drop(dentry);
1720 return status;
1721}
1722
1723/*
1724 * See comments for nfs_proc_create regarding failed operations.
1725 */
Al Viro18bb1db2011-07-26 01:41:39 -04001726static int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 struct iattr attr;
1729 int error;
1730
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001731 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1732 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 attr.ia_valid = ATTR_MODE;
1735 attr.ia_mode = mode | S_IFDIR;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (error != 0)
1739 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return 0;
1741out_err:
1742 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return error;
1744}
1745
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001746static void nfs_dentry_handle_enoent(struct dentry *dentry)
1747{
1748 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1749 d_delete(dentry);
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1753{
1754 int error;
1755
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001756 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1757 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1760 /* Ensure the VFS deletes this inode */
1761 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001762 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001763 else if (error == -ENOENT)
1764 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 return error;
1767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/*
1770 * Remove a file after making sure there are no pending writes,
1771 * and after checking that the file has only one user.
1772 *
1773 * We invalidate the attribute cache and free the inode prior to the operation
1774 * to avoid possible races if the server reuses the inode.
1775 */
1776static int nfs_safe_remove(struct dentry *dentry)
1777{
1778 struct inode *dir = dentry->d_parent->d_inode;
1779 struct inode *inode = dentry->d_inode;
1780 int error = -EBUSY;
1781
1782 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1783 dentry->d_parent->d_name.name, dentry->d_name.name);
1784
1785 /* If the dentry was sillyrenamed, we simply call d_delete() */
1786 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1787 error = 0;
1788 goto out;
1789 }
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001792 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1794 /* The VFS may want to delete this inode */
1795 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001796 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001797 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 } else
1799 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001800 if (error == -ENOENT)
1801 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802out:
1803 return error;
1804}
1805
1806/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1807 * belongs to an active ".nfs..." file and we return -EBUSY.
1808 *
1809 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1810 */
1811static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1812{
1813 int error;
1814 int need_rehash = 0;
1815
1816 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1817 dir->i_ino, dentry->d_name.name);
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001820 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 spin_unlock(&dentry->d_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001822 /* Start asynchronous writeout of the inode */
1823 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return error;
1826 }
1827 if (!d_unhashed(dentry)) {
1828 __d_drop(dentry);
1829 need_rehash = 1;
1830 }
1831 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001833 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1835 } else if (need_rehash)
1836 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return error;
1838}
1839
Chuck Lever873101b2006-08-22 20:06:23 -04001840/*
1841 * To create a symbolic link, most file systems instantiate a new inode,
1842 * add a page to it containing the path, then write it out to the disk
1843 * using prepare_write/commit_write.
1844 *
1845 * Unfortunately the NFS client can't create the in-core inode first
1846 * because it needs a file handle to create an in-core inode (see
1847 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1848 * symlink request has completed on the server.
1849 *
1850 * So instead we allocate a raw page, copy the symname into it, then do
1851 * the SYMLINK request with the page as the buffer. If it succeeds, we
1852 * now have a new file handle and can instantiate an in-core NFS inode
1853 * and move the raw page into its mapping.
1854 */
1855static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Chuck Lever873101b2006-08-22 20:06:23 -04001857 struct pagevec lru_pvec;
1858 struct page *page;
1859 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001861 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int error;
1863
1864 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1865 dir->i_ino, dentry->d_name.name, symname);
1866
Chuck Lever873101b2006-08-22 20:06:23 -04001867 if (pathlen > PAGE_SIZE)
1868 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Chuck Lever873101b2006-08-22 20:06:23 -04001870 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1871 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jeff Layton83d93f22007-06-07 09:58:08 -04001873 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001874 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001875 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001876
Cong Wang2b86ce22011-11-25 23:14:33 +08001877 kaddr = kmap_atomic(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001878 memcpy(kaddr, symname, pathlen);
1879 if (pathlen < PAGE_SIZE)
1880 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
Cong Wang2b86ce22011-11-25 23:14:33 +08001881 kunmap_atomic(kaddr);
Chuck Lever873101b2006-08-22 20:06:23 -04001882
Chuck Lever94a6d752006-08-22 20:06:23 -04001883 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001884 if (error != 0) {
1885 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1886 dir->i_sb->s_id, dir->i_ino,
1887 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001889 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001890 return error;
1891 }
1892
1893 /*
1894 * No big deal if we can't add this page to the page cache here.
1895 * READLINK will get the missing page from the server if needed.
1896 */
1897 pagevec_init(&lru_pvec, 0);
1898 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1899 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001900 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001901 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001902 SetPageUptodate(page);
1903 unlock_page(page);
1904 } else
1905 __free_page(page);
1906
Chuck Lever873101b2006-08-22 20:06:23 -04001907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910static int
1911nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1912{
1913 struct inode *inode = old_dentry->d_inode;
1914 int error;
1915
1916 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1917 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1918 dentry->d_parent->d_name.name, dentry->d_name.name);
1919
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001920 nfs_inode_return_delegation(inode);
1921
Trond Myklebust9697d232007-10-02 21:58:05 -04001922 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001924 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04001925 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04001926 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return error;
1929}
1930
1931/*
1932 * RENAME
1933 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1934 * different file handle for the same inode after a rename (e.g. when
1935 * moving to a different directory). A fail-safe method to do so would
1936 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1937 * rename the old file using the sillyrename stuff. This way, the original
1938 * file in old_dir will go away when the last process iput()s the inode.
1939 *
1940 * FIXED.
1941 *
1942 * It actually works quite well. One needs to have the possibility for
1943 * at least one ".nfs..." file in each directory the file ever gets
1944 * moved or linked to which happens automagically with the new
1945 * implementation that only depends on the dcache stuff instead of
1946 * using the inode layer
1947 *
1948 * Unfortunately, things are a little more complicated than indicated
1949 * above. For a cross-directory move, we want to make sure we can get
1950 * rid of the old inode after the operation. This means there must be
1951 * no pending writes (if it's a file), and the use count must be 1.
1952 * If these conditions are met, we can drop the dentries before doing
1953 * the rename.
1954 */
1955static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1956 struct inode *new_dir, struct dentry *new_dentry)
1957{
1958 struct inode *old_inode = old_dentry->d_inode;
1959 struct inode *new_inode = new_dentry->d_inode;
1960 struct dentry *dentry = NULL, *rehash = NULL;
1961 int error = -EBUSY;
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1964 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1965 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001966 new_dentry->d_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001969 * For non-directories, check whether the target is busy and if so,
1970 * make a copy of the dentry and then do a silly-rename. If the
1971 * silly-rename succeeds, the copied dentry is hashed and becomes
1972 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001974 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1975 /*
1976 * To prevent any new references to the target during the
1977 * rename, we unhash the dentry in advance.
1978 */
1979 if (!d_unhashed(new_dentry)) {
1980 d_drop(new_dentry);
1981 rehash = new_dentry;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001984 if (new_dentry->d_count > 2) {
Miklos Szeredi27226102009-12-03 15:58:56 -05001985 int err;
1986
1987 /* copy the target dentry's name */
1988 dentry = d_alloc(new_dentry->d_parent,
1989 &new_dentry->d_name);
1990 if (!dentry)
1991 goto out;
1992
1993 /* silly-rename the existing target ... */
1994 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001995 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001996 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001997
1998 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001999 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002000 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05002001 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Trond Myklebustcae7a072005-10-18 14:20:19 -07002004 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002005 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01002006 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
2009 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05002010 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011out:
2012 if (rehash)
2013 d_rehash(rehash);
2014 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002015 if (new_inode != NULL)
2016 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07002017 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04002018 nfs_set_verifier(new_dentry,
2019 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002020 } else if (error == -ENOENT)
2021 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 /* new dentry created? */
2024 if (dentry)
2025 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return error;
2027}
2028
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002029static DEFINE_SPINLOCK(nfs_access_lru_lock);
2030static LIST_HEAD(nfs_access_lru_list);
2031static atomic_long_t nfs_access_nr_entries;
2032
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002033static void nfs_access_free_entry(struct nfs_access_entry *entry)
2034{
2035 put_rpccred(entry->cred);
2036 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002037 smp_mb__before_atomic_dec();
2038 atomic_long_dec(&nfs_access_nr_entries);
2039 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002040}
2041
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002042static void nfs_access_free_list(struct list_head *head)
2043{
2044 struct nfs_access_entry *cache;
2045
2046 while (!list_empty(head)) {
2047 cache = list_entry(head->next, struct nfs_access_entry, lru);
2048 list_del(&cache->lru);
2049 nfs_access_free_entry(cache);
2050 }
2051}
2052
Ying Han1495f232011-05-24 17:12:27 -07002053int nfs_access_cache_shrinker(struct shrinker *shrink,
2054 struct shrink_control *sc)
Trond Myklebust979df722006-07-25 11:28:19 -04002055{
2056 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002057 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04002058 struct nfs_access_entry *cache;
Ying Han1495f232011-05-24 17:12:27 -07002059 int nr_to_scan = sc->nr_to_scan;
2060 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebust979df722006-07-25 11:28:19 -04002061
Trond Myklebust61d5eb22010-05-13 12:51:06 -04002062 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2063 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002064
Trond Myklebusta50f7952007-06-05 19:23:43 -04002065 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002066 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04002067 struct inode *inode;
2068
2069 if (nr_to_scan-- == 0)
2070 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002071 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04002072 spin_lock(&inode->i_lock);
2073 if (list_empty(&nfsi->access_cache_entry_lru))
2074 goto remove_lru_entry;
2075 cache = list_entry(nfsi->access_cache_entry_lru.next,
2076 struct nfs_access_entry, lru);
2077 list_move(&cache->lru, &head);
2078 rb_erase(&cache->rb_node, &nfsi->access_cache);
2079 if (!list_empty(&nfsi->access_cache_entry_lru))
2080 list_move_tail(&nfsi->access_cache_inode_lru,
2081 &nfs_access_lru_list);
2082 else {
2083remove_lru_entry:
2084 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002085 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04002086 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002087 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04002088 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002089 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002090 }
2091 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002092 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04002093 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2094}
2095
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002096static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002097{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002098 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002099 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002100 struct nfs_access_entry *entry;
2101
2102 /* Unhook entries from the cache */
2103 while ((n = rb_first(root_node)) != NULL) {
2104 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2105 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002106 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002107 }
2108 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002109}
2110
2111void nfs_access_zap_cache(struct inode *inode)
2112{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002113 LIST_HEAD(head);
2114
2115 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2116 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002117 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002118 spin_lock(&nfs_access_lru_lock);
2119 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002120 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002121
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002122 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002123 __nfs_access_zap_cache(NFS_I(inode), &head);
2124 spin_unlock(&inode->i_lock);
2125 spin_unlock(&nfs_access_lru_lock);
2126 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002127}
2128
2129static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2130{
2131 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2132 struct nfs_access_entry *entry;
2133
2134 while (n != NULL) {
2135 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2136
2137 if (cred < entry->cred)
2138 n = n->rb_left;
2139 else if (cred > entry->cred)
2140 n = n->rb_right;
2141 else
2142 return entry;
2143 }
2144 return NULL;
2145}
2146
Trond Myklebustaf22f942007-08-10 17:45:10 -04002147static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Chuck Lever55296802005-08-18 11:24:09 -07002149 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002150 struct nfs_access_entry *cache;
2151 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002153 spin_lock(&inode->i_lock);
2154 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2155 goto out_zap;
2156 cache = nfs_access_search_rbtree(inode, cred);
2157 if (cache == NULL)
2158 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05002159 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05002160 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002161 goto out_stale;
2162 res->jiffies = cache->jiffies;
2163 res->cred = cache->cred;
2164 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002165 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002166 err = 0;
2167out:
2168 spin_unlock(&inode->i_lock);
2169 return err;
2170out_stale:
2171 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002172 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002173 spin_unlock(&inode->i_lock);
2174 nfs_access_free_entry(cache);
2175 return -ENOENT;
2176out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002177 spin_unlock(&inode->i_lock);
2178 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002179 return -ENOENT;
2180}
2181
2182static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2183{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002184 struct nfs_inode *nfsi = NFS_I(inode);
2185 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002186 struct rb_node **p = &root_node->rb_node;
2187 struct rb_node *parent = NULL;
2188 struct nfs_access_entry *entry;
2189
2190 spin_lock(&inode->i_lock);
2191 while (*p != NULL) {
2192 parent = *p;
2193 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2194
2195 if (set->cred < entry->cred)
2196 p = &parent->rb_left;
2197 else if (set->cred > entry->cred)
2198 p = &parent->rb_right;
2199 else
2200 goto found;
2201 }
2202 rb_link_node(&set->rb_node, parent, p);
2203 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002204 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002205 spin_unlock(&inode->i_lock);
2206 return;
2207found:
2208 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002209 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2210 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002211 spin_unlock(&inode->i_lock);
2212 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Trond Myklebustaf22f942007-08-10 17:45:10 -04002215static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002217 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2218 if (cache == NULL)
2219 return;
2220 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002222 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002224
2225 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002226
2227 /* Update accounting */
2228 smp_mb__before_atomic_inc();
2229 atomic_long_inc(&nfs_access_nr_entries);
2230 smp_mb__after_atomic_inc();
2231
2232 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002233 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002234 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002235 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2236 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2237 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002238 spin_unlock(&nfs_access_lru_lock);
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2243{
2244 struct nfs_access_entry cache;
2245 int status;
2246
2247 status = nfs_access_get_cached(inode, cred, &cache);
2248 if (status == 0)
2249 goto out;
2250
2251 /* Be clever: ask server to check for all possible rights */
2252 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2253 cache.cred = cred;
2254 cache.jiffies = jiffies;
2255 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002256 if (status != 0) {
2257 if (status == -ESTALE) {
2258 nfs_zap_caches(inode);
2259 if (!S_ISDIR(inode->i_mode))
2260 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 nfs_access_add_cache(inode, &cache);
2265out:
Al Viroe6305c42008-07-15 21:03:57 -04002266 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
2268 return -EACCES;
2269}
2270
Trond Myklebustaf22f942007-08-10 17:45:10 -04002271static int nfs_open_permission_mask(int openflags)
2272{
2273 int mask = 0;
2274
Al Viro8a5e9292011-06-25 19:15:54 -04002275 if ((openflags & O_ACCMODE) != O_WRONLY)
Trond Myklebustaf22f942007-08-10 17:45:10 -04002276 mask |= MAY_READ;
Al Viro8a5e9292011-06-25 19:15:54 -04002277 if ((openflags & O_ACCMODE) != O_RDONLY)
Trond Myklebustaf22f942007-08-10 17:45:10 -04002278 mask |= MAY_WRITE;
Al Viro8a5e9292011-06-25 19:15:54 -04002279 if (openflags & __FMODE_EXEC)
Trond Myklebustaf22f942007-08-10 17:45:10 -04002280 mask |= MAY_EXEC;
2281 return mask;
2282}
2283
2284int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2285{
2286 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2287}
2288
Al Viro10556cb22011-06-20 19:28:19 -04002289int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 struct rpc_cred *cred;
2292 int res = 0;
2293
Al Viro10556cb22011-06-20 19:28:19 -04002294 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +11002295 return -ECHILD;
2296
Chuck Lever91d5b472006-03-20 13:44:14 -05002297 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2298
Al Viroe6305c42008-07-15 21:03:57 -04002299 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto out;
2301 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002302 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto force_lookup;
2304
2305 switch (inode->i_mode & S_IFMT) {
2306 case S_IFLNK:
2307 goto out;
2308 case S_IFREG:
2309 /* NFSv4 has atomic_open... */
2310 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002311 && (mask & MAY_OPEN)
2312 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 goto out;
2314 break;
2315 case S_IFDIR:
2316 /*
2317 * Optimize away all write operations, since the server
2318 * will check permissions when we perform the op.
2319 */
2320 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2321 goto out;
2322 }
2323
2324force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (!NFS_PROTO(inode)->access)
2326 goto out_notsup;
2327
Trond Myklebust98a8e322008-03-12 12:25:28 -04002328 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (!IS_ERR(cred)) {
2330 res = nfs_do_access(inode, cred, mask);
2331 put_rpccred(cred);
2332 } else
2333 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002335 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2336 res = -EACCES;
2337
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002338 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2339 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return res;
2341out_notsup:
2342 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2343 if (res == 0)
Al Viro2830ba72011-06-20 19:16:29 -04002344 res = generic_permission(inode, mask);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348/*
2349 * Local variables:
2350 * version-control: t
2351 * kept-new-versions: 5
2352 * End:
2353 */