Blame view

fs/nfs/dir.c 64 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   *  linux/fs/nfs/dir.c
   *
   *  Copyright (C) 1992  Rick Sladkey
   *
   *  nfs directory handling functions
   *
   * 10 Apr 1996	Added silly rename for unlink	--okir
   * 28 Sep 1996	Improved directory cache --okir
   * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de 
   *              Re-implemented silly rename for unlink, newly implemented
   *              silly rename for nfs_rename() following the suggestions
   *              of Olaf Kirch (okir) found in this file.
   *              Following Linus comments on my original hack, this version
   *              depends only on the dcache stuff and doesn't touch the inode
   *              layer (iput() and friends).
   *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
   */
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
19
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
27
28
29
30
31
  #include <linux/time.h>
  #include <linux/errno.h>
  #include <linux/stat.h>
  #include <linux/fcntl.h>
  #include <linux/string.h>
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/mm.h>
  #include <linux/sunrpc/clnt.h>
  #include <linux/nfs_fs.h>
  #include <linux/nfs_mount.h>
  #include <linux/pagemap.h>
873101b33   Chuck Lever   NFS: copy symlink...
32
  #include <linux/pagevec.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  #include <linux/namei.h>
54ceac451   David Howells   NFS: Share NFS su...
34
  #include <linux/mount.h>
a0b8cab3b   Mel Gorman   mm: remove lru pa...
35
  #include <linux/swap.h>
e8edc6e03   Alexey Dobriyan   Detach sched.h fr...
36
  #include <linux/sched.h>
04e4bd1c6   Catalin Marinas   nfs: Ignore kmeml...
37
  #include <linux/kmemleak.h>
64c2ce8b7   Aneesh Kumar K.V   nfsv4: Switch to ...
38
  #include <linux/xattr.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  
  #include "delegation.h"
91d5b4702   Chuck Lever   NFS: add I/O perf...
41
  #include "iostat.h"
4c30d56ed   Adrian Bunk   NFS: fs/nfs/dir.c...
42
  #include "internal.h"
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
43
  #include "fscache.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44

f4ce1299b   Trond Myklebust   NFS: Add event tr...
45
  #include "nfstrace.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
  /* #define NFS_DEBUG_VERBOSE 1 */
  
  static int nfs_opendir(struct inode *, struct file *);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
49
  static int nfs_closedir(struct inode *, struct file *);
23db86206   Al Viro   [readdir] convert...
50
  static int nfs_readdir(struct file *, struct dir_context *);
02c24a821   Josef Bacik   fs: push i_mutex ...
51
  static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
52
  static loff_t nfs_llseek_dir(struct file *, loff_t, int);
11de3b11e   Trond Myklebust   NFS: Fix a memory...
53
  static void nfs_readdir_clear_array(struct page*);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54

4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
55
  const struct file_operations nfs_dir_operations = {
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
56
  	.llseek		= nfs_llseek_dir,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  	.read		= generic_read_dir,
9ac3d3e84   Al Viro   nfs: switch to ->...
58
  	.iterate_shared	= nfs_readdir,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  	.open		= nfs_opendir,
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
60
  	.release	= nfs_closedir,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
  	.fsync		= nfs_fsync_dir,
  };
11de3b11e   Trond Myklebust   NFS: Fix a memory...
63
64
  const struct address_space_operations nfs_dir_aops = {
  	.freepage = nfs_readdir_clear_array,
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
65
  };
0c0308066   Trond Myklebust   NFS: Fix spurious...
66
  static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
67
  {
311324ad1   Trond Myklebust   NFS: Be more aggr...
68
  	struct nfs_inode *nfsi = NFS_I(dir);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
69
70
71
  	struct nfs_open_dir_context *ctx;
  	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  	if (ctx != NULL) {
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
72
  		ctx->duped = 0;
311324ad1   Trond Myklebust   NFS: Be more aggr...
73
  		ctx->attr_gencount = nfsi->attr_gencount;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
74
  		ctx->dir_cookie = 0;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
75
  		ctx->dup_cookie = 0;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
76
  		ctx->cred = get_rpccred(cred);
311324ad1   Trond Myklebust   NFS: Be more aggr...
77
78
79
  		spin_lock(&dir->i_lock);
  		list_add(&ctx->list, &nfsi->open_files);
  		spin_unlock(&dir->i_lock);
0c0308066   Trond Myklebust   NFS: Fix spurious...
80
81
82
  		return ctx;
  	}
  	return  ERR_PTR(-ENOMEM);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
83
  }
311324ad1   Trond Myklebust   NFS: Be more aggr...
84
  static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
