Blame view

fs/nfs/dir.c 65.1 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,
b044f6451   Benjamin Coddington   NFS: switch back ...
58
  	.iterate	= 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;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
116
117
  out:
  	put_rpccred(cred);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
  	return res;
  }
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
120
121
122
  static int
  nfs_closedir(struct inode *inode, struct file *filp)
  {
a455589f1   Al Viro   assorted conversi...
123
  	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
124
125
  	return 0;
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
126
127
128
129
  struct nfs_cache_array_entry {
  	u64 cookie;
  	u64 ino;
  	struct qstr string;
0b26a0bf6   Trond Myklebust   NFS: Ensure we re...
130
  	unsigned char d_type;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
131
132
133
  };
  
  struct nfs_cache_array {
88b8e133c   Chuck Lever   NFS: Make nfs_cac...
134
  	int size;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
135
136
137
138
  	int eof_index;
  	u64 last_cookie;
  	struct nfs_cache_array_entry array[0];
  };
a7a3b1e97   Benjamin Coddington   NFS: convert flag...
139
  typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
  typedef struct {
  	struct file	*file;
  	struct page	*page;
23db86206   Al Viro   [readdir] convert...
143
  	struct dir_context *ctx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
  	unsigned long	page_index;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
145
  	u64		*dir_cookie;
0aded708d   Trond Myklebust   NFS: Ensure we us...
146
  	u64		last_cookie;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
147
  	loff_t		current_index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
  	decode_dirent_t	decode;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
149

1f4eab7e7   Neil Brown   NFS: Set meaningf...
150
  	unsigned long	timestamp;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
151
  	unsigned long	gencount;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
152
  	unsigned int	cache_entry_index;
a7a3b1e97   Benjamin Coddington   NFS: convert flag...
153
154
  	bool plus;
  	bool eof;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  } nfs_readdir_descriptor_t;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
156
  /*
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
157
158
159
   * we are freeing strings created by nfs_add_to_readdir_array()
   */
  static
11de3b11e   Trond Myklebust   NFS: Fix a memory...
160
  void nfs_readdir_clear_array(struct page *page)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
161
  {
11de3b11e   Trond Myklebust   NFS: Fix a memory...
162
  	struct nfs_cache_array *array;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
163
  	int i;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
164

2b86ce2db   Cong Wang   nfs: remove the s...
165
  	array = kmap_atomic(page);
b044f6451   Benjamin Coddington   NFS: switch back ...
166
167
  	for (i = 0; i < array->size; i++)
  		kfree(array->array[i].string.name);
2b86ce2db   Cong Wang   nfs: remove the s...
168
  	kunmap_atomic(array);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
169
170
171
172
173
174
175
176
  }
  
  /*
   * 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...
177
  int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
178
179
180
  {
  	string->len = len;
  	string->name = kmemdup(name, len, GFP_KERNEL);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
181
182
  	if (string->name == NULL)
  		return -ENOMEM;
04e4bd1c6   Catalin Marinas   nfs: Ignore kmeml...
183
184
185
186
187
  	/*
  	 * 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...
188
  	string->hash = full_name_hash(NULL, name, len);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
189
  	return 0;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
190
191
192
193
194
  }
  
  static
  int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  {
0795bf835   Fabian Frederick   nfs: use kmap/kun...
195
  	struct nfs_cache_array *array = kmap(page);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
196
197
  	struct nfs_cache_array_entry *cache_entry;
  	int ret;
3020093f5   Trond Myklebust   NFS: Correct the ...
198
199
200
  	cache_entry = &array->array[array->size];
  
  	/* Check that this entry lies within the page bounds */
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
201
  	ret = -ENOSPC;
3020093f5   Trond Myklebust   NFS: Correct the ...
202
  	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
203
  		goto out;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
204

4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
205
206
  	cache_entry->cookie = entry->prev_cookie;
  	cache_entry->ino = entry->ino;
0b26a0bf6   Trond Myklebust   NFS: Ensure we re...
207
  	cache_entry->d_type = entry->d_type;
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
208
209
210
  	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  	if (ret)
  		goto out;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
211
  	array->last_cookie = entry->cookie;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
212
  	array->size++;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
213
  	if (entry->eof != 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
214
  		array->eof_index = array->size;
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
215
  out:
0795bf835   Fabian Frederick   nfs: use kmap/kun...
216
  	kunmap(page);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
217
  	return ret;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
218
219
220
221
222
  }
  
  static
  int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  {
23db86206   Al Viro   [readdir] convert...
223
  	loff_t diff = desc->ctx->pos - desc->current_index;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
224
225
226
227
228
  	unsigned int index;
  
  	if (diff < 0)
  		goto out_eof;
  	if (diff >= array->size) {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
229
  		if (array->eof_index >= 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
230
  			goto out_eof;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
231
232
233
234
235
236
  		return -EAGAIN;
  	}
  
  	index = (unsigned int)diff;
  	*desc->dir_cookie = array->array[index].cookie;
  	desc->cache_entry_index = index;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
237
238
  	return 0;
  out_eof:
6089dd0d7   Thomas Meyer   NFS: Fix bool ini...
239
  	desc->eof = true;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
240
241
  	return -EBADCOOKIE;
  }
4db72b40f   Jeff Layton   nfs: add memory b...
242
243
244
245
246
247
248
249
  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 ...
250
251
252
253
  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...
254
  	loff_t new_pos;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
255
256
257
  	int status = -EAGAIN;
  
  	for (i = 0; i < array->size; i++) {
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
258
  		if (array->array[i].cookie == *desc->dir_cookie) {
496ad9aa8   Al Viro   new helper: file_...
259
  			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
0c0308066   Trond Myklebust   NFS: Fix spurious...
260
  			struct nfs_open_dir_context *ctx = desc->file->private_data;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
261
  			new_pos = desc->current_index + i;
4db72b40f   Jeff Layton   nfs: add memory b...
262
263
  			if (ctx->attr_gencount != nfsi->attr_gencount ||
  			    !nfs_readdir_inode_mapping_valid(nfsi)) {
0c0308066   Trond Myklebust   NFS: Fix spurious...
264
265
  				ctx->duped = 0;
  				ctx->attr_gencount = nfsi->attr_gencount;
23db86206   Al Viro   [readdir] convert...
266
  			} else if (new_pos < desc->ctx->pos) {
0c0308066   Trond Myklebust   NFS: Fix spurious...
267
268
269
  				if (ctx->duped > 0
  				    && ctx->dup_cookie == *desc->dir_cookie) {
  					if (printk_ratelimit()) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
270
  						pr_notice("NFS: directory %pD2 contains a readdir loop."
0c0308066   Trond Myklebust   NFS: Fix spurious...
271
  								"Please contact your server vendor.  "
9581a4ae7   Jeff Layton   nfs: pass string ...
272
273
274
275
  								"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...
276
277
278
279
  					}
  					status = -ELOOP;
  					goto out;
  				}
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
280
  				ctx->dup_cookie = *desc->dir_cookie;
0c0308066   Trond Myklebust   NFS: Fix spurious...
281
  				ctx->duped = -1;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
282
  			}
23db86206   Al Viro   [readdir] convert...
283
  			desc->ctx->pos = new_pos;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
284
  			desc->cache_entry_index = i;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
285
  			return 0;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
286
287
  		}
  	}
47c716cbf   Trond Myklebust   NFS: Readdir clea...
288
  	if (array->eof_index >= 0) {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
289
  		status = -EBADCOOKIE;
18fb5fe40   Trond Myklebust   NFS: nfs_readdir_...
290
  		if (*desc->dir_cookie == array->last_cookie)
6089dd0d7   Thomas Meyer   NFS: Fix bool ini...
291
  			desc->eof = true;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
292
  	}
0c0308066   Trond Myklebust   NFS: Fix spurious...
293
  out:
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
294
295
296
297
298
299
300
  	return status;
  }
  
  static
  int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  {
  	struct nfs_cache_array *array;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
301
  	int status;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
302

0795bf835   Fabian Frederick   nfs: use kmap/kun...
303
  	array = kmap(desc->page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
304
305
306
307
308
  
  	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...
309
  	if (status == -EAGAIN) {
0aded708d   Trond Myklebust   NFS: Ensure we us...
310
  		desc->last_cookie = array->last_cookie;
e47c085af   Trond Myklebust   NFS: Ensure that ...
311
  		desc->current_index += array->size;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
312
313
  		desc->page_index++;
  	}
0795bf835   Fabian Frederick   nfs: use kmap/kun...
314
  	kunmap(desc->page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
315
316
317
318
319
  	return status;
  }
  
  /* Fill a page with xdr information before transferring to the cache page */
  static
56e4ebf87   Bryan Schumaker   NFS: readdir with...
320
  int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
321
  			struct nfs_entry *entry, struct file *file, struct inode *inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
  {
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
323
324
  	struct nfs_open_dir_context *ctx = file->private_data;
  	struct rpc_cred	*cred = ctx->cred;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
325
  	unsigned long	timestamp, gencount;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  	int		error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
   again:
  	timestamp = jiffies;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
329
  	gencount = nfs_inc_attr_generation_counter();
be62a1a8f   Miklos Szeredi   nfs: use file_den...
330
  	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
331
332
333
334
335
  					  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...
336
  			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
a7a3b1e97   Benjamin Coddington   NFS: convert flag...
337
  			desc->plus = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
  			goto again;
  		}
  		goto error;
  	}
1f4eab7e7   Neil Brown   NFS: Set meaningf...
342
  	desc->timestamp = timestamp;
4704f0e27   Trond Myklebust   NFS: Fix the reso...
343
  	desc->gencount = gencount;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
344
345
  error:
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
  }
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
347
348
  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
349
  {
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
350
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351

573c4e1ef   Chuck Lever   NFS: Simplify ->d...
352
353
354
  	error = desc->decode(xdr, entry, desc->plus);
  	if (error)
  		return error;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
355
356
357
  	entry->fattr->time_start = desc->timestamp;
  	entry->fattr->gencount = desc->gencount;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
  }
fa9233699   Trond Myklebust   NFS: Don't requir...
359
360
361
  /* Match file and dirent using either filehandle or fileid
   * Note: caller is responsible for checking the fsid
   */
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
362
363
364
  static
  int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  {
d8fdb47fa   Trond Myklebust   NFS: Don't let re...
365
  	struct inode *inode;
fa9233699   Trond Myklebust   NFS: Don't requir...
366
  	struct nfs_inode *nfsi;
2b0143b5c   David Howells   VFS: normal files...
367
368
  	if (d_really_is_negative(dentry))
  		return 0;
fa9233699   Trond Myklebust   NFS: Don't requir...
369

d8fdb47fa   Trond Myklebust   NFS: Don't let re...
370
371
372
373
374
  	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...
375
376
377
378
379
  	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...
380
381
382
  }
  
  static
23db86206   Al Viro   [readdir] convert...
383
  bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
384
385
386
387
388
  {
  	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...
389
  	if (ctx->pos == 0)
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
390
391
392
393
394
  		return true;
  	return false;
  }
  
  /*
63519fbc6   Trond Myklebust   NFS: Be more targ...
395
396
   * This function is called by the lookup and getattr code to request the
   * use of readdirplus to accelerate any future lookups in the same
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
397
398
   * directory.
   */
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
399
400
  void nfs_advise_use_readdirplus(struct inode *dir)
  {
63519fbc6   Trond Myklebust   NFS: Be more targ...
401
402
403
404
405
  	struct nfs_inode *nfsi = NFS_I(dir);
  
  	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
  	    !list_empty(&nfsi->open_files))
  		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
406
  }
