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