blob: 657050ad7430e68777a326c18fad5a4d8a04cefb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#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/smp_lock.h>
11#include <linux/buffer_head.h>
12#include <asm/uaccess.h>
13
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070014extern struct reiserfs_key MIN_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070016static int reiserfs_readdir(struct file *, void *, filldir_t);
17static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
18 int datasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080020const struct file_operations reiserfs_dir_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070021 .read = generic_read_dir,
22 .readdir = reiserfs_readdir,
23 .fsync = reiserfs_dir_fsync,
24 .ioctl = reiserfs_ioctl,
David Howells52b499c2006-08-29 19:06:18 +010025#ifdef CONFIG_COMPAT
26 .compat_ioctl = reiserfs_compat_ioctl,
27#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
29
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070030static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
31 int datasync)
32{
33 struct inode *inode = dentry->d_inode;
34 int err;
35 reiserfs_write_lock(inode->i_sb);
36 err = reiserfs_commit_for_inode(inode);
37 reiserfs_write_unlock(inode->i_sb);
38 if (err < 0)
39 return err;
40 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define store_ih(where,what) copy_item_head (where, what)
44
45//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048 struct inode *inode = filp->f_dentry->d_inode;
49 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
50 INITIALIZE_PATH(path_to_entry);
51 struct buffer_head *bh;
52 int item_num, entry_num;
53 const struct reiserfs_key *rkey;
54 struct item_head *ih, tmp_ih;
55 int search_res;
56 char *local_buf;
57 loff_t next_pos;
58 char small_buf[32]; /* avoid kmalloc if we can */
59 struct reiserfs_dir_entry de;
60 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 reiserfs_check_lock_depth(inode->i_sb, "readdir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066 /* form key for search the next directory entry using f_pos field of
67 file structure */
68 make_cpu_key(&pos_key, inode,
69 (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET, TYPE_DIRENTRY,
70 3);
71 next_pos = cpu_key_k_offset(&pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 /* reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos); */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070075 path_to_entry.reada = PATH_READA;
76 while (1) {
77 research:
78 /* search the directory item, containing entry with specified key */
79 search_res =
80 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
81 &de);
82 if (search_res == IO_ERROR) {
83 // FIXME: we could just skip part of directory which could
84 // not be read
85 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 entry_num = de.de_entry_num;
89 bh = de.de_bh;
90 item_num = de.de_item_num;
91 ih = de.de_ih;
92 store_ih(&tmp_ih, ih);
93
94 /* we must have found item, that is item of this directory, */
95 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
96 "vs-9000: found item %h does not match to dir we readdir %K",
97 ih, &pos_key);
98 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
99 "vs-9005 item_num == %d, item amount == %d",
100 item_num, B_NR_ITEMS(bh));
101
102 /* and entry must be not more than number of entries in the item */
103 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
104 "vs-9010: entry number is too big %d (%d)",
105 entry_num, I_ENTRY_COUNT(ih));
106
107 if (search_res == POSITION_FOUND
108 || entry_num < I_ENTRY_COUNT(ih)) {
109 /* go through all entries in the directory item beginning from the entry, that has been found */
110 struct reiserfs_de_head *deh =
111 B_I_DEH(bh, ih) + entry_num;
112
113 for (; entry_num < I_ENTRY_COUNT(ih);
114 entry_num++, deh++) {
115 int d_reclen;
116 char *d_name;
117 off_t d_off;
118 ino_t d_ino;
119
120 if (!de_visible(deh))
121 /* it is hidden entry */
122 continue;
123 d_reclen = entry_length(bh, ih, entry_num);
124 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
125 if (!d_name[d_reclen - 1])
126 d_reclen = strlen(d_name);
127
128 if (d_reclen >
129 REISERFS_MAX_NAME(inode->i_sb->
130 s_blocksize)) {
131 /* too big to send back to VFS */
132 continue;
133 }
134
135 /* Ignore the .reiserfs_priv entry */
136 if (reiserfs_xattrs(inode->i_sb) &&
137 !old_format_only(inode->i_sb) &&
138 filp->f_dentry == inode->i_sb->s_root &&
139 REISERFS_SB(inode->i_sb)->priv_root &&
140 REISERFS_SB(inode->i_sb)->priv_root->d_inode
141 && deh_objectid(deh) ==
142 le32_to_cpu(INODE_PKEY
143 (REISERFS_SB(inode->i_sb)->
144 priv_root->d_inode)->
145 k_objectid)) {
146 continue;
147 }
148
149 d_off = deh_offset(deh);
150 filp->f_pos = d_off;
151 d_ino = deh_objectid(deh);
152 if (d_reclen <= 32) {
153 local_buf = small_buf;
154 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800155 local_buf = kmalloc(d_reclen,
156 GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 if (!local_buf) {
158 pathrelse(&path_to_entry);
159 ret = -ENOMEM;
160 goto out;
161 }
162 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800163 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 goto research;
165 }
166 }
167 // Note, that we copy name to user space via temporary
168 // buffer (local_buf) because filldir will block if
169 // user space buffer is swapped out. At that time
170 // entry can move to somewhere else
171 memcpy(local_buf, d_name, d_reclen);
172 if (filldir
173 (dirent, local_buf, d_reclen, d_off, d_ino,
174 DT_UNKNOWN) < 0) {
175 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800176 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 }
178 goto end;
179 }
180 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800181 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700182 }
183 // next entry should be looked for with such offset
184 next_pos = deh_offset(deh) + 1;
185
186 if (item_moved(&tmp_ih, &path_to_entry)) {
187 goto research;
188 }
189 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 if (item_num != B_NR_ITEMS(bh) - 1)
193 // end of directory has been reached
194 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 /* item we went through is last item of node. Using right
197 delimiting key check is it directory end */
198 rkey = get_rkey(&path_to_entry, inode->i_sb);
199 if (!comp_le_keys(rkey, &MIN_KEY)) {
200 /* set pos_key to key, that is the smallest and greater
201 that key of the last entry in the item */
202 set_cpu_key_k_offset(&pos_key, next_pos);
203 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
207 // end of directory has been reached
208 goto end;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700211 /* directory continues in the right neighboring block */
212 set_cpu_key_k_offset(&pos_key,
213 le_key_k_offset(KEY_FORMAT_3_5, rkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217 end:
218 filp->f_pos = next_pos;
219 pathrelse(&path_to_entry);
220 reiserfs_check_path(&path_to_entry);
221 out:
222 reiserfs_write_unlock(inode->i_sb);
223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/* compose directory item containing "." and ".." entries (entries are
227 not aligned to 4 byte boundary) */
228/* the last four params are LE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
230 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 memset(body, 0, EMPTY_DIR_SIZE_V1);
235 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 /* direntry header of "." */
238 put_deh_offset(&(deh[0]), DOT_OFFSET);
239 /* these two are from make_le_item_head, and are are LE */
240 deh[0].deh_dir_id = dirid;
241 deh[0].deh_objectid = objid;
242 deh[0].deh_state = 0; /* Endian safe if 0 */
243 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
244 mark_de_visible(&(deh[0]));
245
246 /* direntry header of ".." */
247 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
248 /* key of ".." for the root directory */
249 /* these two are from the inode, and are are LE */
250 deh[1].deh_dir_id = par_dirid;
251 deh[1].deh_objectid = par_objid;
252 deh[1].deh_state = 0; /* Endian safe if 0 */
253 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
254 mark_de_visible(&(deh[1]));
255
256 /* copy ".." and "." */
257 memcpy(body + deh_location(&(deh[0])), ".", 1);
258 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/* compose directory item containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
263 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700265 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267 memset(body, 0, EMPTY_DIR_SIZE);
268 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270 /* direntry header of "." */
271 put_deh_offset(&(deh[0]), DOT_OFFSET);
272 /* these two are from make_le_item_head, and are are LE */
273 deh[0].deh_dir_id = dirid;
274 deh[0].deh_objectid = objid;
275 deh[0].deh_state = 0; /* Endian safe if 0 */
276 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
277 mark_de_visible(&(deh[0]));
278
279 /* direntry header of ".." */
280 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
281 /* key of ".." for the root directory */
282 /* these two are from the inode, and are are LE */
283 deh[1].deh_dir_id = par_dirid;
284 deh[1].deh_objectid = par_objid;
285 deh[1].deh_state = 0; /* Endian safe if 0 */
286 put_deh_location(&(deh[1]),
287 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
288 mark_de_visible(&(deh[1]));
289
290 /* copy ".." and "." */
291 memcpy(body + deh_location(&(deh[0])), ".", 1);
292 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}