• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  linux/fs/nfs/dir.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs directory handling functions
7  *
8  * 10 Apr 1996	Added silly rename for unlink	--okir
9  * 28 Sep 1996	Improved directory cache --okir
10  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
11  *              Re-implemented silly rename for unlink, newly implemented
12  *              silly rename for nfs_rename() following the suggestions
13  *              of Olaf Kirch (okir) found in this file.
14  *              Following Linus comments on my original hack, this version
15  *              depends only on the dcache stuff and doesn't touch the inode
16  *              layer (iput() and friends).
17  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
18  */
19 
20 #include <linux/module.h>
21 #include <linux/time.h>
22 #include <linux/errno.h>
23 #include <linux/stat.h>
24 #include <linux/fcntl.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/mm.h>
29 #include <linux/sunrpc/clnt.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_mount.h>
32 #include <linux/pagemap.h>
33 #include <linux/pagevec.h>
34 #include <linux/namei.h>
35 #include <linux/mount.h>
36 #include <linux/swap.h>
37 #include <linux/sched.h>
38 #include <linux/kmemleak.h>
39 #include <linux/xattr.h>
40 
41 #include "delegation.h"
42 #include "iostat.h"
43 #include "internal.h"
44 #include "fscache.h"
45 
46 #include "nfstrace.h"
47 
48 /* #define NFS_DEBUG_VERBOSE 1 */
49 
50 static int nfs_opendir(struct inode *, struct file *);
51 static int nfs_closedir(struct inode *, struct file *);
52 static int nfs_readdir(struct file *, struct dir_context *);
53 static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
54 static loff_t nfs_llseek_dir(struct file *, loff_t, int);
55 static void nfs_readdir_clear_array(struct page*);
56 
57 const struct file_operations nfs_dir_operations = {
58 	.llseek		= nfs_llseek_dir,
59 	.read		= generic_read_dir,
60 	.iterate	= nfs_readdir,
61 	.open		= nfs_opendir,
62 	.release	= nfs_closedir,
63 	.fsync		= nfs_fsync_dir,
64 };
65 
66 const struct address_space_operations nfs_dir_aops = {
67 	.freepage = nfs_readdir_clear_array,
68 };
69 
alloc_nfs_open_dir_context(struct inode * dir,struct rpc_cred * cred)70 static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
71 {
72 	struct nfs_inode *nfsi = NFS_I(dir);
73 	struct nfs_open_dir_context *ctx;
74 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
75 	if (ctx != NULL) {
76 		ctx->duped = 0;
77 		ctx->attr_gencount = nfsi->attr_gencount;
78 		ctx->dir_cookie = 0;
79 		ctx->dup_cookie = 0;
80 		ctx->cred = get_rpccred(cred);
81 		spin_lock(&dir->i_lock);
82 		list_add(&ctx->list, &nfsi->open_files);
83 		spin_unlock(&dir->i_lock);
84 		return ctx;
85 	}
86 	return  ERR_PTR(-ENOMEM);
87 }
88 
put_nfs_open_dir_context(struct inode * dir,struct nfs_open_dir_context * ctx)89 static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
90 {
91 	spin_lock(&dir->i_lock);
92 	list_del(&ctx->list);
93 	spin_unlock(&dir->i_lock);
94 	put_rpccred(ctx->cred);
95 	kfree(ctx);
96 }
97 
98 /*
99  * Open file
100  */
101 static int
nfs_opendir(struct inode * inode,struct file * filp)102 nfs_opendir(struct inode *inode, struct file *filp)
103 {
104 	int res = 0;
105 	struct nfs_open_dir_context *ctx;
106 	struct rpc_cred *cred;
107 
108 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
109 
110 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
111 
112 	cred = rpc_lookup_cred();
113 	if (IS_ERR(cred))
114 		return PTR_ERR(cred);
115 	ctx = alloc_nfs_open_dir_context(inode, cred);
116 	if (IS_ERR(ctx)) {
117 		res = PTR_ERR(ctx);
118 		goto out;
119 	}
120 	filp->private_data = ctx;
121 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
122 		/* This is a mountpoint, so d_revalidate will never
123 		 * have been called, so we need to refresh the
124 		 * inode (for close-open consistency) ourselves.
125 		 */
126 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
127 	}
128 out:
129 	put_rpccred(cred);
130 	return res;
131 }
132 
133 static int
nfs_closedir(struct inode * inode,struct file * filp)134 nfs_closedir(struct inode *inode, struct file *filp)
135 {
136 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
137 	return 0;
138 }
139 
140 struct nfs_cache_array_entry {
141 	u64 cookie;
142 	u64 ino;
143 	struct qstr string;
144 	unsigned char d_type;
145 };
146 
147 struct nfs_cache_array {
148 	int size;
149 	int eof_index;
150 	u64 last_cookie;
151 	struct nfs_cache_array_entry array[0];
152 };
153 
154 typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
155 typedef struct {
156 	struct file	*file;
157 	struct page	*page;
158 	struct dir_context *ctx;
159 	unsigned long	page_index;
160 	u64		*dir_cookie;
161 	u64		last_cookie;
162 	loff_t		current_index;
163 	decode_dirent_t	decode;
164 
165 	unsigned long	timestamp;
166 	unsigned long	gencount;
167 	unsigned int	cache_entry_index;
168 	bool plus;
169 	bool eof;
170 } nfs_readdir_descriptor_t;
171 
172 static
nfs_readdir_init_array(struct page * page)173 void nfs_readdir_init_array(struct page *page)
174 {
175 	struct nfs_cache_array *array;
176 
177 	array = kmap_atomic(page);
178 	memset(array, 0, sizeof(struct nfs_cache_array));
179 	array->eof_index = -1;
180 	kunmap_atomic(array);
181 }
182 
183 /*
184  * we are freeing strings created by nfs_add_to_readdir_array()
185  */
186 static
nfs_readdir_clear_array(struct page * page)187 void nfs_readdir_clear_array(struct page *page)
188 {
189 	struct nfs_cache_array *array;
190 	int i;
191 
192 	array = kmap_atomic(page);
193 	for (i = 0; i < array->size; i++)
194 		kfree(array->array[i].string.name);
195 	array->size = 0;
196 	kunmap_atomic(array);
197 }
198 
199 /*
200  * the caller is responsible for freeing qstr.name
201  * when called by nfs_readdir_add_to_array, the strings will be freed in
202  * nfs_clear_readdir_array()
203  */
204 static
nfs_readdir_make_qstr(struct qstr * string,const char * name,unsigned int len)205 int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
206 {
207 	string->len = len;
208 	string->name = kmemdup(name, len, GFP_KERNEL);
209 	if (string->name == NULL)
210 		return -ENOMEM;
211 	/*
212 	 * Avoid a kmemleak false positive. The pointer to the name is stored
213 	 * in a page cache page which kmemleak does not scan.
214 	 */
215 	kmemleak_not_leak(string->name);
216 	string->hash = full_name_hash(NULL, name, len);
217 	return 0;
218 }
219 
220 static
nfs_readdir_add_to_array(struct nfs_entry * entry,struct page * page)221 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
222 {
223 	struct nfs_cache_array *array = kmap(page);
224 	struct nfs_cache_array_entry *cache_entry;
225 	int ret;
226 
227 	cache_entry = &array->array[array->size];
228 
229 	/* Check that this entry lies within the page bounds */
230 	ret = -ENOSPC;
231 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
232 		goto out;
233 
234 	cache_entry->cookie = entry->prev_cookie;
235 	cache_entry->ino = entry->ino;
236 	cache_entry->d_type = entry->d_type;
237 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
238 	if (ret)
239 		goto out;
240 	array->last_cookie = entry->cookie;
241 	array->size++;
242 	if (entry->eof != 0)
243 		array->eof_index = array->size;
244 out:
245 	kunmap(page);
246 	return ret;
247 }
248 
249 static
nfs_readdir_search_for_pos(struct nfs_cache_array * array,nfs_readdir_descriptor_t * desc)250 int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
251 {
252 	loff_t diff = desc->ctx->pos - desc->current_index;
253 	unsigned int index;
254 
255 	if (diff < 0)
256 		goto out_eof;
257 	if (diff >= array->size) {
258 		if (array->eof_index >= 0)
259 			goto out_eof;
260 		return -EAGAIN;
261 	}
262 
263 	index = (unsigned int)diff;
264 	*desc->dir_cookie = array->array[index].cookie;
265 	desc->cache_entry_index = index;
266 	return 0;
267 out_eof:
268 	desc->eof = 1;
269 	return -EBADCOOKIE;
270 }
271 
272 static bool
nfs_readdir_inode_mapping_valid(struct nfs_inode * nfsi)273 nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
274 {
275 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
276 		return false;
277 	smp_rmb();
278 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
279 }
280 
281 static
nfs_readdir_search_for_cookie(struct nfs_cache_array * array,nfs_readdir_descriptor_t * desc)282 int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
283 {
284 	int i;
285 	loff_t new_pos;
286 	int status = -EAGAIN;
287 
288 	for (i = 0; i < array->size; i++) {
289 		if (array->array[i].cookie == *desc->dir_cookie) {
290 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
291 			struct nfs_open_dir_context *ctx = desc->file->private_data;
292 
293 			new_pos = desc->current_index + i;
294 			if (ctx->attr_gencount != nfsi->attr_gencount ||
295 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
296 				ctx->duped = 0;
297 				ctx->attr_gencount = nfsi->attr_gencount;
298 			} else if (new_pos < desc->ctx->pos) {
299 				if (ctx->duped > 0
300 				    && ctx->dup_cookie == *desc->dir_cookie) {
301 					if (printk_ratelimit()) {
302 						pr_notice("NFS: directory %pD2 contains a readdir loop."
303 								"Please contact your server vendor.  "
304 								"The file: %.*s has duplicate cookie %llu\n",
305 								desc->file, array->array[i].string.len,
306 								array->array[i].string.name, *desc->dir_cookie);
307 					}
308 					status = -ELOOP;
309 					goto out;
310 				}
311 				ctx->dup_cookie = *desc->dir_cookie;
312 				ctx->duped = -1;
313 			}
314 			desc->ctx->pos = new_pos;
315 			desc->cache_entry_index = i;
316 			return 0;
317 		}
318 	}
319 	if (array->eof_index >= 0) {
320 		status = -EBADCOOKIE;
321 		if (*desc->dir_cookie == array->last_cookie)
322 			desc->eof = 1;
323 	}
324 out:
325 	return status;
326 }
327 
328 static
nfs_readdir_search_array(nfs_readdir_descriptor_t * desc)329 int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
330 {
331 	struct nfs_cache_array *array;
332 	int status;
333 
334 	array = kmap(desc->page);
335 
336 	if (*desc->dir_cookie == 0)
337 		status = nfs_readdir_search_for_pos(array, desc);
338 	else
339 		status = nfs_readdir_search_for_cookie(array, desc);
340 
341 	if (status == -EAGAIN) {
342 		desc->last_cookie = array->last_cookie;
343 		desc->current_index += array->size;
344 		desc->page_index++;
345 	}
346 	kunmap(desc->page);
347 	return status;
348 }
349 
350 /* Fill a page with xdr information before transferring to the cache page */
351 static
nfs_readdir_xdr_filler(struct page ** pages,nfs_readdir_descriptor_t * desc,struct nfs_entry * entry,struct file * file,struct inode * inode)352 int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
353 			struct nfs_entry *entry, struct file *file, struct inode *inode)
354 {
355 	struct nfs_open_dir_context *ctx = file->private_data;
356 	struct rpc_cred	*cred = ctx->cred;
357 	unsigned long	timestamp, gencount;
358 	int		error;
359 
360  again:
361 	timestamp = jiffies;
362 	gencount = nfs_inc_attr_generation_counter();
363 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
364 					  NFS_SERVER(inode)->dtsize, desc->plus);
365 	if (error < 0) {
366 		/* We requested READDIRPLUS, but the server doesn't grok it */
367 		if (error == -ENOTSUPP && desc->plus) {
368 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
369 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
370 			desc->plus = false;
371 			goto again;
372 		}
373 		goto error;
374 	}
375 	desc->timestamp = timestamp;
376 	desc->gencount = gencount;
377 error:
378 	return error;
379 }
380 
xdr_decode(nfs_readdir_descriptor_t * desc,struct nfs_entry * entry,struct xdr_stream * xdr)381 static int xdr_decode(nfs_readdir_descriptor_t *desc,
382 		      struct nfs_entry *entry, struct xdr_stream *xdr)
383 {
384 	int error;
385 
386 	error = desc->decode(xdr, entry, desc->plus);
387 	if (error)
388 		return error;
389 	entry->fattr->time_start = desc->timestamp;
390 	entry->fattr->gencount = desc->gencount;
391 	return 0;
392 }
393 
394 /* Match file and dirent using either filehandle or fileid
395  * Note: caller is responsible for checking the fsid
396  */
397 static
nfs_same_file(struct dentry * dentry,struct nfs_entry * entry)398 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
399 {
400 	struct inode *inode;
401 	struct nfs_inode *nfsi;
402 
403 	if (d_really_is_negative(dentry))
404 		return 0;
405 
406 	inode = d_inode(dentry);
407 	if (is_bad_inode(inode) || NFS_STALE(inode))
408 		return 0;
409 
410 	nfsi = NFS_I(inode);
411 	if (entry->fattr->fileid != nfsi->fileid)
412 		return 0;
413 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
414 		return 0;
415 	return 1;
416 }
417 
418 static
nfs_use_readdirplus(struct inode * dir,struct dir_context * ctx)419 bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
420 {
421 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
422 		return false;
423 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
424 		return true;
425 	if (ctx->pos == 0)
426 		return true;
427 	return false;
428 }
429 
430 /*
431  * This function is called by the lookup and getattr code to request the
432  * use of readdirplus to accelerate any future lookups in the same
433  * directory.
434  */
nfs_advise_use_readdirplus(struct inode * dir)435 void nfs_advise_use_readdirplus(struct inode *dir)
436 {
437 	struct nfs_inode *nfsi = NFS_I(dir);
438 
439 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
440 	    !list_empty(&nfsi->open_files))
441 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
442 }
443 
444 /*
445  * This function is mainly for use by nfs_getattr().
446  *
447  * If this is an 'ls -l', we want to force use of readdirplus.
448  * Do this by checking if there is an active file descriptor
449  * and calling nfs_advise_use_readdirplus, then forcing a
450  * cache flush.
451  */
nfs_force_use_readdirplus(struct inode * dir)452 void nfs_force_use_readdirplus(struct inode *dir)
453 {
454 	struct nfs_inode *nfsi = NFS_I(dir);
455 
456 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
457 	    !list_empty(&nfsi->open_files)) {
458 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
459 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
460 	}
461 }
462 
463 static
nfs_prime_dcache(struct dentry * parent,struct nfs_entry * entry)464 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
465 {
466 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
467 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
468 	struct dentry *dentry;
469 	struct dentry *alias;
470 	struct inode *dir = d_inode(parent);
471 	struct inode *inode;
472 	int status;
473 
474 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
475 		return;
476 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
477 		return;
478 	if (filename.len == 0)
479 		return;
480 	/* Validate that the name doesn't contain any illegal '\0' */
481 	if (strnlen(filename.name, filename.len) != filename.len)
482 		return;
483 	/* ...or '/' */
484 	if (strnchr(filename.name, filename.len, '/'))
485 		return;
486 	if (filename.name[0] == '.') {
487 		if (filename.len == 1)
488 			return;
489 		if (filename.len == 2 && filename.name[1] == '.')
490 			return;
491 	}
492 	filename.hash = full_name_hash(parent, filename.name, filename.len);
493 
494 	dentry = d_lookup(parent, &filename);
495 again:
496 	if (!dentry) {
497 		dentry = d_alloc_parallel(parent, &filename, &wq);
498 		if (IS_ERR(dentry))
499 			return;
500 	}
501 	if (!d_in_lookup(dentry)) {
502 		/* Is there a mountpoint here? If so, just exit */
503 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
504 					&entry->fattr->fsid))
505 			goto out;
506 		if (nfs_same_file(dentry, entry)) {
507 			if (!entry->fh->size)
508 				goto out;
509 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
510 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
511 			if (!status)
512 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
513 			goto out;
514 		} else {
515 			d_invalidate(dentry);
516 			dput(dentry);
517 			dentry = NULL;
518 			goto again;
519 		}
520 	}
521 	if (!entry->fh->size) {
522 		d_lookup_done(dentry);
523 		goto out;
524 	}
525 
526 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
527 	alias = d_splice_alias(inode, dentry);
528 	d_lookup_done(dentry);
529 	if (alias) {
530 		if (IS_ERR(alias))
531 			goto out;
532 		dput(dentry);
533 		dentry = alias;
534 	}
535 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
536 out:
537 	dput(dentry);
538 }
539 
540 /* Perform conversion from xdr to cache array */
541 static
nfs_readdir_page_filler(nfs_readdir_descriptor_t * desc,struct nfs_entry * entry,struct page ** xdr_pages,struct page * page,unsigned int buflen)542 int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
543 				struct page **xdr_pages, struct page *page, unsigned int buflen)
544 {
545 	struct xdr_stream stream;
546 	struct xdr_buf buf;
547 	struct page *scratch;
548 	struct nfs_cache_array *array;
549 	unsigned int count = 0;
550 	int status;
551 
552 	scratch = alloc_page(GFP_KERNEL);
553 	if (scratch == NULL)
554 		return -ENOMEM;
555 
556 	if (buflen == 0)
557 		goto out_nopages;
558 
559 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
560 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
561 
562 	do {
563 		status = xdr_decode(desc, entry, &stream);
564 		if (status != 0) {
565 			if (status == -EAGAIN)
566 				status = 0;
567 			break;
568 		}
569 
570 		count++;
571 
572 		if (desc->plus)
573 			nfs_prime_dcache(file_dentry(desc->file), entry);
574 
575 		status = nfs_readdir_add_to_array(entry, page);
576 		if (status != 0)
577 			break;
578 	} while (!entry->eof);
579 
580 out_nopages:
581 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
582 		array = kmap(page);
583 		array->eof_index = array->size;
584 		status = 0;
585 		kunmap(page);
586 	}
587 
588 	put_page(scratch);
589 	return status;
590 }
591 
592 static
nfs_readdir_free_pages(struct page ** pages,unsigned int npages)593 void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
594 {
595 	unsigned int i;
596 	for (i = 0; i < npages; i++)
597 		put_page(pages[i]);
598 }
599 
600 /*
601  * nfs_readdir_large_page will allocate pages that must be freed with a call
602  * to nfs_readdir_free_pagearray
603  */
604 static
nfs_readdir_alloc_pages(struct page ** pages,unsigned int npages)605 int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
606 {
607 	unsigned int i;
608 
609 	for (i = 0; i < npages; i++) {
610 		struct page *page = alloc_page(GFP_KERNEL);
611 		if (page == NULL)
612 			goto out_freepages;
613 		pages[i] = page;
614 	}
615 	return 0;
616 
617 out_freepages:
618 	nfs_readdir_free_pages(pages, i);
619 	return -ENOMEM;
620 }
621 
622 static
nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t * desc,struct page * page,struct inode * inode)623 int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
624 {
625 	struct page *pages[NFS_MAX_READDIR_PAGES];
626 	struct nfs_entry entry;
627 	struct file	*file = desc->file;
628 	struct nfs_cache_array *array;
629 	int status = -ENOMEM;
630 	unsigned int array_size = ARRAY_SIZE(pages);
631 
632 	nfs_readdir_init_array(page);
633 
634 	entry.prev_cookie = 0;
635 	entry.cookie = desc->last_cookie;
636 	entry.eof = 0;
637 	entry.fh = nfs_alloc_fhandle();
638 	entry.fattr = nfs_alloc_fattr();
639 	entry.server = NFS_SERVER(inode);
640 	if (entry.fh == NULL || entry.fattr == NULL)
641 		goto out;
642 
643 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
644 	if (IS_ERR(entry.label)) {
645 		status = PTR_ERR(entry.label);
646 		goto out;
647 	}
648 
649 	array = kmap(page);
650 
651 	status = nfs_readdir_alloc_pages(pages, array_size);
652 	if (status < 0)
653 		goto out_release_array;
654 	do {
655 		unsigned int pglen;
656 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
657 
658 		if (status < 0)
659 			break;
660 		pglen = status;
661 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
662 		if (status < 0) {
663 			if (status == -ENOSPC)
664 				status = 0;
665 			break;
666 		}
667 	} while (array->eof_index < 0);
668 
669 	nfs_readdir_free_pages(pages, array_size);
670 out_release_array:
671 	kunmap(page);
672 	nfs4_label_free(entry.label);
673 out:
674 	nfs_free_fattr(entry.fattr);
675 	nfs_free_fhandle(entry.fh);
676 	return status;
677 }
678 
679 /*
680  * Now we cache directories properly, by converting xdr information
681  * to an array that can be used for lookups later.  This results in
682  * fewer cache pages, since we can store more information on each page.
683  * We only need to convert from xdr once so future lookups are much simpler
684  */
685 static
nfs_readdir_filler(struct file * file,struct page * page)686 int nfs_readdir_filler(struct file *file, struct page* page)
687 {
688 	nfs_readdir_descriptor_t *desc = (nfs_readdir_descriptor_t *)file;
689 	struct inode	*inode = file_inode(desc->file);
690 	int ret;
691 
692 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
693 	if (ret < 0)
694 		goto error;
695 	SetPageUptodate(page);
696 
697 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
698 		/* Should never happen */
699 		nfs_zap_mapping(inode, inode->i_mapping);
700 	}
701 	unlock_page(page);
702 	return 0;
703  error:
704 	nfs_readdir_clear_array(page);
705 	unlock_page(page);
706 	return ret;
707 }
708 
709 static
cache_page_release(nfs_readdir_descriptor_t * desc)710 void cache_page_release(nfs_readdir_descriptor_t *desc)
711 {
712 	put_page(desc->page);
713 	desc->page = NULL;
714 }
715 
716 static
get_cache_page(nfs_readdir_descriptor_t * desc)717 struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
718 {
719 	return read_cache_page(desc->file->f_mapping,
720 			desc->page_index, nfs_readdir_filler, desc);
721 }
722 
723 /*
724  * Returns 0 if desc->dir_cookie was found on page desc->page_index
725  * and locks the page to prevent removal from the page cache.
726  */
727 static
find_and_lock_cache_page(nfs_readdir_descriptor_t * desc)728 int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
729 {
730 	int res;
731 
732 	desc->page = get_cache_page(desc);
733 	if (IS_ERR(desc->page))
734 		return PTR_ERR(desc->page);
735 	res = lock_page_killable(desc->page);
736 	if (res != 0)
737 		goto error;
738 	res = -EAGAIN;
739 	if (desc->page->mapping != NULL) {
740 		res = nfs_readdir_search_array(desc);
741 		if (res == 0)
742 			return 0;
743 	}
744 	unlock_page(desc->page);
745 error:
746 	cache_page_release(desc);
747 	return res;
748 }
749 
750 /* Search for desc->dir_cookie from the beginning of the page cache */
751 static inline
readdir_search_pagecache(nfs_readdir_descriptor_t * desc)752 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
753 {
754 	int res;
755 
756 	if (desc->page_index == 0) {
757 		desc->current_index = 0;
758 		desc->last_cookie = 0;
759 	}
760 	do {
761 		res = find_and_lock_cache_page(desc);
762 	} while (res == -EAGAIN);
763 	return res;
764 }
765 
766 /*
767  * Once we've found the start of the dirent within a page: fill 'er up...
768  */
769 static
nfs_do_filldir(nfs_readdir_descriptor_t * desc)770 int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
771 {
772 	struct file	*file = desc->file;
773 	int i = 0;
774 	int res = 0;
775 	struct nfs_cache_array *array = NULL;
776 	struct nfs_open_dir_context *ctx = file->private_data;
777 
778 	array = kmap(desc->page);
779 	for (i = desc->cache_entry_index; i < array->size; i++) {
780 		struct nfs_cache_array_entry *ent;
781 
782 		ent = &array->array[i];
783 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
784 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
785 			desc->eof = 1;
786 			break;
787 		}
788 		desc->ctx->pos++;
789 		if (i < (array->size-1))
790 			*desc->dir_cookie = array->array[i+1].cookie;
791 		else
792 			*desc->dir_cookie = array->last_cookie;
793 		if (ctx->duped != 0)
794 			ctx->duped = 1;
795 	}
796 	if (array->eof_index >= 0)
797 		desc->eof = 1;
798 
799 	kunmap(desc->page);
800 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
801 			(unsigned long long)*desc->dir_cookie, res);
802 	return res;
803 }
804 
805 /*
806  * If we cannot find a cookie in our cache, we suspect that this is
807  * because it points to a deleted file, so we ask the server to return
808  * whatever it thinks is the next entry. We then feed this to filldir.
809  * If all goes well, we should then be able to find our way round the
810  * cache on the next call to readdir_search_pagecache();
811  *
812  * NOTE: we cannot add the anonymous page to the pagecache because
813  *	 the data it contains might not be page aligned. Besides,
814  *	 we should already have a complete representation of the
815  *	 directory in the page cache by the time we get here.
816  */
817 static inline
uncached_readdir(nfs_readdir_descriptor_t * desc)818 int uncached_readdir(nfs_readdir_descriptor_t *desc)
819 {
820 	struct page	*page = NULL;
821 	int		status;
822 	struct inode *inode = file_inode(desc->file);
823 	struct nfs_open_dir_context *ctx = desc->file->private_data;
824 
825 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
826 			(unsigned long long)*desc->dir_cookie);
827 
828 	page = alloc_page(GFP_HIGHUSER);
829 	if (!page) {
830 		status = -ENOMEM;
831 		goto out;
832 	}
833 
834 	desc->page_index = 0;
835 	desc->last_cookie = *desc->dir_cookie;
836 	desc->page = page;
837 	ctx->duped = 0;
838 
839 	status = nfs_readdir_xdr_to_array(desc, page, inode);
840 	if (status < 0)
841 		goto out_release;
842 
843 	status = nfs_do_filldir(desc);
844 
845  out_release:
846 	nfs_readdir_clear_array(desc->page);
847 	cache_page_release(desc);
848  out:
849 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
850 			__func__, status);
851 	return status;
852 }
853 
854 /* The file offset position represents the dirent entry number.  A
855    last cookie cache takes care of the common case of reading the
856    whole directory.
857  */
nfs_readdir(struct file * file,struct dir_context * ctx)858 static int nfs_readdir(struct file *file, struct dir_context *ctx)
859 {
860 	struct dentry	*dentry = file_dentry(file);
861 	struct inode	*inode = d_inode(dentry);
862 	nfs_readdir_descriptor_t my_desc,
863 			*desc = &my_desc;
864 	struct nfs_open_dir_context *dir_ctx = file->private_data;
865 	int res = 0;
866 
867 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
868 			file, (long long)ctx->pos);
869 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
870 
871 	/*
872 	 * ctx->pos points to the dirent entry number.
873 	 * *desc->dir_cookie has the cookie for the next entry. We have
874 	 * to either find the entry with the appropriate number or
875 	 * revalidate the cookie.
876 	 */
877 	memset(desc, 0, sizeof(*desc));
878 
879 	desc->file = file;
880 	desc->ctx = ctx;
881 	desc->dir_cookie = &dir_ctx->dir_cookie;
882 	desc->decode = NFS_PROTO(inode)->decode_dirent;
883 	desc->plus = nfs_use_readdirplus(inode, ctx);
884 
885 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
886 		res = nfs_revalidate_mapping(inode, file->f_mapping);
887 	if (res < 0)
888 		goto out;
889 
890 	do {
891 		res = readdir_search_pagecache(desc);
892 
893 		if (res == -EBADCOOKIE) {
894 			res = 0;
895 			/* This means either end of directory */
896 			if (*desc->dir_cookie && desc->eof == 0) {
897 				/* Or that the server has 'lost' a cookie */
898 				res = uncached_readdir(desc);
899 				if (res == 0)
900 					continue;
901 			}
902 			break;
903 		}
904 		if (res == -ETOOSMALL && desc->plus) {
905 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
906 			nfs_zap_caches(inode);
907 			desc->page_index = 0;
908 			desc->plus = false;
909 			desc->eof = false;
910 			continue;
911 		}
912 		if (res < 0)
913 			break;
914 
915 		res = nfs_do_filldir(desc);
916 		unlock_page(desc->page);
917 		cache_page_release(desc);
918 		if (res < 0)
919 			break;
920 	} while (!desc->eof);
921 out:
922 	if (res > 0)
923 		res = 0;
924 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
925 	return res;
926 }
927 
nfs_llseek_dir(struct file * filp,loff_t offset,int whence)928 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
929 {
930 	struct inode *inode = file_inode(filp);
931 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
932 
933 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
934 			filp, offset, whence);
935 
936 	inode_lock(inode);
937 	switch (whence) {
938 		case 1:
939 			offset += filp->f_pos;
940 		case 0:
941 			if (offset >= 0)
942 				break;
943 		default:
944 			offset = -EINVAL;
945 			goto out;
946 	}
947 	if (offset != filp->f_pos) {
948 		filp->f_pos = offset;
949 		dir_ctx->dir_cookie = 0;
950 		dir_ctx->duped = 0;
951 	}
952 out:
953 	inode_unlock(inode);
954 	return offset;
955 }
956 
957 /*
958  * All directory operations under NFS are synchronous, so fsync()
959  * is a dummy operation.
960  */
nfs_fsync_dir(struct file * filp,loff_t start,loff_t end,int datasync)961 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
962 			 int datasync)
963 {
964 	struct inode *inode = file_inode(filp);
965 
966 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
967 
968 	inode_lock(inode);
969 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
970 	inode_unlock(inode);
971 	return 0;
972 }
973 
974 /**
975  * nfs_force_lookup_revalidate - Mark the directory as having changed
976  * @dir - pointer to directory inode
977  *
978  * This forces the revalidation code in nfs_lookup_revalidate() to do a
979  * full lookup on all child dentries of 'dir' whenever a change occurs
980  * on the server that might have invalidated our dcache.
981  *
982  * The caller should be holding dir->i_lock
983  */
nfs_force_lookup_revalidate(struct inode * dir)984 void nfs_force_lookup_revalidate(struct inode *dir)
985 {
986 	NFS_I(dir)->cache_change_attribute++;
987 }
988 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
989 
990 /*
991  * A check for whether or not the parent directory has changed.
992  * In the case it has, we assume that the dentries are untrustworthy
993  * and may need to be looked up again.
994  * If rcu_walk prevents us from performing a full check, return 0.
995  */
nfs_check_verifier(struct inode * dir,struct dentry * dentry,int rcu_walk)996 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
997 			      int rcu_walk)
998 {
999 	if (IS_ROOT(dentry))
1000 		return 1;
1001 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1002 		return 0;
1003 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1004 		return 0;
1005 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1006 	if (nfs_mapping_need_revalidate_inode(dir)) {
1007 		if (rcu_walk)
1008 			return 0;
1009 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1010 			return 0;
1011 	}
1012 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1013 		return 0;
1014 	return 1;
1015 }
1016 
1017 /*
1018  * Use intent information to check whether or not we're going to do
1019  * an O_EXCL create using this path component.
1020  */
nfs_is_exclusive_create(struct inode * dir,unsigned int flags)1021 static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1022 {
1023 	if (NFS_PROTO(dir)->version == 2)
1024 		return 0;
1025 	return flags & LOOKUP_EXCL;
1026 }
1027 
1028 /*
1029  * Inode and filehandle revalidation for lookups.
1030  *
1031  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1032  * or if the intent information indicates that we're about to open this
1033  * particular file and the "nocto" mount flag is not set.
1034  *
1035  */
1036 static
nfs_lookup_verify_inode(struct inode * inode,unsigned int flags)1037 int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1038 {
1039 	struct nfs_server *server = NFS_SERVER(inode);
1040 	int ret;
1041 
1042 	if (IS_AUTOMOUNT(inode))
1043 		return 0;
1044 	/* VFS wants an on-the-wire revalidation */
1045 	if (flags & LOOKUP_REVAL)
1046 		goto out_force;
1047 	/* This is an open(2) */
1048 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1049 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1050 		goto out_force;
1051 out:
1052 	return (inode->i_nlink == 0) ? -ENOENT : 0;
1053 out_force:
1054 	if (flags & LOOKUP_RCU)
1055 		return -ECHILD;
1056 	ret = __nfs_revalidate_inode(server, inode);
1057 	if (ret != 0)
1058 		return ret;
1059 	goto out;
1060 }
1061 
1062 /*
1063  * We judge how long we want to trust negative
1064  * dentries by looking at the parent inode mtime.
1065  *
1066  * If parent mtime has changed, we revalidate, else we wait for a
1067  * period corresponding to the parent's attribute cache timeout value.
1068  *
1069  * If LOOKUP_RCU prevents us from performing a full check, return 1
1070  * suggesting a reval is needed.
1071  */
1072 static inline
nfs_neg_need_reval(struct inode * dir,struct dentry * dentry,unsigned int flags)1073 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1074 		       unsigned int flags)
1075 {
1076 	/* Don't revalidate a negative dentry if we're creating a new file */
1077 	if (flags & LOOKUP_CREATE)
1078 		return 0;
1079 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1080 		return 1;
1081 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
1082 }
1083 
1084 static int
nfs_lookup_revalidate_done(struct inode * dir,struct dentry * dentry,struct inode * inode,int error)1085 nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1086 			   struct inode *inode, int error)
1087 {
1088 	switch (error) {
1089 	case 1:
1090 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
1091 			__func__, dentry);
1092 		return 1;
1093 	case 0:
1094 		nfs_mark_for_revalidate(dir);
1095 		if (inode && S_ISDIR(inode->i_mode)) {
1096 			/* Purge readdir caches. */
1097 			nfs_zap_caches(inode);
1098 			/*
1099 			 * We can't d_drop the root of a disconnected tree:
1100 			 * its d_hash is on the s_anon list and d_drop() would hide
1101 			 * it from shrink_dcache_for_unmount(), leading to busy
1102 			 * inodes on unmount and further oopses.
1103 			 */
1104 			if (IS_ROOT(dentry))
1105 				return 1;
1106 		}
1107 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
1108 				__func__, dentry);
1109 		return 0;
1110 	}
1111 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
1112 				__func__, dentry, error);
1113 	return error;
1114 }
1115 
1116 static int
nfs_lookup_revalidate_negative(struct inode * dir,struct dentry * dentry,unsigned int flags)1117 nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1118 			       unsigned int flags)
1119 {
1120 	int ret = 1;
1121 	if (nfs_neg_need_reval(dir, dentry, flags)) {
1122 		if (flags & LOOKUP_RCU)
1123 			return -ECHILD;
1124 		ret = 0;
1125 	}
1126 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1127 }
1128 
1129 static int
nfs_lookup_revalidate_delegated(struct inode * dir,struct dentry * dentry,struct inode * inode)1130 nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1131 				struct inode *inode)
1132 {
1133 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1134 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1135 }
1136 
1137 static int
nfs_lookup_revalidate_dentry(struct inode * dir,struct dentry * dentry,struct inode * inode)1138 nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
1139 			     struct inode *inode)
1140 {
1141 	struct nfs_fh *fhandle;
1142 	struct nfs_fattr *fattr;
1143 	struct nfs4_label *label;
1144 	int ret;
1145 
1146 	ret = -ENOMEM;
1147 	fhandle = nfs_alloc_fhandle();
1148 	fattr = nfs_alloc_fattr();
1149 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1150 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
1151 		goto out;
1152 
1153 	ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1154 	if (ret < 0) {
1155 		if (ret == -ESTALE || ret == -ENOENT)
1156 			ret = 0;
1157 		goto out;
1158 	}
1159 	ret = 0;
1160 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
1161 		goto out;
1162 	if (nfs_refresh_inode(inode, fattr) < 0)
1163 		goto out;
1164 
1165 	nfs_setsecurity(inode, fattr, label);
1166 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1167 
1168 	/* set a readdirplus hint that we had a cache miss */
1169 	nfs_force_use_readdirplus(dir);
1170 	ret = 1;
1171 out:
1172 	nfs_free_fattr(fattr);
1173 	nfs_free_fhandle(fhandle);
1174 	nfs4_label_free(label);
1175 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1176 }
1177 
1178 /*
1179  * This is called every time the dcache has a lookup hit,
1180  * and we should check whether we can really trust that
1181  * lookup.
1182  *
1183  * NOTE! The hit can be a negative hit too, don't assume
1184  * we have an inode!
1185  *
1186  * If the parent directory is seen to have changed, we throw out the
1187  * cached dentry and do a new lookup.
1188  */
1189 static int
nfs_do_lookup_revalidate(struct inode * dir,struct dentry * dentry,unsigned int flags)1190 nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1191 			 unsigned int flags)
1192 {
1193 	struct inode *inode;
1194 	int error;
1195 
1196 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1197 	inode = d_inode(dentry);
1198 
1199 	if (!inode)
1200 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
1201 
1202 	if (is_bad_inode(inode)) {
1203 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1204 				__func__, dentry);
1205 		goto out_bad;
1206 	}
1207 
1208 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1209 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1210 
1211 	/* Force a full look up iff the parent directory has changed */
1212 	if (!nfs_is_exclusive_create(dir, flags) &&
1213 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1214 		error = nfs_lookup_verify_inode(inode, flags);
1215 		if (error) {
1216 			if (error == -ESTALE)
1217 				nfs_zap_caches(dir);
1218 			goto out_bad;
1219 		}
1220 		nfs_advise_use_readdirplus(dir);
1221 		goto out_valid;
1222 	}
1223 
1224 	if (flags & LOOKUP_RCU)
1225 		return -ECHILD;
1226 
1227 	if (NFS_STALE(inode))
1228 		goto out_bad;
1229 
1230 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1231 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
1232 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
1233 	return error;
1234 out_valid:
1235 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1236 out_bad:
1237 	if (flags & LOOKUP_RCU)
1238 		return -ECHILD;
1239 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1240 }
1241 
1242 static int
__nfs_lookup_revalidate(struct dentry * dentry,unsigned int flags,int (* reval)(struct inode *,struct dentry *,unsigned int))1243 __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1244 			int (*reval)(struct inode *, struct dentry *, unsigned int))
1245 {
1246 	struct dentry *parent;
1247 	struct inode *dir;
1248 	int ret;
1249 
1250 	if (flags & LOOKUP_RCU) {
1251 		parent = ACCESS_ONCE(dentry->d_parent);
1252 		dir = d_inode_rcu(parent);
1253 		if (!dir)
1254 			return -ECHILD;
1255 		ret = reval(dir, dentry, flags);
1256 		if (parent != ACCESS_ONCE(dentry->d_parent))
1257 			return -ECHILD;
1258 	} else {
1259 		parent = dget_parent(dentry);
1260 		ret = reval(d_inode(parent), dentry, flags);
1261 		dput(parent);
1262 	}
1263 	return ret;
1264 }
1265 
nfs_lookup_revalidate(struct dentry * dentry,unsigned int flags)1266 static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1267 {
1268 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1269 }
1270 
1271 /*
1272  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1273  * when we don't really care about the dentry name. This is called when a
1274  * pathwalk ends on a dentry that was not found via a normal lookup in the
1275  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1276  *
1277  * In this situation, we just want to verify that the inode itself is OK
1278  * since the dentry might have changed on the server.
1279  */
nfs_weak_revalidate(struct dentry * dentry,unsigned int flags)1280 static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1281 {
1282 	struct inode *inode = d_inode(dentry);
1283 	int error = 0;
1284 
1285 	/*
1286 	 * I believe we can only get a negative dentry here in the case of a
1287 	 * procfs-style symlink. Just assume it's correct for now, but we may
1288 	 * eventually need to do something more here.
1289 	 */
1290 	if (!inode) {
1291 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1292 				__func__, dentry);
1293 		return 1;
1294 	}
1295 
1296 	if (is_bad_inode(inode)) {
1297 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1298 				__func__, dentry);
1299 		return 0;
1300 	}
1301 
1302 	error = nfs_lookup_verify_inode(inode, flags);
1303 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1304 			__func__, inode->i_ino, error ? "invalid" : "valid");
1305 	return !error;
1306 }
1307 
1308 /*
1309  * This is called from dput() when d_count is going to 0.
1310  */
nfs_dentry_delete(const struct dentry * dentry)1311 static int nfs_dentry_delete(const struct dentry *dentry)
1312 {
1313 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1314 		dentry, dentry->d_flags);
1315 
1316 	/* Unhash any dentry with a stale inode */
1317 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
1318 		return 1;
1319 
1320 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1321 		/* Unhash it, so that ->d_iput() would be called */
1322 		return 1;
1323 	}
1324 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1325 		/* Unhash it, so that ancestors of killed async unlink
1326 		 * files will be cleaned up during umount */
1327 		return 1;
1328 	}
1329 	return 0;
1330 
1331 }
1332 
1333 /* Ensure that we revalidate inode->i_nlink */
nfs_drop_nlink(struct inode * inode)1334 static void nfs_drop_nlink(struct inode *inode)
1335 {
1336 	spin_lock(&inode->i_lock);
1337 	/* drop the inode if we're reasonably sure this is the last link */
1338 	if (inode->i_nlink == 1)
1339 		clear_nlink(inode);
1340 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
1341 	spin_unlock(&inode->i_lock);
1342 }
1343 
1344 /*
1345  * Called when the dentry loses inode.
1346  * We use it to clean up silly-renamed files.
1347  */
nfs_dentry_iput(struct dentry * dentry,struct inode * inode)1348 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1349 {
1350 	if (S_ISDIR(inode->i_mode))
1351 		/* drop any readdir cache as it could easily be old */
1352 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1353 
1354 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1355 		nfs_complete_unlink(dentry, inode);
1356 		nfs_drop_nlink(inode);
1357 	}
1358 	iput(inode);
1359 }
1360 
nfs_d_release(struct dentry * dentry)1361 static void nfs_d_release(struct dentry *dentry)
1362 {
1363 	/* free cached devname value, if it survived that far */
1364 	if (unlikely(dentry->d_fsdata)) {
1365 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1366 			WARN_ON(1);
1367 		else
1368 			kfree(dentry->d_fsdata);
1369 	}
1370 }
1371 
1372 const struct dentry_operations nfs_dentry_operations = {
1373 	.d_revalidate	= nfs_lookup_revalidate,
1374 	.d_weak_revalidate	= nfs_weak_revalidate,
1375 	.d_delete	= nfs_dentry_delete,
1376 	.d_iput		= nfs_dentry_iput,
1377 	.d_automount	= nfs_d_automount,
1378 	.d_release	= nfs_d_release,
1379 };
1380 EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1381 
nfs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1382 struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1383 {
1384 	struct dentry *res;
1385 	struct inode *inode = NULL;
1386 	struct nfs_fh *fhandle = NULL;
1387 	struct nfs_fattr *fattr = NULL;
1388 	struct nfs4_label *label = NULL;
1389 	int error;
1390 
1391 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
1392 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1393 
1394 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1395 		return ERR_PTR(-ENAMETOOLONG);
1396 
1397 	/*
1398 	 * If we're doing an exclusive create, optimize away the lookup
1399 	 * but don't hash the dentry.
1400 	 */
1401 	if (nfs_is_exclusive_create(dir, flags))
1402 		return NULL;
1403 
1404 	res = ERR_PTR(-ENOMEM);
1405 	fhandle = nfs_alloc_fhandle();
1406 	fattr = nfs_alloc_fattr();
1407 	if (fhandle == NULL || fattr == NULL)
1408 		goto out;
1409 
1410 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
1411 	if (IS_ERR(label))
1412 		goto out;
1413 
1414 	trace_nfs_lookup_enter(dir, dentry, flags);
1415 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1416 	if (error == -ENOENT)
1417 		goto no_entry;
1418 	if (error < 0) {
1419 		res = ERR_PTR(error);
1420 		goto out_label;
1421 	}
1422 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1423 	res = ERR_CAST(inode);
1424 	if (IS_ERR(res))
1425 		goto out_label;
1426 
1427 	/* Notify readdir to use READDIRPLUS */
1428 	nfs_force_use_readdirplus(dir);
1429 
1430 no_entry:
1431 	res = d_splice_alias(inode, dentry);
1432 	if (res != NULL) {
1433 		if (IS_ERR(res))
1434 			goto out_label;
1435 		dentry = res;
1436 	}
1437 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1438 out_label:
1439 	trace_nfs_lookup_exit(dir, dentry, flags, error);
1440 	nfs4_label_free(label);
1441 out:
1442 	nfs_free_fattr(fattr);
1443 	nfs_free_fhandle(fhandle);
1444 	return res;
1445 }
1446 EXPORT_SYMBOL_GPL(nfs_lookup);
1447 
1448 #if IS_ENABLED(CONFIG_NFS_V4)
1449 static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
1450 
1451 const struct dentry_operations nfs4_dentry_operations = {
1452 	.d_revalidate	= nfs4_lookup_revalidate,
1453 	.d_weak_revalidate	= nfs_weak_revalidate,
1454 	.d_delete	= nfs_dentry_delete,
1455 	.d_iput		= nfs_dentry_iput,
1456 	.d_automount	= nfs_d_automount,
1457 	.d_release	= nfs_d_release,
1458 };
1459 EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1460 
flags_to_mode(int flags)1461 static fmode_t flags_to_mode(int flags)
1462 {
1463 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1464 	if ((flags & O_ACCMODE) != O_WRONLY)
1465 		res |= FMODE_READ;
1466 	if ((flags & O_ACCMODE) != O_RDONLY)
1467 		res |= FMODE_WRITE;
1468 	return res;
1469 }
1470 
create_nfs_open_context(struct dentry * dentry,int open_flags,struct file * filp)1471 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1472 {
1473 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1474 }
1475 
do_open(struct inode * inode,struct file * filp)1476 static int do_open(struct inode *inode, struct file *filp)
1477 {
1478 	nfs_fscache_open_file(inode, filp);
1479 	return 0;
1480 }
1481 
nfs_finish_open(struct nfs_open_context * ctx,struct dentry * dentry,struct file * file,unsigned open_flags,int * opened)1482 static int nfs_finish_open(struct nfs_open_context *ctx,
1483 			   struct dentry *dentry,
1484 			   struct file *file, unsigned open_flags,
1485 			   int *opened)
1486 {
1487 	int err;
1488 
1489 	err = finish_open(file, dentry, do_open, opened);
1490 	if (err)
1491 		goto out;
1492 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1493 		nfs_file_set_open_context(file, ctx);
1494 	else
1495 		err = -EOPENSTALE;
1496 out:
1497 	return err;
1498 }
1499 
nfs_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned open_flags,umode_t mode,int * opened)1500 int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1501 		    struct file *file, unsigned open_flags,
1502 		    umode_t mode, int *opened)
1503 {
1504 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1505 	struct nfs_open_context *ctx;
1506 	struct dentry *res;
1507 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1508 	struct inode *inode;
1509 	unsigned int lookup_flags = 0;
1510 	bool switched = false;
1511 	int err;
1512 
1513 	/* Expect a negative dentry */
1514 	BUG_ON(d_inode(dentry));
1515 
1516 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
1517 			dir->i_sb->s_id, dir->i_ino, dentry);
1518 
1519 	err = nfs_check_flags(open_flags);
1520 	if (err)
1521 		return err;
1522 
1523 	/* NFS only supports OPEN on regular files */
1524 	if ((open_flags & O_DIRECTORY)) {
1525 		if (!d_in_lookup(dentry)) {
1526 			/*
1527 			 * Hashed negative dentry with O_DIRECTORY: dentry was
1528 			 * revalidated and is fine, no need to perform lookup
1529 			 * again
1530 			 */
1531 			return -ENOENT;
1532 		}
1533 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
1534 		goto no_open;
1535 	}
1536 
1537 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1538 		return -ENAMETOOLONG;
1539 
1540 	if (open_flags & O_CREAT) {
1541 		struct nfs_server *server = NFS_SERVER(dir);
1542 
1543 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1544 			mode &= ~current_umask();
1545 
1546 		attr.ia_valid |= ATTR_MODE;
1547 		attr.ia_mode = mode;
1548 	}
1549 	if (open_flags & O_TRUNC) {
1550 		attr.ia_valid |= ATTR_SIZE;
1551 		attr.ia_size = 0;
1552 	}
1553 
1554 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1555 		d_drop(dentry);
1556 		switched = true;
1557 		dentry = d_alloc_parallel(dentry->d_parent,
1558 					  &dentry->d_name, &wq);
1559 		if (IS_ERR(dentry))
1560 			return PTR_ERR(dentry);
1561 		if (unlikely(!d_in_lookup(dentry)))
1562 			return finish_no_open(file, dentry);
1563 	}
1564 
1565 	ctx = create_nfs_open_context(dentry, open_flags, file);
1566 	err = PTR_ERR(ctx);
1567 	if (IS_ERR(ctx))
1568 		goto out;
1569 
1570 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
1571 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1572 	if (IS_ERR(inode)) {
1573 		err = PTR_ERR(inode);
1574 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
1575 		put_nfs_open_context(ctx);
1576 		d_drop(dentry);
1577 		switch (err) {
1578 		case -ENOENT:
1579 			d_splice_alias(NULL, dentry);
1580 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1581 			break;
1582 		case -EISDIR:
1583 		case -ENOTDIR:
1584 			goto no_open;
1585 		case -ELOOP:
1586 			if (!(open_flags & O_NOFOLLOW))
1587 				goto no_open;
1588 			break;
1589 			/* case -EINVAL: */
1590 		default:
1591 			break;
1592 		}
1593 		goto out;
1594 	}
1595 
1596 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
1597 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
1598 	put_nfs_open_context(ctx);
1599 out:
1600 	if (unlikely(switched)) {
1601 		d_lookup_done(dentry);
1602 		dput(dentry);
1603 	}
1604 	return err;
1605 
1606 no_open:
1607 	res = nfs_lookup(dir, dentry, lookup_flags);
1608 	if (switched) {
1609 		d_lookup_done(dentry);
1610 		if (!res)
1611 			res = dentry;
1612 		else
1613 			dput(dentry);
1614 	}
1615 	if (IS_ERR(res))
1616 		return PTR_ERR(res);
1617 	return finish_no_open(file, res);
1618 }
1619 EXPORT_SYMBOL_GPL(nfs_atomic_open);
1620 
1621 static int
nfs4_do_lookup_revalidate(struct inode * dir,struct dentry * dentry,unsigned int flags)1622 nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1623 			  unsigned int flags)
1624 {
1625 	struct inode *inode;
1626 
1627 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1628 		goto full_reval;
1629 	if (d_mountpoint(dentry))
1630 		goto full_reval;
1631 
1632 	inode = d_inode(dentry);
1633 
1634 	/* We can't create new files in nfs_open_revalidate(), so we
1635 	 * optimize away revalidation of negative dentries.
1636 	 */
1637 	if (inode == NULL)
1638 		goto full_reval;
1639 
1640 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1641 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1642 
1643 	/* NFS only supports OPEN on regular files */
1644 	if (!S_ISREG(inode->i_mode))
1645 		goto full_reval;
1646 
1647 	/* We cannot do exclusive creation on a positive dentry */
1648 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1649 		goto reval_dentry;
1650 
1651 	/* Check if the directory changed */
1652 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1653 		goto reval_dentry;
1654 
1655 	/* Let f_op->open() actually open (and revalidate) the file */
1656 	return 1;
1657 reval_dentry:
1658 	if (flags & LOOKUP_RCU)
1659 		return -ECHILD;
1660 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);;
1661 
1662 full_reval:
1663 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1664 }
1665 
nfs4_lookup_revalidate(struct dentry * dentry,unsigned int flags)1666 static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1667 {
1668 	return __nfs_lookup_revalidate(dentry, flags,
1669 			nfs4_do_lookup_revalidate);
1670 }
1671 
1672 #endif /* CONFIG_NFSV4 */
1673 
1674 /*
1675  * Code common to create, mkdir, and mknod.
1676  */
nfs_instantiate(struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct nfs4_label * label)1677 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1678 				struct nfs_fattr *fattr,
1679 				struct nfs4_label *label)
1680 {
1681 	struct dentry *parent = dget_parent(dentry);
1682 	struct inode *dir = d_inode(parent);
1683 	struct inode *inode;
1684 	int error = -EACCES;
1685 
1686 	d_drop(dentry);
1687 
1688 	/* We may have been initialized further down */
1689 	if (d_really_is_positive(dentry))
1690 		goto out;
1691 	if (fhandle->size == 0) {
1692 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
1693 		if (error)
1694 			goto out_error;
1695 	}
1696 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1697 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
1698 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1699 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
1700 		if (error < 0)
1701 			goto out_error;
1702 	}
1703 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1704 	error = PTR_ERR(inode);
1705 	if (IS_ERR(inode))
1706 		goto out_error;
1707 	d_add(dentry, inode);
1708 out:
1709 	dput(parent);
1710 	return 0;
1711 out_error:
1712 	nfs_mark_for_revalidate(dir);
1713 	dput(parent);
1714 	return error;
1715 }
1716 EXPORT_SYMBOL_GPL(nfs_instantiate);
1717 
1718 /*
1719  * Following a failed create operation, we drop the dentry rather
1720  * than retain a negative dentry. This avoids a problem in the event
1721  * that the operation succeeded on the server, but an error in the
1722  * reply path made it appear to have failed.
1723  */
nfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)1724 int nfs_create(struct inode *dir, struct dentry *dentry,
1725 		umode_t mode, bool excl)
1726 {
1727 	struct iattr attr;
1728 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
1729 	int error;
1730 
1731 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
1732 			dir->i_sb->s_id, dir->i_ino, dentry);
1733 
1734 	attr.ia_mode = mode;
1735 	attr.ia_valid = ATTR_MODE;
1736 
1737 	trace_nfs_create_enter(dir, dentry, open_flags);
1738 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
1739 	trace_nfs_create_exit(dir, dentry, open_flags, error);
1740 	if (error != 0)
1741 		goto out_err;
1742 	return 0;
1743 out_err:
1744 	d_drop(dentry);
1745 	return error;
1746 }
1747 EXPORT_SYMBOL_GPL(nfs_create);
1748 
1749 /*
1750  * See comments for nfs_proc_create regarding failed operations.
1751  */
1752 int
nfs_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)1753 nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1754 {
1755 	struct iattr attr;
1756 	int status;
1757 
1758 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
1759 			dir->i_sb->s_id, dir->i_ino, dentry);
1760 
1761 	attr.ia_mode = mode;
1762 	attr.ia_valid = ATTR_MODE;
1763 
1764 	trace_nfs_mknod_enter(dir, dentry);
1765 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1766 	trace_nfs_mknod_exit(dir, dentry, status);
1767 	if (status != 0)
1768 		goto out_err;
1769 	return 0;
1770 out_err:
1771 	d_drop(dentry);
1772 	return status;
1773 }
1774 EXPORT_SYMBOL_GPL(nfs_mknod);
1775 
1776 /*
1777  * See comments for nfs_proc_create regarding failed operations.
1778  */
nfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)1779 int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1780 {
1781 	struct iattr attr;
1782 	int error;
1783 
1784 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
1785 			dir->i_sb->s_id, dir->i_ino, dentry);
1786 
1787 	attr.ia_valid = ATTR_MODE;
1788 	attr.ia_mode = mode | S_IFDIR;
1789 
1790 	trace_nfs_mkdir_enter(dir, dentry);
1791 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1792 	trace_nfs_mkdir_exit(dir, dentry, error);
1793 	if (error != 0)
1794 		goto out_err;
1795 	return 0;
1796 out_err:
1797 	d_drop(dentry);
1798 	return error;
1799 }
1800 EXPORT_SYMBOL_GPL(nfs_mkdir);
1801 
nfs_dentry_handle_enoent(struct dentry * dentry)1802 static void nfs_dentry_handle_enoent(struct dentry *dentry)
1803 {
1804 	if (simple_positive(dentry))
1805 		d_delete(dentry);
1806 }
1807 
nfs_rmdir(struct inode * dir,struct dentry * dentry)1808 int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1809 {
1810 	int error;
1811 
1812 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
1813 			dir->i_sb->s_id, dir->i_ino, dentry);
1814 
1815 	trace_nfs_rmdir_enter(dir, dentry);
1816 	if (d_really_is_positive(dentry)) {
1817 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1818 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1819 		/* Ensure the VFS deletes this inode */
1820 		switch (error) {
1821 		case 0:
1822 			clear_nlink(d_inode(dentry));
1823 			break;
1824 		case -ENOENT:
1825 			nfs_dentry_handle_enoent(dentry);
1826 		}
1827 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1828 	} else
1829 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1830 	trace_nfs_rmdir_exit(dir, dentry, error);
1831 
1832 	return error;
1833 }
1834 EXPORT_SYMBOL_GPL(nfs_rmdir);
1835 
1836 /*
1837  * Remove a file after making sure there are no pending writes,
1838  * and after checking that the file has only one user.
1839  *
1840  * We invalidate the attribute cache and free the inode prior to the operation
1841  * to avoid possible races if the server reuses the inode.
1842  */
nfs_safe_remove(struct dentry * dentry)1843 static int nfs_safe_remove(struct dentry *dentry)
1844 {
1845 	struct inode *dir = d_inode(dentry->d_parent);
1846 	struct inode *inode = d_inode(dentry);
1847 	int error = -EBUSY;
1848 
1849 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
1850 
1851 	/* If the dentry was sillyrenamed, we simply call d_delete() */
1852 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1853 		error = 0;
1854 		goto out;
1855 	}
1856 
1857 	trace_nfs_remove_enter(dir, dentry);
1858 	if (inode != NULL) {
1859 		NFS_PROTO(inode)->return_delegation(inode);
1860 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1861 		if (error == 0)
1862 			nfs_drop_nlink(inode);
1863 	} else
1864 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1865 	if (error == -ENOENT)
1866 		nfs_dentry_handle_enoent(dentry);
1867 	trace_nfs_remove_exit(dir, dentry, error);
1868 out:
1869 	return error;
1870 }
1871 
1872 /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
1873  *  belongs to an active ".nfs..." file and we return -EBUSY.
1874  *
1875  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
1876  */
nfs_unlink(struct inode * dir,struct dentry * dentry)1877 int nfs_unlink(struct inode *dir, struct dentry *dentry)
1878 {
1879 	int error;
1880 	int need_rehash = 0;
1881 
1882 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
1883 		dir->i_ino, dentry);
1884 
1885 	trace_nfs_unlink_enter(dir, dentry);
1886 	spin_lock(&dentry->d_lock);
1887 	if (d_count(dentry) > 1) {
1888 		spin_unlock(&dentry->d_lock);
1889 		/* Start asynchronous writeout of the inode */
1890 		write_inode_now(d_inode(dentry), 0);
1891 		error = nfs_sillyrename(dir, dentry);
1892 		goto out;
1893 	}
1894 	if (!d_unhashed(dentry)) {
1895 		__d_drop(dentry);
1896 		need_rehash = 1;
1897 	}
1898 	spin_unlock(&dentry->d_lock);
1899 	error = nfs_safe_remove(dentry);
1900 	if (!error || error == -ENOENT) {
1901 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1902 	} else if (need_rehash)
1903 		d_rehash(dentry);
1904 out:
1905 	trace_nfs_unlink_exit(dir, dentry, error);
1906 	return error;
1907 }
1908 EXPORT_SYMBOL_GPL(nfs_unlink);
1909 
1910 /*
1911  * To create a symbolic link, most file systems instantiate a new inode,
1912  * add a page to it containing the path, then write it out to the disk
1913  * using prepare_write/commit_write.
1914  *
1915  * Unfortunately the NFS client can't create the in-core inode first
1916  * because it needs a file handle to create an in-core inode (see
1917  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1918  * symlink request has completed on the server.
1919  *
1920  * So instead we allocate a raw page, copy the symname into it, then do
1921  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1922  * now have a new file handle and can instantiate an in-core NFS inode
1923  * and move the raw page into its mapping.
1924  */
nfs_symlink(struct inode * dir,struct dentry * dentry,const char * symname)1925 int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1926 {
1927 	struct page *page;
1928 	char *kaddr;
1929 	struct iattr attr;
1930 	unsigned int pathlen = strlen(symname);
1931 	int error;
1932 
1933 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
1934 		dir->i_ino, dentry, symname);
1935 
1936 	if (pathlen > PAGE_SIZE)
1937 		return -ENAMETOOLONG;
1938 
1939 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1940 	attr.ia_valid = ATTR_MODE;
1941 
1942 	page = alloc_page(GFP_USER);
1943 	if (!page)
1944 		return -ENOMEM;
1945 
1946 	kaddr = page_address(page);
1947 	memcpy(kaddr, symname, pathlen);
1948 	if (pathlen < PAGE_SIZE)
1949 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1950 
1951 	trace_nfs_symlink_enter(dir, dentry);
1952 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1953 	trace_nfs_symlink_exit(dir, dentry, error);
1954 	if (error != 0) {
1955 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1956 			dir->i_sb->s_id, dir->i_ino,
1957 			dentry, symname, error);
1958 		d_drop(dentry);
1959 		__free_page(page);
1960 		return error;
1961 	}
1962 
1963 	/*
1964 	 * No big deal if we can't add this page to the page cache here.
1965 	 * READLINK will get the missing page from the server if needed.
1966 	 */
1967 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
1968 							GFP_KERNEL)) {
1969 		SetPageUptodate(page);
1970 		unlock_page(page);
1971 		/*
1972 		 * add_to_page_cache_lru() grabs an extra page refcount.
1973 		 * Drop it here to avoid leaking this page later.
1974 		 */
1975 		put_page(page);
1976 	} else
1977 		__free_page(page);
1978 
1979 	return 0;
1980 }
1981 EXPORT_SYMBOL_GPL(nfs_symlink);
1982 
1983 int
nfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1984 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1985 {
1986 	struct inode *inode = d_inode(old_dentry);
1987 	int error;
1988 
1989 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
1990 		old_dentry, dentry);
1991 
1992 	trace_nfs_link_enter(inode, dir, dentry);
1993 	NFS_PROTO(inode)->return_delegation(inode);
1994 
1995 	d_drop(dentry);
1996 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1997 	if (error == 0) {
1998 		ihold(inode);
1999 		d_add(dentry, inode);
2000 	}
2001 	trace_nfs_link_exit(inode, dir, dentry, error);
2002 	return error;
2003 }
2004 EXPORT_SYMBOL_GPL(nfs_link);
2005 
2006 /*
2007  * RENAME
2008  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2009  * different file handle for the same inode after a rename (e.g. when
2010  * moving to a different directory). A fail-safe method to do so would
2011  * be to look up old_dir/old_name, create a link to new_dir/new_name and
2012  * rename the old file using the sillyrename stuff. This way, the original
2013  * file in old_dir will go away when the last process iput()s the inode.
2014  *
2015  * FIXED.
2016  *
2017  * It actually works quite well. One needs to have the possibility for
2018  * at least one ".nfs..." file in each directory the file ever gets
2019  * moved or linked to which happens automagically with the new
2020  * implementation that only depends on the dcache stuff instead of
2021  * using the inode layer
2022  *
2023  * Unfortunately, things are a little more complicated than indicated
2024  * above. For a cross-directory move, we want to make sure we can get
2025  * rid of the old inode after the operation.  This means there must be
2026  * no pending writes (if it's a file), and the use count must be 1.
2027  * If these conditions are met, we can drop the dentries before doing
2028  * the rename.
2029  */
nfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)2030 int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2031 	       struct inode *new_dir, struct dentry *new_dentry,
2032 	       unsigned int flags)
2033 {
2034 	struct inode *old_inode = d_inode(old_dentry);
2035 	struct inode *new_inode = d_inode(new_dentry);
2036 	struct dentry *dentry = NULL, *rehash = NULL;
2037 	struct rpc_task *task;
2038 	int error = -EBUSY;
2039 
2040 	if (flags)
2041 		return -EINVAL;
2042 
2043 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2044 		 old_dentry, new_dentry,
2045 		 d_count(new_dentry));
2046 
2047 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
2048 	/*
2049 	 * For non-directories, check whether the target is busy and if so,
2050 	 * make a copy of the dentry and then do a silly-rename. If the
2051 	 * silly-rename succeeds, the copied dentry is hashed and becomes
2052 	 * the new target.
2053 	 */
2054 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2055 		/*
2056 		 * To prevent any new references to the target during the
2057 		 * rename, we unhash the dentry in advance.
2058 		 */
2059 		if (!d_unhashed(new_dentry)) {
2060 			d_drop(new_dentry);
2061 			rehash = new_dentry;
2062 		}
2063 
2064 		if (d_count(new_dentry) > 2) {
2065 			int err;
2066 
2067 			/* copy the target dentry's name */
2068 			dentry = d_alloc(new_dentry->d_parent,
2069 					 &new_dentry->d_name);
2070 			if (!dentry)
2071 				goto out;
2072 
2073 			/* silly-rename the existing target ... */
2074 			err = nfs_sillyrename(new_dir, new_dentry);
2075 			if (err)
2076 				goto out;
2077 
2078 			new_dentry = dentry;
2079 			rehash = NULL;
2080 			new_inode = NULL;
2081 		}
2082 	}
2083 
2084 	NFS_PROTO(old_inode)->return_delegation(old_inode);
2085 	if (new_inode != NULL)
2086 		NFS_PROTO(new_inode)->return_delegation(new_inode);
2087 
2088 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
2089 	if (IS_ERR(task)) {
2090 		error = PTR_ERR(task);
2091 		goto out;
2092 	}
2093 
2094 	error = rpc_wait_for_completion_task(task);
2095 	if (error != 0) {
2096 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2097 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2098 		smp_wmb();
2099 	} else
2100 		error = task->tk_status;
2101 	rpc_put_task(task);
2102 	nfs_mark_for_revalidate(old_inode);
2103 out:
2104 	if (rehash)
2105 		d_rehash(rehash);
2106 	trace_nfs_rename_exit(old_dir, old_dentry,
2107 			new_dir, new_dentry, error);
2108 	if (!error) {
2109 		if (new_inode != NULL)
2110 			nfs_drop_nlink(new_inode);
2111 		/*
2112 		 * The d_move() should be here instead of in an async RPC completion
2113 		 * handler because we need the proper locks to move the dentry.  If
2114 		 * we're interrupted by a signal, the async RPC completion handler
2115 		 * should mark the directories for revalidation.
2116 		 */
2117 		d_move(old_dentry, new_dentry);
2118 		nfs_set_verifier(old_dentry,
2119 					nfs_save_change_attribute(new_dir));
2120 	} else if (error == -ENOENT)
2121 		nfs_dentry_handle_enoent(old_dentry);
2122 
2123 	/* new dentry created? */
2124 	if (dentry)
2125 		dput(dentry);
2126 	return error;
2127 }
2128 EXPORT_SYMBOL_GPL(nfs_rename);
2129 
2130 static DEFINE_SPINLOCK(nfs_access_lru_lock);
2131 static LIST_HEAD(nfs_access_lru_list);
2132 static atomic_long_t nfs_access_nr_entries;
2133 
2134 static unsigned long nfs_access_max_cachesize = ULONG_MAX;
2135 module_param(nfs_access_max_cachesize, ulong, 0644);
2136 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2137 
nfs_access_free_entry(struct nfs_access_entry * entry)2138 static void nfs_access_free_entry(struct nfs_access_entry *entry)
2139 {
2140 	put_rpccred(entry->cred);
2141 	kfree_rcu(entry, rcu_head);
2142 	smp_mb__before_atomic();
2143 	atomic_long_dec(&nfs_access_nr_entries);
2144 	smp_mb__after_atomic();
2145 }
2146 
nfs_access_free_list(struct list_head * head)2147 static void nfs_access_free_list(struct list_head *head)
2148 {
2149 	struct nfs_access_entry *cache;
2150 
2151 	while (!list_empty(head)) {
2152 		cache = list_entry(head->next, struct nfs_access_entry, lru);
2153 		list_del(&cache->lru);
2154 		nfs_access_free_entry(cache);
2155 	}
2156 }
2157 
2158 static unsigned long
nfs_do_access_cache_scan(unsigned int nr_to_scan)2159 nfs_do_access_cache_scan(unsigned int nr_to_scan)
2160 {
2161 	LIST_HEAD(head);
2162 	struct nfs_inode *nfsi, *next;
2163 	struct nfs_access_entry *cache;
2164 	long freed = 0;
2165 
2166 	spin_lock(&nfs_access_lru_lock);
2167 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2168 		struct inode *inode;
2169 
2170 		if (nr_to_scan-- == 0)
2171 			break;
2172 		inode = &nfsi->vfs_inode;
2173 		spin_lock(&inode->i_lock);
2174 		if (list_empty(&nfsi->access_cache_entry_lru))
2175 			goto remove_lru_entry;
2176 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2177 				struct nfs_access_entry, lru);
2178 		list_move(&cache->lru, &head);
2179 		rb_erase(&cache->rb_node, &nfsi->access_cache);
2180 		freed++;
2181 		if (!list_empty(&nfsi->access_cache_entry_lru))
2182 			list_move_tail(&nfsi->access_cache_inode_lru,
2183 					&nfs_access_lru_list);
2184 		else {
2185 remove_lru_entry:
2186 			list_del_init(&nfsi->access_cache_inode_lru);
2187 			smp_mb__before_atomic();
2188 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
2189 			smp_mb__after_atomic();
2190 		}
2191 		spin_unlock(&inode->i_lock);
2192 	}
2193 	spin_unlock(&nfs_access_lru_lock);
2194 	nfs_access_free_list(&head);
2195 	return freed;
2196 }
2197 
2198 unsigned long
nfs_access_cache_scan(struct shrinker * shrink,struct shrink_control * sc)2199 nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2200 {
2201 	int nr_to_scan = sc->nr_to_scan;
2202 	gfp_t gfp_mask = sc->gfp_mask;
2203 
2204 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2205 		return SHRINK_STOP;
2206 	return nfs_do_access_cache_scan(nr_to_scan);
2207 }
2208 
2209 
2210 unsigned long
nfs_access_cache_count(struct shrinker * shrink,struct shrink_control * sc)2211 nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2212 {
2213 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2214 }
2215 
2216 static void
nfs_access_cache_enforce_limit(void)2217 nfs_access_cache_enforce_limit(void)
2218 {
2219 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2220 	unsigned long diff;
2221 	unsigned int nr_to_scan;
2222 
2223 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2224 		return;
2225 	nr_to_scan = 100;
2226 	diff = nr_entries - nfs_access_max_cachesize;
2227 	if (diff < nr_to_scan)
2228 		nr_to_scan = diff;
2229 	nfs_do_access_cache_scan(nr_to_scan);
2230 }
2231 
__nfs_access_zap_cache(struct nfs_inode * nfsi,struct list_head * head)2232 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
2233 {
2234 	struct rb_root *root_node = &nfsi->access_cache;
2235 	struct rb_node *n;
2236 	struct nfs_access_entry *entry;
2237 
2238 	/* Unhook entries from the cache */
2239 	while ((n = rb_first(root_node)) != NULL) {
2240 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
2241 		rb_erase(n, root_node);
2242 		list_move(&entry->lru, head);
2243 	}
2244 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
2245 }
2246 
nfs_access_zap_cache(struct inode * inode)2247 void nfs_access_zap_cache(struct inode *inode)
2248 {
2249 	LIST_HEAD(head);
2250 
2251 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2252 		return;
2253 	/* Remove from global LRU init */
2254 	spin_lock(&nfs_access_lru_lock);
2255 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2256 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2257 
2258 	spin_lock(&inode->i_lock);
2259 	__nfs_access_zap_cache(NFS_I(inode), &head);
2260 	spin_unlock(&inode->i_lock);
2261 	spin_unlock(&nfs_access_lru_lock);
2262 	nfs_access_free_list(&head);
2263 }
2264 EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
2265 
nfs_access_search_rbtree(struct inode * inode,struct rpc_cred * cred)2266 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2267 {
2268 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2269 	struct nfs_access_entry *entry;
2270 
2271 	while (n != NULL) {
2272 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
2273 
2274 		if (cred < entry->cred)
2275 			n = n->rb_left;
2276 		else if (cred > entry->cred)
2277 			n = n->rb_right;
2278 		else
2279 			return entry;
2280 	}
2281 	return NULL;
2282 }
2283 
nfs_access_get_cached(struct inode * inode,struct rpc_cred * cred,struct nfs_access_entry * res,bool may_block)2284 static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block)
2285 {
2286 	struct nfs_inode *nfsi = NFS_I(inode);
2287 	struct nfs_access_entry *cache;
2288 	bool retry = true;
2289 	int err;
2290 
2291 	spin_lock(&inode->i_lock);
2292 	for(;;) {
2293 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2294 			goto out_zap;
2295 		cache = nfs_access_search_rbtree(inode, cred);
2296 		err = -ENOENT;
2297 		if (cache == NULL)
2298 			goto out;
2299 		/* Found an entry, is our attribute cache valid? */
2300 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2301 			break;
2302 		err = -ECHILD;
2303 		if (!may_block)
2304 			goto out;
2305 		if (!retry)
2306 			goto out_zap;
2307 		spin_unlock(&inode->i_lock);
2308 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2309 		if (err)
2310 			return err;
2311 		spin_lock(&inode->i_lock);
2312 		retry = false;
2313 	}
2314 	res->cred = cache->cred;
2315 	res->mask = cache->mask;
2316 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
2317 	err = 0;
2318 out:
2319 	spin_unlock(&inode->i_lock);
2320 	return err;
2321 out_zap:
2322 	spin_unlock(&inode->i_lock);
2323 	nfs_access_zap_cache(inode);
2324 	return -ENOENT;
2325 }
2326 
nfs_access_get_cached_rcu(struct inode * inode,struct rpc_cred * cred,struct nfs_access_entry * res)2327 static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2328 {
2329 	/* Only check the most recently returned cache entry,
2330 	 * but do it without locking.
2331 	 */
2332 	struct nfs_inode *nfsi = NFS_I(inode);
2333 	struct nfs_access_entry *cache;
2334 	int err = -ECHILD;
2335 	struct list_head *lh;
2336 
2337 	rcu_read_lock();
2338 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2339 		goto out;
2340 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2341 	cache = list_entry(lh, struct nfs_access_entry, lru);
2342 	if (lh == &nfsi->access_cache_entry_lru ||
2343 	    cred != cache->cred)
2344 		cache = NULL;
2345 	if (cache == NULL)
2346 		goto out;
2347 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2348 		goto out;
2349 	res->cred = cache->cred;
2350 	res->mask = cache->mask;
2351 	err = 0;
2352 out:
2353 	rcu_read_unlock();
2354 	return err;
2355 }
2356 
nfs_access_add_rbtree(struct inode * inode,struct nfs_access_entry * set)2357 static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2358 {
2359 	struct nfs_inode *nfsi = NFS_I(inode);
2360 	struct rb_root *root_node = &nfsi->access_cache;
2361 	struct rb_node **p = &root_node->rb_node;
2362 	struct rb_node *parent = NULL;
2363 	struct nfs_access_entry *entry;
2364 
2365 	spin_lock(&inode->i_lock);
2366 	while (*p != NULL) {
2367 		parent = *p;
2368 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2369 
2370 		if (set->cred < entry->cred)
2371 			p = &parent->rb_left;
2372 		else if (set->cred > entry->cred)
2373 			p = &parent->rb_right;
2374 		else
2375 			goto found;
2376 	}
2377 	rb_link_node(&set->rb_node, parent, p);
2378 	rb_insert_color(&set->rb_node, root_node);
2379 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2380 	spin_unlock(&inode->i_lock);
2381 	return;
2382 found:
2383 	rb_replace_node(parent, &set->rb_node, root_node);
2384 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2385 	list_del(&entry->lru);
2386 	spin_unlock(&inode->i_lock);
2387 	nfs_access_free_entry(entry);
2388 }
2389 
nfs_access_add_cache(struct inode * inode,struct nfs_access_entry * set)2390 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
2391 {
2392 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2393 	if (cache == NULL)
2394 		return;
2395 	RB_CLEAR_NODE(&cache->rb_node);
2396 	cache->cred = get_rpccred(set->cred);
2397 	cache->mask = set->mask;
2398 
2399 	/* The above field assignments must be visible
2400 	 * before this item appears on the lru.  We cannot easily
2401 	 * use rcu_assign_pointer, so just force the memory barrier.
2402 	 */
2403 	smp_wmb();
2404 	nfs_access_add_rbtree(inode, cache);
2405 
2406 	/* Update accounting */
2407 	smp_mb__before_atomic();
2408 	atomic_long_inc(&nfs_access_nr_entries);
2409 	smp_mb__after_atomic();
2410 
2411 	/* Add inode to global LRU list */
2412 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2413 		spin_lock(&nfs_access_lru_lock);
2414 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2415 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2416 					&nfs_access_lru_list);
2417 		spin_unlock(&nfs_access_lru_lock);
2418 	}
2419 	nfs_access_cache_enforce_limit();
2420 }
2421 EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2422 
2423 #define NFS_MAY_READ (NFS4_ACCESS_READ)
2424 #define NFS_MAY_WRITE (NFS4_ACCESS_MODIFY | \
2425 		NFS4_ACCESS_EXTEND | \
2426 		NFS4_ACCESS_DELETE)
2427 #define NFS_FILE_MAY_WRITE (NFS4_ACCESS_MODIFY | \
2428 		NFS4_ACCESS_EXTEND)
2429 #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
2430 #define NFS_MAY_LOOKUP (NFS4_ACCESS_LOOKUP)
2431 #define NFS_MAY_EXECUTE (NFS4_ACCESS_EXECUTE)
2432 static int
nfs_access_calc_mask(u32 access_result,umode_t umode)2433 nfs_access_calc_mask(u32 access_result, umode_t umode)
2434 {
2435 	int mask = 0;
2436 
2437 	if (access_result & NFS_MAY_READ)
2438 		mask |= MAY_READ;
2439 	if (S_ISDIR(umode)) {
2440 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
2441 			mask |= MAY_WRITE;
2442 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
2443 			mask |= MAY_EXEC;
2444 	} else if (S_ISREG(umode)) {
2445 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2446 			mask |= MAY_WRITE;
2447 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
2448 			mask |= MAY_EXEC;
2449 	} else if (access_result & NFS_MAY_WRITE)
2450 			mask |= MAY_WRITE;
2451 	return mask;
2452 }
2453 
nfs_access_set_mask(struct nfs_access_entry * entry,u32 access_result)2454 void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2455 {
2456 	entry->mask = access_result;
2457 }
2458 EXPORT_SYMBOL_GPL(nfs_access_set_mask);
2459 
nfs_do_access(struct inode * inode,struct rpc_cred * cred,int mask)2460 static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2461 {
2462 	struct nfs_access_entry cache;
2463 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2464 	int cache_mask;
2465 	int status;
2466 
2467 	trace_nfs_access_enter(inode);
2468 
2469 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2470 	if (status != 0)
2471 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
2472 	if (status == 0)
2473 		goto out_cached;
2474 
2475 	status = -ECHILD;
2476 	if (!may_block)
2477 		goto out;
2478 
2479 	/* Be clever: ask server to check for all possible rights */
2480 	cache.mask = NFS_MAY_LOOKUP | NFS_MAY_EXECUTE
2481 		     | NFS_MAY_WRITE | NFS_MAY_READ;
2482 	cache.cred = cred;
2483 	status = NFS_PROTO(inode)->access(inode, &cache);
2484 	if (status != 0) {
2485 		if (status == -ESTALE) {
2486 			nfs_zap_caches(inode);
2487 			if (!S_ISDIR(inode->i_mode))
2488 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2489 		}
2490 		goto out;
2491 	}
2492 	nfs_access_add_cache(inode, &cache);
2493 out_cached:
2494 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2495 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2496 		status = -EACCES;
2497 out:
2498 	trace_nfs_access_exit(inode, status);
2499 	return status;
2500 }
2501 
nfs_open_permission_mask(int openflags)2502 static int nfs_open_permission_mask(int openflags)
2503 {
2504 	int mask = 0;
2505 
2506 	if (openflags & __FMODE_EXEC) {
2507 		/* ONLY check exec rights */
2508 		mask = MAY_EXEC;
2509 	} else {
2510 		if ((openflags & O_ACCMODE) != O_WRONLY)
2511 			mask |= MAY_READ;
2512 		if ((openflags & O_ACCMODE) != O_RDONLY)
2513 			mask |= MAY_WRITE;
2514 	}
2515 
2516 	return mask;
2517 }
2518 
nfs_may_open(struct inode * inode,struct rpc_cred * cred,int openflags)2519 int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2520 {
2521 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2522 }
2523 EXPORT_SYMBOL_GPL(nfs_may_open);
2524 
nfs_execute_ok(struct inode * inode,int mask)2525 static int nfs_execute_ok(struct inode *inode, int mask)
2526 {
2527 	struct nfs_server *server = NFS_SERVER(inode);
2528 	int ret = 0;
2529 
2530 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) {
2531 		if (mask & MAY_NOT_BLOCK)
2532 			return -ECHILD;
2533 		ret = __nfs_revalidate_inode(server, inode);
2534 	}
2535 	if (ret == 0 && !execute_ok(inode))
2536 		ret = -EACCES;
2537 	return ret;
2538 }
2539 
nfs_permission(struct inode * inode,int mask)2540 int nfs_permission(struct inode *inode, int mask)
2541 {
2542 	struct rpc_cred *cred;
2543 	int res = 0;
2544 
2545 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2546 
2547 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2548 		goto out;
2549 	/* Is this sys_access() ? */
2550 	if (mask & (MAY_ACCESS | MAY_CHDIR))
2551 		goto force_lookup;
2552 
2553 	switch (inode->i_mode & S_IFMT) {
2554 		case S_IFLNK:
2555 			goto out;
2556 		case S_IFREG:
2557 			if ((mask & MAY_OPEN) &&
2558 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2559 				return 0;
2560 			break;
2561 		case S_IFDIR:
2562 			/*
2563 			 * Optimize away all write operations, since the server
2564 			 * will check permissions when we perform the op.
2565 			 */
2566 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2567 				goto out;
2568 	}
2569 
2570 force_lookup:
2571 	if (!NFS_PROTO(inode)->access)
2572 		goto out_notsup;
2573 
2574 	/* Always try fast lookups first */
2575 	rcu_read_lock();
2576 	cred = rpc_lookup_cred_nonblock();
2577 	if (!IS_ERR(cred))
2578 		res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2579 	else
2580 		res = PTR_ERR(cred);
2581 	rcu_read_unlock();
2582 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2583 		/* Fast lookup failed, try the slow way */
2584 		cred = rpc_lookup_cred();
2585 		if (!IS_ERR(cred)) {
2586 			res = nfs_do_access(inode, cred, mask);
2587 			put_rpccred(cred);
2588 		} else
2589 			res = PTR_ERR(cred);
2590 	}
2591 out:
2592 	if (!res && (mask & MAY_EXEC))
2593 		res = nfs_execute_ok(inode, mask);
2594 
2595 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
2596 		inode->i_sb->s_id, inode->i_ino, mask, res);
2597 	return res;
2598 out_notsup:
2599 	if (mask & MAY_NOT_BLOCK)
2600 		return -ECHILD;
2601 
2602 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2603 	if (res == 0)
2604 		res = generic_permission(inode, mask);
2605 	goto out;
2606 }
2607 EXPORT_SYMBOL_GPL(nfs_permission);
2608 
2609 /*
2610  * Local variables:
2611  *  version-control: t
2612  *  kept-new-versions: 5
2613  * End:
2614  */
2615