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