85
  {
311324ad1   Trond Myklebust   NFS: Be more aggr...
86
87
88
  	spin_lock(&dir->i_lock);
  	list_del(&ctx->list);
  	spin_unlock(&dir->i_lock);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
89
90
91
  	put_rpccred(ctx->cred);
  	kfree(ctx);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
96
97
  /*
   * Open file
   */
  static int
  nfs_opendir(struct inode *inode, struct file *filp)
  {
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
98
99
100
  	int res = 0;
  	struct nfs_open_dir_context *ctx;
  	struct rpc_cred *cred;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101

6de1472f1   Al Viro   nfs: use %p[dD] i...
102
103
  	dfprintk(FILE, "NFS: open dir(%pD2)
  ", filp);
cc0dd2d10   Chuck Lever   NFS: Make nfs_ope...
104
105
  
  	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
106

480c2006e   Bryan Schumaker   NFS: Create nfs_o...
107
108
109
  	cred = rpc_lookup_cred();
  	if (IS_ERR(cred))
  		return PTR_ERR(cred);
0c0308066   Trond Myklebust   NFS: Fix spurious...
110
  	ctx = alloc_nfs_open_dir_context(inode, cred);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
111
112
113
114
115
  	if (IS_ERR(ctx)) {
  		res = PTR_ERR(ctx);
  		goto out;
  	}
  	filp->private_data = ctx;
f5a73672d   Neil Brown   NFS: allow close-...
116
117
118
119
120
121
122
  	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
  		/* This is a mountpoint, so d_revalidate will never
  		 * have been called, so we need to refresh the
  		 * inode (for close-open consistency) ourselves.
  		 */
  		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
  	}
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
123
124
  out:
  	put_rpccred(cred);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
  	return res;
  }
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
127
128
129
  static int
  nfs_closedir(struct inode *inode, struct file *filp)
  {
a455589f1   Al Viro   assorted conversi...
130
  	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
131
132
  	return 0;
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
133
134
135
136
  struct nfs_cache_array_entry {
  	u64 cookie;
  	u64 ino;
  	struct qstr string;
0b26a0bf6   Trond Myklebust   NFS: Ensure we re...
137
  	unsigned char d_type;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
138
139
140
  };
  
  struct nfs_cache_array {
9ac3d3e84   Al Viro   nfs: switch to ->...
141
  	atomic_t refcount;
88b8e133c   Chuck Lever   NFS: Make nfs_cac...
142
  	int size;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
143
144
145
146
  	int eof_index;
  	u64 last_cookie;
  	struct nfs_cache_array_entry array[0];
  };
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
147
  typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
  typedef struct {
  	struct file	*file;
  	struct page	*page;
23db86206   Al Viro   [readdir] convert...
151
  	struct dir_context *ctx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  	unsigned long	page_index;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
153
  	u64		*dir_cookie;
0aded708d   Trond Myklebust   NFS: Ensure we us...
154
  	u64		last_cookie;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
155
  	loff_t		current_index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
  	decode_dirent_t	decode;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
157

1f4eab7e7   Neil Brown   NFS: Set meaningf...
158
  	unsigned long	timestamp;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
159
  	unsigned long	gencount;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
160
161
162
  	unsigned int	cache_entry_index;
  	unsigned int	plus:1;
  	unsigned int	eof:1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
  } nfs_readdir_descriptor_t;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
164
165
  /*
   * The caller is responsible for calling nfs_readdir_release_array(page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
   */
  static
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
168
169
  struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
  {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
170
  	void *ptr;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
171
172
  	if (page == NULL)
  		return ERR_PTR(-EIO);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
173
174
175
176
  	ptr = kmap(page);
  	if (ptr == NULL)
  		return ERR_PTR(-ENOMEM);
  	return ptr;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
177
178
179
180
181
182
183
184
185
186
187
188
  }
  
  static
  void nfs_readdir_release_array(struct page *page)
  {
  	kunmap(page);
  }
  
  /*
   * we are freeing strings created by nfs_add_to_readdir_array()
   */
  static
11de3b11e   Trond Myklebust   NFS: Fix a memory...
189
  void nfs_readdir_clear_array(struct page *page)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
190
  {
11de3b11e   Trond Myklebust   NFS: Fix a memory...
191
  	struct nfs_cache_array *array;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
192
  	int i;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
193

2b86ce2db   Cong Wang   nfs: remove the s...
194
  	array = kmap_atomic(page);
9ac3d3e84   Al Viro   nfs: switch to ->...
195
196
197
  	if (atomic_dec_and_test(&array->refcount))
  		for (i = 0; i < array->size; i++)
  			kfree(array->array[i].string.name);
2b86ce2db   Cong Wang   nfs: remove the s...
198
  	kunmap_atomic(array);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
199
  }
9ac3d3e84   Al Viro   nfs: switch to ->...
200
201
202
203
204
205
206
  static bool grab_page(struct page *page)
  {
  	struct nfs_cache_array *array = kmap_atomic(page);
  	bool res = atomic_inc_not_zero(&array->refcount);
  	kunmap_atomic(array);
  	return res;
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
207
208
209
210
211
212
  /*
   * the caller is responsible for freeing qstr.name
   * when called by nfs_readdir_add_to_array, the strings will be freed in
   * nfs_clear_readdir_array()
   */
  static
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
213
  int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
214
215
216
  {
  	string->len = len;
  	string->name = kmemdup(name, len, GFP_KERNEL);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
217
218
  	if (string->name == NULL)
  		return -ENOMEM;
04e4bd1c6   Catalin Marinas   nfs: Ignore kmeml...
219
220
221
222
223
  	/*
  	 * Avoid a kmemleak false positive. The pointer to the name is stored
  	 * in a page cache page which kmemleak does not scan.
  	 */
  	kmemleak_not_leak(string->name);
8387ff257   Linus Torvalds   vfs: make the str...
224
  	string->hash = full_name_hash(NULL, name, len);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
225
  	return 0;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
226
227
228
229
230
231
  }
  
  static
  int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  {
  	struct nfs_cache_array *array = nfs_readdir_get_array(page);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
232
233
  	struct nfs_cache_array_entry *cache_entry;
  	int ret;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
234
235
  	if (IS_ERR(array))
  		return PTR_ERR(array);
3020093f5   Trond Myklebust   NFS: Correct the ...
236
237
238
239
  
  	cache_entry = &array->array[array->size];
  
  	/* Check that this entry lies within the page bounds */
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
240
  	ret = -ENOSPC;
3020093f5   Trond Myklebust   NFS: Correct the ...
241
  	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
242
  		goto out;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
243

4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
244
245
  	cache_entry->cookie = entry->prev_cookie;
  	cache_entry->ino = entry->ino;
0b26a0bf6   Trond Myklebust   NFS: Ensure we re...
246
  	cache_entry->d_type = entry->d_type;
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
247
248
249
  	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  	if (ret)
  		goto out;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
250
  	array->last_cookie = entry->cookie;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
251
  	array->size++;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
252
  	if (entry->eof != 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
253
  		array->eof_index = array->size;
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
254
  out:
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
255
  	nfs_readdir_release_array(page);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
256
  	return ret;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
257
258
259
260
261
  }
  
  static
  int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  {
23db86206   Al Viro   [readdir] convert...
262
  	loff_t diff = desc->ctx->pos - desc->current_index;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
263
264
265
266
267
  	unsigned int index;
  
  	if (diff < 0)
  		goto out_eof;
  	if (diff >= array->size) {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
268
  		if (array->eof_index >= 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
269
  			goto out_eof;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
270
271
272
273
274
275
  		return -EAGAIN;
  	}
  
  	index = (unsigned int)diff;
  	*desc->dir_cookie = array->array[index].cookie;
  	desc->cache_entry_index = index;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
276
277
278
279
280
  	return 0;
  out_eof:
  	desc->eof = 1;
  	return -EBADCOOKIE;
  }
4db72b40f   Jeff Layton   nfs: add memory b...
281
282
283
284
285
286
287
288
  static bool
  nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
  {
  	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
  		return false;
  	smp_rmb();
  	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
289
290
291
292
  static
  int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  {
  	int i;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
293
  	loff_t new_pos;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
294
295
296
  	int status = -EAGAIN;
  
  	for (i = 0; i < array->size; i++) {
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
297
  		if (array->array[i].cookie == *desc->dir_cookie) {
496ad9aa8   Al Viro   new helper: file_...
298
  			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
0c0308066   Trond Myklebust   NFS: Fix spurious...
299
  			struct nfs_open_dir_context *ctx = desc->file->private_data;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
300
  			new_pos = desc->current_index + i;
4db72b40f   Jeff Layton   nfs: add memory b...
301
302
  			if (ctx->attr_gencount != nfsi->attr_gencount ||
  			    !nfs_readdir_inode_mapping_valid(nfsi)) {
0c0308066   Trond Myklebust   NFS: Fix spurious...
303
304
  				ctx->duped = 0;
  				ctx->attr_gencount = nfsi->attr_gencount;
23db86206   Al Viro   [readdir] convert...
305
  			} else if (new_pos < desc->ctx->pos) {
0c0308066   Trond Myklebust   NFS: Fix spurious...
306
307
308
  				if (ctx->duped > 0
  				    && ctx->dup_cookie == *desc->dir_cookie) {
  					if (printk_ratelimit()) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
309
  						pr_notice("NFS: directory %pD2 contains a readdir loop."
0c0308066   Trond Myklebust   NFS: Fix spurious...
310
  								"Please contact your server vendor.  "
9581a4ae7   Jeff Layton   nfs: pass string ...
311
312
313
314
  								"The file: %.*s has duplicate cookie %llu
  ",
  								desc->file, array->array[i].string.len,
  								array->array[i].string.name, *desc->dir_cookie);
0c0308066   Trond Myklebust   NFS: Fix spurious...
315
316
317
318
  					}
  					status = -ELOOP;
  					goto out;
  				}
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
319
  				ctx->dup_cookie = *desc->dir_cookie;
0c0308066   Trond Myklebust   NFS: Fix spurious...
320
  				ctx->duped = -1;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
321
  			}
23db86206   Al Viro   [readdir] convert...
322
  			desc->ctx->pos = new_pos;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
323
  			desc->cache_entry_index = i;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
324
  			return 0;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
325
326
  		}
  	}
47c716cbf   Trond Myklebust   NFS: Readdir clea...
327
  	if (array->eof_index >= 0) {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
328
  		status = -EBADCOOKIE;
18fb5fe40   Trond Myklebust   NFS: nfs_readdir_...
329
330
  		if (*desc->dir_cookie == array->last_cookie)
  			desc->eof = 1;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
331
  	}
0c0308066   Trond Myklebust   NFS: Fix spurious...
332
  out:
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
333
334
335
336
337
338
339
  	return status;
  }
  
  static
  int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  {
  	struct nfs_cache_array *array;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
340
  	int status;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
341
342
343
344
345
346
347
348
349
350
351
  
  	array = nfs_readdir_get_array(desc->page);
  	if (IS_ERR(array)) {
  		status = PTR_ERR(array);
  		goto out;
  	}
  
  	if (*desc->dir_cookie == 0)
  		status = nfs_readdir_search_for_pos(array, desc);
  	else
  		status = nfs_readdir_search_for_cookie(array, desc);
47c716cbf   Trond Myklebust   NFS: Readdir clea...
352
  	if (status == -EAGAIN) {
0aded708d   Trond Myklebust   NFS: Ensure we us...
353
  		desc->last_cookie = array->last_cookie;
e47c085af   Trond Myklebust   NFS: Ensure that ...
354
  		desc->current_index += array->size;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
355
356
  		desc->page_index++;
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
357
358
359
360
361
362
363
  	nfs_readdir_release_array(desc->page);
  out:
  	return status;
  }
  
  /* Fill a page with xdr information before transferring to the cache page */
  static
56e4ebf87   Bryan Schumaker   NFS: readdir with...
364
  int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
365
  			struct nfs_entry *entry, struct file *file, struct inode *inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
  {
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
367
368
  	struct nfs_open_dir_context *ctx = file->private_data;
  	struct rpc_cred	*cred = ctx->cred;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
369
  	unsigned long	timestamp, gencount;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
  	int		error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
372
   again:
  	timestamp = jiffies;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
373
  	gencount = nfs_inc_attr_generation_counter();
be62a1a8f   Miklos Szeredi   nfs: use file_den...
374
  	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
377
378
379
  					  NFS_SERVER(inode)->dtsize, desc->plus);
  	if (error < 0) {
  		/* We requested READDIRPLUS, but the server doesn't grok it */
  		if (error == -ENOTSUPP && desc->plus) {
  			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3a10c30ac   Benny Halevy   nfs: obliterate N...
380
  			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
  			desc->plus = 0;
  			goto again;
  		}
  		goto error;
  	}
1f4eab7e7   Neil Brown   NFS: Set meaningf...
386
  	desc->timestamp = timestamp;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
387
  	desc->gencount = gencount;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
388
389
  error:
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
  }
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
391
392
  static int xdr_decode(nfs_readdir_descriptor_t *desc,
  		      struct nfs_entry *entry, struct xdr_stream *xdr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
  {
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
394
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395

573c4e1ef   Chuck Lever   NFS: Simplify ->d...
396
397
398
  	error = desc->decode(xdr, entry, desc->plus);
  	if (error)
  		return error;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
399
400
401
  	entry->fattr->time_start = desc->timestamp;
  	entry->fattr->gencount = desc->gencount;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
  }
fa9233699   Trond Myklebust   NFS: Don't requir...
403
404
405
  /* Match file and dirent using either filehandle or fileid
   * Note: caller is responsible for checking the fsid
   */
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
406
407
408
  static
  int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  {
d8fdb47fa   Trond Myklebust   NFS: Don't let re...
409
  	struct inode *inode;
fa9233699   Trond Myklebust   NFS: Don't requir...
410
  	struct nfs_inode *nfsi;
2b0143b5c   David Howells   VFS: normal files...
411
412
  	if (d_really_is_negative(dentry))
  		return 0;
fa9233699   Trond Myklebust   NFS: Don't requir...
413

d8fdb47fa   Trond Myklebust   NFS: Don't let re...
414
415
416
417
418
  	inode = d_inode(dentry);
  	if (is_bad_inode(inode) || NFS_STALE(inode))
  		return 0;
  
  	nfsi = NFS_I(inode);
7dc72d5f7   Trond Myklebust   NFS: Fix inode co...
419
420
421
422
423
  	if (entry->fattr->fileid != nfsi->fileid)
  		return 0;
  	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
  		return 0;
  	return 1;
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
424
425
426
  }
  
  static
23db86206   Al Viro   [readdir] convert...
427
  bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
428
429
430
431
432
  {
  	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
  		return false;
  	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
  		return true;
23db86206   Al Viro   [readdir] convert...
433
  	if (ctx->pos == 0)
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  		return true;
  	return false;
  }
  
  /*
   * This function is called by the lookup code to request the use of
   * readdirplus to accelerate any future lookups in the same
   * directory.
   */
  static
  void nfs_advise_use_readdirplus(struct inode *dir)
  {
  	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
  }
311324ad1   Trond Myklebust   NFS: Be more aggr...
448
449
450
451
452
453
454
455
456
457
458
459
  /*
   * This function is mainly for use by nfs_getattr().
   *
   * If this is an 'ls -l', we want to force use of readdirplus.
   * Do this by checking if there is an active file descriptor
   * and calling nfs_advise_use_readdirplus, then forcing a
   * cache flush.
   */
  void nfs_force_use_readdirplus(struct inode *dir)
  {
  	if (!list_empty(&NFS_I(dir)->open_files)) {
  		nfs_advise_use_readdirplus(dir);
e331f2f2b   Trond Myklebust   NFS: Fix a perfor...
460
  		invalidate_mapping_pages(dir->i_mapping, 0, -1);
311324ad1   Trond Myklebust   NFS: Be more aggr...
461
462
  	}
  }
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
463
  static
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
464
465
  void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  {
26fe57502   Linus Torvalds   vfs: make it poss...
466
  	struct qstr filename = QSTR_INIT(entry->name, entry->len);
9ac3d3e84   Al Viro   nfs: switch to ->...
467
  	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
468
469
  	struct dentry *dentry;
  	struct dentry *alias;
2b0143b5c   David Howells   VFS: normal files...
470
  	struct inode *dir = d_inode(parent);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
471
  	struct inode *inode;
aa9c26696   David Quigley   NFS: Client imple...
472
  	int status;
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
473

fa9233699   Trond Myklebust   NFS: Don't requir...
474
475
  	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
  		return;
6c441c254   Trond Myklebust   NFS: Don't invali...
476
477
  	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
  		return;
78d04af49   Trond Myklebust   NFS: nfs_prime_dc...
478
479
480
481
482
483
484
485
  	if (filename.len == 0)
  		return;
  	/* Validate that the name doesn't contain any illegal '\0' */
  	if (strnlen(filename.name, filename.len) != filename.len)
  		return;
  	/* ...or '/' */
  	if (strnchr(filename.name, filename.len, '/'))
  		return;
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
486
487
488
489
490
491
  	if (filename.name[0] == '.') {
  		if (filename.len == 1)
  			return;
  		if (filename.len == 2 && filename.name[1] == '.')
  			return;
  	}
8387ff257   Linus Torvalds   vfs: make the str...
492
  	filename.hash = full_name_hash(parent, filename.name, filename.len);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
493

4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
494
  	dentry = d_lookup(parent, &filename);
9ac3d3e84   Al Viro   nfs: switch to ->...
495
496
497
498
499
500
501
  again:
  	if (!dentry) {
  		dentry = d_alloc_parallel(parent, &filename, &wq);
  		if (IS_ERR(dentry))
  			return;
  	}
  	if (!d_in_lookup(dentry)) {
6c441c254   Trond Myklebust   NFS: Don't invali...
502
503
504
505
  		/* Is there a mountpoint here? If so, just exit */
  		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
  					&entry->fattr->fsid))
  			goto out;
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
506
  		if (nfs_same_file(dentry, entry)) {
7dc72d5f7   Trond Myklebust   NFS: Fix inode co...
507
508
  			if (!entry->fh->size)
  				goto out;
cda57a1ef   Jeff Layton   nfs: set verifier...
509
  			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2b0143b5c   David Howells   VFS: normal files...
510
  			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
aa9c26696   David Quigley   NFS: Client imple...
511
  			if (!status)
2b0143b5c   David Howells   VFS: normal files...
512
  				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
513
514
  			goto out;
  		} else {
5542aa2fa   Eric W. Biederman   vfs: Make d_inval...
515
  			d_invalidate(dentry);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
516
  			dput(dentry);
9ac3d3e84   Al Viro   nfs: switch to ->...
517
518
  			dentry = NULL;
  			goto again;
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
519
520
  		}
  	}
7dc72d5f7   Trond Myklebust   NFS: Fix inode co...
521
522
523
524
  	if (!entry->fh->size) {
  		d_lookup_done(dentry);
  		goto out;
  	}
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
525

1775fd3e8   David Quigley   NFS:Add labels to...
526
  	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
41d28bca2   Al Viro   switch d_material...
527
  	alias = d_splice_alias(inode, dentry);
9ac3d3e84   Al Viro   nfs: switch to ->...
528
529
530
531
532
533
534
535
  	d_lookup_done(dentry);
  	if (alias) {
  		if (IS_ERR(alias))
  			goto out;
  		dput(dentry);
  		dentry = alias;
  	}
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
536
537
  out:
  	dput(dentry);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
538
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
539
540
  /* Perform conversion from xdr to cache array */
  static
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
541
  int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
6650239a4   Trond Myklebust   NFS: Don't use vm...
542
  				struct page **xdr_pages, struct page *page, unsigned int buflen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
  {
babddc72a   Bryan Schumaker   NFS: decode_diren...
544
  	struct xdr_stream stream;
f7da7a129   Benny Halevy   SUNRPC: introduce...
545
  	struct xdr_buf buf;
6650239a4   Trond Myklebust   NFS: Don't use vm...
546
  	struct page *scratch;
994243808   Bryan Schumaker   NFS: check xdr_de...
547
  	struct nfs_cache_array *array;
5c346854d   Trond Myklebust   NFS: Assume eof i...
548
549
  	unsigned int count = 0;
  	int status;
babddc72a   Bryan Schumaker   NFS: decode_diren...
550

6650239a4   Trond Myklebust   NFS: Don't use vm...
551
552
553
  	scratch = alloc_page(GFP_KERNEL);
  	if (scratch == NULL)
  		return -ENOMEM;
babddc72a   Bryan Schumaker   NFS: decode_diren...
554

ce85cfbed   Benjamin Coddington   NFS: Don't attemp...
555
556
  	if (buflen == 0)
  		goto out_nopages;
f7da7a129   Benny Halevy   SUNRPC: introduce...
557
  	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
6650239a4   Trond Myklebust   NFS: Don't use vm...
558
  	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
994243808   Bryan Schumaker   NFS: check xdr_de...
559
560
561
  
  	do {
  		status = xdr_decode(desc, entry, &stream);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
562
563
564
  		if (status != 0) {
  			if (status == -EAGAIN)
  				status = 0;
994243808   Bryan Schumaker   NFS: check xdr_de...
565
  			break;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
566
  		}
994243808   Bryan Schumaker   NFS: check xdr_de...
567

5c346854d   Trond Myklebust   NFS: Assume eof i...
568
  		count++;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
569
  		if (desc->plus != 0)
be62a1a8f   Miklos Szeredi   nfs: use file_den...
570
  			nfs_prime_dcache(file_dentry(desc->file), entry);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
571
572
573
574
  
  		status = nfs_readdir_add_to_array(entry, page);
  		if (status != 0)
  			break;
994243808   Bryan Schumaker   NFS: check xdr_de...
575
  	} while (!entry->eof);
ce85cfbed   Benjamin Coddington   NFS: Don't attemp...
576
  out_nopages:
47c716cbf   Trond Myklebust   NFS: Readdir clea...
577
  	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
994243808   Bryan Schumaker   NFS: check xdr_de...
578
  		array = nfs_readdir_get_array(page);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
579
580
581
582
  		if (!IS_ERR(array)) {
  			array->eof_index = array->size;
  			status = 0;
  			nfs_readdir_release_array(page);
5c346854d   Trond Myklebust   NFS: Assume eof i...
583
584
  		} else
  			status = PTR_ERR(array);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
  	}
6650239a4   Trond Myklebust   NFS: Don't use vm...
586
587
  
  	put_page(scratch);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
588
  	return status;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
589
590
591
  }
  
  static
c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
592
  void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
56e4ebf87   Bryan Schumaker   NFS: readdir with...
593
594
595
596
597
  {
  	unsigned int i;
  	for (i = 0; i < npages; i++)
  		put_page(pages[i]);
  }
56e4ebf87   Bryan Schumaker   NFS: readdir with...
598
599
  /*
   * nfs_readdir_large_page will allocate pages that must be freed with a call
0b936e37d   Anna Schumaker   NFS: Remove unuse...
600
   * to nfs_readdir_free_pagearray
56e4ebf87   Bryan Schumaker   NFS: readdir with...
601
602
   */
  static
c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
603
  int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
56e4ebf87   Bryan Schumaker   NFS: readdir with...
604
  {
56e4ebf87   Bryan Schumaker   NFS: readdir with...
605
606
607
608
609
610
611
612
  	unsigned int i;
  
  	for (i = 0; i < npages; i++) {
  		struct page *page = alloc_page(GFP_KERNEL);
  		if (page == NULL)
  			goto out_freepages;
  		pages[i] = page;
  	}
6650239a4   Trond Myklebust   NFS: Don't use vm...
613
  	return 0;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
614

56e4ebf87   Bryan Schumaker   NFS: readdir with...
615
  out_freepages:
c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
616
  	nfs_readdir_free_pages(pages, i);
6650239a4   Trond Myklebust   NFS: Don't use vm...
617
  	return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
619
620
  static
  int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
621
  {
56e4ebf87   Bryan Schumaker   NFS: readdir with...
622
  	struct page *pages[NFS_MAX_READDIR_PAGES];
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
623
624
625
  	struct nfs_entry entry;
  	struct file	*file = desc->file;
  	struct nfs_cache_array *array;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
626
  	int status = -ENOMEM;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
627
  	unsigned int array_size = ARRAY_SIZE(pages);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
628
629
  
  	entry.prev_cookie = 0;
0aded708d   Trond Myklebust   NFS: Ensure we us...
630
  	entry.cookie = desc->last_cookie;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
631
632
633
  	entry.eof = 0;
  	entry.fh = nfs_alloc_fhandle();
  	entry.fattr = nfs_alloc_fattr();
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
634
  	entry.server = NFS_SERVER(inode);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
635
636
  	if (entry.fh == NULL || entry.fattr == NULL)
  		goto out;
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
637

14c43f767   David Quigley   NFS: Add label li...
638
639
640
641
642
  	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  	if (IS_ERR(entry.label)) {
  		status = PTR_ERR(entry.label);
  		goto out;
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
643
  	array = nfs_readdir_get_array(page);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
644
645
  	if (IS_ERR(array)) {
  		status = PTR_ERR(array);
14c43f767   David Quigley   NFS: Add label li...
646
  		goto out_label_free;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
647
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
648
  	memset(array, 0, sizeof(struct nfs_cache_array));
9ac3d3e84   Al Viro   nfs: switch to ->...
649
  	atomic_set(&array->refcount, 1);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
650
  	array->eof_index = -1;
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
651

c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
652
  	status = nfs_readdir_alloc_pages(pages, array_size);
6650239a4   Trond Myklebust   NFS: Don't use vm...
653
  	if (status < 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
654
655
  		goto out_release_array;
  	do {
ac3961282   Trond Myklebust   NFS: readdir shou...
656
  		unsigned int pglen;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
657
  		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
babddc72a   Bryan Schumaker   NFS: decode_diren...
658

d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
659
  		if (status < 0)
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
660
  			break;
ac3961282   Trond Myklebust   NFS: readdir shou...
661
  		pglen = status;
6650239a4   Trond Myklebust   NFS: Don't use vm...
662
  		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
663
664
665
666
667
668
  		if (status < 0) {
  			if (status == -ENOSPC)
  				status = 0;
  			break;
  		}
  	} while (array->eof_index < 0);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
669

c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
670
  	nfs_readdir_free_pages(pages, array_size);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
671
672
  out_release_array:
  	nfs_readdir_release_array(page);
14c43f767   David Quigley   NFS: Add label li...
673
674
  out_label_free:
  	nfs4_label_free(entry.label);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
675
676
677
  out:
  	nfs_free_fattr(entry.fattr);
  	nfs_free_fhandle(entry.fh);
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
678
679
680
681
  	return status;
  }
  
  /*
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
682
683
684
685
   * Now we cache directories properly, by converting xdr information
   * to an array that can be used for lookups later.  This results in
   * fewer cache pages, since we can store more information on each page.
   * We only need to convert from xdr once so future lookups are much simpler
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
   */
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
687
688
  static
  int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
  {
496ad9aa8   Al Viro   new helper: file_...
690
  	struct inode	*inode = file_inode(desc->file);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
691
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692

8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
693
694
  	ret = nfs_readdir_xdr_to_array(desc, page, inode);
  	if (ret < 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
695
696
  		goto error;
  	SetPageUptodate(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697

d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
698
699
700
  	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  		/* Should never happen */
  		nfs_zap_mapping(inode, inode->i_mapping);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
702
703
704
705
  	unlock_page(page);
  	return 0;
   error:
  	unlock_page(page);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
706
  	return ret;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
707
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708

d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
709
710
711
  static
  void cache_page_release(nfs_readdir_descriptor_t *desc)
  {
9ac3d3e84   Al Viro   nfs: switch to ->...
712
  	nfs_readdir_clear_array(desc->page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
713
  	put_page(desc->page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
714
715
716
717
718
719
  	desc->page = NULL;
  }
  
  static
  struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  {
9ac3d3e84   Al Viro   nfs: switch to ->...
720
721
722
  	struct page *page;
  
  	for (;;) {
93c76a3d4   Al Viro   file_inode(f)->i_...
723
  		page = read_cache_page(desc->file->f_mapping,
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
724
  			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
9ac3d3e84   Al Viro   nfs: switch to ->...
725
726
727
728
729
  		if (IS_ERR(page) || grab_page(page))
  			break;
  		put_page(page);
  	}
  	return page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
731
732
  }
  
  /*
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
733
   * Returns 0 if desc->dir_cookie was found on page desc->page_index
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
734
   */
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
735
736
737
738
739
740
741
742
743
744
  static
  int find_cache_page(nfs_readdir_descriptor_t *desc)
  {
  	int res;
  
  	desc->page = get_cache_page(desc);
  	if (IS_ERR(desc->page))
  		return PTR_ERR(desc->page);
  
  	res = nfs_readdir_search_array(desc);
47c716cbf   Trond Myklebust   NFS: Readdir clea...
745
746
  	if (res != 0)
  		cache_page_release(desc);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
747
748
749
750
  	return res;
  }
  
  /* Search for desc->dir_cookie from the beginning of the page cache */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
752
753
  static inline
  int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
754
  	int res;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
755

0aded708d   Trond Myklebust   NFS: Ensure we us...
756
  	if (desc->page_index == 0) {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
757
  		desc->current_index = 0;
0aded708d   Trond Myklebust   NFS: Ensure we us...
758
759
  		desc->last_cookie = 0;
  	}
47c716cbf   Trond Myklebust   NFS: Readdir clea...
760
  	do {
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
761
  		res = find_cache_page(desc);
47c716cbf   Trond Myklebust   NFS: Readdir clea...
762
  	} while (res == -EAGAIN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
763
764
  	return res;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
766
767
768
  /*
   * Once we've found the start of the dirent within a page: fill 'er up...
   */
  static 
23db86206   Al Viro   [readdir] convert...
769
  int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770
771
  {
  	struct file	*file = desc->file;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
772
773
774
  	int i = 0;
  	int res = 0;
  	struct nfs_cache_array *array = NULL;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
775
  	struct nfs_open_dir_context *ctx = file->private_data;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
776
  	array = nfs_readdir_get_array(desc->page);
e7c58e974   Trond Myklebust   NFS: Fix a page l...
777
778
779
780
  	if (IS_ERR(array)) {
  		res = PTR_ERR(array);
  		goto out;
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
781
782
  
  	for (i = desc->cache_entry_index; i < array->size; i++) {
ece0b4233   Trond Myklebust   NFS: Don't ignore...
783
  		struct nfs_cache_array_entry *ent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784

ece0b4233   Trond Myklebust   NFS: Don't ignore...
785
  		ent = &array->array[i];
23db86206   Al Viro   [readdir] convert...
786
787
  		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
  		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
ece0b4233   Trond Myklebust   NFS: Don't ignore...
788
  			desc->eof = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
  			break;
ece0b4233   Trond Myklebust   NFS: Don't ignore...
790
  		}
23db86206   Al Viro   [readdir] convert...
791
  		desc->ctx->pos++;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
792
793
794
795
  		if (i < (array->size-1))
  			*desc->dir_cookie = array->array[i+1].cookie;
  		else
  			*desc->dir_cookie = array->last_cookie;
0c0308066   Trond Myklebust   NFS: Fix spurious...
796
797
  		if (ctx->duped != 0)
  			ctx->duped = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
  	}
47c716cbf   Trond Myklebust   NFS: Readdir clea...
799
  	if (array->eof_index >= 0)
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
800
  		desc->eof = 1;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
801
802
  
  	nfs_readdir_release_array(desc->page);
e7c58e974   Trond Myklebust   NFS: Fix a page l...
803
  out:
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
804
  	cache_page_release(desc);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
805
806
807
  	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d
  ",
  			(unsigned long long)*desc->dir_cookie, res);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
  	return res;
  }
  
  /*
   * If we cannot find a cookie in our cache, we suspect that this is
   * because it points to a deleted file, so we ask the server to return
   * whatever it thinks is the next entry. We then feed this to filldir.
   * If all goes well, we should then be able to find our way round the
   * cache on the next call to readdir_search_pagecache();
   *
   * NOTE: we cannot add the anonymous page to the pagecache because
   *	 the data it contains might not be page aligned. Besides,
   *	 we should already have a complete representation of the
   *	 directory in the page cache by the time we get here.
   */
  static inline
23db86206   Al Viro   [readdir] convert...
824
  int uncached_readdir(nfs_readdir_descriptor_t *desc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
827
  	struct page	*page = NULL;
  	int		status;
496ad9aa8   Al Viro   new helper: file_...
828
  	struct inode *inode = file_inode(desc->file);
0c0308066   Trond Myklebust   NFS: Fix spurious...
829
  	struct nfs_open_dir_context *ctx = desc->file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830

1e7cb3dc1   Chuck Lever   NFS: directory tr...
831
832
833
  	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu
  ",
  			(unsigned long long)*desc->dir_cookie);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
835
836
837
838
839
  
  	page = alloc_page(GFP_HIGHUSER);
  	if (!page) {
  		status = -ENOMEM;
  		goto out;
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
840

7a8e1dc34   Trond Myklebust   NFS: Fix a page l...
841
  	desc->page_index = 0;
0aded708d   Trond Myklebust   NFS: Ensure we us...
842
  	desc->last_cookie = *desc->dir_cookie;
7a8e1dc34   Trond Myklebust   NFS: Fix a page l...
843
  	desc->page = page;
0c0308066   Trond Myklebust   NFS: Fix spurious...
844
  	ctx->duped = 0;
7a8e1dc34   Trond Myklebust   NFS: Fix a page l...
845

85f8607e1   Trond Myklebust   NFS: Fix the erro...
846
847
  	status = nfs_readdir_xdr_to_array(desc, page, inode);
  	if (status < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848
  		goto out_release;
23db86206   Al Viro   [readdir] convert...
849
  	status = nfs_do_filldir(desc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
   out:
1e7cb3dc1   Chuck Lever   NFS: directory tr...
852
853
  	dfprintk(DIRCACHE, "NFS: %s: returns %d
  ",
3110ff804   Harvey Harrison   nfs: replace rema...
854
  			__func__, status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
  	return status;
   out_release:
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
857
  	cache_page_release(desc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
859
  	goto out;
  }
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
860
861
862
  /* The file offset position represents the dirent entry number.  A
     last cookie cache takes care of the common case of reading the
     whole directory.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863
   */
23db86206   Al Viro   [readdir] convert...
864
  static int nfs_readdir(struct file *file, struct dir_context *ctx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
  {
be62a1a8f   Miklos Szeredi   nfs: use file_den...
866
  	struct dentry	*dentry = file_dentry(file);
2b0143b5c   David Howells   VFS: normal files...
867
  	struct inode	*inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
869
  	nfs_readdir_descriptor_t my_desc,
  			*desc = &my_desc;
23db86206   Al Viro   [readdir] convert...
870
  	struct nfs_open_dir_context *dir_ctx = file->private_data;
07b5ce8ef   Scott Mayhew   NFS: Make nfs_rea...
871
  	int res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
872

6de1472f1   Al Viro   nfs: use %p[dD] i...
873
874
875
  	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu
  ",
  			file, (long long)ctx->pos);
91d5b4702   Chuck Lever   NFS: add I/O perf...
876
  	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
877
  	/*
23db86206   Al Viro   [readdir] convert...
878
  	 * ctx->pos points to the dirent entry number.
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
879
  	 * *desc->dir_cookie has the cookie for the next entry. We have
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
880
881
  	 * to either find the entry with the appropriate number or
  	 * revalidate the cookie.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
  	 */
  	memset(desc, 0, sizeof(*desc));
23db86206   Al Viro   [readdir] convert...
884
885
  	desc->file = file;
  	desc->ctx = ctx;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
886
  	desc->dir_cookie = &dir_ctx->dir_cookie;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
  	desc->decode = NFS_PROTO(inode)->decode_dirent;
23db86206   Al Viro   [readdir] convert...
888
  	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889

e331f2f2b   Trond Myklebust   NFS: Fix a perfor...
890
  	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
07b5ce8ef   Scott Mayhew   NFS: Make nfs_rea...
891
  		res = nfs_revalidate_mapping(inode, file->f_mapping);
fccca7fc6   Trond Myklebust   NFS: Fix a sillyr...
892
893
  	if (res < 0)
  		goto out;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
894
  	do {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
  		res = readdir_search_pagecache(desc);
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
896

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
897
  		if (res == -EBADCOOKIE) {
ece0b4233   Trond Myklebust   NFS: Don't ignore...
898
  			res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
  			/* This means either end of directory */
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
900
  			if (*desc->dir_cookie && desc->eof == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
901
  				/* Or that the server has 'lost' a cookie */
23db86206   Al Viro   [readdir] convert...
902
  				res = uncached_readdir(desc);
ece0b4233   Trond Myklebust   NFS: Don't ignore...
903
  				if (res == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904
905
  					continue;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
907
908
  			break;
  		}
  		if (res == -ETOOSMALL && desc->plus) {
3a10c30ac   Benny Halevy   nfs: obliterate N...
909
  			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  			nfs_zap_caches(inode);
baf57a09e   Trond Myklebust   NFS: Optimise the...
911
  			desc->page_index = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
  			desc->plus = 0;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
913
  			desc->eof = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
915
916
917
  			continue;
  		}
  		if (res < 0)
  			break;
23db86206   Al Viro   [readdir] convert...
918
  		res = nfs_do_filldir(desc);
ece0b4233   Trond Myklebust   NFS: Don't ignore...
919
  		if (res < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
  			break;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
921
  	} while (!desc->eof);
fccca7fc6   Trond Myklebust   NFS: Fix a sillyr...
922
  out:
1e7cb3dc1   Chuck Lever   NFS: directory tr...
923
924
  	if (res > 0)
  		res = 0;
6de1472f1   Al Viro   nfs: use %p[dD] i...
925
926
  	dfprintk(FILE, "NFS: readdir(%pD2) returns %d
  ", file, res);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
927
  	return res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
  }
965c8e59c   Andrew Morton   lseek: the "whenc...
929
  static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
930
  {
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
931
  	struct nfs_open_dir_context *dir_ctx = filp->private_data;
b84e06c58   Chuck Lever   NFS: Make nfs_lls...
932

6de1472f1   Al Viro   nfs: use %p[dD] i...
933
934
935
  	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)
  ",
  			filp, offset, whence);
b84e06c58   Chuck Lever   NFS: Make nfs_lls...
936

965c8e59c   Andrew Morton   lseek: the "whenc...
937
  	switch (whence) {
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
938
939
940
941
942
943
  		case 1:
  			offset += filp->f_pos;
  		case 0:
  			if (offset >= 0)
  				break;
  		default:
9ac3d3e84   Al Viro   nfs: switch to ->...
944
  			return -EINVAL;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
945
946
947
  	}
  	if (offset != filp->f_pos) {
  		filp->f_pos = offset;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
948
  		dir_ctx->dir_cookie = 0;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
949
  		dir_ctx->duped = 0;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
950
  	}
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
951
952
  	return offset;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
953
954
955
956
  /*
   * All directory operations under NFS are synchronous, so fsync()
   * is a dummy operation.
   */
02c24a821   Josef Bacik   fs: push i_mutex ...
957
958
  static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
  			 int datasync)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
960
  	struct inode *inode = file_inode(filp);
7ea808591   Christoph Hellwig   drop unused dentr...
961

6de1472f1   Al Viro   nfs: use %p[dD] i...
962
963
  	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d
  ", filp, datasync);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
964

5955102c9   Al Viro   wrappers for ->i_...
965
  	inode_lock(inode);
6de1472f1   Al Viro   nfs: use %p[dD] i...
966
  	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
5955102c9   Al Viro   wrappers for ->i_...
967
  	inode_unlock(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
969
  	return 0;
  }
bfc69a456   Trond Myklebust   NFS: define a fun...
970
971
972
973
974
975
976
977
978
979
980
981
  /**
   * nfs_force_lookup_revalidate - Mark the directory as having changed
   * @dir - pointer to directory inode
   *
   * This forces the revalidation code in nfs_lookup_revalidate() to do a
   * full lookup on all child dentries of 'dir' whenever a change occurs
   * on the server that might have invalidated our dcache.
   *
   * The caller should be holding dir->i_lock
   */
  void nfs_force_lookup_revalidate(struct inode *dir)
  {
011935a0a   Trond Myklebust   NFS: Fix a resolu...
982
  	NFS_I(dir)->cache_change_attribute++;
bfc69a456   Trond Myklebust   NFS: define a fun...
983
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
984
  EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
bfc69a456   Trond Myklebust   NFS: define a fun...
985

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
987
988
989
  /*
   * A check for whether or not the parent directory has changed.
   * In the case it has, we assume that the dentries are untrustworthy
   * and may need to be looked up again.
912a108da   NeilBrown   NFS: teach nfs_ne...
990
   * If rcu_walk prevents us from performing a full check, return 0.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
991
   */
912a108da   NeilBrown   NFS: teach nfs_ne...
992
993
  static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
  			      int rcu_walk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
994
  {
912a108da   NeilBrown   NFS: teach nfs_ne...
995
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
997
  	if (IS_ROOT(dentry))
  		return 1;
4eec952e4   Trond Myklebust   NFS: Add options ...
998
999
  	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  		return 0;
f2c77f4e6   Trond Myklebust   NFS: Optimise nfs...
1000
1001
1002
  	if (!nfs_verify_change_attribute(dir, dentry->d_time))
  		return 0;
  	/* Revalidate nfsi->cache_change_attribute before we declare a match */
912a108da   NeilBrown   NFS: teach nfs_ne...
1003
1004
1005
1006
1007
  	if (rcu_walk)
  		ret = nfs_revalidate_inode_rcu(NFS_SERVER(dir), dir);
  	else
  		ret = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  	if (ret < 0)
f2c77f4e6   Trond Myklebust   NFS: Optimise nfs...
1008
1009
1010
1011
  		return 0;
  	if (!nfs_verify_change_attribute(dir, dentry->d_time))
  		return 0;
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1012
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
  /*
a12802cab   Trond Myklebust   NFS: Be strict ab...
1014
1015
1016
   * Use intent information to check whether or not we're going to do
   * an O_EXCL create using this path component.
   */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1017
  static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
a12802cab   Trond Myklebust   NFS: Be strict ab...
1018
1019
1020
  {
  	if (NFS_PROTO(dir)->version == 2)
  		return 0;
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1021
  	return flags & LOOKUP_EXCL;
a12802cab   Trond Myklebust   NFS: Be strict ab...
1022
1023
1024
  }
  
  /*
1d6757fbf   Trond Myklebust   [PATCH] NFS: Fix ...
1025
1026
1027
1028
1029
1030
1031
   * Inode and filehandle revalidation for lookups.
   *
   * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
   * or if the intent information indicates that we're about to open this
   * particular file and the "nocto" mount flag is not set.
   *
   */
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
1032
  static
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1033
  int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1034
1035
  {
  	struct nfs_server *server = NFS_SERVER(inode);
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
1036
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1037

36d43a437   David Howells   NFS: Use d_automo...
1038
  	if (IS_AUTOMOUNT(inode))
4e99a1ff3   Trond Myklebust   NFS: Fix dentry r...
1039
  		return 0;
facc3530f   Al Viro   nfs_lookup_verify...
1040
  	/* VFS wants an on-the-wire revalidation */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1041
  	if (flags & LOOKUP_REVAL)
facc3530f   Al Viro   nfs_lookup_verify...
1042
1043
  		goto out_force;
  	/* This is an open(2) */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1044
1045
  	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
  	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
facc3530f   Al Viro   nfs_lookup_verify...
1046
  		goto out_force;
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
1047
1048
  out:
  	return (inode->i_nlink == 0) ? -ENOENT : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1049
  out_force:
1fa1e3844   NeilBrown   NFS: teach nfs_lo...
1050
1051
  	if (flags & LOOKUP_RCU)
  		return -ECHILD;
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
1052
1053
1054
1055
  	ret = __nfs_revalidate_inode(server, inode);
  	if (ret != 0)
  		return ret;
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
1059
1060
1061
1062
1063
  }
  
  /*
   * We judge how long we want to trust negative
   * dentries by looking at the parent inode mtime.
   *
   * If parent mtime has changed, we revalidate, else we wait for a
   * period corresponding to the parent's attribute cache timeout value.
912a108da   NeilBrown   NFS: teach nfs_ne...
1064
1065
1066
   *
   * If LOOKUP_RCU prevents us from performing a full check, return 1
   * suggesting a reval is needed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1067
1068
1069
   */
  static inline
  int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1070
  		       unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1071
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1072
  	/* Don't revalidate a negative dentry if we're creating a new file */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1073
  	if (flags & LOOKUP_CREATE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1074
  		return 0;
4eec952e4   Trond Myklebust   NFS: Add options ...
1075
1076
  	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  		return 1;
912a108da   NeilBrown   NFS: teach nfs_ne...
1077
  	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
  }
  
  /*
   * This is called every time the dcache has a lookup hit,
   * and we should check whether we can really trust that
   * lookup.
   *
   * NOTE! The hit can be a negative hit too, don't assume
   * we have an inode!
   *
   * If the parent directory is seen to have changed, we throw out the
   * cached dentry and do a new lookup.
   */
0b728e191   Al Viro   stop passing name...
1091
  static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1092
1093
1094
1095
  {
  	struct inode *dir;
  	struct inode *inode;
  	struct dentry *parent;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1096
1097
  	struct nfs_fh *fhandle = NULL;
  	struct nfs_fattr *fattr = NULL;
1775fd3e8   David Quigley   NFS:Add labels to...
1098
  	struct nfs4_label *label = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1099
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100

d51ac1a8e   NeilBrown   NFS: prepare for ...
1101
  	if (flags & LOOKUP_RCU) {
50d77739f   NeilBrown   NFS: fix two prob...
1102
  		parent = ACCESS_ONCE(dentry->d_parent);
2b0143b5c   David Howells   VFS: normal files...
1103
  		dir = d_inode_rcu(parent);
d51ac1a8e   NeilBrown   NFS: prepare for ...
1104
1105
1106
1107
  		if (!dir)
  			return -ECHILD;
  	} else {
  		parent = dget_parent(dentry);
2b0143b5c   David Howells   VFS: normal files...
1108
  		dir = d_inode(parent);
d51ac1a8e   NeilBrown   NFS: prepare for ...
1109
  	}
91d5b4702   Chuck Lever   NFS: add I/O perf...
1110
  	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
2b0143b5c   David Howells   VFS: normal files...
1111
  	inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
1113
  
  	if (!inode) {
912a108da   NeilBrown   NFS: teach nfs_ne...
1114
1115
1116
  		if (nfs_neg_need_reval(dir, dentry, flags)) {
  			if (flags & LOOKUP_RCU)
  				return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1117
  			goto out_bad;
912a108da   NeilBrown   NFS: teach nfs_ne...
1118
  		}
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
1119
  		goto out_valid_noent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1120
1121
1122
  	}
  
  	if (is_bad_inode(inode)) {
d51ac1a8e   NeilBrown   NFS: prepare for ...
1123
1124
  		if (flags & LOOKUP_RCU)
  			return -ECHILD;
6de1472f1   Al Viro   nfs: use %p[dD] i...
1125
1126
1127
  		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode
  ",
  				__func__, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1128
1129
  		goto out_bad;
  	}
011e2a7fd   Bryan Schumaker   NFS: Create a hav...
1130
  	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
15860ab1d   Trond Myklebust   NFSv4: Ensure tha...
1131
  		goto out_set_verifier;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
  	/* Force a full look up iff the parent directory has changed */
912a108da   NeilBrown   NFS: teach nfs_ne...
1133
1134
  	if (!nfs_is_exclusive_create(dir, flags) &&
  	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1fa1e3844   NeilBrown   NFS: teach nfs_lo...
1135
1136
1137
  		if (nfs_lookup_verify_inode(inode, flags)) {
  			if (flags & LOOKUP_RCU)
  				return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138
  			goto out_zap_parent;
1fa1e3844   NeilBrown   NFS: teach nfs_lo...
1139
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
1141
  		goto out_valid;
  	}
912a108da   NeilBrown   NFS: teach nfs_ne...
1142
1143
  	if (flags & LOOKUP_RCU)
  		return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144
1145
  	if (NFS_STALE(inode))
  		goto out_bad;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1146
1147
1148
1149
1150
  	error = -ENOMEM;
  	fhandle = nfs_alloc_fhandle();
  	fattr = nfs_alloc_fattr();
  	if (fhandle == NULL || fattr == NULL)
  		goto out_error;
14c43f767   David Quigley   NFS: Add label li...
1151
1152
1153
  	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  	if (IS_ERR(label))
  		goto out_error;
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1154
  	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1775fd3e8   David Quigley   NFS:Add labels to...
1155
  	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1156
  	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
1158
  	if (error)
  		goto out_bad;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1159
  	if (nfs_compare_fh(NFS_FH(inode), fhandle))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1160
  		goto out_bad;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1161
  	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162
  		goto out_bad;
aa9c26696   David Quigley   NFS: Client imple...
1163
  	nfs_setsecurity(inode, fattr, label);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1164
1165
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
14c43f767   David Quigley   NFS: Add label li...
1166
  	nfs4_label_free(label);
15860ab1d   Trond Myklebust   NFSv4: Ensure tha...
1167
  out_set_verifier:
cf8ba45e0   Trond Myklebust   NFS: don't cache ...
1168
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1169
   out_valid:
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
1170
1171
1172
  	/* Success: notify readdir to use READDIRPLUS */
  	nfs_advise_use_readdirplus(dir);
   out_valid_noent:
d51ac1a8e   NeilBrown   NFS: prepare for ...
1173
  	if (flags & LOOKUP_RCU) {
50d77739f   NeilBrown   NFS: fix two prob...
1174
  		if (parent != ACCESS_ONCE(dentry->d_parent))
d51ac1a8e   NeilBrown   NFS: prepare for ...
1175
1176
1177
  			return -ECHILD;
  	} else
  		dput(parent);
6de1472f1   Al Viro   nfs: use %p[dD] i...
1178
1179
1180
  	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid
  ",
  			__func__, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181
1182
1183
1184
  	return 1;
  out_zap_parent:
  	nfs_zap_caches(dir);
   out_bad:
d51ac1a8e   NeilBrown   NFS: prepare for ...
1185
  	WARN_ON(flags & LOOKUP_RCU);
c44600c9d   Al Viro   nfs_lookup_revali...
1186
1187
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
14c43f767   David Quigley   NFS: Add label li...
1188
  	nfs4_label_free(label);
a1643a92f   Trond Myklebust   NFS: NFS_CACHEINV...
1189
  	nfs_mark_for_revalidate(dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1190
1191
1192
  	if (inode && S_ISDIR(inode->i_mode)) {
  		/* Purge readdir caches. */
  		nfs_zap_caches(inode);
a3f432bfd   J. Bruce Fields   nfs: use IS_ROOT ...
1193
1194
1195
1196
1197
1198
1199
  		/*
  		 * We can't d_drop the root of a disconnected tree:
  		 * its d_hash is on the s_anon list and d_drop() would hide
  		 * it from shrink_dcache_for_unmount(), leading to busy
  		 * inodes on unmount and further oopses.
  		 */
  		if (IS_ROOT(dentry))
d9e80b7de   Al Viro   nfs d_revalidate(...
1200
  			goto out_valid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1201
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1202
  	dput(parent);
6de1472f1   Al Viro   nfs: use %p[dD] i...
1203
1204
1205
  	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid
  ",
  			__func__, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1206
  	return 0;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1207
  out_error:
d51ac1a8e   NeilBrown   NFS: prepare for ...
1208
  	WARN_ON(flags & LOOKUP_RCU);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1209
1210
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
14c43f767   David Quigley   NFS: Add label li...
1211
  	nfs4_label_free(label);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1212
  	dput(parent);
6de1472f1   Al Viro   nfs: use %p[dD] i...
1213
1214
1215
  	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d
  ",
  			__func__, dentry, error);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1216
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
  }
  
  /*
2b0143b5c   David Howells   VFS: normal files...
1220
   * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
   * when we don't really care about the dentry name. This is called when a
   * pathwalk ends on a dentry that was not found via a normal lookup in the
   * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
   *
   * In this situation, we just want to verify that the inode itself is OK
   * since the dentry might have changed on the server.
   */
  static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
  {
  	int error;
2b0143b5c   David Howells   VFS: normal files...
1231
  	struct inode *inode = d_inode(dentry);
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1232
1233
1234
1235
1236
1237
1238
  
  	/*
  	 * I believe we can only get a negative dentry here in the case of a
  	 * procfs-style symlink. Just assume it's correct for now, but we may
  	 * eventually need to do something more here.
  	 */
  	if (!inode) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
1239
1240
1241
  		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode
  ",
  				__func__, dentry);
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1242
1243
1244
1245
  		return 1;
  	}
  
  	if (is_bad_inode(inode)) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
1246
1247
1248
  		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode
  ",
  				__func__, dentry);
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
  		return 0;
  	}
  
  	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s
  ",
  			__func__, inode->i_ino, error ? "invalid" : "valid");
  	return !error;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1260
1261
   * This is called from dput() when d_count is going to 0.
   */
fe15ce446   Nick Piggin   fs: change d_dele...
1262
  static int nfs_dentry_delete(const struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1263
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
1264
1265
1266
  	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)
  ",
  		dentry, dentry->d_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1267

77f111929   Trond Myklebust   NFS: Ensure that ...
1268
  	/* Unhash any dentry with a stale inode */
2b0143b5c   David Howells   VFS: normal files...
1269
  	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
77f111929   Trond Myklebust   NFS: Ensure that ...
1270
  		return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
  	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  		/* Unhash it, so that ->d_iput() would be called */
  		return 1;
  	}
  	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  		/* Unhash it, so that ancestors of killed async unlink
  		 * files will be cleaned up during umount */
  		return 1;
  	}
  	return 0;
  
  }
1f018458b   Trond Myklebust   NFS: Fix calls to...
1283
  /* Ensure that we revalidate inode->i_nlink */
1b83d7070   Trond Myklebust   NFS: Protect inod...
1284
1285
1286
  static void nfs_drop_nlink(struct inode *inode)
  {
  	spin_lock(&inode->i_lock);
1f018458b   Trond Myklebust   NFS: Fix calls to...
1287
1288
1289
1290
  	/* drop the inode if we're reasonably sure this is the last link */
  	if (inode->i_nlink == 1)
  		clear_nlink(inode);
  	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
1b83d7070   Trond Myklebust   NFS: Protect inod...
1291
1292
  	spin_unlock(&inode->i_lock);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1293
1294
1295
1296
1297
1298
  /*
   * Called when the dentry loses inode.
   * We use it to clean up silly-renamed files.
   */
  static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  {
83672d392   Neil Brown   NFS: Fix director...
1299
1300
1301
  	if (S_ISDIR(inode->i_mode))
  		/* drop any readdir cache as it could easily be old */
  		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1302
  	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
e4eff1a62   Trond Myklebust   SUNRPC: Clean up ...
1303
  		nfs_complete_unlink(dentry, inode);
1f018458b   Trond Myklebust   NFS: Fix calls to...
1304
  		nfs_drop_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1305
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1306
1307
  	iput(inode);
  }
b1942c5f8   Al Viro   nfs: store devnam...
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
  static void nfs_d_release(struct dentry *dentry)
  {
  	/* free cached devname value, if it survived that far */
  	if (unlikely(dentry->d_fsdata)) {
  		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  			WARN_ON(1);
  		else
  			kfree(dentry->d_fsdata);
  	}
  }
f786aa90e   Al Viro   constify dentry_o...
1318
  const struct dentry_operations nfs_dentry_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1319
  	.d_revalidate	= nfs_lookup_revalidate,
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1320
  	.d_weak_revalidate	= nfs_weak_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1321
1322
  	.d_delete	= nfs_dentry_delete,
  	.d_iput		= nfs_dentry_iput,
36d43a437   David Howells   NFS: Use d_automo...
1323
  	.d_automount	= nfs_d_automount,
b1942c5f8   Al Viro   nfs: store devnam...
1324
  	.d_release	= nfs_d_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1325
  };
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1326
  EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1327

597d92891   Bryan Schumaker   NFS: Split out NF...
1328
  struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1329
1330
1331
  {
  	struct dentry *res;
  	struct inode *inode = NULL;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1332
1333
  	struct nfs_fh *fhandle = NULL;
  	struct nfs_fattr *fattr = NULL;
1775fd3e8   David Quigley   NFS:Add labels to...
1334
  	struct nfs4_label *label = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1335
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336

6de1472f1   Al Viro   nfs: use %p[dD] i...
1337
1338
  	dfprintk(VFS, "NFS: lookup(%pd2)
  ", dentry);
91d5b4702   Chuck Lever   NFS: add I/O perf...
1339
  	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1340

130f9ab75   Al Viro   nfs_lookup: don't...
1341
1342
  	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
  		return ERR_PTR(-ENAMETOOLONG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1343

fd6840714   Trond Myklebust   NFS: nfs_lookup -...
1344
1345
1346
1347
  	/*
  	 * If we're doing an exclusive create, optimize away the lookup
  	 * but don't hash the dentry.
  	 */
130f9ab75   Al Viro   nfs_lookup: don't...
1348
1349
  	if (nfs_is_exclusive_create(dir, flags))
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1350

e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1351
1352
1353
1354
1355
  	res = ERR_PTR(-ENOMEM);
  	fhandle = nfs_alloc_fhandle();
  	fattr = nfs_alloc_fattr();
  	if (fhandle == NULL || fattr == NULL)
  		goto out;
14c43f767   David Quigley   NFS: Add label li...
1356
1357
1358
  	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
  	if (IS_ERR(label))
  		goto out;
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1359
  	trace_nfs_lookup_enter(dir, dentry, flags);
1775fd3e8   David Quigley   NFS:Add labels to...
1360
  	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1361
1362
1363
1364
  	if (error == -ENOENT)
  		goto no_entry;
  	if (error < 0) {
  		res = ERR_PTR(error);
bf1309142   Al Viro   nfs_lookup(): rem...
1365
  		goto out_label;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1366
  	}
1775fd3e8   David Quigley   NFS:Add labels to...
1367
  	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
bf0c84f16   Namhyung Kim   NFS: use ERR_CAST()
1368
  	res = ERR_CAST(inode);
03f28e3a2   Trond Myklebust   NFS: Make nfs_fhg...
1369
  	if (IS_ERR(res))
bf1309142   Al Viro   nfs_lookup(): rem...
1370
  		goto out_label;
54ceac451   David Howells   NFS: Share NFS su...
1371

d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
1372
1373
  	/* Success: notify readdir to use READDIRPLUS */
  	nfs_advise_use_readdirplus(dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1374
  no_entry:
41d28bca2   Al Viro   switch d_material...
1375
  	res = d_splice_alias(inode, dentry);
9eaef27b3   Trond Myklebust   [PATCH] VFS: Make...
1376
1377
  	if (res != NULL) {
  		if (IS_ERR(res))
bf1309142   Al Viro   nfs_lookup(): rem...
1378
  			goto out_label;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1379
  		dentry = res;
9eaef27b3   Trond Myklebust   [PATCH] VFS: Make...
1380
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1381
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
bf1309142   Al Viro   nfs_lookup(): rem...
1382
  out_label:
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1383
  	trace_nfs_lookup_exit(dir, dentry, flags, error);
14c43f767   David Quigley   NFS: Add label li...
1384
  	nfs4_label_free(label);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1385
  out:
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1386
1387
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1388
1389
  	return res;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1390
  EXPORT_SYMBOL_GPL(nfs_lookup);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1391

89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
1392
  #if IS_ENABLED(CONFIG_NFS_V4)
0b728e191   Al Viro   stop passing name...
1393
  static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394

f786aa90e   Al Viro   constify dentry_o...
1395
  const struct dentry_operations nfs4_dentry_operations = {
0ef97dcfc   Miklos Szeredi   nfs: don't open i...
1396
  	.d_revalidate	= nfs4_lookup_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1397
1398
  	.d_delete	= nfs_dentry_delete,
  	.d_iput		= nfs_dentry_iput,
36d43a437   David Howells   NFS: Use d_automo...
1399
  	.d_automount	= nfs_d_automount,
b1942c5f8   Al Viro   nfs: store devnam...
1400
  	.d_release	= nfs_d_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1401
  };
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
1402
  EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1403

8a5e929dd   Al Viro   don't translitera...
1404
1405
1406
1407
1408
1409
1410
1411
1412
  static fmode_t flags_to_mode(int flags)
  {
  	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
  	if ((flags & O_ACCMODE) != O_WRONLY)
  		res |= FMODE_READ;
  	if ((flags & O_ACCMODE) != O_RDONLY)
  		res |= FMODE_WRITE;
  	return res;
  }
511415980   Al Viro   nameidata_to_nfs_...
1413
  static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1414
  {
5ede7b1cf   Al Viro   pull manipulation...
1415
  	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1416
1417
1418
1419
  }
  
  static int do_open(struct inode *inode, struct file *filp)
  {
f1fe29b4a   David Howells   NFS: Use i_writec...
1420
  	nfs_fscache_open_file(inode, filp);
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1421
1422
  	return 0;
  }
d95852777   Al Viro   make ->atomic_ope...
1423
1424
  static int nfs_finish_open(struct nfs_open_context *ctx,
  			   struct dentry *dentry,
30d904947   Al Viro   kill struct opendata
1425
  			   struct file *file, unsigned open_flags,
d95852777   Al Viro   make ->atomic_ope...
1426
  			   int *opened)
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1427
  {
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1428
  	int err;
30d904947   Al Viro   kill struct opendata
1429
1430
  	err = finish_open(file, dentry, do_open, opened);
  	if (err)
d95852777   Al Viro   make ->atomic_ope...
1431
  		goto out;
30d904947   Al Viro   kill struct opendata
1432
  	nfs_file_set_open_context(file, ctx);
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1433

cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1434
  out:
d95852777   Al Viro   make ->atomic_ope...
1435
  	return err;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1436
  }
73a79706d   Bryan Schumaker   NFS: Split out NF...
1437
1438
1439
  int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  		    struct file *file, unsigned open_flags,
  		    umode_t mode, int *opened)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1440
  {
c94c09535   Al Viro   nfs_atomic_open()...
1441
  	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1442
  	struct nfs_open_context *ctx;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1443
1444
  	struct dentry *res;
  	struct iattr attr = { .ia_valid = ATTR_OPEN };
f46e0bd34   Trond Myklebust   NFSv4: Further mi...
1445
  	struct inode *inode;
1472b83ea   Trond Myklebust   NFS: Pass in look...
1446
  	unsigned int lookup_flags = 0;
c94c09535   Al Viro   nfs_atomic_open()...
1447
  	bool switched = false;
898f635c4   Trond Myklebust   NFSv4: Don't igno...
1448
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1449

0dd2b474d   Miklos Szeredi   nfs: implement i_...
1450
  	/* Expect a negative dentry */
2b0143b5c   David Howells   VFS: normal files...
1451
  	BUG_ON(d_inode(dentry));
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1452

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1453
1454
  	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
1455
  			dir->i_sb->s_id, dir->i_ino, dentry);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
1456

9597c13b2   Jeff Layton   nfs: verify open ...
1457
1458
1459
  	err = nfs_check_flags(open_flags);
  	if (err)
  		return err;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1460
1461
  	/* NFS only supports OPEN on regular files */
  	if ((open_flags & O_DIRECTORY)) {
00699ad85   Al Viro   Use the right pre...
1462
  		if (!d_in_lookup(dentry)) {
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1463
1464
1465
1466
1467
  			/*
  			 * Hashed negative dentry with O_DIRECTORY: dentry was
  			 * revalidated and is fine, no need to perform lookup
  			 * again
  			 */
d95852777   Al Viro   make ->atomic_ope...
1468
  			return -ENOENT;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1469
  		}
1472b83ea   Trond Myklebust   NFS: Pass in look...
1470
  		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1471
  		goto no_open;
02a913a73   Trond Myklebust   NFSv4: Eliminate ...
1472
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1473

0dd2b474d   Miklos Szeredi   nfs: implement i_...
1474
  	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
d95852777   Al Viro   make ->atomic_ope...
1475
  		return -ENAMETOOLONG;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1476

0dd2b474d   Miklos Szeredi   nfs: implement i_...
1477
  	if (open_flags & O_CREAT) {
536e43d12   Trond Myklebust   NFS: Optimise awa...
1478
  		attr.ia_valid |= ATTR_MODE;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1479
1480
  		attr.ia_mode = mode & ~current_umask();
  	}
536e43d12   Trond Myklebust   NFS: Optimise awa...
1481
1482
1483
  	if (open_flags & O_TRUNC) {
  		attr.ia_valid |= ATTR_SIZE;
  		attr.ia_size = 0;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1484
  	}
c94c09535   Al Viro   nfs_atomic_open()...
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
  	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
  		d_drop(dentry);
  		switched = true;
  		dentry = d_alloc_parallel(dentry->d_parent,
  					  &dentry->d_name, &wq);
  		if (IS_ERR(dentry))
  			return PTR_ERR(dentry);
  		if (unlikely(!d_in_lookup(dentry)))
  			return finish_no_open(file, dentry);
  	}
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1495
1496
1497
  	ctx = create_nfs_open_context(dentry, open_flags);
  	err = PTR_ERR(ctx);
  	if (IS_ERR(ctx))
d95852777   Al Viro   make ->atomic_ope...
1498
  		goto out;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1499

6e0d0be71   Trond Myklebust   NFS: Add event tr...
1500
  	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
5bc2afc2b   Trond Myklebust   NFSv4: Honour the...
1501
  	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
f46e0bd34   Trond Myklebust   NFSv4: Further mi...
1502
  	if (IS_ERR(inode)) {
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1503
  		err = PTR_ERR(inode);
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1504
  		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2d9db7500   Trond Myklebust   NFS: Fix up two u...
1505
  		put_nfs_open_context(ctx);
d20cb71db   Al Viro   make nfs_atomic_o...
1506
  		d_drop(dentry);
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1507
1508
1509
  		switch (err) {
  		case -ENOENT:
  			d_add(dentry, NULL);
809fd143d   Trond Myklebust   NFSv4: Ensure nfs...
1510
  			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1511
1512
1513
1514
1515
1516
  			break;
  		case -EISDIR:
  		case -ENOTDIR:
  			goto no_open;
  		case -ELOOP:
  			if (!(open_flags & O_NOFOLLOW))
6f926b5ba   Trond Myklebust   [NFS]: Check that...
1517
  				goto no_open;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1518
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1519
  			/* case -EINVAL: */
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1520
1521
  		default:
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1522
  		}
d95852777   Al Viro   make ->atomic_ope...
1523
  		goto out;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1524
  	}
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1525

275bb3078   Trond Myklebust   NFSv4: Move dentr...
1526
  	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1527
  	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2d9db7500   Trond Myklebust   NFS: Fix up two u...
1528
  	put_nfs_open_context(ctx);
d95852777   Al Viro   make ->atomic_ope...
1529
  out:
c94c09535   Al Viro   nfs_atomic_open()...
1530
1531
1532
1533
  	if (unlikely(switched)) {
  		d_lookup_done(dentry);
  		dput(dentry);
  	}
d95852777   Al Viro   make ->atomic_ope...
1534
  	return err;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1535

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1536
  no_open:
1472b83ea   Trond Myklebust   NFS: Pass in look...
1537
  	res = nfs_lookup(dir, dentry, lookup_flags);
c94c09535   Al Viro   nfs_atomic_open()...
1538
1539
1540
1541
1542
1543
1544
  	if (switched) {
  		d_lookup_done(dentry);
  		if (!res)
  			res = dentry;
  		else
  			dput(dentry);
  	}
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1545
  	if (IS_ERR(res))
c94c09535   Al Viro   nfs_atomic_open()...
1546
  		return PTR_ERR(res);
e45198a6a   Al Viro   make finish_no_op...
1547
  	return finish_no_open(file, res);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1548
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
1549
  EXPORT_SYMBOL_GPL(nfs_atomic_open);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1550

0b728e191   Al Viro   stop passing name...
1551
  static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1552
  {
657e94b67   Nick Piggin   nfs: add missing ...
1553
  	struct inode *inode;
50de348c3   Miklos Szeredi   nfs: don't use nd...
1554
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1555

fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1556
  	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
eda72afb9   Miklos Szeredi   nfs: don't use in...
1557
1558
  		goto no_open;
  	if (d_mountpoint(dentry))
5584c3063   Trond Myklebust   NFSv4: Clean up i...
1559
  		goto no_open;
49f9a0faf   Trond Myklebust   NFSv4.1: Enable o...
1560
1561
  	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
  		goto no_open;
2b484297e   Trond Myklebust   NFS: Add an 'open...
1562

2b0143b5c   David Howells   VFS: normal files...
1563
  	inode = d_inode(dentry);
2b484297e   Trond Myklebust   NFS: Add an 'open...
1564

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1565
1566
1567
  	/* We can't create new files in nfs_open_revalidate(), so we
  	 * optimize away revalidation of negative dentries.
  	 */
216d5d068   Trond Myklebust   NFSv4: Use NFSv2/...
1568
  	if (inode == NULL) {
49317a7fd   NeilBrown   NFS: nfs4_lookup_...
1569
1570
  		struct dentry *parent;
  		struct inode *dir;
912a108da   NeilBrown   NFS: teach nfs_ne...
1571
  		if (flags & LOOKUP_RCU) {
50d77739f   NeilBrown   NFS: fix two prob...
1572
  			parent = ACCESS_ONCE(dentry->d_parent);
2b0143b5c   David Howells   VFS: normal files...
1573
  			dir = d_inode_rcu(parent);
912a108da   NeilBrown   NFS: teach nfs_ne...
1574
1575
1576
1577
  			if (!dir)
  				return -ECHILD;
  		} else {
  			parent = dget_parent(dentry);
2b0143b5c   David Howells   VFS: normal files...
1578
  			dir = d_inode(parent);
912a108da   NeilBrown   NFS: teach nfs_ne...
1579
  		}
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1580
  		if (!nfs_neg_need_reval(dir, dentry, flags))
216d5d068   Trond Myklebust   NFSv4: Use NFSv2/...
1581
  			ret = 1;
912a108da   NeilBrown   NFS: teach nfs_ne...
1582
1583
1584
1585
  		else if (flags & LOOKUP_RCU)
  			ret = -ECHILD;
  		if (!(flags & LOOKUP_RCU))
  			dput(parent);
50d77739f   NeilBrown   NFS: fix two prob...
1586
  		else if (parent != ACCESS_ONCE(dentry->d_parent))
912a108da   NeilBrown   NFS: teach nfs_ne...
1587
  			return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1588
  		goto out;
216d5d068   Trond Myklebust   NFSv4: Use NFSv2/...
1589
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1590
1591
  	/* NFS only supports OPEN on regular files */
  	if (!S_ISREG(inode->i_mode))
49317a7fd   NeilBrown   NFS: nfs4_lookup_...
1592
  		goto no_open;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1593
  	/* We cannot do exclusive creation on a positive dentry */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1594
  	if (flags & LOOKUP_EXCL)
49317a7fd   NeilBrown   NFS: nfs4_lookup_...
1595
  		goto no_open;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596

0ef97dcfc   Miklos Szeredi   nfs: don't open i...
1597
1598
  	/* Let f_op->open() actually open (and revalidate) the file */
  	ret = 1;
536e43d12   Trond Myklebust   NFS: Optimise awa...
1599

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1600
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1601
  	return ret;
535918f14   Trond Myklebust   NFSv4: Further cl...
1602

5584c3063   Trond Myklebust   NFSv4: Clean up i...
1603
  no_open:
0b728e191   Al Viro   stop passing name...
1604
  	return nfs_lookup_revalidate(dentry, flags);
c0204fd2b   Trond Myklebust   NFS: Clean up nfs...
1605
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1606
  #endif /* CONFIG_NFSV4 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1607
1608
1609
1610
  /*
   * Code common to create, mkdir, and mknod.
   */
  int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1775fd3e8   David Quigley   NFS:Add labels to...
1611
1612
  				struct nfs_fattr *fattr,
  				struct nfs4_label *label)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1613
  {
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1614
  	struct dentry *parent = dget_parent(dentry);
2b0143b5c   David Howells   VFS: normal files...
1615
  	struct inode *dir = d_inode(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1616
1617
  	struct inode *inode;
  	int error = -EACCES;
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1618
  	d_drop(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1619
  	/* We may have been initialized further down */
2b0143b5c   David Howells   VFS: normal files...
1620
  	if (d_really_is_positive(dentry))
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1621
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1622
  	if (fhandle->size == 0) {
1775fd3e8   David Quigley   NFS:Add labels to...
1623
  		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1624
  		if (error)
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1625
  			goto out_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1626
  	}
5724ab378   Trond Myklebust   NFS: nfs_instanti...
1627
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1628
1629
  	if (!(fattr->valid & NFS_ATTR_FATTR)) {
  		struct nfs_server *server = NFS_SB(dentry->d_sb);
1775fd3e8   David Quigley   NFS:Add labels to...
1630
  		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1631
  		if (error < 0)
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1632
  			goto out_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1633
  	}
1775fd3e8   David Quigley   NFS:Add labels to...
1634
  	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
03f28e3a2   Trond Myklebust   NFS: Make nfs_fhg...
1635
1636
  	error = PTR_ERR(inode);
  	if (IS_ERR(inode))
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1637
1638
1639
1640
  		goto out_error;
  	d_add(dentry, inode);
  out:
  	dput(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1641
  	return 0;
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1642
1643
1644
1645
  out_error:
  	nfs_mark_for_revalidate(dir);
  	dput(parent);
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1647
  EXPORT_SYMBOL_GPL(nfs_instantiate);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1648
1649
1650
1651
1652
1653
1654
  
  /*
   * Following a failed create operation, we drop the dentry rather
   * than retain a negative dentry. This avoids a problem in the event
   * that the operation succeeded on the server, but an error in the
   * reply path made it appear to have failed.
   */
597d92891   Bryan Schumaker   NFS: Split out NF...
1655
  int nfs_create(struct inode *dir, struct dentry *dentry,
ebfc3b49a   Al Viro   don't pass nameid...
1656
  		umode_t mode, bool excl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1657
1658
  {
  	struct iattr attr;
ebfc3b49a   Al Viro   don't pass nameid...
1659
  	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1660
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1661

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1662
1663
  	dfprintk(VFS, "NFS: create(%s/%lu), %pd
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
1664
  			dir->i_sb->s_id, dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1665
1666
1667
  
  	attr.ia_mode = mode;
  	attr.ia_valid = ATTR_MODE;
8b0ad3d48   Trond Myklebust   NFS: Add tracepoi...
1668
  	trace_nfs_create_enter(dir, dentry, open_flags);
8867fe589   Miklos Szeredi   nfs: clean up ->c...
1669
  	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
8b0ad3d48   Trond Myklebust   NFS: Add tracepoi...
1670
  	trace_nfs_create_exit(dir, dentry, open_flags, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671
1672
  	if (error != 0)
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
1674
  	return 0;
  out_err:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1675
1676
1677
  	d_drop(dentry);
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1678
  EXPORT_SYMBOL_GPL(nfs_create);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1679
1680
1681
1682
  
  /*
   * See comments for nfs_proc_create regarding failed operations.
   */
597d92891   Bryan Schumaker   NFS: Split out NF...
1683
  int
1a67aafb5   Al Viro   switch ->mknod() ...
1684
  nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1685
1686
1687
  {
  	struct iattr attr;
  	int status;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1688
1689
  	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
1690
  			dir->i_sb->s_id, dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1691

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1692
1693
  	attr.ia_mode = mode;
  	attr.ia_valid = ATTR_MODE;
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1694
  	trace_nfs_mknod_enter(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1695
  	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1696
  	trace_nfs_mknod_exit(dir, dentry, status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1697
1698
  	if (status != 0)
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1699
1700
  	return 0;
  out_err:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1701
1702
1703
  	d_drop(dentry);
  	return status;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1704
  EXPORT_SYMBOL_GPL(nfs_mknod);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1705
1706
1707
1708
  
  /*
   * See comments for nfs_proc_create regarding failed operations.
   */
597d92891   Bryan Schumaker   NFS: Split out NF...
1709
  int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1710
1711
1712
  {
  	struct iattr attr;
  	int error;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1713
1714
  	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
1715
  			dir->i_sb->s_id, dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1716
1717
1718
  
  	attr.ia_valid = ATTR_MODE;
  	attr.ia_mode = mode | S_IFDIR;
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1719
  	trace_nfs_mkdir_enter(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1720
  	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1721
  	trace_nfs_mkdir_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1722
1723
  	if (error != 0)
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1724
1725
1726
  	return 0;
  out_err:
  	d_drop(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1727
1728
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1729
  EXPORT_SYMBOL_GPL(nfs_mkdir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1730

d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1731
1732
  static void nfs_dentry_handle_enoent(struct dentry *dentry)
  {
dc3f4198e   Al Viro   make simple_posit...
1733
  	if (simple_positive(dentry))
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1734
1735
  		d_delete(dentry);
  }
597d92891   Bryan Schumaker   NFS: Split out NF...
1736
  int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1737
1738
  {
  	int error;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1739
1740
  	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
1741
  			dir->i_sb->s_id, dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1742

1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1743
  	trace_nfs_rmdir_enter(dir, dentry);
2b0143b5c   David Howells   VFS: normal files...
1744
  	if (d_really_is_positive(dentry)) {
884be1753   Al Viro   nfs: per-name sil...
1745
  		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
ba6c05928   Trond Myklebust   NFS: Ensure that ...
1746
1747
1748
1749
  		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  		/* Ensure the VFS deletes this inode */
  		switch (error) {
  		case 0:
2b0143b5c   David Howells   VFS: normal files...
1750
  			clear_nlink(d_inode(dentry));
ba6c05928   Trond Myklebust   NFS: Ensure that ...
1751
1752
1753
1754
  			break;
  		case -ENOENT:
  			nfs_dentry_handle_enoent(dentry);
  		}
884be1753   Al Viro   nfs: per-name sil...
1755
  		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
ba6c05928   Trond Myklebust   NFS: Ensure that ...
1756
1757
  	} else
  		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1758
  	trace_nfs_rmdir_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1759
1760
1761
  
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1762
  EXPORT_SYMBOL_GPL(nfs_rmdir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1763

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1764
1765
1766
1767
1768
1769
1770
1771
1772
  /*
   * Remove a file after making sure there are no pending writes,
   * and after checking that the file has only one user. 
   *
   * We invalidate the attribute cache and free the inode prior to the operation
   * to avoid possible races if the server reuses the inode.
   */
  static int nfs_safe_remove(struct dentry *dentry)
  {
2b0143b5c   David Howells   VFS: normal files...
1773
1774
  	struct inode *dir = d_inode(dentry->d_parent);
  	struct inode *inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1775
1776
  	int error = -EBUSY;
  		
6de1472f1   Al Viro   nfs: use %p[dD] i...
1777
1778
  	dfprintk(VFS, "NFS: safe_remove(%pd2)
  ", dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1779
1780
1781
1782
1783
1784
  
  	/* If the dentry was sillyrenamed, we simply call d_delete() */
  	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  		error = 0;
  		goto out;
  	}
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1785
  	trace_nfs_remove_enter(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1786
  	if (inode != NULL) {
57ec14c55   Bryan Schumaker   NFS: Create a ret...
1787
  		NFS_PROTO(inode)->return_delegation(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1788
  		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1789
  		if (error == 0)
1b83d7070   Trond Myklebust   NFS: Protect inod...
1790
  			nfs_drop_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1791
1792
  	} else
  		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1793
1794
  	if (error == -ENOENT)
  		nfs_dentry_handle_enoent(dentry);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1795
  	trace_nfs_remove_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1796
1797
1798
1799
1800
1801
1802
1803
1804
  out:
  	return error;
  }
  
  /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
   *  belongs to an active ".nfs..." file and we return -EBUSY.
   *
   *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
   */
597d92891   Bryan Schumaker   NFS: Split out NF...
1805
  int nfs_unlink(struct inode *dir, struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1806
1807
1808
  {
  	int error;
  	int need_rehash = 0;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1809
1810
  	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)
  ", dir->i_sb->s_id,
6de1472f1   Al Viro   nfs: use %p[dD] i...
1811
  		dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1812

1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1813
  	trace_nfs_unlink_enter(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1814
  	spin_lock(&dentry->d_lock);
84d08fa88   Al Viro   helper for readin...
1815
  	if (d_count(dentry) > 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1816
  		spin_unlock(&dentry->d_lock);
ccfeb5062   Trond Myklebust   NFS: Fix up "rm -...
1817
  		/* Start asynchronous writeout of the inode */
2b0143b5c   David Howells   VFS: normal files...
1818
  		write_inode_now(d_inode(dentry), 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1819
  		error = nfs_sillyrename(dir, dentry);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1820
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1821
1822
1823
1824
1825
1826
  	}
  	if (!d_unhashed(dentry)) {
  		__d_drop(dentry);
  		need_rehash = 1;
  	}
  	spin_unlock(&dentry->d_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1827
  	error = nfs_safe_remove(dentry);
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1828
  	if (!error || error == -ENOENT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1829
1830
1831
  		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  	} else if (need_rehash)
  		d_rehash(dentry);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1832
1833
  out:
  	trace_nfs_unlink_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1834
1835
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1836
  EXPORT_SYMBOL_GPL(nfs_unlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1837

873101b33   Chuck Lever   NFS: copy symlink...
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
  /*
   * To create a symbolic link, most file systems instantiate a new inode,
   * add a page to it containing the path, then write it out to the disk
   * using prepare_write/commit_write.
   *
   * Unfortunately the NFS client can't create the in-core inode first
   * because it needs a file handle to create an in-core inode (see
   * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
   * symlink request has completed on the server.
   *
   * So instead we allocate a raw page, copy the symname into it, then do
   * the SYMLINK request with the page as the buffer.  If it succeeds, we
   * now have a new file handle and can instantiate an in-core NFS inode
   * and move the raw page into its mapping.
   */
597d92891   Bryan Schumaker   NFS: Split out NF...
1853
  int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1854
  {
873101b33   Chuck Lever   NFS: copy symlink...
1855
1856
  	struct page *page;
  	char *kaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1857
  	struct iattr attr;
873101b33   Chuck Lever   NFS: copy symlink...
1858
  	unsigned int pathlen = strlen(symname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1859
  	int error;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1860
1861
  	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)
  ", dir->i_sb->s_id,
6de1472f1   Al Viro   nfs: use %p[dD] i...
1862
  		dir->i_ino, dentry, symname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1863

873101b33   Chuck Lever   NFS: copy symlink...
1864
1865
  	if (pathlen > PAGE_SIZE)
  		return -ENAMETOOLONG;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1866

873101b33   Chuck Lever   NFS: copy symlink...
1867
1868
  	attr.ia_mode = S_IFLNK | S_IRWXUGO;
  	attr.ia_valid = ATTR_MODE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1869

e8ecde25f   Al Viro   Make sure that hi...
1870
  	page = alloc_page(GFP_USER);
76566991f   Trond Myklebust   NFS: Remove BKL f...
1871
  	if (!page)
873101b33   Chuck Lever   NFS: copy symlink...
1872
  		return -ENOMEM;
873101b33   Chuck Lever   NFS: copy symlink...
1873

e8ecde25f   Al Viro   Make sure that hi...
1874
  	kaddr = page_address(page);
873101b33   Chuck Lever   NFS: copy symlink...
1875
1876
1877
  	memcpy(kaddr, symname, pathlen);
  	if (pathlen < PAGE_SIZE)
  		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
873101b33   Chuck Lever   NFS: copy symlink...
1878

1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1879
  	trace_nfs_symlink_enter(dir, dentry);
94a6d7532   Chuck Lever   NFS: Use cached p...
1880
  	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1881
  	trace_nfs_symlink_exit(dir, dentry, error);
873101b33   Chuck Lever   NFS: copy symlink...
1882
  	if (error != 0) {
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1883
1884
  		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d
  ",
873101b33   Chuck Lever   NFS: copy symlink...
1885
  			dir->i_sb->s_id, dir->i_ino,
6de1472f1   Al Viro   nfs: use %p[dD] i...
1886
  			dentry, symname, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1887
  		d_drop(dentry);
873101b33   Chuck Lever   NFS: copy symlink...
1888
  		__free_page(page);
873101b33   Chuck Lever   NFS: copy symlink...
1889
1890
1891
1892
1893
1894
1895
  		return error;
  	}
  
  	/*
  	 * No big deal if we can't add this page to the page cache here.
  	 * READLINK will get the missing page from the server if needed.
  	 */
2b0143b5c   David Howells   VFS: normal files...
1896
  	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
873101b33   Chuck Lever   NFS: copy symlink...
1897
  							GFP_KERNEL)) {
873101b33   Chuck Lever   NFS: copy symlink...
1898
1899
  		SetPageUptodate(page);
  		unlock_page(page);
a0b54adda   Rafael Aquini   mm: fix page leak...
1900
1901
1902
1903
  		/*
  		 * add_to_page_cache_lru() grabs an extra page refcount.
  		 * Drop it here to avoid leaking this page later.
  		 */
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
1904
  		put_page(page);
873101b33   Chuck Lever   NFS: copy symlink...
1905
1906
  	} else
  		__free_page(page);
873101b33   Chuck Lever   NFS: copy symlink...
1907
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1908
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1909
  EXPORT_SYMBOL_GPL(nfs_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1910

597d92891   Bryan Schumaker   NFS: Split out NF...
1911
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1912
1913
  nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  {
2b0143b5c   David Howells   VFS: normal files...
1914
  	struct inode *inode = d_inode(old_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1915
  	int error;
6de1472f1   Al Viro   nfs: use %p[dD] i...
1916
1917
1918
  	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)
  ",
  		old_dentry, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1919

1fd1085b4   Trond Myklebust   NFS: Add tracepoi...
1920
  	trace_nfs_link_enter(inode, dir, dentry);
57ec14c55   Bryan Schumaker   NFS: Create a ret...
1921
  	NFS_PROTO(inode)->return_delegation(inode);
9a3936aac   Trond Myklebust   NFSv4: The link()...
1922

9697d2342   Trond Myklebust   NFS: Ensure that ...
1923
  	d_drop(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1924
  	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
cf8095561   Trond Myklebust   NFS: Ensure that ...
1925
  	if (error == 0) {
7de9c6ee3   Al Viro   new helper: ihold()
1926
  		ihold(inode);
9697d2342   Trond Myklebust   NFS: Ensure that ...
1927
  		d_add(dentry, inode);
cf8095561   Trond Myklebust   NFS: Ensure that ...
1928
  	}
1fd1085b4   Trond Myklebust   NFS: Add tracepoi...
1929
  	trace_nfs_link_exit(inode, dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1930
1931
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1932
  EXPORT_SYMBOL_GPL(nfs_link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
  
  /*
   * RENAME
   * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
   * different file handle for the same inode after a rename (e.g. when
   * moving to a different directory). A fail-safe method to do so would
   * be to look up old_dir/old_name, create a link to new_dir/new_name and
   * rename the old file using the sillyrename stuff. This way, the original
   * file in old_dir will go away when the last process iput()s the inode.
   *
   * FIXED.
   * 
   * It actually works quite well. One needs to have the possibility for
   * at least one ".nfs..." file in each directory the file ever gets
   * moved or linked to which happens automagically with the new
   * implementation that only depends on the dcache stuff instead of
   * using the inode layer
   *
   * Unfortunately, things are a little more complicated than indicated
   * above. For a cross-directory move, we want to make sure we can get
   * rid of the old inode after the operation.  This means there must be
   * no pending writes (if it's a file), and the use count must be 1.
   * If these conditions are met, we can drop the dentries before doing
   * the rename.
   */
597d92891   Bryan Schumaker   NFS: Split out NF...
1958
  int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93b   Miklos Szeredi   fs: make remainin...
1959
1960
  	       struct inode *new_dir, struct dentry *new_dentry,
  	       unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1961
  {
2b0143b5c   David Howells   VFS: normal files...
1962
1963
  	struct inode *old_inode = d_inode(old_dentry);
  	struct inode *new_inode = d_inode(new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1964
  	struct dentry *dentry = NULL, *rehash = NULL;
80a491fd4   Jeff Layton   nfs: convert nfs_...
1965
  	struct rpc_task *task;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1966
  	int error = -EBUSY;
1cd66c93b   Miklos Szeredi   fs: make remainin...
1967
1968
  	if (flags)
  		return -EINVAL;
6de1472f1   Al Viro   nfs: use %p[dD] i...
1969
1970
1971
  	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)
  ",
  		 old_dentry, new_dentry,
84d08fa88   Al Viro   helper for readin...
1972
  		 d_count(new_dentry));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1973

70ded2017   Trond Myklebust   NFS: Add tracepoi...
1974
  	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1975
  	/*
28f79a1a6   Miklos Szeredi   nfs: fix comments...
1976
1977
1978
1979
  	 * For non-directories, check whether the target is busy and if so,
  	 * make a copy of the dentry and then do a silly-rename. If the
  	 * silly-rename succeeds, the copied dentry is hashed and becomes
  	 * the new target.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1980
  	 */
27226104e   Miklos Szeredi   nfs: dont unhash ...
1981
1982
1983
1984
1985
1986
1987
1988
1989
  	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  		/*
  		 * To prevent any new references to the target during the
  		 * rename, we unhash the dentry in advance.
  		 */
  		if (!d_unhashed(new_dentry)) {
  			d_drop(new_dentry);
  			rehash = new_dentry;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1990

84d08fa88   Al Viro   helper for readin...
1991
  		if (d_count(new_dentry) > 2) {
27226104e   Miklos Szeredi   nfs: dont unhash ...
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
  			int err;
  
  			/* copy the target dentry's name */
  			dentry = d_alloc(new_dentry->d_parent,
  					 &new_dentry->d_name);
  			if (!dentry)
  				goto out;
  
  			/* silly-rename the existing target ... */
  			err = nfs_sillyrename(new_dir, new_dentry);
24e93025e   Miklos Szeredi   nfs: clean up sil...
2002
  			if (err)
27226104e   Miklos Szeredi   nfs: dont unhash ...
2003
  				goto out;
24e93025e   Miklos Szeredi   nfs: clean up sil...
2004
2005
  
  			new_dentry = dentry;
56335936d   OGAWA Hirofumi   nfs: fix oops in ...
2006
  			rehash = NULL;
24e93025e   Miklos Szeredi   nfs: clean up sil...
2007
  			new_inode = NULL;
27226104e   Miklos Szeredi   nfs: dont unhash ...
2008
  		}
b1e4adf4e   Trond Myklebust   NFS: Fix the noti...
2009
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2010

57ec14c55   Bryan Schumaker   NFS: Create a ret...
2011
  	NFS_PROTO(old_inode)->return_delegation(old_inode);
b1e4adf4e   Trond Myklebust   NFS: Fix the noti...
2012
  	if (new_inode != NULL)
57ec14c55   Bryan Schumaker   NFS: Create a ret...
2013
  		NFS_PROTO(new_inode)->return_delegation(new_inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2014

80a491fd4   Jeff Layton   nfs: convert nfs_...
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
  	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
  	if (IS_ERR(task)) {
  		error = PTR_ERR(task);
  		goto out;
  	}
  
  	error = rpc_wait_for_completion_task(task);
  	if (error == 0)
  		error = task->tk_status;
  	rpc_put_task(task);
5ba7cc480   Trond Myklebust   NFS: Fix post-op ...
2025
  	nfs_mark_for_revalidate(old_inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2026
2027
2028
  out:
  	if (rehash)
  		d_rehash(rehash);
70ded2017   Trond Myklebust   NFS: Add tracepoi...
2029
2030
  	trace_nfs_rename_exit(old_dir, old_dentry,
  			new_dir, new_dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2031
  	if (!error) {
b1e4adf4e   Trond Myklebust   NFS: Fix the noti...
2032
2033
  		if (new_inode != NULL)
  			nfs_drop_nlink(new_inode);
349457ccf   Mark Fasheh   [PATCH] Allow fil...
2034
  		d_move(old_dentry, new_dentry);
8fb559f87   Chuck Lever   NFS: Eliminate nf...
2035
2036
  		nfs_set_verifier(new_dentry,
  					nfs_save_change_attribute(new_dir));
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
2037
2038
  	} else if (error == -ENOENT)
  		nfs_dentry_handle_enoent(old_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2039
2040
2041
2042
  
  	/* new dentry created? */
  	if (dentry)
  		dput(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2043
2044
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
2045
  EXPORT_SYMBOL_GPL(nfs_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2046

cfcea3e8c   Trond Myklebust   NFS: Add a global...
2047
2048
2049
  static DEFINE_SPINLOCK(nfs_access_lru_lock);
  static LIST_HEAD(nfs_access_lru_list);
  static atomic_long_t nfs_access_nr_entries;
3a505845c   Trond Myklebust   NFS: Enforce an u...
2050
2051
2052
  static unsigned long nfs_access_max_cachesize = ULONG_MAX;
  module_param(nfs_access_max_cachesize, ulong, 0644);
  MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2053
2054
2055
  static void nfs_access_free_entry(struct nfs_access_entry *entry)
  {
  	put_rpccred(entry->cred);
f682a398b   NeilBrown   NFS: allow lockle...
2056
  	kfree_rcu(entry, rcu_head);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2057
  	smp_mb__before_atomic();
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2058
  	atomic_long_dec(&nfs_access_nr_entries);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2059
  	smp_mb__after_atomic();
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2060
  }
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
  static void nfs_access_free_list(struct list_head *head)
  {
  	struct nfs_access_entry *cache;
  
  	while (!list_empty(head)) {
  		cache = list_entry(head->next, struct nfs_access_entry, lru);
  		list_del(&cache->lru);
  		nfs_access_free_entry(cache);
  	}
  }
3a505845c   Trond Myklebust   NFS: Enforce an u...
2071
2072
  static unsigned long
  nfs_do_access_cache_scan(unsigned int nr_to_scan)
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2073
2074
  {
  	LIST_HEAD(head);
aa510da5b   Trond Myklebust   NFS: We must use ...
2075
  	struct nfs_inode *nfsi, *next;
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2076
  	struct nfs_access_entry *cache;
1ab6c4997   Dave Chinner   fs: convert fs sh...
2077
  	long freed = 0;
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2078

a50f7951a   Trond Myklebust   NFS: Fix an Oops ...
2079
  	spin_lock(&nfs_access_lru_lock);
aa510da5b   Trond Myklebust   NFS: We must use ...
2080
  	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2081
2082
2083
2084
  		struct inode *inode;
  
  		if (nr_to_scan-- == 0)
  			break;
9c7e7e233   Trond Myklebust   NFS: Don't call i...
2085
  		inode = &nfsi->vfs_inode;
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2086
2087
2088
2089
2090
2091
2092
  		spin_lock(&inode->i_lock);
  		if (list_empty(&nfsi->access_cache_entry_lru))
  			goto remove_lru_entry;
  		cache = list_entry(nfsi->access_cache_entry_lru.next,
  				struct nfs_access_entry, lru);
  		list_move(&cache->lru, &head);
  		rb_erase(&cache->rb_node, &nfsi->access_cache);
1ab6c4997   Dave Chinner   fs: convert fs sh...
2093
  		freed++;
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2094
2095
2096
2097
2098
2099
  		if (!list_empty(&nfsi->access_cache_entry_lru))
  			list_move_tail(&nfsi->access_cache_inode_lru,
  					&nfs_access_lru_list);
  		else {
  remove_lru_entry:
  			list_del_init(&nfsi->access_cache_inode_lru);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2100
  			smp_mb__before_atomic();
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2101
  			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2102
  			smp_mb__after_atomic();
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2103
  		}
59844a9bd   Trond Myklebust   NFS: Fix a lock i...
2104
  		spin_unlock(&inode->i_lock);
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2105
2106
  	}
  	spin_unlock(&nfs_access_lru_lock);
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2107
  	nfs_access_free_list(&head);
1ab6c4997   Dave Chinner   fs: convert fs sh...
2108
2109
2110
2111
  	return freed;
  }
  
  unsigned long
3a505845c   Trond Myklebust   NFS: Enforce an u...
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
  nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
  {
  	int nr_to_scan = sc->nr_to_scan;
  	gfp_t gfp_mask = sc->gfp_mask;
  
  	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  		return SHRINK_STOP;
  	return nfs_do_access_cache_scan(nr_to_scan);
  }
  
  
  unsigned long
1ab6c4997   Dave Chinner   fs: convert fs sh...
2124
2125
  nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
  {
55f841ce9   Glauber Costa   super: fix calcul...
2126
  	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2127
  }
3a505845c   Trond Myklebust   NFS: Enforce an u...
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
  static void
  nfs_access_cache_enforce_limit(void)
  {
  	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
  	unsigned long diff;
  	unsigned int nr_to_scan;
  
  	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
  		return;
  	nr_to_scan = 100;
  	diff = nr_entries - nfs_access_max_cachesize;
  	if (diff < nr_to_scan)
  		nr_to_scan = diff;
  	nfs_do_access_cache_scan(nr_to_scan);
  }
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2143
  static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2144
  {
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2145
  	struct rb_root *root_node = &nfsi->access_cache;
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2146
  	struct rb_node *n;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2147
2148
2149
2150
2151
2152
  	struct nfs_access_entry *entry;
  
  	/* Unhook entries from the cache */
  	while ((n = rb_first(root_node)) != NULL) {
  		entry = rb_entry(n, struct nfs_access_entry, rb_node);
  		rb_erase(n, root_node);
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2153
  		list_move(&entry->lru, head);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2154
2155
  	}
  	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2156
  }
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2157
  void nfs_access_zap_cache(struct inode *inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2158
  {
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2159
2160
2161
2162
  	LIST_HEAD(head);
  
  	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  		return;
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2163
  	/* Remove from global LRU init */
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2164
2165
  	spin_lock(&nfs_access_lru_lock);
  	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2166
  		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2167

1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2168
  	spin_lock(&inode->i_lock);
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2169
2170
2171
2172
  	__nfs_access_zap_cache(NFS_I(inode), &head);
  	spin_unlock(&inode->i_lock);
  	spin_unlock(&nfs_access_lru_lock);
  	nfs_access_free_list(&head);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2173
  }
1c606fb74   Bryan Schumaker   NFS: Convert v3 i...
2174
  EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2175

1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
  static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  {
  	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  	struct nfs_access_entry *entry;
  
  	while (n != NULL) {
  		entry = rb_entry(n, struct nfs_access_entry, rb_node);
  
  		if (cred < entry->cred)
  			n = n->rb_left;
  		else if (cred > entry->cred)
  			n = n->rb_right;
  		else
  			return entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2190
  	}
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2191
2192
  	return NULL;
  }
57b691819   Trond Myklebust   NFS: Cache access...
2193
  static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block)
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2194
2195
2196
  {
  	struct nfs_inode *nfsi = NFS_I(inode);
  	struct nfs_access_entry *cache;
57b691819   Trond Myklebust   NFS: Cache access...
2197
2198
  	bool retry = true;
  	int err;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2199

dc59250c6   Chuck Lever   [PATCH] NFS: Intr...
2200
  	spin_lock(&inode->i_lock);
57b691819   Trond Myklebust   NFS: Cache access...
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
  	for(;;) {
  		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  			goto out_zap;
  		cache = nfs_access_search_rbtree(inode, cred);
  		err = -ENOENT;
  		if (cache == NULL)
  			goto out;
  		/* Found an entry, is our attribute cache valid? */
  		if (!nfs_attribute_cache_expired(inode) &&
  		    !(nfsi->cache_validity & NFS_INO_INVALID_ATTR))
  			break;
  		err = -ECHILD;
  		if (!may_block)
  			goto out;
  		if (!retry)
  			goto out_zap;
  		spin_unlock(&inode->i_lock);
  		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  		if (err)
  			return err;
  		spin_lock(&inode->i_lock);
  		retry = false;
  	}
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2224
2225
2226
  	res->jiffies = cache->jiffies;
  	res->cred = cache->cred;
  	res->mask = cache->mask;
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2227
  	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2228
2229
2230
2231
  	err = 0;
  out:
  	spin_unlock(&inode->i_lock);
  	return err;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2232
  out_zap:
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2233
2234
  	spin_unlock(&inode->i_lock);
  	nfs_access_zap_cache(inode);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2235
2236
  	return -ENOENT;
  }
f682a398b   NeilBrown   NFS: allow lockle...
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
  static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  {
  	/* Only check the most recently returned cache entry,
  	 * but do it without locking.
  	 */
  	struct nfs_inode *nfsi = NFS_I(inode);
  	struct nfs_access_entry *cache;
  	int err = -ECHILD;
  	struct list_head *lh;
  
  	rcu_read_lock();
  	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  		goto out;
  	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
  	cache = list_entry(lh, struct nfs_access_entry, lru);
  	if (lh == &nfsi->access_cache_entry_lru ||
  	    cred != cache->cred)
  		cache = NULL;
  	if (cache == NULL)
  		goto out;
57b691819   Trond Myklebust   NFS: Cache access...
2257
2258
  	err = nfs_revalidate_inode_rcu(NFS_SERVER(inode), inode);
  	if (err)
f682a398b   NeilBrown   NFS: allow lockle...
2259
2260
2261
2262
  		goto out;
  	res->jiffies = cache->jiffies;
  	res->cred = cache->cred;
  	res->mask = cache->mask;
f682a398b   NeilBrown   NFS: allow lockle...
2263
2264
2265
2266
  out:
  	rcu_read_unlock();
  	return err;
  }
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2267
2268
  static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  {
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2269
2270
  	struct nfs_inode *nfsi = NFS_I(inode);
  	struct rb_root *root_node = &nfsi->access_cache;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
  	struct rb_node **p = &root_node->rb_node;
  	struct rb_node *parent = NULL;
  	struct nfs_access_entry *entry;
  
  	spin_lock(&inode->i_lock);
  	while (*p != NULL) {
  		parent = *p;
  		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  
  		if (set->cred < entry->cred)
  			p = &parent->rb_left;
  		else if (set->cred > entry->cred)
  			p = &parent->rb_right;
  		else
  			goto found;
  	}
  	rb_link_node(&set->rb_node, parent, p);
  	rb_insert_color(&set->rb_node, root_node);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2289
  	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
dc59250c6   Chuck Lever   [PATCH] NFS: Intr...
2290
  	spin_unlock(&inode->i_lock);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2291
2292
2293
  	return;
  found:
  	rb_replace_node(parent, &set->rb_node, root_node);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2294
2295
  	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  	list_del(&entry->lru);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2296
2297
2298
  	spin_unlock(&inode->i_lock);
  	nfs_access_free_entry(entry);
  }
6168f62cb   Weston Andros Adamson   NFSv4: Add ACCESS...
2299
  void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2300
2301
2302
2303
2304
  {
  	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  	if (cache == NULL)
  		return;
  	RB_CLEAR_NODE(&cache->rb_node);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2305
  	cache->jiffies = set->jiffies;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2306
  	cache->cred = get_rpccred(set->cred);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2307
  	cache->mask = set->mask;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2308

f682a398b   NeilBrown   NFS: allow lockle...
2309
2310
2311
2312
2313
  	/* The above field assignments must be visible
  	 * before this item appears on the lru.  We cannot easily
  	 * use rcu_assign_pointer, so just force the memory barrier.
  	 */
  	smp_wmb();
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2314
  	nfs_access_add_rbtree(inode, cache);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2315
2316
  
  	/* Update accounting */
4e857c58e   Peter Zijlstra   arch: Mass conver...
2317
  	smp_mb__before_atomic();
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2318
  	atomic_long_inc(&nfs_access_nr_entries);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2319
  	smp_mb__after_atomic();
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2320
2321
  
  	/* Add inode to global LRU list */
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2322
  	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2323
  		spin_lock(&nfs_access_lru_lock);
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2324
2325
2326
  		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  					&nfs_access_lru_list);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2327
2328
  		spin_unlock(&nfs_access_lru_lock);
  	}
3a505845c   Trond Myklebust   NFS: Enforce an u...
2329
  	nfs_access_cache_enforce_limit();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2330
  }
6168f62cb   Weston Andros Adamson   NFSv4: Add ACCESS...
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
  EXPORT_SYMBOL_GPL(nfs_access_add_cache);
  
  void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
  {
  	entry->mask = 0;
  	if (access_result & NFS4_ACCESS_READ)
  		entry->mask |= MAY_READ;
  	if (access_result &
  	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  		entry->mask |= MAY_WRITE;
  	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  		entry->mask |= MAY_EXEC;
  }
  EXPORT_SYMBOL_GPL(nfs_access_set_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2345
2346
2347
2348
  
  static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  {
  	struct nfs_access_entry cache;
57b691819   Trond Myklebust   NFS: Cache access...
2349
  	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2350
  	int status;
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2351
  	trace_nfs_access_enter(inode);
f682a398b   NeilBrown   NFS: allow lockle...
2352
2353
  	status = nfs_access_get_cached_rcu(inode, cred, &cache);
  	if (status != 0)
57b691819   Trond Myklebust   NFS: Cache access...
2354
  		status = nfs_access_get_cached(inode, cred, &cache, may_block);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2355
  	if (status == 0)
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2356
  		goto out_cached;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2357

f3324a2a9   NeilBrown   NFS: support RCU_...
2358
  	status = -ECHILD;
57b691819   Trond Myklebust   NFS: Cache access...
2359
  	if (!may_block)
f3324a2a9   NeilBrown   NFS: support RCU_...
2360
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2361
2362
2363
2364
2365
  	/* Be clever: ask server to check for all possible rights */
  	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  	cache.cred = cred;
  	cache.jiffies = jiffies;
  	status = NFS_PROTO(inode)->access(inode, &cache);
a71ee337b   Suresh Jayaraman   NFS: Handle -ESTA...
2366
2367
2368
2369
2370
2371
  	if (status != 0) {
  		if (status == -ESTALE) {
  			nfs_zap_caches(inode);
  			if (!S_ISDIR(inode->i_mode))
  				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  		}
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2372
  		goto out;
a71ee337b   Suresh Jayaraman   NFS: Handle -ESTA...
2373
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2374
  	nfs_access_add_cache(inode, &cache);
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2375
2376
2377
  out_cached:
  	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
  		status = -EACCES;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2378
  out:
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2379
2380
  	trace_nfs_access_exit(inode, status);
  	return status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2381
  }
af22f94ae   Trond Myklebust   NFSv4: Simplify _...
2382
2383
2384
  static int nfs_open_permission_mask(int openflags)
  {
  	int mask = 0;
f8d9a897d   Weston Andros Adamson   NFS: Fix access t...
2385
2386
2387
2388
2389
2390
2391
2392
2393
  	if (openflags & __FMODE_EXEC) {
  		/* ONLY check exec rights */
  		mask = MAY_EXEC;
  	} else {
  		if ((openflags & O_ACCMODE) != O_WRONLY)
  			mask |= MAY_READ;
  		if ((openflags & O_ACCMODE) != O_RDONLY)
  			mask |= MAY_WRITE;
  	}
af22f94ae   Trond Myklebust   NFSv4: Simplify _...
2394
2395
2396
2397
2398
2399
2400
  	return mask;
  }
  
  int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  {
  	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
2401
  EXPORT_SYMBOL_GPL(nfs_may_open);
af22f94ae   Trond Myklebust   NFSv4: Simplify _...
2402

5c5fc09a1   Trond Myklebust   NFS: Ensure we re...
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
  static int nfs_execute_ok(struct inode *inode, int mask)
  {
  	struct nfs_server *server = NFS_SERVER(inode);
  	int ret;
  
  	if (mask & MAY_NOT_BLOCK)
  		ret = nfs_revalidate_inode_rcu(server, inode);
  	else
  		ret = nfs_revalidate_inode(server, inode);
  	if (ret == 0 && !execute_ok(inode))
  		ret = -EACCES;
  	return ret;
  }
10556cb21   Al Viro   ->permission() sa...
2416
  int nfs_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2417
2418
2419
  {
  	struct rpc_cred *cred;
  	int res = 0;
91d5b4702   Chuck Lever   NFS: add I/O perf...
2420
  	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
e6305c43e   Al Viro   [PATCH] sanitize ...
2421
  	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2422
2423
  		goto out;
  	/* Is this sys_access() ? */
9cfcac810   Eric Paris   vfs: re-introduce...
2424
  	if (mask & (MAY_ACCESS | MAY_CHDIR))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2425
2426
2427
2428
2429
2430
  		goto force_lookup;
  
  	switch (inode->i_mode & S_IFMT) {
  		case S_IFLNK:
  			goto out;
  		case S_IFREG:
762674f86   Trond Myklebust   NFSv4: Don't perf...
2431
2432
2433
  			if ((mask & MAY_OPEN) &&
  			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
  				return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
  			break;
  		case S_IFDIR:
  			/*
  			 * Optimize away all write operations, since the server
  			 * will check permissions when we perform the op.
  			 */
  			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  				goto out;
  	}
  
  force_lookup:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2445
2446
  	if (!NFS_PROTO(inode)->access)
  		goto out_notsup;
f3324a2a9   NeilBrown   NFS: support RCU_...
2447
2448
2449
2450
2451
2452
  	/* Always try fast lookups first */
  	rcu_read_lock();
  	cred = rpc_lookup_cred_nonblock();
  	if (!IS_ERR(cred))
  		res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2453
  		res = PTR_ERR(cred);
f3324a2a9   NeilBrown   NFS: support RCU_...
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
  	rcu_read_unlock();
  	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
  		/* Fast lookup failed, try the slow way */
  		cred = rpc_lookup_cred();
  		if (!IS_ERR(cred)) {
  			res = nfs_do_access(inode, cred, mask);
  			put_rpccred(cred);
  		} else
  			res = PTR_ERR(cred);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2464
  out:
5c5fc09a1   Trond Myklebust   NFS: Ensure we re...
2465
2466
  	if (!res && (mask & MAY_EXEC))
  		res = nfs_execute_ok(inode, mask);
f696a3659   Miklos Szeredi   [PATCH] move exec...
2467

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
2468
2469
  	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d
  ",
1e7cb3dc1   Chuck Lever   NFS: directory tr...
2470
  		inode->i_sb->s_id, inode->i_ino, mask, res);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2471
2472
  	return res;
  out_notsup:
d51ac1a8e   NeilBrown   NFS: prepare for ...
2473
2474
  	if (mask & MAY_NOT_BLOCK)
  		return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2475
2476
  	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  	if (res == 0)
2830ba7f3   Al Viro   ->permission() sa...
2477
  		res = generic_permission(inode, mask);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
2478
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2479
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
2480
  EXPORT_SYMBOL_GPL(nfs_permission);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2481
2482
2483
2484
2485
2486
2487
  
  /*
   * Local variables:
   *  version-control: t
   *  kept-new-versions: 5
   * End:
   */