xref: /openbmc/linux/fs/afs/dir.c (revision f3ddee8d)
1 /* dir.c: AFS filesystem directory handling
2  *
3  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/fs.h>
14 #include <linux/namei.h>
15 #include <linux/pagemap.h>
16 #include <linux/swap.h>
17 #include <linux/ctype.h>
18 #include <linux/sched.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include "internal.h"
21 
22 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
23 				 unsigned int flags);
24 static int afs_dir_open(struct inode *inode, struct file *file);
25 static int afs_readdir(struct file *file, struct dir_context *ctx);
26 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
27 static int afs_d_delete(const struct dentry *dentry);
28 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
29 				  loff_t fpos, u64 ino, unsigned dtype);
30 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
31 			      loff_t fpos, u64 ino, unsigned dtype);
32 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
33 		      bool excl);
34 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
35 static int afs_rmdir(struct inode *dir, struct dentry *dentry);
36 static int afs_unlink(struct inode *dir, struct dentry *dentry);
37 static int afs_link(struct dentry *from, struct inode *dir,
38 		    struct dentry *dentry);
39 static int afs_symlink(struct inode *dir, struct dentry *dentry,
40 		       const char *content);
41 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
42 		      struct inode *new_dir, struct dentry *new_dentry,
43 		      unsigned int flags);
44 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
45 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
46 				   unsigned int length);
47 
48 static int afs_dir_set_page_dirty(struct page *page)
49 {
50 	BUG(); /* This should never happen. */
51 }
52 
53 const struct file_operations afs_dir_file_operations = {
54 	.open		= afs_dir_open,
55 	.release	= afs_release,
56 	.iterate_shared	= afs_readdir,
57 	.lock		= afs_lock,
58 	.llseek		= generic_file_llseek,
59 };
60 
61 const struct inode_operations afs_dir_inode_operations = {
62 	.create		= afs_create,
63 	.lookup		= afs_lookup,
64 	.link		= afs_link,
65 	.unlink		= afs_unlink,
66 	.symlink	= afs_symlink,
67 	.mkdir		= afs_mkdir,
68 	.rmdir		= afs_rmdir,
69 	.rename		= afs_rename,
70 	.permission	= afs_permission,
71 	.getattr	= afs_getattr,
72 	.setattr	= afs_setattr,
73 	.listxattr	= afs_listxattr,
74 };
75 
76 const struct address_space_operations afs_dir_aops = {
77 	.set_page_dirty	= afs_dir_set_page_dirty,
78 	.releasepage	= afs_dir_releasepage,
79 	.invalidatepage	= afs_dir_invalidatepage,
80 };
81 
82 const struct dentry_operations afs_fs_dentry_operations = {
83 	.d_revalidate	= afs_d_revalidate,
84 	.d_delete	= afs_d_delete,
85 	.d_release	= afs_d_release,
86 	.d_automount	= afs_d_automount,
87 };
88 
89 #define AFS_DIR_HASHTBL_SIZE	128
90 #define AFS_DIR_DIRENT_SIZE	32
91 #define AFS_DIRENT_PER_BLOCK	64
92 
93 union afs_dirent {
94 	struct {
95 		uint8_t		valid;
96 		uint8_t		unused[1];
97 		__be16		hash_next;
98 		__be32		vnode;
99 		__be32		unique;
100 		uint8_t		name[16];
101 		uint8_t		overflow[4];	/* if any char of the name (inc
102 						 * NUL) reaches here, consume
103 						 * the next dirent too */
104 	} u;
105 	uint8_t	extended_name[32];
106 };
107 
108 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
109 struct afs_dir_pagehdr {
110 	__be16		npages;
111 	__be16		magic;
112 #define AFS_DIR_MAGIC htons(1234)
113 	uint8_t		nentries;
114 	uint8_t		bitmap[8];
115 	uint8_t		pad[19];
116 };
117 
118 /* directory block layout */
119 union afs_dir_block {
120 
121 	struct afs_dir_pagehdr pagehdr;
122 
123 	struct {
124 		struct afs_dir_pagehdr	pagehdr;
125 		uint8_t			alloc_ctrs[128];
126 		/* dir hash table */
127 		uint16_t		hashtable[AFS_DIR_HASHTBL_SIZE];
128 	} hdr;
129 
130 	union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
131 };
132 
133 /* layout on a linux VM page */
134 struct afs_dir_page {
135 	union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
136 };
137 
138 struct afs_lookup_one_cookie {
139 	struct dir_context	ctx;
140 	struct qstr		name;
141 	bool			found;
142 	struct afs_fid		fid;
143 };
144 
145 struct afs_lookup_cookie {
146 	struct dir_context	ctx;
147 	struct qstr		name;
148 	bool			found;
149 	bool			one_only;
150 	unsigned short		nr_fids;
151 	struct afs_file_status	*statuses;
152 	struct afs_callback	*callbacks;
153 	struct afs_fid		fids[50];
154 };
155 
156 /*
157  * check that a directory page is valid
158  */
159 static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
160 			       loff_t i_size)
161 {
162 	struct afs_dir_page *dbuf;
163 	loff_t latter, off;
164 	int tmp, qty;
165 
166 	/* Determine how many magic numbers there should be in this page, but
167 	 * we must take care because the directory may change size under us.
168 	 */
169 	off = page_offset(page);
170 	if (i_size <= off)
171 		goto checked;
172 
173 	latter = i_size - off;
174 	if (latter >= PAGE_SIZE)
175 		qty = PAGE_SIZE;
176 	else
177 		qty = latter;
178 	qty /= sizeof(union afs_dir_block);
179 
180 	/* check them */
181 	dbuf = page_address(page);
182 	for (tmp = 0; tmp < qty; tmp++) {
183 		if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
184 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
185 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
186 			       ntohs(dbuf->blocks[tmp].pagehdr.magic));
187 			trace_afs_dir_check_failed(dvnode, off, i_size);
188 			goto error;
189 		}
190 	}
191 
192 checked:
193 	afs_stat_v(dvnode, n_read_dir);
194 	return true;
195 
196 error:
197 	return false;
198 }
199 
200 /*
201  * open an AFS directory file
202  */
203 static int afs_dir_open(struct inode *inode, struct file *file)
204 {
205 	_enter("{%lu}", inode->i_ino);
206 
207 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
208 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
209 
210 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
211 		return -ENOENT;
212 
213 	return afs_open(inode, file);
214 }
215 
216 /*
217  * Read the directory into the pagecache in one go, scrubbing the previous
218  * contents.  The list of pages is returned, pinning them so that they don't
219  * get reclaimed during the iteration.
220  */
221 static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
222 {
223 	struct afs_read *req;
224 	loff_t i_size;
225 	int nr_pages, nr_inline, i, n;
226 	int ret = -ENOMEM;
227 
228 retry:
229 	i_size = i_size_read(&dvnode->vfs_inode);
230 	if (i_size < 2048)
231 		return ERR_PTR(-EIO);
232 	if (i_size > 2048 * 1024)
233 		return ERR_PTR(-EFBIG);
234 
235 	_enter("%llu", i_size);
236 
237 	/* Get a request record to hold the page list.  We want to hold it
238 	 * inline if we can, but we don't want to make an order 1 allocation.
239 	 */
240 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
241 	nr_inline = nr_pages;
242 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
243 		nr_inline = 0;
244 
245 	req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
246 		      GFP_KERNEL);
247 	if (!req)
248 		return ERR_PTR(-ENOMEM);
249 
250 	refcount_set(&req->usage, 1);
251 	req->nr_pages = nr_pages;
252 	req->actual_len = i_size; /* May change */
253 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
254 	req->data_version = dvnode->status.data_version; /* May change */
255 	if (nr_inline > 0) {
256 		req->pages = req->array;
257 	} else {
258 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
259 				     GFP_KERNEL);
260 		if (!req->pages)
261 			goto error;
262 	}
263 
264 	/* Get a list of all the pages that hold or will hold the directory
265 	 * content.  We need to fill in any gaps that we might find where the
266 	 * memory reclaimer has been at work.  If there are any gaps, we will
267 	 * need to reread the entire directory contents.
268 	 */
269 	i = 0;
270 	do {
271 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
272 					  req->nr_pages - i,
273 					  req->pages + i);
274 		_debug("find %u at %u/%u", n, i, req->nr_pages);
275 		if (n == 0) {
276 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
277 
278 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
279 				afs_stat_v(dvnode, n_inval);
280 
281 			ret = -ENOMEM;
282 			req->pages[i] = __page_cache_alloc(gfp);
283 			if (!req->pages[i])
284 				goto error;
285 			ret = add_to_page_cache_lru(req->pages[i],
286 						    dvnode->vfs_inode.i_mapping,
287 						    i, gfp);
288 			if (ret < 0)
289 				goto error;
290 
291 			set_page_private(req->pages[i], 1);
292 			SetPagePrivate(req->pages[i]);
293 			unlock_page(req->pages[i]);
294 			i++;
295 		} else {
296 			i += n;
297 		}
298 	} while (i < req->nr_pages);
299 
300 	/* If we're going to reload, we need to lock all the pages to prevent
301 	 * races.
302 	 */
303 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
304 		ret = -ERESTARTSYS;
305 		for (i = 0; i < req->nr_pages; i++)
306 			if (lock_page_killable(req->pages[i]) < 0)
307 				goto error_unlock;
308 
309 		if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
310 			goto success;
311 
312 		ret = afs_fetch_data(dvnode, key, req);
313 		if (ret < 0)
314 			goto error_unlock_all;
315 
316 		task_io_account_read(PAGE_SIZE * req->nr_pages);
317 
318 		if (req->len < req->file_size)
319 			goto content_has_grown;
320 
321 		/* Validate the data we just read. */
322 		ret = -EIO;
323 		for (i = 0; i < req->nr_pages; i++)
324 			if (!afs_dir_check_page(dvnode, req->pages[i],
325 						req->actual_len))
326 				goto error_unlock_all;
327 
328 		// TODO: Trim excess pages
329 
330 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
331 	}
332 
333 success:
334 	i = req->nr_pages;
335 	while (i > 0)
336 		unlock_page(req->pages[--i]);
337 	return req;
338 
339 error_unlock_all:
340 	i = req->nr_pages;
341 error_unlock:
342 	while (i > 0)
343 		unlock_page(req->pages[--i]);
344 error:
345 	afs_put_read(req);
346 	_leave(" = %d", ret);
347 	return ERR_PTR(ret);
348 
349 content_has_grown:
350 	i = req->nr_pages;
351 	while (i > 0)
352 		unlock_page(req->pages[--i]);
353 	afs_put_read(req);
354 	goto retry;
355 }
356 
357 /*
358  * deal with one block in an AFS directory
359  */
360 static int afs_dir_iterate_block(struct dir_context *ctx,
361 				 union afs_dir_block *block,
362 				 unsigned blkoff)
363 {
364 	union afs_dirent *dire;
365 	unsigned offset, next, curr;
366 	size_t nlen;
367 	int tmp;
368 
369 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
370 
371 	curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
372 
373 	/* walk through the block, an entry at a time */
374 	for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
375 	     offset < AFS_DIRENT_PER_BLOCK;
376 	     offset = next
377 	     ) {
378 		next = offset + 1;
379 
380 		/* skip entries marked unused in the bitmap */
381 		if (!(block->pagehdr.bitmap[offset / 8] &
382 		      (1 << (offset % 8)))) {
383 			_debug("ENT[%zu.%u]: unused",
384 			       blkoff / sizeof(union afs_dir_block), offset);
385 			if (offset >= curr)
386 				ctx->pos = blkoff +
387 					next * sizeof(union afs_dirent);
388 			continue;
389 		}
390 
391 		/* got a valid entry */
392 		dire = &block->dirents[offset];
393 		nlen = strnlen(dire->u.name,
394 			       sizeof(*block) -
395 			       offset * sizeof(union afs_dirent));
396 
397 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
398 		       blkoff / sizeof(union afs_dir_block), offset,
399 		       (offset < curr ? "skip" : "fill"),
400 		       nlen, dire->u.name);
401 
402 		/* work out where the next possible entry is */
403 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
404 			if (next >= AFS_DIRENT_PER_BLOCK) {
405 				_debug("ENT[%zu.%u]:"
406 				       " %u travelled beyond end dir block"
407 				       " (len %u/%zu)",
408 				       blkoff / sizeof(union afs_dir_block),
409 				       offset, next, tmp, nlen);
410 				return -EIO;
411 			}
412 			if (!(block->pagehdr.bitmap[next / 8] &
413 			      (1 << (next % 8)))) {
414 				_debug("ENT[%zu.%u]:"
415 				       " %u unmarked extension (len %u/%zu)",
416 				       blkoff / sizeof(union afs_dir_block),
417 				       offset, next, tmp, nlen);
418 				return -EIO;
419 			}
420 
421 			_debug("ENT[%zu.%u]: ext %u/%zu",
422 			       blkoff / sizeof(union afs_dir_block),
423 			       next, tmp, nlen);
424 			next++;
425 		}
426 
427 		/* skip if starts before the current position */
428 		if (offset < curr)
429 			continue;
430 
431 		/* found the next entry */
432 		if (!dir_emit(ctx, dire->u.name, nlen,
433 			      ntohl(dire->u.vnode),
434 			      (ctx->actor == afs_lookup_filldir ||
435 			       ctx->actor == afs_lookup_one_filldir)?
436 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
437 			_leave(" = 0 [full]");
438 			return 0;
439 		}
440 
441 		ctx->pos = blkoff + next * sizeof(union afs_dirent);
442 	}
443 
444 	_leave(" = 1 [more]");
445 	return 1;
446 }
447 
448 /*
449  * iterate through the data blob that lists the contents of an AFS directory
450  */
451 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
452 			   struct key *key)
453 {
454 	struct afs_vnode *dvnode = AFS_FS_I(dir);
455 	union afs_dir_block *dblock;
456 	struct afs_dir_page *dbuf;
457 	struct afs_read *req;
458 	struct page *page;
459 	unsigned blkoff, limit;
460 	int ret;
461 
462 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
463 
464 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
465 		_leave(" = -ESTALE");
466 		return -ESTALE;
467 	}
468 
469 	req = afs_read_dir(dvnode, key);
470 	if (IS_ERR(req))
471 		return PTR_ERR(req);
472 
473 	/* round the file position up to the next entry boundary */
474 	ctx->pos += sizeof(union afs_dirent) - 1;
475 	ctx->pos &= ~(sizeof(union afs_dirent) - 1);
476 
477 	/* walk through the blocks in sequence */
478 	ret = 0;
479 	while (ctx->pos < req->actual_len) {
480 		blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
481 
482 		/* Fetch the appropriate page from the directory and re-add it
483 		 * to the LRU.
484 		 */
485 		page = req->pages[blkoff / PAGE_SIZE];
486 		if (!page) {
487 			ret = -EIO;
488 			break;
489 		}
490 		mark_page_accessed(page);
491 
492 		limit = blkoff & ~(PAGE_SIZE - 1);
493 
494 		dbuf = kmap(page);
495 
496 		/* deal with the individual blocks stashed on this page */
497 		do {
498 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
499 					       sizeof(union afs_dir_block)];
500 			ret = afs_dir_iterate_block(ctx, dblock, blkoff);
501 			if (ret != 1) {
502 				kunmap(page);
503 				goto out;
504 			}
505 
506 			blkoff += sizeof(union afs_dir_block);
507 
508 		} while (ctx->pos < dir->i_size && blkoff < limit);
509 
510 		kunmap(page);
511 		ret = 0;
512 	}
513 
514 out:
515 	afs_put_read(req);
516 	_leave(" = %d", ret);
517 	return ret;
518 }
519 
520 /*
521  * read an AFS directory
522  */
523 static int afs_readdir(struct file *file, struct dir_context *ctx)
524 {
525 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
526 }
527 
528 /*
529  * Search the directory for a single name
530  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
531  *   uniquifier through dtype
532  */
533 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
534 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
535 {
536 	struct afs_lookup_one_cookie *cookie =
537 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
538 
539 	_enter("{%s,%u},%s,%u,,%llu,%u",
540 	       cookie->name.name, cookie->name.len, name, nlen,
541 	       (unsigned long long) ino, dtype);
542 
543 	/* insanity checks first */
544 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
545 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
546 
547 	if (cookie->name.len != nlen ||
548 	    memcmp(cookie->name.name, name, nlen) != 0) {
549 		_leave(" = 0 [no]");
550 		return 0;
551 	}
552 
553 	cookie->fid.vnode = ino;
554 	cookie->fid.unique = dtype;
555 	cookie->found = 1;
556 
557 	_leave(" = -1 [found]");
558 	return -1;
559 }
560 
561 /*
562  * Do a lookup of a single name in a directory
563  * - just returns the FID the dentry name maps to if found
564  */
565 static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
566 			     struct afs_fid *fid, struct key *key)
567 {
568 	struct afs_super_info *as = dir->i_sb->s_fs_info;
569 	struct afs_lookup_one_cookie cookie = {
570 		.ctx.actor = afs_lookup_one_filldir,
571 		.name = dentry->d_name,
572 		.fid.vid = as->volume->vid
573 	};
574 	int ret;
575 
576 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
577 
578 	/* search the directory */
579 	ret = afs_dir_iterate(dir, &cookie.ctx, key);
580 	if (ret < 0) {
581 		_leave(" = %d [iter]", ret);
582 		return ret;
583 	}
584 
585 	ret = -ENOENT;
586 	if (!cookie.found) {
587 		_leave(" = -ENOENT [not found]");
588 		return -ENOENT;
589 	}
590 
591 	*fid = cookie.fid;
592 	_leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
593 	return 0;
594 }
595 
596 /*
597  * search the directory for a name
598  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
599  *   uniquifier through dtype
600  */
601 static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
602 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
603 {
604 	struct afs_lookup_cookie *cookie =
605 		container_of(ctx, struct afs_lookup_cookie, ctx);
606 	int ret;
607 
608 	_enter("{%s,%u},%s,%u,,%llu,%u",
609 	       cookie->name.name, cookie->name.len, name, nlen,
610 	       (unsigned long long) ino, dtype);
611 
612 	/* insanity checks first */
613 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
614 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
615 
616 	if (cookie->found) {
617 		if (cookie->nr_fids < 50) {
618 			cookie->fids[cookie->nr_fids].vnode	= ino;
619 			cookie->fids[cookie->nr_fids].unique	= dtype;
620 			cookie->nr_fids++;
621 		}
622 	} else if (cookie->name.len == nlen &&
623 		   memcmp(cookie->name.name, name, nlen) == 0) {
624 		cookie->fids[0].vnode	= ino;
625 		cookie->fids[0].unique	= dtype;
626 		cookie->found = 1;
627 		if (cookie->one_only)
628 			return -1;
629 	}
630 
631 	ret = cookie->nr_fids >= 50 ? -1 : 0;
632 	_leave(" = %d", ret);
633 	return ret;
634 }
635 
636 /*
637  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
638  * files in one go and create inodes for them.  The inode of the file we were
639  * asked for is returned.
640  */
641 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
642 				   struct key *key)
643 {
644 	struct afs_lookup_cookie *cookie;
645 	struct afs_cb_interest *cbi = NULL;
646 	struct afs_super_info *as = dir->i_sb->s_fs_info;
647 	struct afs_iget_data data;
648 	struct afs_fs_cursor fc;
649 	struct afs_vnode *dvnode = AFS_FS_I(dir);
650 	struct inode *inode = NULL;
651 	int ret, i;
652 
653 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
654 
655 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
656 	if (!cookie)
657 		return ERR_PTR(-ENOMEM);
658 
659 	cookie->ctx.actor = afs_lookup_filldir;
660 	cookie->name = dentry->d_name;
661 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
662 
663 	read_seqlock_excl(&dvnode->cb_lock);
664 	if (dvnode->cb_interest &&
665 	    dvnode->cb_interest->server &&
666 	    test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
667 		cookie->one_only = true;
668 	read_sequnlock_excl(&dvnode->cb_lock);
669 
670 	for (i = 0; i < 50; i++)
671 		cookie->fids[i].vid = as->volume->vid;
672 
673 	/* search the directory */
674 	ret = afs_dir_iterate(dir, &cookie->ctx, key);
675 	if (ret < 0) {
676 		inode = ERR_PTR(ret);
677 		goto out;
678 	}
679 
680 	inode = ERR_PTR(-ENOENT);
681 	if (!cookie->found)
682 		goto out;
683 
684 	/* Check to see if we already have an inode for the primary fid. */
685 	data.volume = dvnode->volume;
686 	data.fid = cookie->fids[0];
687 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
688 	if (inode)
689 		goto out;
690 
691 	/* Need space for examining all the selected files */
692 	inode = ERR_PTR(-ENOMEM);
693 	cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
694 				   GFP_KERNEL);
695 	if (!cookie->statuses)
696 		goto out;
697 
698 	cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
699 				    GFP_KERNEL);
700 	if (!cookie->callbacks)
701 		goto out_s;
702 
703 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
704 	 * lookups contained therein are stored in the reply without aborting
705 	 * the whole operation.
706 	 */
707 	if (cookie->one_only)
708 		goto no_inline_bulk_status;
709 
710 	inode = ERR_PTR(-ERESTARTSYS);
711 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
712 		while (afs_select_fileserver(&fc)) {
713 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
714 				      &fc.cbi->server->flags)) {
715 				fc.ac.abort_code = RX_INVALID_OPERATION;
716 				fc.ac.error = -ECONNABORTED;
717 				break;
718 			}
719 			afs_fs_inline_bulk_status(&fc,
720 						  afs_v2net(dvnode),
721 						  cookie->fids,
722 						  cookie->statuses,
723 						  cookie->callbacks,
724 						  cookie->nr_fids, NULL);
725 		}
726 
727 		if (fc.ac.error == 0)
728 			cbi = afs_get_cb_interest(fc.cbi);
729 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
730 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
731 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
732 	}
733 
734 	if (!IS_ERR(inode))
735 		goto success;
736 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
737 		goto out_c;
738 
739 no_inline_bulk_status:
740 	/* We could try FS.BulkStatus next, but this aborts the entire op if
741 	 * any of the lookups fails - so, for the moment, revert to
742 	 * FS.FetchStatus for just the primary fid.
743 	 */
744 	cookie->nr_fids = 1;
745 	inode = ERR_PTR(-ERESTARTSYS);
746 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
747 		while (afs_select_fileserver(&fc)) {
748 			afs_fs_fetch_status(&fc,
749 					    afs_v2net(dvnode),
750 					    cookie->fids,
751 					    cookie->statuses,
752 					    cookie->callbacks,
753 					    NULL);
754 		}
755 
756 		if (fc.ac.error == 0)
757 			cbi = afs_get_cb_interest(fc.cbi);
758 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
759 	}
760 
761 	if (IS_ERR(inode))
762 		goto out_c;
763 
764 	for (i = 0; i < cookie->nr_fids; i++)
765 		cookie->statuses[i].abort_code = 0;
766 
767 success:
768 	/* Turn all the files into inodes and save the first one - which is the
769 	 * one we actually want.
770 	 */
771 	if (cookie->statuses[0].abort_code != 0)
772 		inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
773 
774 	for (i = 0; i < cookie->nr_fids; i++) {
775 		struct inode *ti;
776 
777 		if (cookie->statuses[i].abort_code != 0)
778 			continue;
779 
780 		ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
781 			      &cookie->statuses[i],
782 			      &cookie->callbacks[i],
783 			      cbi);
784 		if (i == 0) {
785 			inode = ti;
786 		} else {
787 			if (!IS_ERR(ti))
788 				iput(ti);
789 		}
790 	}
791 
792 out_c:
793 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
794 	kfree(cookie->callbacks);
795 out_s:
796 	kfree(cookie->statuses);
797 out:
798 	kfree(cookie);
799 	return inode;
800 }
801 
802 /*
803  * Look up an entry in a directory with @sys substitution.
804  */
805 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
806 				       struct key *key)
807 {
808 	struct afs_sysnames *subs;
809 	struct afs_net *net = afs_i2net(dir);
810 	struct dentry *ret;
811 	char *buf, *p, *name;
812 	int len, i;
813 
814 	_enter("");
815 
816 	ret = ERR_PTR(-ENOMEM);
817 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
818 	if (!buf)
819 		goto out_p;
820 	if (dentry->d_name.len > 4) {
821 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
822 		p += dentry->d_name.len - 4;
823 	}
824 
825 	/* There is an ordered list of substitutes that we have to try. */
826 	read_lock(&net->sysnames_lock);
827 	subs = net->sysnames;
828 	refcount_inc(&subs->usage);
829 	read_unlock(&net->sysnames_lock);
830 
831 	for (i = 0; i < subs->nr; i++) {
832 		name = subs->subs[i];
833 		len = dentry->d_name.len - 4 + strlen(name);
834 		if (len >= AFSNAMEMAX) {
835 			ret = ERR_PTR(-ENAMETOOLONG);
836 			goto out_s;
837 		}
838 
839 		strcpy(p, name);
840 		ret = lookup_one_len(buf, dentry->d_parent, len);
841 		if (IS_ERR(ret) || d_is_positive(ret))
842 			goto out_s;
843 		dput(ret);
844 	}
845 
846 	/* We don't want to d_add() the @sys dentry here as we don't want to
847 	 * the cached dentry to hide changes to the sysnames list.
848 	 */
849 	ret = NULL;
850 out_s:
851 	afs_put_sysnames(subs);
852 	kfree(buf);
853 out_p:
854 	key_put(key);
855 	return ret;
856 }
857 
858 /*
859  * look up an entry in a directory
860  */
861 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
862 				 unsigned int flags)
863 {
864 	struct afs_vnode *dvnode = AFS_FS_I(dir);
865 	struct inode *inode;
866 	struct key *key;
867 	int ret;
868 
869 	_enter("{%x:%u},%p{%pd},",
870 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
871 
872 	ASSERTCMP(d_inode(dentry), ==, NULL);
873 
874 	if (dentry->d_name.len >= AFSNAMEMAX) {
875 		_leave(" = -ENAMETOOLONG");
876 		return ERR_PTR(-ENAMETOOLONG);
877 	}
878 
879 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
880 		_leave(" = -ESTALE");
881 		return ERR_PTR(-ESTALE);
882 	}
883 
884 	key = afs_request_key(dvnode->volume->cell);
885 	if (IS_ERR(key)) {
886 		_leave(" = %ld [key]", PTR_ERR(key));
887 		return ERR_CAST(key);
888 	}
889 
890 	ret = afs_validate(dvnode, key);
891 	if (ret < 0) {
892 		key_put(key);
893 		_leave(" = %d [val]", ret);
894 		return ERR_PTR(ret);
895 	}
896 
897 	if (dentry->d_name.len >= 4 &&
898 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
899 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
900 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
901 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
902 		return afs_lookup_atsys(dir, dentry, key);
903 
904 	afs_stat_v(dvnode, n_lookup);
905 	inode = afs_do_lookup(dir, dentry, key);
906 	if (IS_ERR(inode)) {
907 		ret = PTR_ERR(inode);
908 		if (ret == -ENOENT) {
909 			inode = afs_try_auto_mntpt(dentry, dir);
910 			if (!IS_ERR(inode)) {
911 				key_put(key);
912 				goto success;
913 			}
914 
915 			ret = PTR_ERR(inode);
916 		}
917 
918 		key_put(key);
919 		if (ret == -ENOENT) {
920 			d_add(dentry, NULL);
921 			_leave(" = NULL [negative]");
922 			return NULL;
923 		}
924 		_leave(" = %d [do]", ret);
925 		return ERR_PTR(ret);
926 	}
927 	dentry->d_fsdata = (void *)(unsigned long)dvnode->status.data_version;
928 
929 	/* instantiate the dentry */
930 	key_put(key);
931 	if (IS_ERR(inode)) {
932 		_leave(" = %ld", PTR_ERR(inode));
933 		return ERR_CAST(inode);
934 	}
935 
936 success:
937 	d_add(dentry, inode);
938 	_leave(" = 0 { ino=%lu v=%u }",
939 	       d_inode(dentry)->i_ino,
940 	       d_inode(dentry)->i_generation);
941 
942 	return NULL;
943 }
944 
945 /*
946  * check that a dentry lookup hit has found a valid entry
947  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
948  *   inode
949  */
950 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
951 {
952 	struct afs_vnode *vnode, *dir;
953 	struct afs_fid uninitialized_var(fid);
954 	struct dentry *parent;
955 	struct inode *inode;
956 	struct key *key;
957 	long dir_version, de_version;
958 	int ret;
959 
960 	if (flags & LOOKUP_RCU)
961 		return -ECHILD;
962 
963 	if (d_really_is_positive(dentry)) {
964 		vnode = AFS_FS_I(d_inode(dentry));
965 		_enter("{v={%x:%u} n=%pd fl=%lx},",
966 		       vnode->fid.vid, vnode->fid.vnode, dentry,
967 		       vnode->flags);
968 	} else {
969 		_enter("{neg n=%pd}", dentry);
970 	}
971 
972 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
973 	if (IS_ERR(key))
974 		key = NULL;
975 
976 	if (d_really_is_positive(dentry)) {
977 		inode = d_inode(dentry);
978 		if (inode) {
979 			vnode = AFS_FS_I(inode);
980 			afs_validate(vnode, key);
981 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
982 				goto out_bad;
983 		}
984 	}
985 
986 	/* lock down the parent dentry so we can peer at it */
987 	parent = dget_parent(dentry);
988 	dir = AFS_FS_I(d_inode(parent));
989 
990 	/* validate the parent directory */
991 	afs_validate(dir, key);
992 
993 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
994 		_debug("%pd: parent dir deleted", dentry);
995 		goto out_bad_parent;
996 	}
997 
998 	/* We only need to invalidate a dentry if the server's copy changed
999 	 * behind our back.  If we made the change, it's no problem.  Note that
1000 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1001 	 * version.
1002 	 */
1003 	dir_version = (long)dir->status.data_version;
1004 	de_version = (long)dentry->d_fsdata;
1005 	if (de_version == dir_version)
1006 		goto out_valid;
1007 
1008 	dir_version = (long)dir->invalid_before;
1009 	if (de_version - dir_version >= 0)
1010 		goto out_valid;
1011 
1012 	_debug("dir modified");
1013 	afs_stat_v(dir, n_reval);
1014 
1015 	/* search the directory for this vnode */
1016 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
1017 	switch (ret) {
1018 	case 0:
1019 		/* the filename maps to something */
1020 		if (d_really_is_negative(dentry))
1021 			goto out_bad_parent;
1022 		inode = d_inode(dentry);
1023 		if (is_bad_inode(inode)) {
1024 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1025 			       dentry);
1026 			goto out_bad_parent;
1027 		}
1028 
1029 		vnode = AFS_FS_I(inode);
1030 
1031 		/* if the vnode ID has changed, then the dirent points to a
1032 		 * different file */
1033 		if (fid.vnode != vnode->fid.vnode) {
1034 			_debug("%pd: dirent changed [%u != %u]",
1035 			       dentry, fid.vnode,
1036 			       vnode->fid.vnode);
1037 			goto not_found;
1038 		}
1039 
1040 		/* if the vnode ID uniqifier has changed, then the file has
1041 		 * been deleted and replaced, and the original vnode ID has
1042 		 * been reused */
1043 		if (fid.unique != vnode->fid.unique) {
1044 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1045 			       dentry, fid.unique,
1046 			       vnode->fid.unique,
1047 			       vnode->vfs_inode.i_generation);
1048 			write_seqlock(&vnode->cb_lock);
1049 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1050 			write_sequnlock(&vnode->cb_lock);
1051 			goto not_found;
1052 		}
1053 		goto out_valid;
1054 
1055 	case -ENOENT:
1056 		/* the filename is unknown */
1057 		_debug("%pd: dirent not found", dentry);
1058 		if (d_really_is_positive(dentry))
1059 			goto not_found;
1060 		goto out_valid;
1061 
1062 	default:
1063 		_debug("failed to iterate dir %pd: %d",
1064 		       parent, ret);
1065 		goto out_bad_parent;
1066 	}
1067 
1068 out_valid:
1069 	dentry->d_fsdata = (void *)dir_version;
1070 	dput(parent);
1071 	key_put(key);
1072 	_leave(" = 1 [valid]");
1073 	return 1;
1074 
1075 	/* the dirent, if it exists, now points to a different vnode */
1076 not_found:
1077 	spin_lock(&dentry->d_lock);
1078 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1079 	spin_unlock(&dentry->d_lock);
1080 
1081 out_bad_parent:
1082 	_debug("dropping dentry %pd2", dentry);
1083 	dput(parent);
1084 out_bad:
1085 	key_put(key);
1086 
1087 	_leave(" = 0 [bad]");
1088 	return 0;
1089 }
1090 
1091 /*
1092  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1093  * sleep)
1094  * - called from dput() when d_count is going to 0.
1095  * - return 1 to request dentry be unhashed, 0 otherwise
1096  */
1097 static int afs_d_delete(const struct dentry *dentry)
1098 {
1099 	_enter("%pd", dentry);
1100 
1101 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1102 		goto zap;
1103 
1104 	if (d_really_is_positive(dentry) &&
1105 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
1106 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1107 		goto zap;
1108 
1109 	_leave(" = 0 [keep]");
1110 	return 0;
1111 
1112 zap:
1113 	_leave(" = 1 [zap]");
1114 	return 1;
1115 }
1116 
1117 /*
1118  * handle dentry release
1119  */
1120 void afs_d_release(struct dentry *dentry)
1121 {
1122 	_enter("%pd", dentry);
1123 }
1124 
1125 /*
1126  * Create a new inode for create/mkdir/symlink
1127  */
1128 static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1129 				struct dentry *new_dentry,
1130 				struct afs_fid *newfid,
1131 				struct afs_file_status *newstatus,
1132 				struct afs_callback *newcb)
1133 {
1134 	struct inode *inode;
1135 
1136 	if (fc->ac.error < 0)
1137 		return;
1138 
1139 	d_drop(new_dentry);
1140 
1141 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1142 			 newfid, newstatus, newcb, fc->cbi);
1143 	if (IS_ERR(inode)) {
1144 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1145 		 * the new directory on the server.
1146 		 */
1147 		fc->ac.error = PTR_ERR(inode);
1148 		return;
1149 	}
1150 
1151 	d_add(new_dentry, inode);
1152 }
1153 
1154 /*
1155  * create a directory on an AFS filesystem
1156  */
1157 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1158 {
1159 	struct afs_file_status newstatus;
1160 	struct afs_fs_cursor fc;
1161 	struct afs_callback newcb;
1162 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1163 	struct afs_fid newfid;
1164 	struct key *key;
1165 	int ret;
1166 
1167 	mode |= S_IFDIR;
1168 
1169 	_enter("{%x:%u},{%pd},%ho",
1170 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1171 
1172 	key = afs_request_key(dvnode->volume->cell);
1173 	if (IS_ERR(key)) {
1174 		ret = PTR_ERR(key);
1175 		goto error;
1176 	}
1177 
1178 	ret = -ERESTARTSYS;
1179 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1180 		while (afs_select_fileserver(&fc)) {
1181 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1182 			afs_fs_create(&fc, dentry->d_name.name, mode,
1183 				      &newfid, &newstatus, &newcb);
1184 		}
1185 
1186 		afs_check_for_remote_deletion(&fc, fc.vnode);
1187 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1188 		afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1189 		ret = afs_end_vnode_operation(&fc);
1190 		if (ret < 0)
1191 			goto error_key;
1192 	} else {
1193 		goto error_key;
1194 	}
1195 
1196 	key_put(key);
1197 	_leave(" = 0");
1198 	return 0;
1199 
1200 error_key:
1201 	key_put(key);
1202 error:
1203 	d_drop(dentry);
1204 	_leave(" = %d", ret);
1205 	return ret;
1206 }
1207 
1208 /*
1209  * Remove a subdir from a directory.
1210  */
1211 static void afs_dir_remove_subdir(struct dentry *dentry)
1212 {
1213 	if (d_really_is_positive(dentry)) {
1214 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1215 
1216 		clear_nlink(&vnode->vfs_inode);
1217 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1218 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1219 	}
1220 }
1221 
1222 /*
1223  * remove a directory from an AFS filesystem
1224  */
1225 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1226 {
1227 	struct afs_fs_cursor fc;
1228 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1229 	struct key *key;
1230 	int ret;
1231 
1232 	_enter("{%x:%u},{%pd}",
1233 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1234 
1235 	key = afs_request_key(dvnode->volume->cell);
1236 	if (IS_ERR(key)) {
1237 		ret = PTR_ERR(key);
1238 		goto error;
1239 	}
1240 
1241 	ret = -ERESTARTSYS;
1242 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1243 		while (afs_select_fileserver(&fc)) {
1244 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1245 			afs_fs_remove(&fc, dentry->d_name.name, true);
1246 		}
1247 
1248 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1249 		ret = afs_end_vnode_operation(&fc);
1250 		if (ret == 0)
1251 			afs_dir_remove_subdir(dentry);
1252 	}
1253 
1254 	key_put(key);
1255 error:
1256 	return ret;
1257 }
1258 
1259 /*
1260  * Remove a link to a file or symlink from a directory.
1261  *
1262  * If the file was not deleted due to excess hard links, the fileserver will
1263  * break the callback promise on the file - if it had one - before it returns
1264  * to us, and if it was deleted, it won't
1265  *
1266  * However, if we didn't have a callback promise outstanding, or it was
1267  * outstanding on a different server, then it won't break it either...
1268  */
1269 static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1270 			       unsigned long d_version_before,
1271 			       unsigned long d_version_after)
1272 {
1273 	bool dir_valid;
1274 	int ret = 0;
1275 
1276 	/* There were no intervening changes on the server if the version
1277 	 * number we got back was incremented by exactly 1.
1278 	 */
1279 	dir_valid = (d_version_after == d_version_before + 1);
1280 
1281 	if (d_really_is_positive(dentry)) {
1282 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1283 
1284 		if (dir_valid) {
1285 			drop_nlink(&vnode->vfs_inode);
1286 			if (vnode->vfs_inode.i_nlink == 0) {
1287 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1288 				clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1289 			}
1290 			ret = 0;
1291 		} else {
1292 			clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1293 
1294 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1295 				kdebug("AFS_VNODE_DELETED");
1296 
1297 			ret = afs_validate(vnode, key);
1298 			if (ret == -ESTALE)
1299 				ret = 0;
1300 		}
1301 		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1302 	}
1303 
1304 	return ret;
1305 }
1306 
1307 /*
1308  * Remove a file or symlink from an AFS filesystem.
1309  */
1310 static int afs_unlink(struct inode *dir, struct dentry *dentry)
1311 {
1312 	struct afs_fs_cursor fc;
1313 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
1314 	struct key *key;
1315 	unsigned long d_version = (unsigned long)dentry->d_fsdata;
1316 	int ret;
1317 
1318 	_enter("{%x:%u},{%pd}",
1319 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1320 
1321 	if (dentry->d_name.len >= AFSNAMEMAX)
1322 		return -ENAMETOOLONG;
1323 
1324 	key = afs_request_key(dvnode->volume->cell);
1325 	if (IS_ERR(key)) {
1326 		ret = PTR_ERR(key);
1327 		goto error;
1328 	}
1329 
1330 	/* Try to make sure we have a callback promise on the victim. */
1331 	if (d_really_is_positive(dentry)) {
1332 		vnode = AFS_FS_I(d_inode(dentry));
1333 		ret = afs_validate(vnode, key);
1334 		if (ret < 0)
1335 			goto error_key;
1336 	}
1337 
1338 	ret = -ERESTARTSYS;
1339 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1340 		while (afs_select_fileserver(&fc)) {
1341 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1342 			afs_fs_remove(&fc, dentry->d_name.name, false);
1343 		}
1344 
1345 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1346 		ret = afs_end_vnode_operation(&fc);
1347 		if (ret == 0)
1348 			ret = afs_dir_remove_link(
1349 				dentry, key, d_version,
1350 				(unsigned long)dvnode->status.data_version);
1351 	}
1352 
1353 error_key:
1354 	key_put(key);
1355 error:
1356 	_leave(" = %d", ret);
1357 	return ret;
1358 }
1359 
1360 /*
1361  * create a regular file on an AFS filesystem
1362  */
1363 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1364 		      bool excl)
1365 {
1366 	struct afs_fs_cursor fc;
1367 	struct afs_file_status newstatus;
1368 	struct afs_callback newcb;
1369 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1370 	struct afs_fid newfid;
1371 	struct key *key;
1372 	int ret;
1373 
1374 	mode |= S_IFREG;
1375 
1376 	_enter("{%x:%u},{%pd},%ho,",
1377 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1378 
1379 	ret = -ENAMETOOLONG;
1380 	if (dentry->d_name.len >= AFSNAMEMAX)
1381 		goto error;
1382 
1383 	key = afs_request_key(dvnode->volume->cell);
1384 	if (IS_ERR(key)) {
1385 		ret = PTR_ERR(key);
1386 		goto error;
1387 	}
1388 
1389 	ret = -ERESTARTSYS;
1390 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1391 		while (afs_select_fileserver(&fc)) {
1392 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1393 			afs_fs_create(&fc, dentry->d_name.name, mode,
1394 				      &newfid, &newstatus, &newcb);
1395 		}
1396 
1397 		afs_check_for_remote_deletion(&fc, fc.vnode);
1398 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1399 		afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1400 		ret = afs_end_vnode_operation(&fc);
1401 		if (ret < 0)
1402 			goto error_key;
1403 	} else {
1404 		goto error_key;
1405 	}
1406 
1407 	key_put(key);
1408 	_leave(" = 0");
1409 	return 0;
1410 
1411 error_key:
1412 	key_put(key);
1413 error:
1414 	d_drop(dentry);
1415 	_leave(" = %d", ret);
1416 	return ret;
1417 }
1418 
1419 /*
1420  * create a hard link between files in an AFS filesystem
1421  */
1422 static int afs_link(struct dentry *from, struct inode *dir,
1423 		    struct dentry *dentry)
1424 {
1425 	struct afs_fs_cursor fc;
1426 	struct afs_vnode *dvnode, *vnode;
1427 	struct key *key;
1428 	int ret;
1429 
1430 	vnode = AFS_FS_I(d_inode(from));
1431 	dvnode = AFS_FS_I(dir);
1432 
1433 	_enter("{%x:%u},{%x:%u},{%pd}",
1434 	       vnode->fid.vid, vnode->fid.vnode,
1435 	       dvnode->fid.vid, dvnode->fid.vnode,
1436 	       dentry);
1437 
1438 	ret = -ENAMETOOLONG;
1439 	if (dentry->d_name.len >= AFSNAMEMAX)
1440 		goto error;
1441 
1442 	key = afs_request_key(dvnode->volume->cell);
1443 	if (IS_ERR(key)) {
1444 		ret = PTR_ERR(key);
1445 		goto error;
1446 	}
1447 
1448 	ret = -ERESTARTSYS;
1449 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1450 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1451 			afs_end_vnode_operation(&fc);
1452 			goto error_key;
1453 		}
1454 
1455 		while (afs_select_fileserver(&fc)) {
1456 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1457 			fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break;
1458 			afs_fs_link(&fc, vnode, dentry->d_name.name);
1459 		}
1460 
1461 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1462 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1463 		ihold(&vnode->vfs_inode);
1464 		d_instantiate(dentry, &vnode->vfs_inode);
1465 
1466 		mutex_unlock(&vnode->io_lock);
1467 		ret = afs_end_vnode_operation(&fc);
1468 		if (ret < 0)
1469 			goto error_key;
1470 	} else {
1471 		goto error_key;
1472 	}
1473 
1474 	key_put(key);
1475 	_leave(" = 0");
1476 	return 0;
1477 
1478 error_key:
1479 	key_put(key);
1480 error:
1481 	d_drop(dentry);
1482 	_leave(" = %d", ret);
1483 	return ret;
1484 }
1485 
1486 /*
1487  * create a symlink in an AFS filesystem
1488  */
1489 static int afs_symlink(struct inode *dir, struct dentry *dentry,
1490 		       const char *content)
1491 {
1492 	struct afs_fs_cursor fc;
1493 	struct afs_file_status newstatus;
1494 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1495 	struct afs_fid newfid;
1496 	struct key *key;
1497 	int ret;
1498 
1499 	_enter("{%x:%u},{%pd},%s",
1500 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1501 	       content);
1502 
1503 	ret = -ENAMETOOLONG;
1504 	if (dentry->d_name.len >= AFSNAMEMAX)
1505 		goto error;
1506 
1507 	ret = -EINVAL;
1508 	if (strlen(content) >= AFSPATHMAX)
1509 		goto error;
1510 
1511 	key = afs_request_key(dvnode->volume->cell);
1512 	if (IS_ERR(key)) {
1513 		ret = PTR_ERR(key);
1514 		goto error;
1515 	}
1516 
1517 	ret = -ERESTARTSYS;
1518 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1519 		while (afs_select_fileserver(&fc)) {
1520 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1521 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1522 				       &newfid, &newstatus);
1523 		}
1524 
1525 		afs_check_for_remote_deletion(&fc, fc.vnode);
1526 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1527 		afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1528 		ret = afs_end_vnode_operation(&fc);
1529 		if (ret < 0)
1530 			goto error_key;
1531 	} else {
1532 		goto error_key;
1533 	}
1534 
1535 	key_put(key);
1536 	_leave(" = 0");
1537 	return 0;
1538 
1539 error_key:
1540 	key_put(key);
1541 error:
1542 	d_drop(dentry);
1543 	_leave(" = %d", ret);
1544 	return ret;
1545 }
1546 
1547 /*
1548  * rename a file in an AFS filesystem and/or move it between directories
1549  */
1550 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1551 		      struct inode *new_dir, struct dentry *new_dentry,
1552 		      unsigned int flags)
1553 {
1554 	struct afs_fs_cursor fc;
1555 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1556 	struct key *key;
1557 	int ret;
1558 
1559 	if (flags)
1560 		return -EINVAL;
1561 
1562 	vnode = AFS_FS_I(d_inode(old_dentry));
1563 	orig_dvnode = AFS_FS_I(old_dir);
1564 	new_dvnode = AFS_FS_I(new_dir);
1565 
1566 	_enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
1567 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1568 	       vnode->fid.vid, vnode->fid.vnode,
1569 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1570 	       new_dentry);
1571 
1572 	key = afs_request_key(orig_dvnode->volume->cell);
1573 	if (IS_ERR(key)) {
1574 		ret = PTR_ERR(key);
1575 		goto error;
1576 	}
1577 
1578 	ret = -ERESTARTSYS;
1579 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1580 		if (orig_dvnode != new_dvnode) {
1581 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1582 				afs_end_vnode_operation(&fc);
1583 				goto error_key;
1584 			}
1585 		}
1586 		while (afs_select_fileserver(&fc)) {
1587 			fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break;
1588 			fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break;
1589 			afs_fs_rename(&fc, old_dentry->d_name.name,
1590 				      new_dvnode, new_dentry->d_name.name);
1591 		}
1592 
1593 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1594 		afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1595 		if (orig_dvnode != new_dvnode)
1596 			mutex_unlock(&new_dvnode->io_lock);
1597 		ret = afs_end_vnode_operation(&fc);
1598 		if (ret < 0)
1599 			goto error_key;
1600 	}
1601 
1602 error_key:
1603 	key_put(key);
1604 error:
1605 	_leave(" = %d", ret);
1606 	return ret;
1607 }
1608 
1609 /*
1610  * Release a directory page and clean up its private state if it's not busy
1611  * - return true if the page can now be released, false if not
1612  */
1613 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1614 {
1615 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1616 
1617 	_enter("{{%x:%u}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1618 
1619 	set_page_private(page, 0);
1620 	ClearPagePrivate(page);
1621 
1622 	/* The directory will need reloading. */
1623 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1624 		afs_stat_v(dvnode, n_relpg);
1625 	return 1;
1626 }
1627 
1628 /*
1629  * invalidate part or all of a page
1630  * - release a page and clean up its private data if offset is 0 (indicating
1631  *   the entire page)
1632  */
1633 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1634 				   unsigned int length)
1635 {
1636 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1637 
1638 	_enter("{%lu},%u,%u", page->index, offset, length);
1639 
1640 	BUG_ON(!PageLocked(page));
1641 
1642 	/* The directory will need reloading. */
1643 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1644 		afs_stat_v(dvnode, n_inval);
1645 
1646 	/* we clean up only if the entire page is being invalidated */
1647 	if (offset == 0 && length == PAGE_SIZE) {
1648 		set_page_private(page, 0);
1649 		ClearPagePrivate(page);
1650 	}
1651 }
1652