2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/string.h>
6 #include <linux/errno.h>
8 #include <linux/reiserfs_fs.h>
9 #include <linux/stat.h>
10 #include <linux/buffer_head.h>
11 #include <asm/uaccess.h>
13 extern const struct reiserfs_key MIN_KEY;
15 static int reiserfs_readdir(struct file *, void *, filldir_t);
16 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
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,
25 .compat_ioctl = reiserfs_compat_ioctl,
29 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
32 struct inode *inode = dentry->d_inode;
34 reiserfs_write_lock(inode->i_sb);
35 err = reiserfs_commit_for_inode(inode);
36 reiserfs_write_unlock(inode->i_sb);
42 #define store_ih(where,what) copy_item_head (where, what)
44 int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
45 filldir_t filldir, loff_t *pos)
47 struct inode *inode = dentry->d_inode;
48 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
49 INITIALIZE_PATH(path_to_entry);
50 struct buffer_head *bh;
51 int item_num, entry_num;
52 const struct reiserfs_key *rkey;
53 struct item_head *ih, tmp_ih;
57 char small_buf[32]; /* avoid kmalloc if we can */
58 struct reiserfs_dir_entry de;
61 reiserfs_write_lock(inode->i_sb);
63 reiserfs_check_lock_depth(inode->i_sb, "readdir");
65 /* form key for search the next directory entry using f_pos field of
67 make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
68 next_pos = cpu_key_k_offset(&pos_key);
70 path_to_entry.reada = PATH_READA;
73 /* search the directory item, containing entry with specified key */
75 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
77 if (search_res == IO_ERROR) {
78 // FIXME: we could just skip part of directory which could
83 entry_num = de.de_entry_num;
85 item_num = de.de_item_num;
87 store_ih(&tmp_ih, ih);
89 /* we must have found item, that is item of this directory, */
90 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
91 "vs-9000: found item %h does not match to dir we readdir %K",
93 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
94 "vs-9005 item_num == %d, item amount == %d",
95 item_num, B_NR_ITEMS(bh));
97 /* and entry must be not more than number of entries in the item */
98 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
99 "vs-9010: entry number is too big %d (%d)",
100 entry_num, I_ENTRY_COUNT(ih));
102 if (search_res == POSITION_FOUND
103 || entry_num < I_ENTRY_COUNT(ih)) {
104 /* go through all entries in the directory item beginning from the entry, that has been found */
105 struct reiserfs_de_head *deh =
106 B_I_DEH(bh, ih) + entry_num;
108 for (; entry_num < I_ENTRY_COUNT(ih);
109 entry_num++, deh++) {
115 if (!de_visible(deh))
116 /* it is hidden entry */
118 d_reclen = entry_length(bh, ih, entry_num);
119 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
122 d_name + d_reclen > bh->b_data + bh->b_size) {
123 /* There is corrupted data in entry,
124 * We'd better stop here */
125 pathrelse(&path_to_entry);
130 if (!d_name[d_reclen - 1])
131 d_reclen = strlen(d_name);
134 REISERFS_MAX_NAME(inode->i_sb->
136 /* too big to send back to VFS */
140 /* Ignore the .reiserfs_priv entry */
141 if (reiserfs_xattrs(inode->i_sb) &&
142 !old_format_only(inode->i_sb) &&
143 dentry == inode->i_sb->s_root &&
144 REISERFS_SB(inode->i_sb)->priv_root &&
145 REISERFS_SB(inode->i_sb)->priv_root->d_inode
146 && deh_objectid(deh) ==
147 le32_to_cpu(INODE_PKEY
148 (REISERFS_SB(inode->i_sb)->
149 priv_root->d_inode)->
154 d_off = deh_offset(deh);
156 d_ino = deh_objectid(deh);
157 if (d_reclen <= 32) {
158 local_buf = small_buf;
160 local_buf = kmalloc(d_reclen,
163 pathrelse(&path_to_entry);
167 if (item_moved(&tmp_ih, &path_to_entry)) {
172 // Note, that we copy name to user space via temporary
173 // buffer (local_buf) because filldir will block if
174 // user space buffer is swapped out. At that time
175 // entry can move to somewhere else
176 memcpy(local_buf, d_name, d_reclen);
178 (dirent, local_buf, d_reclen, d_off, d_ino,
180 if (local_buf != small_buf) {
185 if (local_buf != small_buf) {
188 // next entry should be looked for with such offset
189 next_pos = deh_offset(deh) + 1;
191 if (item_moved(&tmp_ih, &path_to_entry)) {
197 if (item_num != B_NR_ITEMS(bh) - 1)
198 // end of directory has been reached
201 /* item we went through is last item of node. Using right
202 delimiting key check is it directory end */
203 rkey = get_rkey(&path_to_entry, inode->i_sb);
204 if (!comp_le_keys(rkey, &MIN_KEY)) {
205 /* set pos_key to key, that is the smallest and greater
206 that key of the last entry in the item */
207 set_cpu_key_k_offset(&pos_key, next_pos);
211 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
212 // end of directory has been reached
216 /* directory continues in the right neighboring block */
217 set_cpu_key_k_offset(&pos_key,
218 le_key_k_offset(KEY_FORMAT_3_5, rkey));
224 pathrelse(&path_to_entry);
225 reiserfs_check_path(&path_to_entry);
227 reiserfs_write_unlock(inode->i_sb);
231 static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
233 struct dentry *dentry = file->f_path.dentry;
234 return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
237 /* compose directory item containing "." and ".." entries (entries are
238 not aligned to 4 byte boundary) */
239 /* the last four params are LE */
240 void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
241 __le32 par_dirid, __le32 par_objid)
243 struct reiserfs_de_head *deh;
245 memset(body, 0, EMPTY_DIR_SIZE_V1);
246 deh = (struct reiserfs_de_head *)body;
248 /* direntry header of "." */
249 put_deh_offset(&(deh[0]), DOT_OFFSET);
250 /* these two are from make_le_item_head, and are are LE */
251 deh[0].deh_dir_id = dirid;
252 deh[0].deh_objectid = objid;
253 deh[0].deh_state = 0; /* Endian safe if 0 */
254 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
255 mark_de_visible(&(deh[0]));
257 /* direntry header of ".." */
258 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
259 /* key of ".." for the root directory */
260 /* these two are from the inode, and are are LE */
261 deh[1].deh_dir_id = par_dirid;
262 deh[1].deh_objectid = par_objid;
263 deh[1].deh_state = 0; /* Endian safe if 0 */
264 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
265 mark_de_visible(&(deh[1]));
267 /* copy ".." and "." */
268 memcpy(body + deh_location(&(deh[0])), ".", 1);
269 memcpy(body + deh_location(&(deh[1])), "..", 2);
272 /* compose directory item containing "." and ".." entries */
273 void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
274 __le32 par_dirid, __le32 par_objid)
276 struct reiserfs_de_head *deh;
278 memset(body, 0, EMPTY_DIR_SIZE);
279 deh = (struct reiserfs_de_head *)body;
281 /* direntry header of "." */
282 put_deh_offset(&(deh[0]), DOT_OFFSET);
283 /* these two are from make_le_item_head, and are are LE */
284 deh[0].deh_dir_id = dirid;
285 deh[0].deh_objectid = objid;
286 deh[0].deh_state = 0; /* Endian safe if 0 */
287 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
288 mark_de_visible(&(deh[0]));
290 /* direntry header of ".." */
291 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
292 /* key of ".." for the root directory */
293 /* these two are from the inode, and are are LE */
294 deh[1].deh_dir_id = par_dirid;
295 deh[1].deh_objectid = par_objid;
296 deh[1].deh_state = 0; /* Endian safe if 0 */
297 put_deh_location(&(deh[1]),
298 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
299 mark_de_visible(&(deh[1]));
301 /* copy ".." and "." */
302 memcpy(body + deh_location(&(deh[0])), ".", 1);
303 memcpy(body + deh_location(&(deh[1])), "..", 2);