xref: /openbmc/linux/fs/reiserfs/dir.c (revision 82ced6fd)
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  */
4 
5 #include <linux/string.h>
6 #include <linux/errno.h>
7 #include <linux/fs.h>
8 #include <linux/reiserfs_fs.h>
9 #include <linux/stat.h>
10 #include <linux/buffer_head.h>
11 #include <asm/uaccess.h>
12 
13 extern const struct reiserfs_key MIN_KEY;
14 
15 static int reiserfs_readdir(struct file *, void *, filldir_t);
16 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
17 			      int datasync);
18 
19 const struct file_operations reiserfs_dir_operations = {
20 	.read = generic_read_dir,
21 	.readdir = reiserfs_readdir,
22 	.fsync = reiserfs_dir_fsync,
23 	.ioctl = reiserfs_ioctl,
24 #ifdef CONFIG_COMPAT
25 	.compat_ioctl = reiserfs_compat_ioctl,
26 #endif
27 };
28 
29 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
30 			      int datasync)
31 {
32 	struct inode *inode = dentry->d_inode;
33 	int err;
34 	reiserfs_write_lock(inode->i_sb);
35 	err = reiserfs_commit_for_inode(inode);
36 	reiserfs_write_unlock(inode->i_sb);
37 	if (err < 0)
38 		return err;
39 	return 0;
40 }
41 
42 #define store_ih(where,what) copy_item_head (where, what)
43 
44 static inline bool is_privroot_deh(struct dentry *dir,
45 				   struct reiserfs_de_head *deh)
46 {
47 	int ret = 0;
48 #ifdef CONFIG_REISERFS_FS_XATTR
49 	struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
50 	ret = (dir == dir->d_parent && privroot->d_inode &&
51 	       deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
52 #endif
53 	return ret;
54 }
55 
56 int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
57 			   filldir_t filldir, loff_t *pos)
58 {
59 	struct inode *inode = dentry->d_inode;
60 	struct cpu_key pos_key;	/* key of current position in the directory (key of directory entry) */
61 	INITIALIZE_PATH(path_to_entry);
62 	struct buffer_head *bh;
63 	int item_num, entry_num;
64 	const struct reiserfs_key *rkey;
65 	struct item_head *ih, tmp_ih;
66 	int search_res;
67 	char *local_buf;
68 	loff_t next_pos;
69 	char small_buf[32];	/* avoid kmalloc if we can */
70 	struct reiserfs_dir_entry de;
71 	int ret = 0;
72 
73 	reiserfs_write_lock(inode->i_sb);
74 
75 	reiserfs_check_lock_depth(inode->i_sb, "readdir");
76 
77 	/* form key for search the next directory entry using f_pos field of
78 	   file structure */
79 	make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
80 	next_pos = cpu_key_k_offset(&pos_key);
81 
82 	path_to_entry.reada = PATH_READA;
83 	while (1) {
84 	      research:
85 		/* search the directory item, containing entry with specified key */
86 		search_res =
87 		    search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
88 					&de);
89 		if (search_res == IO_ERROR) {
90 			// FIXME: we could just skip part of directory which could
91 			// not be read
92 			ret = -EIO;
93 			goto out;
94 		}
95 		entry_num = de.de_entry_num;
96 		bh = de.de_bh;
97 		item_num = de.de_item_num;
98 		ih = de.de_ih;
99 		store_ih(&tmp_ih, ih);
100 
101 		/* we must have found item, that is item of this directory, */
102 		RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
103 		       "vs-9000: found item %h does not match to dir we readdir %K",
104 		       ih, &pos_key);
105 		RFALSE(item_num > B_NR_ITEMS(bh) - 1,
106 		       "vs-9005 item_num == %d, item amount == %d",
107 		       item_num, B_NR_ITEMS(bh));
108 
109 		/* and entry must be not more than number of entries in the item */
110 		RFALSE(I_ENTRY_COUNT(ih) < entry_num,
111 		       "vs-9010: entry number is too big %d (%d)",
112 		       entry_num, I_ENTRY_COUNT(ih));
113 
114 		if (search_res == POSITION_FOUND
115 		    || entry_num < I_ENTRY_COUNT(ih)) {
116 			/* go through all entries in the directory item beginning from the entry, that has been found */
117 			struct reiserfs_de_head *deh =
118 			    B_I_DEH(bh, ih) + entry_num;
119 
120 			for (; entry_num < I_ENTRY_COUNT(ih);
121 			     entry_num++, deh++) {
122 				int d_reclen;
123 				char *d_name;
124 				off_t d_off;
125 				ino_t d_ino;
126 
127 				if (!de_visible(deh))
128 					/* it is hidden entry */
129 					continue;
130 				d_reclen = entry_length(bh, ih, entry_num);
131 				d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
132 
133 				if (d_reclen <= 0 ||
134 				    d_name + d_reclen > bh->b_data + bh->b_size) {
135 					/* There is corrupted data in entry,
136 					 * We'd better stop here */
137 					pathrelse(&path_to_entry);
138 					ret = -EIO;
139 					goto out;
140 				}
141 
142 				if (!d_name[d_reclen - 1])
143 					d_reclen = strlen(d_name);
144 
145 				if (d_reclen >
146 				    REISERFS_MAX_NAME(inode->i_sb->
147 						      s_blocksize)) {
148 					/* too big to send back to VFS */
149 					continue;
150 				}
151 
152 				/* Ignore the .reiserfs_priv entry */
153 				if (is_privroot_deh(dentry, deh))
154 					continue;
155 
156 				d_off = deh_offset(deh);
157 				*pos = d_off;
158 				d_ino = deh_objectid(deh);
159 				if (d_reclen <= 32) {
160 					local_buf = small_buf;
161 				} else {
162 					local_buf = kmalloc(d_reclen,
163 							    GFP_NOFS);
164 					if (!local_buf) {
165 						pathrelse(&path_to_entry);
166 						ret = -ENOMEM;
167 						goto out;
168 					}
169 					if (item_moved(&tmp_ih, &path_to_entry)) {
170 						kfree(local_buf);
171 						goto research;
172 					}
173 				}
174 				// Note, that we copy name to user space via temporary
175 				// buffer (local_buf) because filldir will block if
176 				// user space buffer is swapped out. At that time
177 				// entry can move to somewhere else
178 				memcpy(local_buf, d_name, d_reclen);
179 				if (filldir
180 				    (dirent, local_buf, d_reclen, d_off, d_ino,
181 				     DT_UNKNOWN) < 0) {
182 					if (local_buf != small_buf) {
183 						kfree(local_buf);
184 					}
185 					goto end;
186 				}
187 				if (local_buf != small_buf) {
188 					kfree(local_buf);
189 				}
190 				// next entry should be looked for with such offset
191 				next_pos = deh_offset(deh) + 1;
192 
193 				if (item_moved(&tmp_ih, &path_to_entry)) {
194 					goto research;
195 				}
196 			}	/* for */
197 		}
198 
199 		if (item_num != B_NR_ITEMS(bh) - 1)
200 			// end of directory has been reached
201 			goto end;
202 
203 		/* item we went through is last item of node. Using right
204 		   delimiting key check is it directory end */
205 		rkey = get_rkey(&path_to_entry, inode->i_sb);
206 		if (!comp_le_keys(rkey, &MIN_KEY)) {
207 			/* set pos_key to key, that is the smallest and greater
208 			   that key of the last entry in the item */
209 			set_cpu_key_k_offset(&pos_key, next_pos);
210 			continue;
211 		}
212 
213 		if (COMP_SHORT_KEYS(rkey, &pos_key)) {
214 			// end of directory has been reached
215 			goto end;
216 		}
217 
218 		/* directory continues in the right neighboring block */
219 		set_cpu_key_k_offset(&pos_key,
220 				     le_key_k_offset(KEY_FORMAT_3_5, rkey));
221 
222 	}			/* while */
223 
224 end:
225 	*pos = next_pos;
226 	pathrelse(&path_to_entry);
227 	reiserfs_check_path(&path_to_entry);
228 out:
229 	reiserfs_write_unlock(inode->i_sb);
230 	return ret;
231 }
232 
233 static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
234 {
235 	struct dentry *dentry = file->f_path.dentry;
236 	return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
237 }
238 
239 /* compose directory item containing "." and ".." entries (entries are
240    not aligned to 4 byte boundary) */
241 /* the last four params are LE */
242 void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
243 			    __le32 par_dirid, __le32 par_objid)
244 {
245 	struct reiserfs_de_head *deh;
246 
247 	memset(body, 0, EMPTY_DIR_SIZE_V1);
248 	deh = (struct reiserfs_de_head *)body;
249 
250 	/* direntry header of "." */
251 	put_deh_offset(&(deh[0]), DOT_OFFSET);
252 	/* these two are from make_le_item_head, and are are LE */
253 	deh[0].deh_dir_id = dirid;
254 	deh[0].deh_objectid = objid;
255 	deh[0].deh_state = 0;	/* Endian safe if 0 */
256 	put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
257 	mark_de_visible(&(deh[0]));
258 
259 	/* direntry header of ".." */
260 	put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
261 	/* key of ".." for the root directory */
262 	/* these two are from the inode, and are are LE */
263 	deh[1].deh_dir_id = par_dirid;
264 	deh[1].deh_objectid = par_objid;
265 	deh[1].deh_state = 0;	/* Endian safe if 0 */
266 	put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
267 	mark_de_visible(&(deh[1]));
268 
269 	/* copy ".." and "." */
270 	memcpy(body + deh_location(&(deh[0])), ".", 1);
271 	memcpy(body + deh_location(&(deh[1])), "..", 2);
272 }
273 
274 /* compose directory item containing "." and ".." entries */
275 void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
276 			 __le32 par_dirid, __le32 par_objid)
277 {
278 	struct reiserfs_de_head *deh;
279 
280 	memset(body, 0, EMPTY_DIR_SIZE);
281 	deh = (struct reiserfs_de_head *)body;
282 
283 	/* direntry header of "." */
284 	put_deh_offset(&(deh[0]), DOT_OFFSET);
285 	/* these two are from make_le_item_head, and are are LE */
286 	deh[0].deh_dir_id = dirid;
287 	deh[0].deh_objectid = objid;
288 	deh[0].deh_state = 0;	/* Endian safe if 0 */
289 	put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
290 	mark_de_visible(&(deh[0]));
291 
292 	/* direntry header of ".." */
293 	put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
294 	/* key of ".." for the root directory */
295 	/* these two are from the inode, and are are LE */
296 	deh[1].deh_dir_id = par_dirid;
297 	deh[1].deh_objectid = par_objid;
298 	deh[1].deh_state = 0;	/* Endian safe if 0 */
299 	put_deh_location(&(deh[1]),
300 			 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
301 	mark_de_visible(&(deh[1]));
302 
303 	/* copy ".." and "." */
304 	memcpy(body + deh_location(&(deh[0])), ".", 1);
305 	memcpy(body + deh_location(&(deh[1])), "..", 2);
306 }
307