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