311324ad1   Trond Myklebust   NFS: Be more aggr...
407
408
409
410
411
412
413
414
415
416
  /*
   * 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)
  {
63519fbc6   Trond Myklebust   NFS: Be more targ...
417
418
419
420
421
  	struct nfs_inode *nfsi = NFS_I(dir);
  
  	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
  	    !list_empty(&nfsi->open_files)) {
  		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
79f687a3d   Trond Myklebust   NFS: Fix a perfor...
422
  		invalidate_mapping_pages(dir->i_mapping, 0, -1);
311324ad1   Trond Myklebust   NFS: Be more aggr...
423
424
  	}
  }
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
425
  static
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
426
427
  void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  {
26fe57502   Linus Torvalds   vfs: make it poss...
428
  	struct qstr filename = QSTR_INIT(entry->name, entry->len);
9ac3d3e84   Al Viro   nfs: switch to ->...
429
  	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
430
431
  	struct dentry *dentry;
  	struct dentry *alias;
2b0143b5c   David Howells   VFS: normal files...
432
  	struct inode *dir = d_inode(parent);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
433
  	struct inode *inode;
aa9c26696   David Quigley   NFS: Client imple...
434
  	int status;
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
435

fa9233699   Trond Myklebust   NFS: Don't requir...
436
437
  	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
  		return;
6c441c254   Trond Myklebust   NFS: Don't invali...
438
439
  	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
  		return;
78d04af49   Trond Myklebust   NFS: nfs_prime_dc...
440
441
442
443
444
445
446
447
  	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...
448
449
450
451
452
453
  	if (filename.name[0] == '.') {
  		if (filename.len == 1)
  			return;
  		if (filename.len == 2 && filename.name[1] == '.')
  			return;
  	}
8387ff257   Linus Torvalds   vfs: make the str...
454
  	filename.hash = full_name_hash(parent, filename.name, filename.len);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
455

4a201d6e3   Trond Myklebust   NFS: Ensure we ch...
456
  	dentry = d_lookup(parent, &filename);
9ac3d3e84   Al Viro   nfs: switch to ->...
457
458
459
460
461
462
463
  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...
464
465
466
467
  		/* 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...
468
  		if (nfs_same_file(dentry, entry)) {
7dc72d5f7   Trond Myklebust   NFS: Fix inode co...
469
470
  			if (!entry->fh->size)
  				goto out;
cda57a1ef   Jeff Layton   nfs: set verifier...
471
  			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2b0143b5c   David Howells   VFS: normal files...
472
  			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
aa9c26696   David Quigley   NFS: Client imple...
473
  			if (!status)
2b0143b5c   David Howells   VFS: normal files...
474
  				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
475
476
  			goto out;
  		} else {
5542aa2fa   Eric W. Biederman   vfs: Make d_inval...
477
  			d_invalidate(dentry);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
478
  			dput(dentry);
9ac3d3e84   Al Viro   nfs: switch to ->...
479
480
  			dentry = NULL;
  			goto again;
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
481
482
  		}
  	}
7dc72d5f7   Trond Myklebust   NFS: Fix inode co...
483
484
485
486
  	if (!entry->fh->size) {
  		d_lookup_done(dentry);
  		goto out;
  	}
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
487

1775fd3e8   David Quigley   NFS:Add labels to...
488
  	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
41d28bca2   Al Viro   switch d_material...
489
  	alias = d_splice_alias(inode, dentry);
9ac3d3e84   Al Viro   nfs: switch to ->...
490
491
492
493
494
495
496
497
  	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...
498
499
  out:
  	dput(dentry);
d39ab9de3   Bryan Schumaker   NFS: re-add readd...
500
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
501
502
  /* Perform conversion from xdr to cache array */
  static
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
503
  int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
6650239a4   Trond Myklebust   NFS: Don't use vm...
504
  				struct page **xdr_pages, struct page *page, unsigned int buflen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
  {
babddc72a   Bryan Schumaker   NFS: decode_diren...
506
  	struct xdr_stream stream;
f7da7a129   Benny Halevy   SUNRPC: introduce...
507
  	struct xdr_buf buf;
6650239a4   Trond Myklebust   NFS: Don't use vm...
508
  	struct page *scratch;
994243808   Bryan Schumaker   NFS: check xdr_de...
509
  	struct nfs_cache_array *array;
5c346854d   Trond Myklebust   NFS: Assume eof i...
510
511
  	unsigned int count = 0;
  	int status;
babddc72a   Bryan Schumaker   NFS: decode_diren...
512

6650239a4   Trond Myklebust   NFS: Don't use vm...
513
514
515
  	scratch = alloc_page(GFP_KERNEL);
  	if (scratch == NULL)
  		return -ENOMEM;
babddc72a   Bryan Schumaker   NFS: decode_diren...
516

ce85cfbed   Benjamin Coddington   NFS: Don't attemp...
517
518
  	if (buflen == 0)
  		goto out_nopages;
f7da7a129   Benny Halevy   SUNRPC: introduce...
519
  	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
6650239a4   Trond Myklebust   NFS: Don't use vm...
520
  	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
994243808   Bryan Schumaker   NFS: check xdr_de...
521
522
523
  
  	do {
  		status = xdr_decode(desc, entry, &stream);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
524
525
526
  		if (status != 0) {
  			if (status == -EAGAIN)
  				status = 0;
994243808   Bryan Schumaker   NFS: check xdr_de...
527
  			break;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
528
  		}
994243808   Bryan Schumaker   NFS: check xdr_de...
529

5c346854d   Trond Myklebust   NFS: Assume eof i...
530
  		count++;
a7a3b1e97   Benjamin Coddington   NFS: convert flag...
531
  		if (desc->plus)
be62a1a8f   Miklos Szeredi   nfs: use file_den...
532
  			nfs_prime_dcache(file_dentry(desc->file), entry);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
533
534
535
536
  
  		status = nfs_readdir_add_to_array(entry, page);
  		if (status != 0)
  			break;
994243808   Bryan Schumaker   NFS: check xdr_de...
537
  	} while (!entry->eof);
ce85cfbed   Benjamin Coddington   NFS: Don't attemp...
538
  out_nopages:
47c716cbf   Trond Myklebust   NFS: Readdir clea...
539
  	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
0795bf835   Fabian Frederick   nfs: use kmap/kun...
540
541
542
543
  		array = kmap(page);
  		array->eof_index = array->size;
  		status = 0;
  		kunmap(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
544
  	}
6650239a4   Trond Myklebust   NFS: Don't use vm...
545
546
  
  	put_page(scratch);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
547
  	return status;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
548
549
550
  }
  
  static
c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
551
  void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
56e4ebf87   Bryan Schumaker   NFS: readdir with...
552
553
554
555
556
  {
  	unsigned int i;
  	for (i = 0; i < npages; i++)
  		put_page(pages[i]);
  }
56e4ebf87   Bryan Schumaker   NFS: readdir with...
557
558
  /*
   * nfs_readdir_large_page will allocate pages that must be freed with a call
0b936e37d   Anna Schumaker   NFS: Remove unuse...
559
   * to nfs_readdir_free_pagearray
56e4ebf87   Bryan Schumaker   NFS: readdir with...
560
561
   */
  static
c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
562
  int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
56e4ebf87   Bryan Schumaker   NFS: readdir with...
563
  {
56e4ebf87   Bryan Schumaker   NFS: readdir with...
564
565
566
567
568
569
570
571
  	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...
572
  	return 0;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
573

56e4ebf87   Bryan Schumaker   NFS: readdir with...
574
  out_freepages:
c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
575
  	nfs_readdir_free_pages(pages, i);
6650239a4   Trond Myklebust   NFS: Don't use vm...
576
  	return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
  }
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
578
579
  static
  int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
