2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/config.h>
6 #include <linux/string.h>
7 #include <linux/errno.h>
9 #include <linux/reiserfs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/smp_lock.h>
12 #include <linux/buffer_head.h>
13 #include <asm/uaccess.h>
15 extern struct reiserfs_key MIN_KEY;
17 static int reiserfs_readdir(struct file *, void *, filldir_t);
18 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
21 struct file_operations reiserfs_dir_operations = {
22 .read = generic_read_dir,
23 .readdir = reiserfs_readdir,
24 .fsync = reiserfs_dir_fsync,
25 .ioctl = reiserfs_ioctl,
28 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
31 struct inode *inode = dentry->d_inode;
33 reiserfs_write_lock(inode->i_sb);
34 err = reiserfs_commit_for_inode(inode);
35 reiserfs_write_unlock(inode->i_sb);
41 #define store_ih(where,what) copy_item_head (where, what)
44 static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
46 struct inode *inode = filp->f_dentry->d_inode;
47 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
48 INITIALIZE_PATH(path_to_entry);
49 struct buffer_head *bh;
50 int item_num, entry_num;
51 const struct reiserfs_key *rkey;
52 struct item_head *ih, tmp_ih;
56 char small_buf[32]; /* avoid kmalloc if we can */
57 struct reiserfs_dir_entry de;
60 reiserfs_write_lock(inode->i_sb);
62 reiserfs_check_lock_depth(inode->i_sb, "readdir");
64 /* form key for search the next directory entry using f_pos field of
66 make_cpu_key(&pos_key, inode,
67 (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET, TYPE_DIRENTRY,
69 next_pos = cpu_key_k_offset(&pos_key);
71 /* reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos); */
73 path_to_entry.reada = PATH_READA;
76 /* search the directory item, containing entry with specified key */
78 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
80 if (search_res == IO_ERROR) {
81 // FIXME: we could just skip part of directory which could
86 entry_num = de.de_entry_num;
88 item_num = de.de_item_num;
90 store_ih(&tmp_ih, ih);
92 /* we must have found item, that is item of this directory, */
93 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
94 "vs-9000: found item %h does not match to dir we readdir %K",
96 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
97 "vs-9005 item_num == %d, item amount == %d",
98 item_num, B_NR_ITEMS(bh));
100 /* and entry must be not more than number of entries in the item */
101 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
102 "vs-9010: entry number is too big %d (%d)",
103 entry_num, I_ENTRY_COUNT(ih));
105 if (search_res == POSITION_FOUND
106 || entry_num < I_ENTRY_COUNT(ih)) {
107 /* go through all entries in the directory item beginning from the entry, that has been found */
108 struct reiserfs_de_head *deh =
109 B_I_DEH(bh, ih) + entry_num;
111 for (; entry_num < I_ENTRY_COUNT(ih);
112 entry_num++, deh++) {
118 if (!de_visible(deh))
119 /* it is hidden entry */
121 d_reclen = entry_length(bh, ih, entry_num);
122 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
123 if (!d_name[d_reclen - 1])
124 d_reclen = strlen(d_name);
127 REISERFS_MAX_NAME(inode->i_sb->
129 /* too big to send back to VFS */
133 /* Ignore the .reiserfs_priv entry */
134 if (reiserfs_xattrs(inode->i_sb) &&
135 !old_format_only(inode->i_sb) &&
136 filp->f_dentry == inode->i_sb->s_root &&
137 REISERFS_SB(inode->i_sb)->priv_root &&
138 REISERFS_SB(inode->i_sb)->priv_root->d_inode
139 && deh_objectid(deh) ==
140 le32_to_cpu(INODE_PKEY
141 (REISERFS_SB(inode->i_sb)->
142 priv_root->d_inode)->
147 d_off = deh_offset(deh);
149 d_ino = deh_objectid(deh);
150 if (d_reclen <= 32) {
151 local_buf = small_buf;
154 reiserfs_kmalloc(d_reclen, GFP_NOFS,
157 pathrelse(&path_to_entry);
161 if (item_moved(&tmp_ih, &path_to_entry)) {
162 reiserfs_kfree(local_buf,
168 // Note, that we copy name to user space via temporary
169 // buffer (local_buf) because filldir will block if
170 // user space buffer is swapped out. At that time
171 // entry can move to somewhere else
172 memcpy(local_buf, d_name, d_reclen);
174 (dirent, local_buf, d_reclen, d_off, d_ino,
176 if (local_buf != small_buf) {
177 reiserfs_kfree(local_buf,
183 if (local_buf != small_buf) {
184 reiserfs_kfree(local_buf, d_reclen,
187 // next entry should be looked for with such offset
188 next_pos = deh_offset(deh) + 1;
190 if (item_moved(&tmp_ih, &path_to_entry)) {
196 if (item_num != B_NR_ITEMS(bh) - 1)
197 // end of directory has been reached
200 /* item we went through is last item of node. Using right
201 delimiting key check is it directory end */
202 rkey = get_rkey(&path_to_entry, inode->i_sb);
203 if (!comp_le_keys(rkey, &MIN_KEY)) {
204 /* set pos_key to key, that is the smallest and greater
205 that key of the last entry in the item */
206 set_cpu_key_k_offset(&pos_key, next_pos);
210 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
211 // end of directory has been reached
215 /* directory continues in the right neighboring block */
216 set_cpu_key_k_offset(&pos_key,
217 le_key_k_offset(KEY_FORMAT_3_5, rkey));
222 filp->f_pos = next_pos;
223 pathrelse(&path_to_entry);
224 reiserfs_check_path(&path_to_entry);
226 reiserfs_write_unlock(inode->i_sb);
230 /* compose directory item containing "." and ".." entries (entries are
231 not aligned to 4 byte boundary) */
232 /* the last four params are LE */
233 void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
234 __le32 par_dirid, __le32 par_objid)
236 struct reiserfs_de_head *deh;
238 memset(body, 0, EMPTY_DIR_SIZE_V1);
239 deh = (struct reiserfs_de_head *)body;
241 /* direntry header of "." */
242 put_deh_offset(&(deh[0]), DOT_OFFSET);
243 /* these two are from make_le_item_head, and are are LE */
244 deh[0].deh_dir_id = dirid;
245 deh[0].deh_objectid = objid;
246 deh[0].deh_state = 0; /* Endian safe if 0 */
247 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
248 mark_de_visible(&(deh[0]));
250 /* direntry header of ".." */
251 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
252 /* key of ".." for the root directory */
253 /* these two are from the inode, and are are LE */
254 deh[1].deh_dir_id = par_dirid;
255 deh[1].deh_objectid = par_objid;
256 deh[1].deh_state = 0; /* Endian safe if 0 */
257 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
258 mark_de_visible(&(deh[1]));
260 /* copy ".." and "." */
261 memcpy(body + deh_location(&(deh[0])), ".", 1);
262 memcpy(body + deh_location(&(deh[1])), "..", 2);
265 /* compose directory item containing "." and ".." entries */
266 void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
267 __le32 par_dirid, __le32 par_objid)
269 struct reiserfs_de_head *deh;
271 memset(body, 0, EMPTY_DIR_SIZE);
272 deh = (struct reiserfs_de_head *)body;
274 /* direntry header of "." */
275 put_deh_offset(&(deh[0]), DOT_OFFSET);
276 /* these two are from make_le_item_head, and are are LE */
277 deh[0].deh_dir_id = dirid;
278 deh[0].deh_objectid = objid;
279 deh[0].deh_state = 0; /* Endian safe if 0 */
280 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
281 mark_de_visible(&(deh[0]));
283 /* direntry header of ".." */
284 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
285 /* key of ".." for the root directory */
286 /* these two are from the inode, and are are LE */
287 deh[1].deh_dir_id = par_dirid;
288 deh[1].deh_objectid = par_objid;
289 deh[1].deh_state = 0; /* Endian safe if 0 */
290 put_deh_location(&(deh[1]),
291 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
292 mark_de_visible(&(deh[1]));
294 /* copy ".." and "." */
295 memcpy(body + deh_location(&(deh[0])), ".", 1);
296 memcpy(body + deh_location(&(deh[1])), "..", 2);