xref: /openbmc/linux/fs/afs/dir.c (revision 1d56a969)
1 /* dir.c: AFS filesystem directory handling
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/smp_lock.h>
20 #include "vnode.h"
21 #include "volume.h"
22 #include <rxrpc/call.h>
23 #include "super.h"
24 #include "internal.h"
25 
26 static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
27 				     struct nameidata *nd);
28 static int afs_dir_open(struct inode *inode, struct file *file);
29 static int afs_dir_readdir(struct file *file, void *dirent, filldir_t filldir);
30 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
31 static int afs_d_delete(struct dentry *dentry);
32 static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
33 				  loff_t fpos, u64 ino, unsigned dtype);
34 
35 const struct file_operations afs_dir_file_operations = {
36 	.open		= afs_dir_open,
37 	.readdir	= afs_dir_readdir,
38 };
39 
40 struct inode_operations afs_dir_inode_operations = {
41 	.lookup		= afs_dir_lookup,
42 	.getattr	= afs_inode_getattr,
43 #if 0 /* TODO */
44 	.create		= afs_dir_create,
45 	.link		= afs_dir_link,
46 	.unlink		= afs_dir_unlink,
47 	.symlink	= afs_dir_symlink,
48 	.mkdir		= afs_dir_mkdir,
49 	.rmdir		= afs_dir_rmdir,
50 	.mknod		= afs_dir_mknod,
51 	.rename		= afs_dir_rename,
52 #endif
53 };
54 
55 static struct dentry_operations afs_fs_dentry_operations = {
56 	.d_revalidate	= afs_d_revalidate,
57 	.d_delete	= afs_d_delete,
58 };
59 
60 #define AFS_DIR_HASHTBL_SIZE	128
61 #define AFS_DIR_DIRENT_SIZE	32
62 #define AFS_DIRENT_PER_BLOCK	64
63 
64 union afs_dirent {
65 	struct {
66 		uint8_t		valid;
67 		uint8_t		unused[1];
68 		__be16		hash_next;
69 		__be32		vnode;
70 		__be32		unique;
71 		uint8_t		name[16];
72 		uint8_t		overflow[4];	/* if any char of the name (inc
73 						 * NUL) reaches here, consume
74 						 * the next dirent too */
75 	} u;
76 	uint8_t	extended_name[32];
77 };
78 
79 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
80 struct afs_dir_pagehdr {
81 	__be16		npages;
82 	__be16		magic;
83 #define AFS_DIR_MAGIC htons(1234)
84 	uint8_t		nentries;
85 	uint8_t		bitmap[8];
86 	uint8_t		pad[19];
87 };
88 
89 /* directory block layout */
90 union afs_dir_block {
91 
92 	struct afs_dir_pagehdr pagehdr;
93 
94 	struct {
95 		struct afs_dir_pagehdr	pagehdr;
96 		uint8_t			alloc_ctrs[128];
97 		/* dir hash table */
98 		uint16_t		hashtable[AFS_DIR_HASHTBL_SIZE];
99 	} hdr;
100 
101 	union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
102 };
103 
104 /* layout on a linux VM page */
105 struct afs_dir_page {
106 	union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
107 };
108 
109 struct afs_dir_lookup_cookie {
110 	struct afs_fid	fid;
111 	const char	*name;
112 	size_t		nlen;
113 	int		found;
114 };
115 
116 /*****************************************************************************/
117 /*
118  * check that a directory page is valid
119  */
120 static inline void afs_dir_check_page(struct inode *dir, struct page *page)
121 {
122 	struct afs_dir_page *dbuf;
123 	loff_t latter;
124 	int tmp, qty;
125 
126 #if 0
127 	/* check the page count */
128 	qty = desc.size / sizeof(dbuf->blocks[0]);
129 	if (qty == 0)
130 		goto error;
131 
132 	if (page->index==0 && qty!=ntohs(dbuf->blocks[0].pagehdr.npages)) {
133 		printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
134 		       __FUNCTION__,dir->i_ino,qty,ntohs(dbuf->blocks[0].pagehdr.npages));
135 		goto error;
136 	}
137 #endif
138 
139 	/* determine how many magic numbers there should be in this page */
140 	latter = dir->i_size - page_offset(page);
141 	if (latter >= PAGE_SIZE)
142 		qty = PAGE_SIZE;
143 	else
144 		qty = latter;
145 	qty /= sizeof(union afs_dir_block);
146 
147 	/* check them */
148 	dbuf = page_address(page);
149 	for (tmp = 0; tmp < qty; tmp++) {
150 		if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
151 			printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
152 			       __FUNCTION__, dir->i_ino, tmp, qty,
153 			       ntohs(dbuf->blocks[tmp].pagehdr.magic));
154 			goto error;
155 		}
156 	}
157 
158 	SetPageChecked(page);
159 	return;
160 
161  error:
162 	SetPageChecked(page);
163 	SetPageError(page);
164 
165 } /* end afs_dir_check_page() */
166 
167 /*****************************************************************************/
168 /*
169  * discard a page cached in the pagecache
170  */
171 static inline void afs_dir_put_page(struct page *page)
172 {
173 	kunmap(page);
174 	page_cache_release(page);
175 
176 } /* end afs_dir_put_page() */
177 
178 /*****************************************************************************/
179 /*
180  * get a page into the pagecache
181  */
182 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index)
183 {
184 	struct page *page;
185 
186 	_enter("{%lu},%lu", dir->i_ino, index);
187 
188 	page = read_mapping_page(dir->i_mapping, index, NULL);
189 	if (!IS_ERR(page)) {
190 		wait_on_page_locked(page);
191 		kmap(page);
192 		if (!PageUptodate(page))
193 			goto fail;
194 		if (!PageChecked(page))
195 			afs_dir_check_page(dir, page);
196 		if (PageError(page))
197 			goto fail;
198 	}
199 	return page;
200 
201  fail:
202 	afs_dir_put_page(page);
203 	return ERR_PTR(-EIO);
204 } /* end afs_dir_get_page() */
205 
206 /*****************************************************************************/
207 /*
208  * open an AFS directory file
209  */
210 static int afs_dir_open(struct inode *inode, struct file *file)
211 {
212 	_enter("{%lu}", inode->i_ino);
213 
214 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
215 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
216 
217 	if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED)
218 		return -ENOENT;
219 
220 	_leave(" = 0");
221 	return 0;
222 
223 } /* end afs_dir_open() */
224 
225 /*****************************************************************************/
226 /*
227  * deal with one block in an AFS directory
228  */
229 static int afs_dir_iterate_block(unsigned *fpos,
230 				 union afs_dir_block *block,
231 				 unsigned blkoff,
232 				 void *cookie,
233 				 filldir_t filldir)
234 {
235 	union afs_dirent *dire;
236 	unsigned offset, next, curr;
237 	size_t nlen;
238 	int tmp, ret;
239 
240 	_enter("%u,%x,%p,,",*fpos,blkoff,block);
241 
242 	curr = (*fpos - blkoff) / sizeof(union afs_dirent);
243 
244 	/* walk through the block, an entry at a time */
245 	for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
246 	     offset < AFS_DIRENT_PER_BLOCK;
247 	     offset = next
248 	     ) {
249 		next = offset + 1;
250 
251 		/* skip entries marked unused in the bitmap */
252 		if (!(block->pagehdr.bitmap[offset / 8] &
253 		      (1 << (offset % 8)))) {
254 			_debug("ENT[%Zu.%u]: unused\n",
255 			       blkoff / sizeof(union afs_dir_block), offset);
256 			if (offset >= curr)
257 				*fpos = blkoff +
258 					next * sizeof(union afs_dirent);
259 			continue;
260 		}
261 
262 		/* got a valid entry */
263 		dire = &block->dirents[offset];
264 		nlen = strnlen(dire->u.name,
265 			       sizeof(*block) -
266 			       offset * sizeof(union afs_dirent));
267 
268 		_debug("ENT[%Zu.%u]: %s %Zu \"%s\"\n",
269 		       blkoff / sizeof(union afs_dir_block), offset,
270 		       (offset < curr ? "skip" : "fill"),
271 		       nlen, dire->u.name);
272 
273 		/* work out where the next possible entry is */
274 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
275 			if (next >= AFS_DIRENT_PER_BLOCK) {
276 				_debug("ENT[%Zu.%u]:"
277 				       " %u travelled beyond end dir block"
278 				       " (len %u/%Zu)\n",
279 				       blkoff / sizeof(union afs_dir_block),
280 				       offset, next, tmp, nlen);
281 				return -EIO;
282 			}
283 			if (!(block->pagehdr.bitmap[next / 8] &
284 			      (1 << (next % 8)))) {
285 				_debug("ENT[%Zu.%u]:"
286 				       " %u unmarked extension (len %u/%Zu)\n",
287 				       blkoff / sizeof(union afs_dir_block),
288 				       offset, next, tmp, nlen);
289 				return -EIO;
290 			}
291 
292 			_debug("ENT[%Zu.%u]: ext %u/%Zu\n",
293 			       blkoff / sizeof(union afs_dir_block),
294 			       next, tmp, nlen);
295 			next++;
296 		}
297 
298 		/* skip if starts before the current position */
299 		if (offset < curr)
300 			continue;
301 
302 		/* found the next entry */
303 		ret = filldir(cookie,
304 			      dire->u.name,
305 			      nlen,
306 			      blkoff + offset * sizeof(union afs_dirent),
307 			      ntohl(dire->u.vnode),
308 			      filldir == afs_dir_lookup_filldir ?
309 			      ntohl(dire->u.unique) : DT_UNKNOWN);
310 		if (ret < 0) {
311 			_leave(" = 0 [full]");
312 			return 0;
313 		}
314 
315 		*fpos = blkoff + next * sizeof(union afs_dirent);
316 	}
317 
318 	_leave(" = 1 [more]");
319 	return 1;
320 } /* end afs_dir_iterate_block() */
321 
322 /*****************************************************************************/
323 /*
324  * read an AFS directory
325  */
326 static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
327 			   filldir_t filldir)
328 {
329 	union afs_dir_block	*dblock;
330 	struct afs_dir_page *dbuf;
331 	struct page *page;
332 	unsigned blkoff, limit;
333 	int ret;
334 
335 	_enter("{%lu},%u,,", dir->i_ino, *fpos);
336 
337 	if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
338 		_leave(" = -ESTALE");
339 		return -ESTALE;
340 	}
341 
342 	/* round the file position up to the next entry boundary */
343 	*fpos += sizeof(union afs_dirent) - 1;
344 	*fpos &= ~(sizeof(union afs_dirent) - 1);
345 
346 	/* walk through the blocks in sequence */
347 	ret = 0;
348 	while (*fpos < dir->i_size) {
349 		blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
350 
351 		/* fetch the appropriate page from the directory */
352 		page = afs_dir_get_page(dir, blkoff / PAGE_SIZE);
353 		if (IS_ERR(page)) {
354 			ret = PTR_ERR(page);
355 			break;
356 		}
357 
358 		limit = blkoff & ~(PAGE_SIZE - 1);
359 
360 		dbuf = page_address(page);
361 
362 		/* deal with the individual blocks stashed on this page */
363 		do {
364 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
365 					       sizeof(union afs_dir_block)];
366 			ret = afs_dir_iterate_block(fpos, dblock, blkoff,
367 						    cookie, filldir);
368 			if (ret != 1) {
369 				afs_dir_put_page(page);
370 				goto out;
371 			}
372 
373 			blkoff += sizeof(union afs_dir_block);
374 
375 		} while (*fpos < dir->i_size && blkoff < limit);
376 
377 		afs_dir_put_page(page);
378 		ret = 0;
379 	}
380 
381  out:
382 	_leave(" = %d", ret);
383 	return ret;
384 } /* end afs_dir_iterate() */
385 
386 /*****************************************************************************/
387 /*
388  * read an AFS directory
389  */
390 static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
391 {
392 	unsigned fpos;
393 	int ret;
394 
395 	_enter("{%Ld,{%lu}}", file->f_pos, file->f_path.dentry->d_inode->i_ino);
396 
397 	fpos = file->f_pos;
398 	ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos, cookie, filldir);
399 	file->f_pos = fpos;
400 
401 	_leave(" = %d", ret);
402 	return ret;
403 } /* end afs_dir_readdir() */
404 
405 /*****************************************************************************/
406 /*
407  * search the directory for a name
408  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
409  *   uniquifier through dtype
410  */
411 static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
412 				  loff_t fpos, u64 ino, unsigned dtype)
413 {
414 	struct afs_dir_lookup_cookie *cookie = _cookie;
415 
416 	_enter("{%s,%Zu},%s,%u,,%lu,%u",
417 	       cookie->name, cookie->nlen, name, nlen, ino, dtype);
418 
419 	if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
420 		_leave(" = 0 [no]");
421 		return 0;
422 	}
423 
424 	cookie->fid.vnode = ino;
425 	cookie->fid.unique = dtype;
426 	cookie->found = 1;
427 
428 	_leave(" = -1 [found]");
429 	return -1;
430 } /* end afs_dir_lookup_filldir() */
431 
432 /*****************************************************************************/
433 /*
434  * look up an entry in a directory
435  */
436 static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
437 				     struct nameidata *nd)
438 {
439 	struct afs_dir_lookup_cookie cookie;
440 	struct afs_super_info *as;
441 	struct afs_vnode *vnode;
442 	struct inode *inode;
443 	unsigned fpos;
444 	int ret;
445 
446 	_enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
447 
448 	/* insanity checks first */
449 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
450 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
451 
452 	if (dentry->d_name.len > 255) {
453 		_leave(" = -ENAMETOOLONG");
454 		return ERR_PTR(-ENAMETOOLONG);
455 	}
456 
457 	vnode = AFS_FS_I(dir);
458 	if (vnode->flags & AFS_VNODE_DELETED) {
459 		_leave(" = -ESTALE");
460 		return ERR_PTR(-ESTALE);
461 	}
462 
463 	as = dir->i_sb->s_fs_info;
464 
465 	/* search the directory */
466 	cookie.name	= dentry->d_name.name;
467 	cookie.nlen	= dentry->d_name.len;
468 	cookie.fid.vid	= as->volume->vid;
469 	cookie.found	= 0;
470 
471 	fpos = 0;
472 	ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
473 	if (ret < 0) {
474 		_leave(" = %d", ret);
475 		return ERR_PTR(ret);
476 	}
477 
478 	ret = -ENOENT;
479 	if (!cookie.found) {
480 		_leave(" = %d", ret);
481 		return ERR_PTR(ret);
482 	}
483 
484 	/* instantiate the dentry */
485 	ret = afs_iget(dir->i_sb, &cookie.fid, &inode);
486 	if (ret < 0) {
487 		_leave(" = %d", ret);
488 		return ERR_PTR(ret);
489 	}
490 
491 	dentry->d_op = &afs_fs_dentry_operations;
492 	dentry->d_fsdata = (void *) (unsigned long) vnode->status.version;
493 
494 	d_add(dentry, inode);
495 	_leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
496 	       cookie.fid.vnode,
497 	       cookie.fid.unique,
498 	       dentry->d_inode->i_ino,
499 	       dentry->d_inode->i_version);
500 
501 	return NULL;
502 } /* end afs_dir_lookup() */
503 
504 /*****************************************************************************/
505 /*
506  * check that a dentry lookup hit has found a valid entry
507  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
508  *   inode
509  * (derived from nfs_lookup_revalidate)
510  */
511 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
512 {
513 	struct afs_dir_lookup_cookie cookie;
514 	struct dentry *parent;
515 	struct inode *inode, *dir;
516 	unsigned fpos;
517 	int ret;
518 
519 	_enter("{sb=%p n=%s},", dentry->d_sb, dentry->d_name.name);
520 
521 	/* lock down the parent dentry so we can peer at it */
522 	parent = dget_parent(dentry->d_parent);
523 
524 	dir = parent->d_inode;
525 	inode = dentry->d_inode;
526 
527 	/* handle a negative dentry */
528 	if (!inode)
529 		goto out_bad;
530 
531 	/* handle a bad inode */
532 	if (is_bad_inode(inode)) {
533 		printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
534 		       dentry->d_parent->d_name.name, dentry->d_name.name);
535 		goto out_bad;
536 	}
537 
538 	/* force a full look up if the parent directory changed since last the
539 	 * server was consulted
540 	 * - otherwise this inode must still exist, even if the inode details
541 	 *   themselves have changed
542 	 */
543 	if (AFS_FS_I(dir)->flags & AFS_VNODE_CHANGED)
544 		afs_vnode_fetch_status(AFS_FS_I(dir));
545 
546 	if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
547 		_debug("%s: parent dir deleted", dentry->d_name.name);
548 		goto out_bad;
549 	}
550 
551 	if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
552 		_debug("%s: file already deleted", dentry->d_name.name);
553 		goto out_bad;
554 	}
555 
556 	if ((unsigned long) dentry->d_fsdata !=
557 	    (unsigned long) AFS_FS_I(dir)->status.version) {
558 		_debug("%s: parent changed %lu -> %u",
559 		       dentry->d_name.name,
560 		       (unsigned long) dentry->d_fsdata,
561 		       (unsigned) AFS_FS_I(dir)->status.version);
562 
563 		/* search the directory for this vnode */
564 		cookie.name	= dentry->d_name.name;
565 		cookie.nlen	= dentry->d_name.len;
566 		cookie.fid.vid	= AFS_FS_I(inode)->volume->vid;
567 		cookie.found	= 0;
568 
569 		fpos = 0;
570 		ret = afs_dir_iterate(dir, &fpos, &cookie,
571 				      afs_dir_lookup_filldir);
572 		if (ret < 0) {
573 			_debug("failed to iterate dir %s: %d",
574 			       parent->d_name.name, ret);
575 			goto out_bad;
576 		}
577 
578 		if (!cookie.found) {
579 			_debug("%s: dirent not found", dentry->d_name.name);
580 			goto not_found;
581 		}
582 
583 		/* if the vnode ID has changed, then the dirent points to a
584 		 * different file */
585 		if (cookie.fid.vnode != AFS_FS_I(inode)->fid.vnode) {
586 			_debug("%s: dirent changed", dentry->d_name.name);
587 			goto not_found;
588 		}
589 
590 		/* if the vnode ID uniqifier has changed, then the file has
591 		 * been deleted */
592 		if (cookie.fid.unique != AFS_FS_I(inode)->fid.unique) {
593 			_debug("%s: file deleted (uq %u -> %u I:%lu)",
594 			       dentry->d_name.name,
595 			       cookie.fid.unique,
596 			       AFS_FS_I(inode)->fid.unique,
597 			       inode->i_version);
598 			spin_lock(&AFS_FS_I(inode)->lock);
599 			AFS_FS_I(inode)->flags |= AFS_VNODE_DELETED;
600 			spin_unlock(&AFS_FS_I(inode)->lock);
601 			invalidate_remote_inode(inode);
602 			goto out_bad;
603 		}
604 
605 		dentry->d_fsdata =
606 			(void *) (unsigned long) AFS_FS_I(dir)->status.version;
607 	}
608 
609  out_valid:
610 	dput(parent);
611 	_leave(" = 1 [valid]");
612 	return 1;
613 
614 	/* the dirent, if it exists, now points to a different vnode */
615  not_found:
616 	spin_lock(&dentry->d_lock);
617 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
618 	spin_unlock(&dentry->d_lock);
619 
620  out_bad:
621 	if (inode) {
622 		/* don't unhash if we have submounts */
623 		if (have_submounts(dentry))
624 			goto out_valid;
625 	}
626 
627 	shrink_dcache_parent(dentry);
628 
629 	_debug("dropping dentry %s/%s",
630 	       dentry->d_parent->d_name.name, dentry->d_name.name);
631 	d_drop(dentry);
632 
633 	dput(parent);
634 
635 	_leave(" = 0 [bad]");
636 	return 0;
637 } /* end afs_d_revalidate() */
638 
639 /*****************************************************************************/
640 /*
641  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
642  * sleep)
643  * - called from dput() when d_count is going to 0.
644  * - return 1 to request dentry be unhashed, 0 otherwise
645  */
646 static int afs_d_delete(struct dentry *dentry)
647 {
648 	_enter("%s", dentry->d_name.name);
649 
650 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
651 		goto zap;
652 
653 	if (dentry->d_inode) {
654 		if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
655 			goto zap;
656 	}
657 
658 	_leave(" = 0 [keep]");
659 	return 0;
660 
661  zap:
662 	_leave(" = 1 [zap]");
663 	return 1;
664 } /* end afs_d_delete() */
665