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