xref: /openbmc/linux/fs/affs/dir.c (revision 31b90347)
1 /*
2  *  linux/fs/affs/dir.c
3  *
4  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
5  *
6  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
7  *
8  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
9  *
10  *  (C) 1991  Linus Torvalds - minix filesystem
11  *
12  *  affs directory handling functions
13  *
14  */
15 
16 #include "affs.h"
17 
18 static int affs_readdir(struct file *, struct dir_context *);
19 
20 const struct file_operations affs_dir_operations = {
21 	.read		= generic_read_dir,
22 	.llseek		= generic_file_llseek,
23 	.iterate	= affs_readdir,
24 	.fsync		= affs_file_fsync,
25 };
26 
27 /*
28  * directories can handle most operations...
29  */
30 const struct inode_operations affs_dir_inode_operations = {
31 	.create		= affs_create,
32 	.lookup		= affs_lookup,
33 	.link		= affs_link,
34 	.unlink		= affs_unlink,
35 	.symlink	= affs_symlink,
36 	.mkdir		= affs_mkdir,
37 	.rmdir		= affs_rmdir,
38 	.rename		= affs_rename,
39 	.setattr	= affs_notify_change,
40 };
41 
42 static int
43 affs_readdir(struct file *file, struct dir_context *ctx)
44 {
45 	struct inode		*inode = file_inode(file);
46 	struct super_block	*sb = inode->i_sb;
47 	struct buffer_head	*dir_bh = NULL;
48 	struct buffer_head	*fh_bh = NULL;
49 	unsigned char		*name;
50 	int			 namelen;
51 	u32			 i;
52 	int			 hash_pos;
53 	int			 chain_pos;
54 	u32			 ino;
55 
56 	pr_debug("AFFS: readdir(ino=%lu,f_pos=%lx)\n",inode->i_ino,(unsigned long)ctx->pos);
57 
58 	if (ctx->pos < 2) {
59 		file->private_data = (void *)0;
60 		if (!dir_emit_dots(file, ctx))
61 			return 0;
62 	}
63 
64 	affs_lock_dir(inode);
65 	chain_pos = (ctx->pos - 2) & 0xffff;
66 	hash_pos  = (ctx->pos - 2) >> 16;
67 	if (chain_pos == 0xffff) {
68 		affs_warning(sb, "readdir", "More than 65535 entries in chain");
69 		chain_pos = 0;
70 		hash_pos++;
71 		ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
72 	}
73 	dir_bh = affs_bread(sb, inode->i_ino);
74 	if (!dir_bh)
75 		goto readdir_out;
76 
77 	/* If the directory hasn't changed since the last call to readdir(),
78 	 * we can jump directly to where we left off.
79 	 */
80 	ino = (u32)(long)file->private_data;
81 	if (ino && file->f_version == inode->i_version) {
82 		pr_debug("AFFS: readdir() left off=%d\n", ino);
83 		goto inside;
84 	}
85 
86 	ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
87 	for (i = 0; ino && i < chain_pos; i++) {
88 		fh_bh = affs_bread(sb, ino);
89 		if (!fh_bh) {
90 			affs_error(sb, "readdir","Cannot read block %d", i);
91 			return -EIO;
92 		}
93 		ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
94 		affs_brelse(fh_bh);
95 		fh_bh = NULL;
96 	}
97 	if (ino)
98 		goto inside;
99 	hash_pos++;
100 
101 	for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
102 		ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
103 		if (!ino)
104 			continue;
105 		ctx->pos = (hash_pos << 16) + 2;
106 inside:
107 		do {
108 			fh_bh = affs_bread(sb, ino);
109 			if (!fh_bh) {
110 				affs_error(sb, "readdir","Cannot read block %d", ino);
111 				break;
112 			}
113 
114 			namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], (u8)30);
115 			name = AFFS_TAIL(sb, fh_bh)->name + 1;
116 			pr_debug("AFFS: readdir(): filldir(\"%.*s\", ino=%u), hash=%d, f_pos=%x\n",
117 				 namelen, name, ino, hash_pos, (u32)ctx->pos);
118 			if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
119 				goto readdir_done;
120 			ctx->pos++;
121 			ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
122 			affs_brelse(fh_bh);
123 			fh_bh = NULL;
124 		} while (ino);
125 	}
126 readdir_done:
127 	file->f_version = inode->i_version;
128 	file->private_data = (void *)(long)ino;
129 
130 readdir_out:
131 	affs_brelse(dir_bh);
132 	affs_brelse(fh_bh);
133 	affs_unlock_dir(inode);
134 	return 0;
135 }
136