580
  {
56e4ebf87   Bryan Schumaker   NFS: readdir with...
581
  	struct page *pages[NFS_MAX_READDIR_PAGES];
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
582
583
584
  	struct nfs_entry entry;
  	struct file	*file = desc->file;
  	struct nfs_cache_array *array;
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
585
  	int status = -ENOMEM;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
586
  	unsigned int array_size = ARRAY_SIZE(pages);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
587
588
  
  	entry.prev_cookie = 0;
0aded708d   Trond Myklebust   NFS: Ensure we us...
589
  	entry.cookie = desc->last_cookie;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
590
591
592
  	entry.eof = 0;
  	entry.fh = nfs_alloc_fhandle();
  	entry.fattr = nfs_alloc_fattr();
573c4e1ef   Chuck Lever   NFS: Simplify ->d...
593
  	entry.server = NFS_SERVER(inode);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
594
595
  	if (entry.fh == NULL || entry.fattr == NULL)
  		goto out;
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
596

14c43f767   David Quigley   NFS: Add label li...
597
598
599
600
601
  	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  	if (IS_ERR(entry.label)) {
  		status = PTR_ERR(entry.label);
  		goto out;
  	}
0795bf835   Fabian Frederick   nfs: use kmap/kun...
602
  	array = kmap(page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
603
604
  	memset(array, 0, sizeof(struct nfs_cache_array));
  	array->eof_index = -1;
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
605

c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
606
  	status = nfs_readdir_alloc_pages(pages, array_size);
6650239a4   Trond Myklebust   NFS: Don't use vm...
607
  	if (status < 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
608
609
  		goto out_release_array;
  	do {
ac3961282   Trond Myklebust   NFS: readdir shou...
610
  		unsigned int pglen;
56e4ebf87   Bryan Schumaker   NFS: readdir with...
611
  		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
babddc72a   Bryan Schumaker   NFS: decode_diren...
612

d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
613
  		if (status < 0)
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
614
  			break;
ac3961282   Trond Myklebust   NFS: readdir shou...
615
  		pglen = status;
6650239a4   Trond Myklebust   NFS: Don't use vm...
616
  		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
617
618
619
620
621
622
  		if (status < 0) {
  			if (status == -ENOSPC)
  				status = 0;
  			break;
  		}
  	} while (array->eof_index < 0);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
623

c7e9668e7   Anna Schumaker   NFS: Rename nfs_r...
624
  	nfs_readdir_free_pages(pages, array_size);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
625
  out_release_array:
0795bf835   Fabian Frederick   nfs: use kmap/kun...
626
  	kunmap(page);
14c43f767   David Quigley   NFS: Add label li...
627
  	nfs4_label_free(entry.label);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
628
629
630
  out:
  	nfs_free_fattr(entry.fattr);
  	nfs_free_fhandle(entry.fh);
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
631
632
633
634
  	return status;
  }
  
  /*
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
635
636
637
638
   * 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
639
   */
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
640
641
  static
  int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
  {
496ad9aa8   Al Viro   new helper: file_...
643
  	struct inode	*inode = file_inode(desc->file);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
644
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
645

8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
646
647
  	ret = nfs_readdir_xdr_to_array(desc, page, inode);
  	if (ret < 0)
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
648
649
  		goto error;
  	SetPageUptodate(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650

d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
651
652
653
  	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
654
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
655
656
657
658
  	unlock_page(page);
  	return 0;
   error:
  	unlock_page(page);
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
659
  	return ret;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
660
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661

d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
662
663
664
  static
  void cache_page_release(nfs_readdir_descriptor_t *desc)
  {
b044f6451   Benjamin Coddington   NFS: switch back ...
665
666
  	if (!desc->page->mapping)
  		nfs_readdir_clear_array(desc->page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
667
  	put_page(desc->page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
668
669
670
671
672
673
  	desc->page = NULL;
  }
  
  static
  struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  {
b044f6451   Benjamin Coddington   NFS: switch back ...
674
  	return read_cache_page(desc->file->f_mapping,
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
675
  			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
677
678
  }
  
  /*
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
679
   * Returns 0 if desc->dir_cookie was found on page desc->page_index
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
   */
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
681
682
683
684
685
686
687
688
689
690
  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...
691
692
  	if (res != 0)
  		cache_page_release(desc);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
693
694
695
696
  	return res;
  }
  
  /* Search for desc->dir_cookie from the beginning of the page cache */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
698
699
  static inline
  int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
700
  	int res;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
701

0aded708d   Trond Myklebust   NFS: Ensure we us...
702
  	if (desc->page_index == 0) {
8cd51a0cc   Trond Myklebust   NFS: Fix a couple...
703
  		desc->current_index = 0;
0aded708d   Trond Myklebust   NFS: Ensure we us...
704
705
  		desc->last_cookie = 0;
  	}
47c716cbf   Trond Myklebust   NFS: Readdir clea...
706
  	do {
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
707
  		res = find_cache_page(desc);
47c716cbf   Trond Myklebust   NFS: Readdir clea...
708
  	} while (res == -EAGAIN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
  	return res;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
714
  /*
   * Once we've found the start of the dirent within a page: fill 'er up...
   */
  static 
23db86206   Al Viro   [readdir] convert...
715
  int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
717
  {
  	struct file	*file = desc->file;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
718
719
720
  	int i = 0;
  	int res = 0;
  	struct nfs_cache_array *array = NULL;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
721
  	struct nfs_open_dir_context *ctx = file->private_data;
0795bf835   Fabian Frederick   nfs: use kmap/kun...
722
  	array = kmap(desc->page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
723
  	for (i = desc->cache_entry_index; i < array->size; i++) {
ece0b4233   Trond Myklebust   NFS: Don't ignore...
724
  		struct nfs_cache_array_entry *ent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725

ece0b4233   Trond Myklebust   NFS: Don't ignore...
726
  		ent = &array->array[i];
23db86206   Al Viro   [readdir] convert...
727
728
  		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
  		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
6089dd0d7   Thomas Meyer   NFS: Fix bool ini...
729
  			desc->eof = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
  			break;
ece0b4233   Trond Myklebust   NFS: Don't ignore...
731
  		}
23db86206   Al Viro   [readdir] convert...
732
  		desc->ctx->pos++;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
733
734
735
736
  		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...
737
738
  		if (ctx->duped != 0)
  			ctx->duped = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
  	}
47c716cbf   Trond Myklebust   NFS: Readdir clea...
740
  	if (array->eof_index >= 0)
6089dd0d7   Thomas Meyer   NFS: Fix bool ini...
741
  		desc->eof = true;
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
742

0795bf835   Fabian Frederick   nfs: use kmap/kun...
743
  	kunmap(desc->page);
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
744
  	cache_page_release(desc);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
745
746
747
  	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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
  	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...
764
  int uncached_readdir(nfs_readdir_descriptor_t *desc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
767
  	struct page	*page = NULL;
  	int		status;
496ad9aa8   Al Viro   new helper: file_...
768
  	struct inode *inode = file_inode(desc->file);
0c0308066   Trond Myklebust   NFS: Fix spurious...
769
  	struct nfs_open_dir_context *ctx = desc->file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770

1e7cb3dc1   Chuck Lever   NFS: directory tr...
771
772
773
  	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu
  ",
  			(unsigned long long)*desc->dir_cookie);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
776
777
778
779
  
  	page = alloc_page(GFP_HIGHUSER);
  	if (!page) {
  		status = -ENOMEM;
  		goto out;
  	}
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
780

7a8e1dc34   Trond Myklebust   NFS: Fix a page l...
781
  	desc->page_index = 0;
0aded708d   Trond Myklebust   NFS: Ensure we us...
782
  	desc->last_cookie = *desc->dir_cookie;
7a8e1dc34   Trond Myklebust   NFS: Fix a page l...
783
  	desc->page = page;
0c0308066   Trond Myklebust   NFS: Fix spurious...
784
  	ctx->duped = 0;
7a8e1dc34   Trond Myklebust   NFS: Fix a page l...
785

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
   out:
1e7cb3dc1   Chuck Lever   NFS: directory tr...
792
793
  	dfprintk(DIRCACHE, "NFS: %s: returns %d
  ",
3110ff804   Harvey Harrison   nfs: replace rema...
794
  			__func__, status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
795
796
  	return status;
   out_release:
d1bacf9eb   Bryan Schumaker   NFS: add readdir ...
797
  	cache_page_release(desc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
799
  	goto out;
  }
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
800
801
802
  /* 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
803
   */
23db86206   Al Viro   [readdir] convert...
804
  static int nfs_readdir(struct file *file, struct dir_context *ctx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
  {
be62a1a8f   Miklos Szeredi   nfs: use file_den...
806
  	struct dentry	*dentry = file_dentry(file);
2b0143b5c   David Howells   VFS: normal files...
807
  	struct inode	*inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
809
  	nfs_readdir_descriptor_t my_desc,
  			*desc = &my_desc;
23db86206   Al Viro   [readdir] convert...
810
  	struct nfs_open_dir_context *dir_ctx = file->private_data;
07b5ce8ef   Scott Mayhew   NFS: Make nfs_rea...
811
  	int res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812

6de1472f1   Al Viro   nfs: use %p[dD] i...
813
814
815
  	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu
  ",
  			file, (long long)ctx->pos);
91d5b4702   Chuck Lever   NFS: add I/O perf...
816
  	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
817
  	/*
23db86206   Al Viro   [readdir] convert...
818
  	 * ctx->pos points to the dirent entry number.
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
819
  	 * *desc->dir_cookie has the cookie for the next entry. We have
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
820
821
  	 * to either find the entry with the appropriate number or
  	 * revalidate the cookie.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
822
823
  	 */
  	memset(desc, 0, sizeof(*desc));
23db86206   Al Viro   [readdir] convert...
824
825
  	desc->file = file;
  	desc->ctx = ctx;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
826
  	desc->dir_cookie = &dir_ctx->dir_cookie;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
  	desc->decode = NFS_PROTO(inode)->decode_dirent;
a7a3b1e97   Benjamin Coddington   NFS: convert flag...
828
  	desc->plus = nfs_use_readdirplus(inode, ctx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
829

79f687a3d   Trond Myklebust   NFS: Fix a perfor...
830
  	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
07b5ce8ef   Scott Mayhew   NFS: Make nfs_rea...
831
  		res = nfs_revalidate_mapping(inode, file->f_mapping);
fccca7fc6   Trond Myklebust   NFS: Fix a sillyr...
832
833
  	if (res < 0)
  		goto out;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
834
  	do {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
  		res = readdir_search_pagecache(desc);
00a926422   Olivier Galibert   [PATCH] NFS: Hide...
836

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
  		if (res == -EBADCOOKIE) {
ece0b4233   Trond Myklebust   NFS: Don't ignore...
838
  			res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
  			/* This means either end of directory */
6089dd0d7   Thomas Meyer   NFS: Fix bool ini...
840
  			if (*desc->dir_cookie && !desc->eof) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
  				/* Or that the server has 'lost' a cookie */
23db86206   Al Viro   [readdir] convert...
842
  				res = uncached_readdir(desc);
ece0b4233   Trond Myklebust   NFS: Don't ignore...
843
  				if (res == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
845
  					continue;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
847
848
  			break;
  		}
  		if (res == -ETOOSMALL && desc->plus) {
3a10c30ac   Benny Halevy   nfs: obliterate N...
849
  			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
  			nfs_zap_caches(inode);
baf57a09e   Trond Myklebust   NFS: Optimise the...
851
  			desc->page_index = 0;
a7a3b1e97   Benjamin Coddington   NFS: convert flag...
852
853
  			desc->plus = false;
  			desc->eof = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
855
856
857
  			continue;
  		}
  		if (res < 0)
  			break;
23db86206   Al Viro   [readdir] convert...
858
  		res = nfs_do_filldir(desc);
ece0b4233   Trond Myklebust   NFS: Don't ignore...
859
  		if (res < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
860
  			break;
47c716cbf   Trond Myklebust   NFS: Readdir clea...
861
  	} while (!desc->eof);
fccca7fc6   Trond Myklebust   NFS: Fix a sillyr...
862
  out:
1e7cb3dc1   Chuck Lever   NFS: directory tr...
863
864
  	if (res > 0)
  		res = 0;
6de1472f1   Al Viro   nfs: use %p[dD] i...
865
866
  	dfprintk(FILE, "NFS: readdir(%pD2) returns %d
  ", file, res);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
867
  	return res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
  }
965c8e59c   Andrew Morton   lseek: the "whenc...
869
  static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
870
  {
b044f6451   Benjamin Coddington   NFS: switch back ...
871
  	struct inode *inode = file_inode(filp);
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
872
  	struct nfs_open_dir_context *dir_ctx = filp->private_data;
b84e06c58   Chuck Lever   NFS: Make nfs_lls...
873

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

965c8e59c   Andrew Morton   lseek: the "whenc...
878
  	switch (whence) {
b2b1ff3da   Trond Myklebust   NFS: Allow optimi...
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
  	default:
  		return -EINVAL;
  	case SEEK_SET:
  		if (offset < 0)
  			return -EINVAL;
  		inode_lock(inode);
  		break;
  	case SEEK_CUR:
  		if (offset == 0)
  			return filp->f_pos;
  		inode_lock(inode);
  		offset += filp->f_pos;
  		if (offset < 0) {
  			inode_unlock(inode);
  			return -EINVAL;
  		}
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
895
896
897
  	}
  	if (offset != filp->f_pos) {
  		filp->f_pos = offset;
480c2006e   Bryan Schumaker   NFS: Create nfs_o...
898
  		dir_ctx->dir_cookie = 0;
8ef2ce3e1   Bryan Schumaker   NFS: Detect loops...
899
  		dir_ctx->duped = 0;
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
900
  	}
b044f6451   Benjamin Coddington   NFS: switch back ...
901
  	inode_unlock(inode);
f0dd2136d   Trond Myklebust   [PATCH] NFS: Clea...
902
903
  	return offset;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904
905
906
907
  /*
   * All directory operations under NFS are synchronous, so fsync()
   * is a dummy operation.
   */
02c24a821   Josef Bacik   fs: push i_mutex ...
908
909
  static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
  			 int datasync)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
911
  	struct inode *inode = file_inode(filp);
7ea808591   Christoph Hellwig   drop unused dentr...
912

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

5955102c9   Al Viro   wrappers for ->i_...
916
  	inode_lock(inode);
6de1472f1   Al Viro   nfs: use %p[dD] i...
917
  	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
5955102c9   Al Viro   wrappers for ->i_...
918
  	inode_unlock(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
920
  	return 0;
  }
bfc69a456   Trond Myklebust   NFS: define a fun...
921
922
923
924
925
926
927
928
929
930
931
932
  /**
   * 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...
933
  	NFS_I(dir)->cache_change_attribute++;
bfc69a456   Trond Myklebust   NFS: define a fun...
934
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
935
  EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
bfc69a456   Trond Myklebust   NFS: define a fun...
936

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
938
939
940
  /*
   * 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...
941
   * If rcu_walk prevents us from performing a full check, return 0.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
942
   */
912a108da   NeilBrown   NFS: teach nfs_ne...
943
944
  static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
  			      int rcu_walk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
946
947
  {
  	if (IS_ROOT(dentry))
  		return 1;
4eec952e4   Trond Myklebust   NFS: Add options ...
948
949
  	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  		return 0;
f2c77f4e6   Trond Myklebust   NFS: Optimise nfs...
950
951
952
  	if (!nfs_verify_change_attribute(dir, dentry->d_time))
  		return 0;
  	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1cd9cb05f   Trond Myklebust   NFS: Only look at...
953
954
955
956
957
958
  	if (nfs_mapping_need_revalidate_inode(dir)) {
  		if (rcu_walk)
  			return 0;
  		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  			return 0;
  	}
f2c77f4e6   Trond Myklebust   NFS: Optimise nfs...
959
960
961
  	if (!nfs_verify_change_attribute(dir, dentry->d_time))
  		return 0;
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
963
  /*
a12802cab   Trond Myklebust   NFS: Be strict ab...
964
965
966
   * 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...
967
  static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
a12802cab   Trond Myklebust   NFS: Be strict ab...
968
969
970
  {
  	if (NFS_PROTO(dir)->version == 2)
  		return 0;
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
971
  	return flags & LOOKUP_EXCL;
a12802cab   Trond Myklebust   NFS: Be strict ab...
972
973
974
  }
  
  /*
1d6757fbf   Trond Myklebust   [PATCH] NFS: Fix ...
975
976
977
978
979
980
981
   * 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...
982
  static
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
983
  int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
984
985
  {
  	struct nfs_server *server = NFS_SERVER(inode);
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
986
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987

36d43a437   David Howells   NFS: Use d_automo...
988
  	if (IS_AUTOMOUNT(inode))
4e99a1ff3   Trond Myklebust   NFS: Fix dentry r...
989
  		return 0;
479219218   Trond Myklebust   NFS: Optimise awa...
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  
  	if (flags & LOOKUP_OPEN) {
  		switch (inode->i_mode & S_IFMT) {
  		case S_IFREG:
  			/* A NFSv4 OPEN will revalidate later */
  			if (server->caps & NFS_CAP_ATOMIC_OPEN)
  				goto out;
  			/* Fallthrough */
  		case S_IFDIR:
  			if (server->flags & NFS_MOUNT_NOCTO)
  				break;
  			/* NFS close-to-open cache consistency validation */
  			goto out_force;
  		}
  	}
facc3530f   Al Viro   nfs_lookup_verify...
1005
  	/* VFS wants an on-the-wire revalidation */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1006
  	if (flags & LOOKUP_REVAL)
facc3530f   Al Viro   nfs_lookup_verify...
1007
  		goto out_force;
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
1008
  out:
a61246c96   Lance Shelton   Fix error code in...
1009
  	return (inode->i_nlink == 0) ? -ESTALE : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
  out_force:
1fa1e3844   NeilBrown   NFS: teach nfs_lo...
1011
1012
  	if (flags & LOOKUP_RCU)
  		return -ECHILD;
65a0c1495   Trond Myklebust   NFS: nfs_lookup_r...
1013
1014
1015
1016
  	ret = __nfs_revalidate_inode(server, inode);
  	if (ret != 0)
  		return ret;
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1017
1018
1019
1020
1021
1022
1023
1024
  }
  
  /*
   * 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...
1025
1026
1027
   *
   * If LOOKUP_RCU prevents us from performing a full check, return 1
   * suggesting a reval is needed.
9f6d44d41   Trond Myklebust   NFS: Optimise awa...
1028
1029
1030
   *
   * Note that when creating a new file, or looking up a rename target,
   * then it shouldn't be necessary to revalidate a negative dentry.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031
1032
1033
   */
  static inline
  int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1034
  		       unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035
  {
9f6d44d41   Trond Myklebust   NFS: Optimise awa...
1036
  	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1037
  		return 0;
4eec952e4   Trond Myklebust   NFS: Add options ...
1038
1039
  	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  		return 1;
912a108da   NeilBrown   NFS: teach nfs_ne...
1040
  	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  }
  
  /*
   * 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...
1054
  static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055
1056
1057
1058
  {
  	struct inode *dir;
  	struct inode *inode;
  	struct dentry *parent;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1059
1060
  	struct nfs_fh *fhandle = NULL;
  	struct nfs_fattr *fattr = NULL;
1775fd3e8   David Quigley   NFS:Add labels to...
1061
  	struct nfs4_label *label = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1062
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1063

d51ac1a8e   NeilBrown   NFS: prepare for ...
1064
  	if (flags & LOOKUP_RCU) {
6aa7de059   Mark Rutland   locking/atomics: ...
1065
  		parent = READ_ONCE(dentry->d_parent);
2b0143b5c   David Howells   VFS: normal files...
1066
  		dir = d_inode_rcu(parent);
d51ac1a8e   NeilBrown   NFS: prepare for ...
1067
1068
1069
1070
  		if (!dir)
  			return -ECHILD;
  	} else {
  		parent = dget_parent(dentry);
2b0143b5c   David Howells   VFS: normal files...
1071
  		dir = d_inode(parent);
d51ac1a8e   NeilBrown   NFS: prepare for ...
1072
  	}
91d5b4702   Chuck Lever   NFS: add I/O perf...
1073
  	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
2b0143b5c   David Howells   VFS: normal files...
1074
  	inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
1076
  
  	if (!inode) {
912a108da   NeilBrown   NFS: teach nfs_ne...
1077
1078
1079
  		if (nfs_neg_need_reval(dir, dentry, flags)) {
  			if (flags & LOOKUP_RCU)
  				return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
  			goto out_bad;
912a108da   NeilBrown   NFS: teach nfs_ne...
1081
  		}
63519fbc6   Trond Myklebust   NFS: Be more targ...
1082
  		goto out_valid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1083
1084
1085
  	}
  
  	if (is_bad_inode(inode)) {
d51ac1a8e   NeilBrown   NFS: prepare for ...
1086
1087
  		if (flags & LOOKUP_RCU)
  			return -ECHILD;
6de1472f1   Al Viro   nfs: use %p[dD] i...
1088
1089
1090
  		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode
  ",
  				__func__, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
1092
  		goto out_bad;
  	}
011e2a7fd   Bryan Schumaker   NFS: Create a hav...
1093
  	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
15860ab1d   Trond Myklebust   NFSv4: Ensure tha...
1094
  		goto out_set_verifier;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
  	/* Force a full look up iff the parent directory has changed */
73dd684a4   Trond Myklebust   NFS: If the VFS s...
1096
  	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
912a108da   NeilBrown   NFS: teach nfs_ne...
1097
  	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
cc89684c9   NeilBrown   NFS: only invalid...
1098
1099
  		error = nfs_lookup_verify_inode(inode, flags);
  		if (error) {
1fa1e3844   NeilBrown   NFS: teach nfs_lo...
1100
1101
  			if (flags & LOOKUP_RCU)
  				return -ECHILD;
cc89684c9   NeilBrown   NFS: only invalid...
1102
1103
1104
  			if (error == -ESTALE)
  				goto out_zap_parent;
  			goto out_error;
1fa1e3844   NeilBrown   NFS: teach nfs_lo...
1105
  		}
63519fbc6   Trond Myklebust   NFS: Be more targ...
1106
  		nfs_advise_use_readdirplus(dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1107
1108
  		goto out_valid;
  	}
912a108da   NeilBrown   NFS: teach nfs_ne...
1109
1110
  	if (flags & LOOKUP_RCU)
  		return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
1112
  	if (NFS_STALE(inode))
  		goto out_bad;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1113
1114
1115
1116
1117
  	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...
1118
1119
1120
  	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  	if (IS_ERR(label))
  		goto out_error;
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1121
  	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1775fd3e8   David Quigley   NFS:Add labels to...
1122
  	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1123
  	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
cc89684c9   NeilBrown   NFS: only invalid...
1124
  	if (error == -ESTALE || error == -ENOENT)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1125
  		goto out_bad;
cc89684c9   NeilBrown   NFS: only invalid...
1126
1127
  	if (error)
  		goto out_error;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1128
  	if (nfs_compare_fh(NFS_FH(inode), fhandle))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1129
  		goto out_bad;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1130
  	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
  		goto out_bad;
aa9c26696   David Quigley   NFS: Client imple...
1132
  	nfs_setsecurity(inode, fattr, label);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1133
1134
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
14c43f767   David Quigley   NFS: Add label li...
1135
  	nfs4_label_free(label);
63519fbc6   Trond Myklebust   NFS: Be more targ...
1136
1137
  	/* set a readdirplus hint that we had a cache miss */
  	nfs_force_use_readdirplus(dir);
15860ab1d   Trond Myklebust   NFSv4: Ensure tha...
1138
  out_set_verifier:
cf8ba45e0   Trond Myklebust   NFS: don't cache ...
1139
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
   out_valid:
d51ac1a8e   NeilBrown   NFS: prepare for ...
1141
  	if (flags & LOOKUP_RCU) {
6aa7de059   Mark Rutland   locking/atomics: ...
1142
  		if (parent != READ_ONCE(dentry->d_parent))
d51ac1a8e   NeilBrown   NFS: prepare for ...
1143
1144
1145
  			return -ECHILD;
  	} else
  		dput(parent);
6de1472f1   Al Viro   nfs: use %p[dD] i...
1146
1147
1148
  	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid
  ",
  			__func__, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1149
1150
1151
1152
  	return 1;
  out_zap_parent:
  	nfs_zap_caches(dir);
   out_bad:
d51ac1a8e   NeilBrown   NFS: prepare for ...
1153
  	WARN_ON(flags & LOOKUP_RCU);
c44600c9d   Al Viro   nfs_lookup_revali...
1154
1155
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
14c43f767   David Quigley   NFS: Add label li...
1156
  	nfs4_label_free(label);
a1643a92f   Trond Myklebust   NFS: NFS_CACHEINV...
1157
  	nfs_mark_for_revalidate(dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1158
1159
1160
  	if (inode && S_ISDIR(inode->i_mode)) {
  		/* Purge readdir caches. */
  		nfs_zap_caches(inode);
a3f432bfd   J. Bruce Fields   nfs: use IS_ROOT ...
1161
1162
1163
1164
1165
1166
1167
  		/*
  		 * 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(...
1168
  			goto out_valid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1169
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1170
  	dput(parent);
6de1472f1   Al Viro   nfs: use %p[dD] i...
1171
1172
1173
  	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid
  ",
  			__func__, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1174
  	return 0;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1175
  out_error:
d51ac1a8e   NeilBrown   NFS: prepare for ...
1176
  	WARN_ON(flags & LOOKUP_RCU);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1177
1178
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
14c43f767   David Quigley   NFS: Add label li...
1179
  	nfs4_label_free(label);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1180
  	dput(parent);
6de1472f1   Al Viro   nfs: use %p[dD] i...
1181
1182
1183
  	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d
  ",
  			__func__, dentry, error);
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1184
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1185
1186
1187
  }
  
  /*
2b0143b5c   David Howells   VFS: normal files...
1188
   * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1189
1190
1191
1192
1193
1194
1195
1196
1197
   * 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)
  {
2b0143b5c   David Howells   VFS: normal files...
1198
  	struct inode *inode = d_inode(dentry);
9cdd1d3f1   Trond Myklebust   NFS: Only look at...
1199
  	int error = 0;
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1200
1201
1202
1203
1204
1205
1206
  
  	/*
  	 * 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...
1207
1208
1209
  		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode
  ",
  				__func__, dentry);
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1210
1211
1212
1213
  		return 1;
  	}
  
  	if (is_bad_inode(inode)) {
6de1472f1   Al Viro   nfs: use %p[dD] i...
1214
1215
1216
  		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode
  ",
  				__func__, dentry);
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1217
1218
  		return 0;
  	}
b688741cb   NeilBrown   NFS: revalidate "...
1219
  	error = nfs_lookup_verify_inode(inode, flags);
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1220
1221
1222
1223
1224
1225
1226
  	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
1227
1228
   * This is called from dput() when d_count is going to 0.
   */
fe15ce446   Nick Piggin   fs: change d_dele...
1229
  static int nfs_dentry_delete(const struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
  {
6de1472f1   Al Viro   nfs: use %p[dD] i...
1231
1232
1233
  	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)
  ",
  		dentry, dentry->d_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234

77f111929   Trond Myklebust   NFS: Ensure that ...
1235
  	/* Unhash any dentry with a stale inode */
2b0143b5c   David Howells   VFS: normal files...
1236
  	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
77f111929   Trond Myklebust   NFS: Ensure that ...
1237
  		return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1238
1239
1240
1241
  	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  		/* Unhash it, so that ->d_iput() would be called */
  		return 1;
  	}
1751e8a6c   Linus Torvalds   Rename superblock...
1242
  	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1243
1244
1245
1246
1247
1248
1249
  		/* 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...
1250
  /* Ensure that we revalidate inode->i_nlink */
1b83d7070   Trond Myklebust   NFS: Protect inod...
1251
1252
1253
  static void nfs_drop_nlink(struct inode *inode)
  {
  	spin_lock(&inode->i_lock);
1f018458b   Trond Myklebust   NFS: Fix calls to...
1254
  	/* drop the inode if we're reasonably sure this is the last link */
59a707b0d   Trond Myklebust   NFS: Ensure we re...
1255
1256
1257
  	if (inode->i_nlink > 0)
  		drop_nlink(inode);
  	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
16e143751   Trond Myklebust   NFS: More fine gr...
1258
1259
  	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
  		| NFS_INO_INVALID_CTIME
59a707b0d   Trond Myklebust   NFS: Ensure we re...
1260
1261
  		| NFS_INO_INVALID_OTHER
  		| NFS_INO_REVAL_FORCED;
1b83d7070   Trond Myklebust   NFS: Protect inod...
1262
1263
  	spin_unlock(&inode->i_lock);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
1265
1266
1267
1268
1269
  /*
   * 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...
1270
1271
1272
  	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
1273
  	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
e4eff1a62   Trond Myklebust   SUNRPC: Clean up ...
1274
  		nfs_complete_unlink(dentry, inode);
1f018458b   Trond Myklebust   NFS: Fix calls to...
1275
  		nfs_drop_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1277
1278
  	iput(inode);
  }
b1942c5f8   Al Viro   nfs: store devnam...
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  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...
1289
  const struct dentry_operations nfs_dentry_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
  	.d_revalidate	= nfs_lookup_revalidate,
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
1291
  	.d_weak_revalidate	= nfs_weak_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1292
1293
  	.d_delete	= nfs_dentry_delete,
  	.d_iput		= nfs_dentry_iput,
36d43a437   David Howells   NFS: Use d_automo...
1294
  	.d_automount	= nfs_d_automount,
b1942c5f8   Al Viro   nfs: store devnam...
1295
  	.d_release	= nfs_d_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1296
  };
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1297
  EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1298

597d92891   Bryan Schumaker   NFS: Split out NF...
1299
  struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1300
1301
1302
  {
  	struct dentry *res;
  	struct inode *inode = NULL;
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1303
1304
  	struct nfs_fh *fhandle = NULL;
  	struct nfs_fattr *fattr = NULL;
1775fd3e8   David Quigley   NFS:Add labels to...
1305
  	struct nfs4_label *label = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1306
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1307

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

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

fd6840714   Trond Myklebust   NFS: nfs_lookup -...
1315
1316
1317
1318
  	/*
  	 * If we're doing an exclusive create, optimize away the lookup
  	 * but don't hash the dentry.
  	 */
9f6d44d41   Trond Myklebust   NFS: Optimise awa...
1319
  	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
130f9ab75   Al Viro   nfs_lookup: don't...
1320
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1321

e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1322
1323
1324
1325
1326
  	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...
1327
1328
1329
  	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
  	if (IS_ERR(label))
  		goto out;
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1330
  	trace_nfs_lookup_enter(dir, dentry, flags);
1775fd3e8   David Quigley   NFS:Add labels to...
1331
  	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1332
1333
1334
1335
  	if (error == -ENOENT)
  		goto no_entry;
  	if (error < 0) {
  		res = ERR_PTR(error);
bf1309142   Al Viro   nfs_lookup(): rem...
1336
  		goto out_label;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1337
  	}
1775fd3e8   David Quigley   NFS:Add labels to...
1338
  	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
bf0c84f16   Namhyung Kim   NFS: use ERR_CAST()
1339
  	res = ERR_CAST(inode);
03f28e3a2   Trond Myklebust   NFS: Make nfs_fhg...
1340
  	if (IS_ERR(res))
bf1309142   Al Viro   nfs_lookup(): rem...
1341
  		goto out_label;
54ceac451   David Howells   NFS: Share NFS su...
1342

63519fbc6   Trond Myklebust   NFS: Be more targ...
1343
1344
  	/* Notify readdir to use READDIRPLUS */
  	nfs_force_use_readdirplus(dir);
d69ee9b85   Trond Myklebust   NFS: Adapt readdi...
1345

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346
  no_entry:
41d28bca2   Al Viro   switch d_material...
1347
  	res = d_splice_alias(inode, dentry);
9eaef27b3   Trond Myklebust   [PATCH] VFS: Make...
1348
1349
  	if (res != NULL) {
  		if (IS_ERR(res))
bf1309142   Al Viro   nfs_lookup(): rem...
1350
  			goto out_label;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1351
  		dentry = res;
9eaef27b3   Trond Myklebust   [PATCH] VFS: Make...
1352
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1353
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
bf1309142   Al Viro   nfs_lookup(): rem...
1354
  out_label:
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1355
  	trace_nfs_lookup_exit(dir, dentry, flags, error);
14c43f767   David Quigley   NFS: Add label li...
1356
  	nfs4_label_free(label);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1357
  out:
e1fb4d05d   Trond Myklebust   NFS: Reduce the s...
1358
1359
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fhandle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1360
1361
  	return res;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1362
  EXPORT_SYMBOL_GPL(nfs_lookup);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1363

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

f786aa90e   Al Viro   constify dentry_o...
1367
  const struct dentry_operations nfs4_dentry_operations = {
0ef97dcfc   Miklos Szeredi   nfs: don't open i...
1368
  	.d_revalidate	= nfs4_lookup_revalidate,
b688741cb   NeilBrown   NFS: revalidate "...
1369
  	.d_weak_revalidate	= nfs_weak_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1370
1371
  	.d_delete	= nfs_dentry_delete,
  	.d_iput		= nfs_dentry_iput,
36d43a437   David Howells   NFS: Use d_automo...
1372
  	.d_automount	= nfs_d_automount,
b1942c5f8   Al Viro   nfs: store devnam...
1373
  	.d_release	= nfs_d_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1374
  };
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
1375
  EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1376

8a5e929dd   Al Viro   don't translitera...
1377
1378
1379
1380
1381
1382
1383
1384
1385
  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;
  }
532d4def2   NeilBrown   NFSv4: add flock_...
1386
  static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1387
  {
532d4def2   NeilBrown   NFSv4: add flock_...
1388
  	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1389
1390
1391
1392
  }
  
  static int do_open(struct inode *inode, struct file *filp)
  {
f1fe29b4a   David Howells   NFS: Use i_writec...
1393
  	nfs_fscache_open_file(inode, filp);
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1394
1395
  	return 0;
  }
d95852777   Al Viro   make ->atomic_ope...
1396
1397
  static int nfs_finish_open(struct nfs_open_context *ctx,
  			   struct dentry *dentry,
b452a458c   Al Viro   getting rid of 'o...
1398
  			   struct file *file, unsigned open_flags)
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1399
  {
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1400
  	int err;
be12af3ef   Al Viro   getting rid of 'o...
1401
  	err = finish_open(file, dentry, do_open);
30d904947   Al Viro   kill struct opendata
1402
  	if (err)
d95852777   Al Viro   make ->atomic_ope...
1403
  		goto out;
eaa2b82c3   NeilBrown   NFS: guard agains...
1404
1405
1406
1407
  	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
  		nfs_file_set_open_context(file, ctx);
  	else
  		err = -ESTALE;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1408
  out:
d95852777   Al Viro   make ->atomic_ope...
1409
  	return err;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1410
  }
73a79706d   Bryan Schumaker   NFS: Split out NF...
1411
1412
  int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  		    struct file *file, unsigned open_flags,
44907d790   Al Viro   get rid of 'opene...
1413
  		    umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
  {
c94c09535   Al Viro   nfs_atomic_open()...
1415
  	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1416
  	struct nfs_open_context *ctx;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1417
1418
  	struct dentry *res;
  	struct iattr attr = { .ia_valid = ATTR_OPEN };
f46e0bd34   Trond Myklebust   NFSv4: Further mi...
1419
  	struct inode *inode;
1472b83ea   Trond Myklebust   NFS: Pass in look...
1420
  	unsigned int lookup_flags = 0;
c94c09535   Al Viro   nfs_atomic_open()...
1421
  	bool switched = false;
73a09dd94   Al Viro   introduce FMODE_C...
1422
  	int created = 0;
898f635c4   Trond Myklebust   NFSv4: Don't igno...
1423
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1424

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

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

9597c13b2   Jeff Layton   nfs: verify open ...
1432
1433
1434
  	err = nfs_check_flags(open_flags);
  	if (err)
  		return err;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1435
1436
  	/* NFS only supports OPEN on regular files */
  	if ((open_flags & O_DIRECTORY)) {
00699ad85   Al Viro   Use the right pre...
1437
  		if (!d_in_lookup(dentry)) {
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1438
1439
1440
1441
1442
  			/*
  			 * Hashed negative dentry with O_DIRECTORY: dentry was
  			 * revalidated and is fine, no need to perform lookup
  			 * again
  			 */
d95852777   Al Viro   make ->atomic_ope...
1443
  			return -ENOENT;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1444
  		}
1472b83ea   Trond Myklebust   NFS: Pass in look...
1445
  		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1446
  		goto no_open;
02a913a73   Trond Myklebust   NFSv4: Eliminate ...
1447
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1448

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

0dd2b474d   Miklos Szeredi   nfs: implement i_...
1452
  	if (open_flags & O_CREAT) {
dff25ddb4   Andreas Gruenbacher   nfs: add support ...
1453
1454
1455
1456
  		struct nfs_server *server = NFS_SERVER(dir);
  
  		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  			mode &= ~current_umask();
536e43d12   Trond Myklebust   NFS: Optimise awa...
1457
  		attr.ia_valid |= ATTR_MODE;
dff25ddb4   Andreas Gruenbacher   nfs: add support ...
1458
  		attr.ia_mode = mode;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1459
  	}
536e43d12   Trond Myklebust   NFS: Optimise awa...
1460
1461
1462
  	if (open_flags & O_TRUNC) {
  		attr.ia_valid |= ATTR_SIZE;
  		attr.ia_size = 0;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1463
  	}
c94c09535   Al Viro   nfs_atomic_open()...
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
  	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);
  	}
532d4def2   NeilBrown   NFSv4: add flock_...
1474
  	ctx = create_nfs_open_context(dentry, open_flags, file);
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1475
1476
  	err = PTR_ERR(ctx);
  	if (IS_ERR(ctx))
d95852777   Al Viro   make ->atomic_ope...
1477
  		goto out;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1478

6e0d0be71   Trond Myklebust   NFS: Add event tr...
1479
  	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
73a09dd94   Al Viro   introduce FMODE_C...
1480
1481
1482
  	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
  	if (created)
  		file->f_mode |= FMODE_CREATED;
f46e0bd34   Trond Myklebust   NFSv4: Further mi...
1483
  	if (IS_ERR(inode)) {
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1484
  		err = PTR_ERR(inode);
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1485
  		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2d9db7500   Trond Myklebust   NFS: Fix up two u...
1486
  		put_nfs_open_context(ctx);
d20cb71db   Al Viro   make nfs_atomic_o...
1487
  		d_drop(dentry);
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1488
1489
  		switch (err) {
  		case -ENOENT:
774d9513a   Peng Tao   nfs: replace d_ad...
1490
  			d_splice_alias(NULL, dentry);
809fd143d   Trond Myklebust   NFSv4: Ensure nfs...
1491
  			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1492
1493
1494
1495
1496
1497
  			break;
  		case -EISDIR:
  		case -ENOTDIR:
  			goto no_open;
  		case -ELOOP:
  			if (!(open_flags & O_NOFOLLOW))
6f926b5ba   Trond Myklebust   [NFS]: Check that...
1498
  				goto no_open;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1499
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1500
  			/* case -EINVAL: */
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1501
1502
  		default:
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
  		}
d95852777   Al Viro   make ->atomic_ope...
1504
  		goto out;
cd9a1c0e5   Trond Myklebust   NFSv4: Clean up n...
1505
  	}
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1506

b452a458c   Al Viro   getting rid of 'o...
1507
  	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
6e0d0be71   Trond Myklebust   NFS: Add event tr...
1508
  	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2d9db7500   Trond Myklebust   NFS: Fix up two u...
1509
  	put_nfs_open_context(ctx);
d95852777   Al Viro   make ->atomic_ope...
1510
  out:
c94c09535   Al Viro   nfs_atomic_open()...
1511
1512
1513
1514
  	if (unlikely(switched)) {
  		d_lookup_done(dentry);
  		dput(dentry);
  	}
d95852777   Al Viro   make ->atomic_ope...
1515
  	return err;
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1516

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1517
  no_open:
1472b83ea   Trond Myklebust   NFS: Pass in look...
1518
  	res = nfs_lookup(dir, dentry, lookup_flags);
c94c09535   Al Viro   nfs_atomic_open()...
1519
1520
1521
1522
1523
1524
1525
  	if (switched) {
  		d_lookup_done(dentry);
  		if (!res)
  			res = dentry;
  		else
  			dput(dentry);
  	}
0dd2b474d   Miklos Szeredi   nfs: implement i_...
1526
  	if (IS_ERR(res))
c94c09535   Al Viro   nfs_atomic_open()...
1527
  		return PTR_ERR(res);
e45198a6a   Al Viro   make finish_no_op...
1528
  	return finish_no_open(file, res);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1529
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
1530
  EXPORT_SYMBOL_GPL(nfs_atomic_open);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1531

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1546
1547
1548
  	/* We can't create new files in nfs_open_revalidate(), so we
  	 * optimize away revalidation of negative dentries.
  	 */
216d5d068   Trond Myklebust   NFSv4: Use NFSv2/...
1549
  	if (inode == NULL) {
49317a7fd   NeilBrown   NFS: nfs4_lookup_...
1550
1551
  		struct dentry *parent;
  		struct inode *dir;
912a108da   NeilBrown   NFS: teach nfs_ne...
1552
  		if (flags & LOOKUP_RCU) {
6aa7de059   Mark Rutland   locking/atomics: ...
1553
  			parent = READ_ONCE(dentry->d_parent);
2b0143b5c   David Howells   VFS: normal files...
1554
  			dir = d_inode_rcu(parent);
912a108da   NeilBrown   NFS: teach nfs_ne...
1555
1556
1557
1558
  			if (!dir)
  				return -ECHILD;
  		} else {
  			parent = dget_parent(dentry);
2b0143b5c   David Howells   VFS: normal files...
1559
  			dir = d_inode(parent);
912a108da   NeilBrown   NFS: teach nfs_ne...
1560
  		}
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1561
  		if (!nfs_neg_need_reval(dir, dentry, flags))
216d5d068   Trond Myklebust   NFSv4: Use NFSv2/...
1562
  			ret = 1;
912a108da   NeilBrown   NFS: teach nfs_ne...
1563
1564
1565
1566
  		else if (flags & LOOKUP_RCU)
  			ret = -ECHILD;
  		if (!(flags & LOOKUP_RCU))
  			dput(parent);
6aa7de059   Mark Rutland   locking/atomics: ...
1567
  		else if (parent != READ_ONCE(dentry->d_parent))
912a108da   NeilBrown   NFS: teach nfs_ne...
1568
  			return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1569
  		goto out;
216d5d068   Trond Myklebust   NFSv4: Use NFSv2/...
1570
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1571
1572
  	/* NFS only supports OPEN on regular files */
  	if (!S_ISREG(inode->i_mode))
49317a7fd   NeilBrown   NFS: nfs4_lookup_...
1573
  		goto no_open;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1574
  	/* We cannot do exclusive creation on a positive dentry */
fa3c56bbd   Al Viro   fs/nfs/dir.c: swi...
1575
  	if (flags & LOOKUP_EXCL)
49317a7fd   NeilBrown   NFS: nfs4_lookup_...
1576
  		goto no_open;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1577

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1581
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1582
  	return ret;
535918f14   Trond Myklebust   NFSv4: Further cl...
1583

5584c3063   Trond Myklebust   NFSv4: Clean up i...
1584
  no_open:
0b728e191   Al Viro   stop passing name...
1585
  	return nfs_lookup_revalidate(dentry, flags);
c0204fd2b   Trond Myklebust   NFS: Clean up nfs...
1586
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1587
  #endif /* CONFIG_NFSV4 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1588
1589
1590
1591
  /*
   * Code common to create, mkdir, and mknod.
   */
  int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1775fd3e8   David Quigley   NFS:Add labels to...
1592
1593
  				struct nfs_fattr *fattr,
  				struct nfs4_label *label)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1594
  {
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1595
  	struct dentry *parent = dget_parent(dentry);
2b0143b5c   David Howells   VFS: normal files...
1596
  	struct inode *dir = d_inode(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1597
  	struct inode *inode;
b0c6108ec   Al Viro   nfs_instantiate()...
1598
  	struct dentry *d;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1599
  	int error = -EACCES;
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1600
  	d_drop(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1601
  	/* We may have been initialized further down */
2b0143b5c   David Howells   VFS: normal files...
1602
  	if (d_really_is_positive(dentry))
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1603
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1604
  	if (fhandle->size == 0) {
1775fd3e8   David Quigley   NFS:Add labels to...
1605
  		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1606
  		if (error)
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1607
  			goto out_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1608
  	}
5724ab378   Trond Myklebust   NFS: nfs_instanti...
1609
  	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1610
1611
  	if (!(fattr->valid & NFS_ATTR_FATTR)) {
  		struct nfs_server *server = NFS_SB(dentry->d_sb);
a841b54db   Trond Myklebust   NFS: Pass the ino...
1612
1613
  		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
  				fattr, NULL, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1614
  		if (error < 0)
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1615
  			goto out_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1616
  	}
1775fd3e8   David Quigley   NFS:Add labels to...
1617
  	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
b0c6108ec   Al Viro   nfs_instantiate()...
1618
1619
1620
  	d = d_splice_alias(inode, dentry);
  	if (IS_ERR(d)) {
  		error = PTR_ERR(d);
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1621
  		goto out_error;
b0c6108ec   Al Viro   nfs_instantiate()...
1622
1623
  	}
  	dput(d);
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1624
1625
  out:
  	dput(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1626
  	return 0;
fab728e15   Trond Myklebust   NFS: Ensure nfs_i...
1627
1628
1629
1630
  out_error:
  	nfs_mark_for_revalidate(dir);
  	dput(parent);
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1631
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1632
  EXPORT_SYMBOL_GPL(nfs_instantiate);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1633
1634
1635
1636
1637
1638
1639
  
  /*
   * 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...
1640
  int nfs_create(struct inode *dir, struct dentry *dentry,
ebfc3b49a   Al Viro   don't pass nameid...
1641
  		umode_t mode, bool excl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1642
1643
  {
  	struct iattr attr;
ebfc3b49a   Al Viro   don't pass nameid...
1644
  	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1645
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646

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

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

d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1716
1717
  static void nfs_dentry_handle_enoent(struct dentry *dentry)
  {
dc3f4198e   Al Viro   make simple_posit...
1718
  	if (simple_positive(dentry))
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1719
1720
  		d_delete(dentry);
  }
597d92891   Bryan Schumaker   NFS: Split out NF...
1721
  int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1722
1723
  {
  	int error;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1724
1725
  	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd
  ",
6de1472f1   Al Viro   nfs: use %p[dD] i...
1726
  			dir->i_sb->s_id, dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1727

1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1728
  	trace_nfs_rmdir_enter(dir, dentry);
2b0143b5c   David Howells   VFS: normal files...
1729
  	if (d_really_is_positive(dentry)) {
884be1753   Al Viro   nfs: per-name sil...
1730
  		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
ba6c05928   Trond Myklebust   NFS: Ensure that ...
1731
1732
1733
1734
  		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...
1735
  			clear_nlink(d_inode(dentry));
ba6c05928   Trond Myklebust   NFS: Ensure that ...
1736
1737
1738
1739
  			break;
  		case -ENOENT:
  			nfs_dentry_handle_enoent(dentry);
  		}
884be1753   Al Viro   nfs: per-name sil...
1740
  		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
ba6c05928   Trond Myklebust   NFS: Ensure that ...
1741
1742
  	} else
  		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1743
  	trace_nfs_rmdir_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1744
1745
1746
  
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1747
  EXPORT_SYMBOL_GPL(nfs_rmdir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1748

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1749
1750
1751
1752
1753
1754
1755
1756
1757
  /*
   * 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...
1758
1759
  	struct inode *dir = d_inode(dentry->d_parent);
  	struct inode *inode = d_inode(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1760
1761
  	int error = -EBUSY;
  		
6de1472f1   Al Viro   nfs: use %p[dD] i...
1762
1763
  	dfprintk(VFS, "NFS: safe_remove(%pd2)
  ", dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1764
1765
1766
1767
1768
1769
  
  	/* 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...
1770
  	trace_nfs_remove_enter(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1771
  	if (inode != NULL) {
912678dbc   Trond Myklebust   NFS: Move the del...
1772
  		error = NFS_PROTO(dir)->remove(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1773
  		if (error == 0)
1b83d7070   Trond Myklebust   NFS: Protect inod...
1774
  			nfs_drop_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1775
  	} else
912678dbc   Trond Myklebust   NFS: Move the del...
1776
  		error = NFS_PROTO(dir)->remove(dir, dentry);
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1777
1778
  	if (error == -ENOENT)
  		nfs_dentry_handle_enoent(dentry);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1779
  	trace_nfs_remove_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1780
1781
1782
1783
1784
1785
1786
1787
1788
  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...
1789
  int nfs_unlink(struct inode *dir, struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1790
1791
1792
  {
  	int error;
  	int need_rehash = 0;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1793
1794
  	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)
  ", dir->i_sb->s_id,
6de1472f1   Al Viro   nfs: use %p[dD] i...
1795
  		dir->i_ino, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1796

1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1797
  	trace_nfs_unlink_enter(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1798
  	spin_lock(&dentry->d_lock);
84d08fa88   Al Viro   helper for readin...
1799
  	if (d_count(dentry) > 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1800
  		spin_unlock(&dentry->d_lock);
ccfeb5062   Trond Myklebust   NFS: Fix up "rm -...
1801
  		/* Start asynchronous writeout of the inode */
2b0143b5c   David Howells   VFS: normal files...
1802
  		write_inode_now(d_inode(dentry), 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1803
  		error = nfs_sillyrename(dir, dentry);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1804
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1805
1806
1807
1808
1809
1810
  	}
  	if (!d_unhashed(dentry)) {
  		__d_drop(dentry);
  		need_rehash = 1;
  	}
  	spin_unlock(&dentry->d_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1811
  	error = nfs_safe_remove(dentry);
d45b9d8ba   Trond Myklebust   NFS: Handle -ENOE...
1812
  	if (!error || error == -ENOENT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1813
1814
1815
  		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  	} else if (need_rehash)
  		d_rehash(dentry);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1816
1817
  out:
  	trace_nfs_unlink_exit(dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1818
1819
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1820
  EXPORT_SYMBOL_GPL(nfs_unlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1821

873101b33   Chuck Lever   NFS: copy symlink...
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
  /*
   * 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...
1837
  int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1838
  {
873101b33   Chuck Lever   NFS: copy symlink...
1839
1840
  	struct page *page;
  	char *kaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1841
  	struct iattr attr;
873101b33   Chuck Lever   NFS: copy symlink...
1842
  	unsigned int pathlen = strlen(symname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1843
  	int error;
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1844
1845
  	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)
  ", dir->i_sb->s_id,
6de1472f1   Al Viro   nfs: use %p[dD] i...
1846
  		dir->i_ino, dentry, symname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1847

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

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

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

e8ecde25f   Al Viro   Make sure that hi...
1858
  	kaddr = page_address(page);
873101b33   Chuck Lever   NFS: copy symlink...
1859
1860
1861
  	memcpy(kaddr, symname, pathlen);
  	if (pathlen < PAGE_SIZE)
  		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
873101b33   Chuck Lever   NFS: copy symlink...
1862

1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1863
  	trace_nfs_symlink_enter(dir, dentry);
94a6d7532   Chuck Lever   NFS: Use cached p...
1864
  	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1ca42382a   Trond Myklebust   NFS: Add tracepoi...
1865
  	trace_nfs_symlink_exit(dir, dentry, error);
873101b33   Chuck Lever   NFS: copy symlink...
1866
  	if (error != 0) {
1e8968c5b   Niels de Vos   NFS: dprintk() sh...
1867
1868
  		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d
  ",
873101b33   Chuck Lever   NFS: copy symlink...
1869
  			dir->i_sb->s_id, dir->i_ino,
6de1472f1   Al Viro   nfs: use %p[dD] i...
1870
  			dentry, symname, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1871
  		d_drop(dentry);
873101b33   Chuck Lever   NFS: copy symlink...
1872
  		__free_page(page);
873101b33   Chuck Lever   NFS: copy symlink...
1873
1874
1875
1876
1877
1878
1879
  		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...
1880
  	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
873101b33   Chuck Lever   NFS: copy symlink...
1881
  							GFP_KERNEL)) {
873101b33   Chuck Lever   NFS: copy symlink...
1882
1883
  		SetPageUptodate(page);
  		unlock_page(page);
a0b54adda   Rafael Aquini   mm: fix page leak...
1884
1885
1886
1887
  		/*
  		 * 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...
1888
  		put_page(page);
873101b33   Chuck Lever   NFS: copy symlink...
1889
1890
  	} else
  		__free_page(page);
873101b33   Chuck Lever   NFS: copy symlink...
1891
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1892
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1893
  EXPORT_SYMBOL_GPL(nfs_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1894

597d92891   Bryan Schumaker   NFS: Split out NF...
1895
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1896
1897
  nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  {
2b0143b5c   David Howells   VFS: normal files...
1898
  	struct inode *inode = d_inode(old_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1899
  	int error;
6de1472f1   Al Viro   nfs: use %p[dD] i...
1900
1901
1902
  	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)
  ",
  		old_dentry, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1903

1fd1085b4   Trond Myklebust   NFS: Add tracepoi...
1904
  	trace_nfs_link_enter(inode, dir, dentry);
9697d2342   Trond Myklebust   NFS: Ensure that ...
1905
  	d_drop(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1906
  	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
cf8095561   Trond Myklebust   NFS: Ensure that ...
1907
  	if (error == 0) {
7de9c6ee3   Al Viro   new helper: ihold()
1908
  		ihold(inode);
9697d2342   Trond Myklebust   NFS: Ensure that ...
1909
  		d_add(dentry, inode);
cf8095561   Trond Myklebust   NFS: Ensure that ...
1910
  	}
1fd1085b4   Trond Myklebust   NFS: Add tracepoi...
1911
  	trace_nfs_link_exit(inode, dir, dentry, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1912
1913
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
1914
  EXPORT_SYMBOL_GPL(nfs_link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
  
  /*
   * 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...
1940
  int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93b   Miklos Szeredi   fs: make remainin...
1941
1942
  	       struct inode *new_dir, struct dentry *new_dentry,
  	       unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1943
  {
2b0143b5c   David Howells   VFS: normal files...
1944
1945
  	struct inode *old_inode = d_inode(old_dentry);
  	struct inode *new_inode = d_inode(new_dentry);
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
1946
  	struct dentry *dentry = NULL, *rehash = NULL;
80a491fd4   Jeff Layton   nfs: convert nfs_...
1947
  	struct rpc_task *task;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1948
  	int error = -EBUSY;
1cd66c93b   Miklos Szeredi   fs: make remainin...
1949
1950
  	if (flags)
  		return -EINVAL;
6de1472f1   Al Viro   nfs: use %p[dD] i...
1951
1952
1953
  	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)
  ",
  		 old_dentry, new_dentry,
84d08fa88   Al Viro   helper for readin...
1954
  		 d_count(new_dentry));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1955

70ded2017   Trond Myklebust   NFS: Add tracepoi...
1956
  	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1957
  	/*
28f79a1a6   Miklos Szeredi   nfs: fix comments...
1958
1959
1960
1961
  	 * 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
1962
  	 */
27226104e   Miklos Szeredi   nfs: dont unhash ...
1963
1964
1965
1966
1967
  	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.
  		 */
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
1968
  		if (!d_unhashed(new_dentry)) {
27226104e   Miklos Szeredi   nfs: dont unhash ...
1969
  			d_drop(new_dentry);
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
1970
1971
  			rehash = new_dentry;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1972

84d08fa88   Al Viro   helper for readin...
1973
  		if (d_count(new_dentry) > 2) {
27226104e   Miklos Szeredi   nfs: dont unhash ...
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
  			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...
1984
  			if (err)
27226104e   Miklos Szeredi   nfs: dont unhash ...
1985
  				goto out;
24e93025e   Miklos Szeredi   nfs: clean up sil...
1986
1987
  
  			new_dentry = dentry;
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
1988
  			rehash = NULL;
24e93025e   Miklos Szeredi   nfs: clean up sil...
1989
  			new_inode = NULL;
27226104e   Miklos Szeredi   nfs: dont unhash ...
1990
  		}
b1e4adf4e   Trond Myklebust   NFS: Fix the noti...
1991
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1992

d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
1993
  	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
80a491fd4   Jeff Layton   nfs: convert nfs_...
1994
1995
1996
1997
1998
1999
  	if (IS_ERR(task)) {
  		error = PTR_ERR(task);
  		goto out;
  	}
  
  	error = rpc_wait_for_completion_task(task);
818a8dbe8   Benjamin Coddington   NFS: nfs_rename()...
2000
2001
2002
2003
2004
  	if (error != 0) {
  		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
  		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
  		smp_wmb();
  	} else
80a491fd4   Jeff Layton   nfs: convert nfs_...
2005
2006
  		error = task->tk_status;
  	rpc_put_task(task);
59a707b0d   Trond Myklebust   NFS: Ensure we re...
2007
2008
2009
2010
2011
2012
2013
2014
2015
  	/* Ensure the inode attributes are revalidated */
  	if (error == 0) {
  		spin_lock(&old_inode->i_lock);
  		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
  		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
  			| NFS_INO_INVALID_CTIME
  			| NFS_INO_REVAL_FORCED;
  		spin_unlock(&old_inode->i_lock);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2016
  out:
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
2017
2018
  	if (rehash)
  		d_rehash(rehash);
70ded2017   Trond Myklebust   NFS: Add tracepoi...
2019
2020
  	trace_nfs_rename_exit(old_dir, old_dentry,
  			new_dir, new_dentry, error);
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
  	if (!error) {
  		if (new_inode != NULL)
  			nfs_drop_nlink(new_inode);
  		/*
  		 * The d_move() should be here instead of in an async RPC completion
  		 * handler because we need the proper locks to move the dentry.  If
  		 * we're interrupted by a signal, the async RPC completion handler
  		 * should mark the directories for revalidation.
  		 */
  		d_move(old_dentry, new_dentry);
d803224c8   Trond Myklebust   NFS: Fix a typo i...
2031
  		nfs_set_verifier(old_dentry,
d9f295000   Benjamin Coddington   Revert "NFS: nfs_...
2032
2033
2034
  					nfs_save_change_attribute(new_dir));
  	} else if (error == -ENOENT)
  		nfs_dentry_handle_enoent(old_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2035
2036
2037
  	/* new dentry created? */
  	if (dentry)
  		dput(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2038
2039
  	return error;
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
2040
  EXPORT_SYMBOL_GPL(nfs_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2041

cfcea3e8c   Trond Myklebust   NFS: Add a global...
2042
2043
2044
  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...
2045
2046
2047
  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...
2048
2049
2050
  static void nfs_access_free_entry(struct nfs_access_entry *entry)
  {
  	put_rpccred(entry->cred);
f682a398b   NeilBrown   NFS: allow lockle...
2051
  	kfree_rcu(entry, rcu_head);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2052
  	smp_mb__before_atomic();
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2053
  	atomic_long_dec(&nfs_access_nr_entries);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2054
  	smp_mb__after_atomic();
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2055
  }
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
  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...
2066
2067
  static unsigned long
  nfs_do_access_cache_scan(unsigned int nr_to_scan)
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2068
2069
  {
  	LIST_HEAD(head);
aa510da5b   Trond Myklebust   NFS: We must use ...
2070
  	struct nfs_inode *nfsi, *next;
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2071
  	struct nfs_access_entry *cache;
1ab6c4997   Dave Chinner   fs: convert fs sh...
2072
  	long freed = 0;
979df72e6   Trond Myklebust   NFS: Add an ACCES...
2073

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

1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2163
  	spin_lock(&inode->i_lock);
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2164
2165
2166
2167
  	__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...
2168
  }
1c606fb74   Bryan Schumaker   NFS: Convert v3 i...
2169
  EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2170

1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
  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
2185
  	}
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2186
2187
  	return NULL;
  }
57b691819   Trond Myklebust   NFS: Cache access...
2188
  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...
2189
2190
2191
  {
  	struct nfs_inode *nfsi = NFS_I(inode);
  	struct nfs_access_entry *cache;
57b691819   Trond Myklebust   NFS: Cache access...
2192
2193
  	bool retry = true;
  	int err;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2194

dc59250c6   Chuck Lever   [PATCH] NFS: Intr...
2195
  	spin_lock(&inode->i_lock);
57b691819   Trond Myklebust   NFS: Cache access...
2196
2197
2198
2199
2200
2201
2202
2203
  	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? */
21c3ba7e5   Trond Myklebust   NFS: Fix and clea...
2204
  		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
57b691819   Trond Myklebust   NFS: Cache access...
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
  			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...
2218
2219
  	res->cred = cache->cred;
  	res->mask = cache->mask;
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2220
  	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2221
2222
2223
2224
  	err = 0;
  out:
  	spin_unlock(&inode->i_lock);
  	return err;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2225
  out_zap:
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2226
2227
  	spin_unlock(&inode->i_lock);
  	nfs_access_zap_cache(inode);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2228
2229
  	return -ENOENT;
  }
f682a398b   NeilBrown   NFS: allow lockle...
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
  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;
21c3ba7e5   Trond Myklebust   NFS: Fix and clea...
2250
  	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
f682a398b   NeilBrown   NFS: allow lockle...
2251
  		goto out;
f682a398b   NeilBrown   NFS: allow lockle...
2252
2253
  	res->cred = cache->cred;
  	res->mask = cache->mask;
21c3ba7e5   Trond Myklebust   NFS: Fix and clea...
2254
  	err = 0;
f682a398b   NeilBrown   NFS: allow lockle...
2255
2256
2257
2258
  out:
  	rcu_read_unlock();
  	return err;
  }
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2259
2260
  static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  {
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2261
2262
  	struct nfs_inode *nfsi = NFS_I(inode);
  	struct rb_root *root_node = &nfsi->access_cache;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
  	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...
2281
  	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
dc59250c6   Chuck Lever   [PATCH] NFS: Intr...
2282
  	spin_unlock(&inode->i_lock);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2283
2284
2285
  	return;
  found:
  	rb_replace_node(parent, &set->rb_node, root_node);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2286
2287
  	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  	list_del(&entry->lru);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2288
2289
2290
  	spin_unlock(&inode->i_lock);
  	nfs_access_free_entry(entry);
  }
6168f62cb   Weston Andros Adamson   NFSv4: Add ACCESS...
2291
  void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2292
2293
2294
2295
2296
  {
  	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  	if (cache == NULL)
  		return;
  	RB_CLEAR_NODE(&cache->rb_node);
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2297
  	cache->cred = get_rpccred(set->cred);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2298
  	cache->mask = set->mask;
1c3c07e9f   Trond Myklebust   NFS: Add a new AC...
2299

f682a398b   NeilBrown   NFS: allow lockle...
2300
2301
2302
2303
2304
  	/* 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...
2305
  	nfs_access_add_rbtree(inode, cache);
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2306
2307
  
  	/* Update accounting */
4e857c58e   Peter Zijlstra   arch: Mass conver...
2308
  	smp_mb__before_atomic();
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2309
  	atomic_long_inc(&nfs_access_nr_entries);
4e857c58e   Peter Zijlstra   arch: Mass conver...
2310
  	smp_mb__after_atomic();
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2311
2312
  
  	/* Add inode to global LRU list */
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2313
  	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
cfcea3e8c   Trond Myklebust   NFS: Add a global...
2314
  		spin_lock(&nfs_access_lru_lock);
1a81bb8a1   Trond Myklebust   NFS: Clean up nfs...
2315
2316
2317
  		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...
2318
2319
  		spin_unlock(&nfs_access_lru_lock);
  	}
3a505845c   Trond Myklebust   NFS: Enforce an u...
2320
  	nfs_access_cache_enforce_limit();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2321
  }
6168f62cb   Weston Andros Adamson   NFSv4: Add ACCESS...
2322
  EXPORT_SYMBOL_GPL(nfs_access_add_cache);
3c1818275   Anna Schumaker   NFS: Create NFS_A...
2323
2324
2325
2326
2327
2328
  #define NFS_MAY_READ (NFS_ACCESS_READ)
  #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
  		NFS_ACCESS_EXTEND | \
  		NFS_ACCESS_DELETE)
  #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
  		NFS_ACCESS_EXTEND)
ecbb903c5   Trond Myklebust   NFS: Be more care...
2329
  #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
3c1818275   Anna Schumaker   NFS: Create NFS_A...
2330
2331
  #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
  #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
15d4b73ac   Trond Myklebust   NFS: Refactor NFS...
2332
  static int
ecbb903c5   Trond Myklebust   NFS: Be more care...
2333
  nfs_access_calc_mask(u32 access_result, umode_t umode)
15d4b73ac   Trond Myklebust   NFS: Refactor NFS...
2334
2335
2336
2337
2338
  {
  	int mask = 0;
  
  	if (access_result & NFS_MAY_READ)
  		mask |= MAY_READ;
ecbb903c5   Trond Myklebust   NFS: Be more care...
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
  	if (S_ISDIR(umode)) {
  		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
  			mask |= MAY_WRITE;
  		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
  			mask |= MAY_EXEC;
  	} else if (S_ISREG(umode)) {
  		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
  			mask |= MAY_WRITE;
  		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
  			mask |= MAY_EXEC;
  	} else if (access_result & NFS_MAY_WRITE)
  			mask |= MAY_WRITE;
15d4b73ac   Trond Myklebust   NFS: Refactor NFS...
2351
2352
  	return mask;
  }
6168f62cb   Weston Andros Adamson   NFSv4: Add ACCESS...
2353
2354
  void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
  {
bd8b24417   Trond Myklebust   NFS: Store the ra...
2355
  	entry->mask = access_result;
6168f62cb   Weston Andros Adamson   NFSv4: Add ACCESS...
2356
2357
  }
  EXPORT_SYMBOL_GPL(nfs_access_set_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2358
2359
2360
2361
  
  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...
2362
  	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
bd8b24417   Trond Myklebust   NFS: Store the ra...
2363
  	int cache_mask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2364
  	int status;
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2365
  	trace_nfs_access_enter(inode);
f682a398b   NeilBrown   NFS: allow lockle...
2366
2367
  	status = nfs_access_get_cached_rcu(inode, cred, &cache);
  	if (status != 0)
57b691819   Trond Myklebust   NFS: Cache access...
2368
  		status = nfs_access_get_cached(inode, cred, &cache, may_block);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2369
  	if (status == 0)
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2370
  		goto out_cached;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2371

f3324a2a9   NeilBrown   NFS: support RCU_...
2372
  	status = -ECHILD;
57b691819   Trond Myklebust   NFS: Cache access...
2373
  	if (!may_block)
f3324a2a9   NeilBrown   NFS: support RCU_...
2374
  		goto out;
1750d929b   Anna Schumaker   NFS: Don't compar...
2375
2376
2377
2378
2379
2380
2381
2382
  	/*
  	 * Determine which access bits we want to ask for...
  	 */
  	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
  	if (S_ISDIR(inode->i_mode))
  		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
  	else
  		cache.mask |= NFS_ACCESS_EXECUTE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2383
  	cache.cred = cred;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2384
  	status = NFS_PROTO(inode)->access(inode, &cache);
a71ee337b   Suresh Jayaraman   NFS: Handle -ESTA...
2385
2386
2387
2388
2389
2390
  	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...
2391
  		goto out;
a71ee337b   Suresh Jayaraman   NFS: Handle -ESTA...
2392
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2393
  	nfs_access_add_cache(inode, &cache);
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2394
  out_cached:
ecbb903c5   Trond Myklebust   NFS: Be more care...
2395
  	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
bd8b24417   Trond Myklebust   NFS: Store the ra...
2396
  	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2397
  		status = -EACCES;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2398
  out:
f4ce1299b   Trond Myklebust   NFS: Add event tr...
2399
2400
  	trace_nfs_access_exit(inode, status);
  	return status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2401
  }
af22f94ae   Trond Myklebust   NFSv4: Simplify _...
2402
2403
2404
  static int nfs_open_permission_mask(int openflags)
  {
  	int mask = 0;
f8d9a897d   Weston Andros Adamson   NFS: Fix access t...
2405
2406
2407
2408
2409
2410
2411
2412
2413
  	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 _...
2414
2415
2416
2417
2418
2419
2420
  	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...
2421
  EXPORT_SYMBOL_GPL(nfs_may_open);
af22f94ae   Trond Myklebust   NFSv4: Simplify _...
2422

5c5fc09a1   Trond Myklebust   NFS: Ensure we re...
2423
2424
2425
  static int nfs_execute_ok(struct inode *inode, int mask)
  {
  	struct nfs_server *server = NFS_SERVER(inode);
21c3ba7e5   Trond Myklebust   NFS: Fix and clea...
2426
  	int ret = 0;
5c5fc09a1   Trond Myklebust   NFS: Ensure we re...
2427

3825827eb   Trond Myklebust   NFS: More excessi...
2428
2429
  	if (S_ISDIR(inode->i_mode))
  		return 0;
cf8340277   Trond Myklebust   NFS: Fix excessiv...
2430
  	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
21c3ba7e5   Trond Myklebust   NFS: Fix and clea...
2431
2432
2433
2434
  		if (mask & MAY_NOT_BLOCK)
  			return -ECHILD;
  		ret = __nfs_revalidate_inode(server, inode);
  	}
5c5fc09a1   Trond Myklebust   NFS: Ensure we re...
2435
2436
2437
2438
  	if (ret == 0 && !execute_ok(inode))
  		ret = -EACCES;
  	return ret;
  }
10556cb21   Al Viro   ->permission() sa...
2439
  int nfs_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2440
2441
2442
  {
  	struct rpc_cred *cred;
  	int res = 0;
91d5b4702   Chuck Lever   NFS: add I/O perf...
2443
  	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
e6305c43e   Al Viro   [PATCH] sanitize ...
2444
  	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2445
2446
  		goto out;
  	/* Is this sys_access() ? */
9cfcac810   Eric Paris   vfs: re-introduce...
2447
  	if (mask & (MAY_ACCESS | MAY_CHDIR))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2448
2449
2450
2451
2452
2453
  		goto force_lookup;
  
  	switch (inode->i_mode & S_IFMT) {
  		case S_IFLNK:
  			goto out;
  		case S_IFREG:
762674f86   Trond Myklebust   NFSv4: Don't perf...
2454
2455
2456
  			if ((mask & MAY_OPEN) &&
  			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
  				return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
  			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
2468
2469
  	if (!NFS_PROTO(inode)->access)
  		goto out_notsup;
f3324a2a9   NeilBrown   NFS: support RCU_...
2470
2471
2472
2473
2474
2475
  	/* 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
2476
  		res = PTR_ERR(cred);
f3324a2a9   NeilBrown   NFS: support RCU_...
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
  	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
2487
  out:
5c5fc09a1   Trond Myklebust   NFS: Ensure we re...
2488
2489
  	if (!res && (mask & MAY_EXEC))
  		res = nfs_execute_ok(inode, mask);
f696a3659   Miklos Szeredi   [PATCH] move exec...
2490

1e8968c5b   Niels de Vos   NFS: dprintk() sh...
2491
2492
  	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d
  ",
1e7cb3dc1   Chuck Lever   NFS: directory tr...
2493
  		inode->i_sb->s_id, inode->i_ino, mask, res);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2494
2495
  	return res;
  out_notsup:
d51ac1a8e   NeilBrown   NFS: prepare for ...
2496
2497
  	if (mask & MAY_NOT_BLOCK)
  		return -ECHILD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2498
2499
  	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  	if (res == 0)
2830ba7f3   Al Viro   ->permission() sa...
2500
  		res = generic_permission(inode, mask);
1e7cb3dc1   Chuck Lever   NFS: directory tr...
2501
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2502
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
2503
  EXPORT_SYMBOL_GPL(nfs_permission);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2504
2505
2506
2507
2508
2509
2510
  
  /*
   * Local variables:
   *  version-control: t
   *  kept-new-versions: 5
   * End:
   */