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 static inline bool is_privroot_deh(struct dentry *dir,
45 struct reiserfs_de_head *deh)
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);
56 int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
57 filldir_t filldir, loff_t *pos)
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;
69 char small_buf[32]; /* avoid kmalloc if we can */
70 struct reiserfs_dir_entry de;
73 reiserfs_write_lock(inode->i_sb);
75 reiserfs_check_lock_depth(inode->i_sb, "readdir");
77 /* form key for search the next directory entry using f_pos field of
79 make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
80 next_pos = cpu_key_k_offset(&pos_key);
82 path_to_entry.reada = PATH_READA;
85 /* search the directory item, containing entry with specified key */
87 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
89 if (search_res == IO_ERROR) {
90 // FIXME: we could just skip part of directory which could
95 entry_num = de.de_entry_num;
97 item_num = de.de_item_num;
99 store_ih(&tmp_ih, ih);
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",
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));
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));
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;
120 for (; entry_num < I_ENTRY_COUNT(ih);
121 entry_num++, deh++) {
127 if (!de_visible(deh))
128 /* it is hidden entry */
130 d_reclen = entry_length(bh, ih, entry_num);
131 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
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);
142 if (!d_name[d_reclen - 1])
143 d_reclen = strlen(d_name);
146 REISERFS_MAX_NAME(inode->i_sb->
148 /* too big to send back to VFS */
152 /* Ignore the .reiserfs_priv entry */
153 if (is_privroot_deh(dentry, deh))
156 d_off = deh_offset(deh);
158 d_ino = deh_objectid(deh);
159 if (d_reclen <= 32) {
160 local_buf = small_buf;
162 local_buf = kmalloc(d_reclen,
165 pathrelse(&path_to_entry);
169 if (item_moved(&tmp_ih, &path_to_entry)) {
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);
180 (dirent, local_buf, d_reclen, d_off, d_ino,
182 if (local_buf != small_buf) {
187 if (local_buf != small_buf) {
190 // next entry should be looked for with such offset
191 next_pos = deh_offset(deh) + 1;
193 if (item_moved(&tmp_ih, &path_to_entry)) {
199 if (item_num != B_NR_ITEMS(bh) - 1)
200 // end of directory has been reached
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);
213 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
214 // end of directory has been reached
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));
226 pathrelse(&path_to_entry);
227 reiserfs_check_path(&path_to_entry);
229 reiserfs_write_unlock(inode->i_sb);
233 static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
235 struct dentry *dentry = file->f_path.dentry;
236 return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
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)
245 struct reiserfs_de_head *deh;
247 memset(body, 0, EMPTY_DIR_SIZE_V1);
248 deh = (struct reiserfs_de_head *)body;
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]));
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]));
269 /* copy ".." and "." */
270 memcpy(body + deh_location(&(deh[0])), ".", 1);
271 memcpy(body + deh_location(&(deh[1])), "..", 2);
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)
278 struct reiserfs_de_head *deh;
280 memset(body, 0, EMPTY_DIR_SIZE);
281 deh = (struct reiserfs_de_head *)body;
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]));
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]));
303 /* copy ".." and "." */
304 memcpy(body + deh_location(&(deh[0])), ".", 1);
305 memcpy(body + deh_location(&(deh[1])), "..", 2);