blob: 9aabcc0ccd2d002401e7611a93f5f6b8458136c0 [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070027static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
28 int datasync)
29{
30 struct inode *inode = dentry->d_inode;
31 int err;
32 reiserfs_write_lock(inode->i_sb);
33 err = reiserfs_commit_for_inode(inode);
34 reiserfs_write_unlock(inode->i_sb);
35 if (err < 0)
36 return err;
37 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define store_ih(where,what) copy_item_head (where, what)
41
42//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070043static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070045 struct inode *inode = filp->f_dentry->d_inode;
46 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
47 INITIALIZE_PATH(path_to_entry);
48 struct buffer_head *bh;
49 int item_num, entry_num;
50 const struct reiserfs_key *rkey;
51 struct item_head *ih, tmp_ih;
52 int search_res;
53 char *local_buf;
54 loff_t next_pos;
55 char small_buf[32]; /* avoid kmalloc if we can */
56 struct reiserfs_dir_entry de;
57 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070059 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070061 reiserfs_check_lock_depth(inode->i_sb, "readdir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 /* form key for search the next directory entry using f_pos field of
64 file structure */
65 make_cpu_key(&pos_key, inode,
66 (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET, TYPE_DIRENTRY,
67 3);
68 next_pos = cpu_key_k_offset(&pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070 /* reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos); */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072 path_to_entry.reada = PATH_READA;
73 while (1) {
74 research:
75 /* search the directory item, containing entry with specified key */
76 search_res =
77 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
78 &de);
79 if (search_res == IO_ERROR) {
80 // FIXME: we could just skip part of directory which could
81 // not be read
82 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 entry_num = de.de_entry_num;
86 bh = de.de_bh;
87 item_num = de.de_item_num;
88 ih = de.de_ih;
89 store_ih(&tmp_ih, ih);
90
91 /* we must have found item, that is item of this directory, */
92 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
93 "vs-9000: found item %h does not match to dir we readdir %K",
94 ih, &pos_key);
95 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
96 "vs-9005 item_num == %d, item amount == %d",
97 item_num, B_NR_ITEMS(bh));
98
99 /* and entry must be not more than number of entries in the item */
100 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
101 "vs-9010: entry number is too big %d (%d)",
102 entry_num, I_ENTRY_COUNT(ih));
103
104 if (search_res == POSITION_FOUND
105 || entry_num < I_ENTRY_COUNT(ih)) {
106 /* go through all entries in the directory item beginning from the entry, that has been found */
107 struct reiserfs_de_head *deh =
108 B_I_DEH(bh, ih) + entry_num;
109
110 for (; entry_num < I_ENTRY_COUNT(ih);
111 entry_num++, deh++) {
112 int d_reclen;
113 char *d_name;
114 off_t d_off;
115 ino_t d_ino;
116
117 if (!de_visible(deh))
118 /* it is hidden entry */
119 continue;
120 d_reclen = entry_length(bh, ih, entry_num);
121 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
122 if (!d_name[d_reclen - 1])
123 d_reclen = strlen(d_name);
124
125 if (d_reclen >
126 REISERFS_MAX_NAME(inode->i_sb->
127 s_blocksize)) {
128 /* too big to send back to VFS */
129 continue;
130 }
131
132 /* Ignore the .reiserfs_priv entry */
133 if (reiserfs_xattrs(inode->i_sb) &&
134 !old_format_only(inode->i_sb) &&
135 filp->f_dentry == inode->i_sb->s_root &&
136 REISERFS_SB(inode->i_sb)->priv_root &&
137 REISERFS_SB(inode->i_sb)->priv_root->d_inode
138 && deh_objectid(deh) ==
139 le32_to_cpu(INODE_PKEY
140 (REISERFS_SB(inode->i_sb)->
141 priv_root->d_inode)->
142 k_objectid)) {
143 continue;
144 }
145
146 d_off = deh_offset(deh);
147 filp->f_pos = d_off;
148 d_ino = deh_objectid(deh);
149 if (d_reclen <= 32) {
150 local_buf = small_buf;
151 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800152 local_buf = kmalloc(d_reclen,
153 GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 if (!local_buf) {
155 pathrelse(&path_to_entry);
156 ret = -ENOMEM;
157 goto out;
158 }
159 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800160 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 goto research;
162 }
163 }
164 // Note, that we copy name to user space via temporary
165 // buffer (local_buf) because filldir will block if
166 // user space buffer is swapped out. At that time
167 // entry can move to somewhere else
168 memcpy(local_buf, d_name, d_reclen);
169 if (filldir
170 (dirent, local_buf, d_reclen, d_off, d_ino,
171 DT_UNKNOWN) < 0) {
172 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800173 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174 }
175 goto end;
176 }
177 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800178 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 }
180 // next entry should be looked for with such offset
181 next_pos = deh_offset(deh) + 1;
182
183 if (item_moved(&tmp_ih, &path_to_entry)) {
184 goto research;
185 }
186 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189 if (item_num != B_NR_ITEMS(bh) - 1)
190 // end of directory has been reached
191 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700193 /* item we went through is last item of node. Using right
194 delimiting key check is it directory end */
195 rkey = get_rkey(&path_to_entry, inode->i_sb);
196 if (!comp_le_keys(rkey, &MIN_KEY)) {
197 /* set pos_key to key, that is the smallest and greater
198 that key of the last entry in the item */
199 set_cpu_key_k_offset(&pos_key, next_pos);
200 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
204 // end of directory has been reached
205 goto end;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 /* directory continues in the right neighboring block */
209 set_cpu_key_k_offset(&pos_key,
210 le_key_k_offset(KEY_FORMAT_3_5, rkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 end:
215 filp->f_pos = next_pos;
216 pathrelse(&path_to_entry);
217 reiserfs_check_path(&path_to_entry);
218 out:
219 reiserfs_write_unlock(inode->i_sb);
220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/* compose directory item containing "." and ".." entries (entries are
224 not aligned to 4 byte boundary) */
225/* the last four params are LE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
227 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 memset(body, 0, EMPTY_DIR_SIZE_V1);
232 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 /* direntry header of "." */
235 put_deh_offset(&(deh[0]), DOT_OFFSET);
236 /* these two are from make_le_item_head, and are are LE */
237 deh[0].deh_dir_id = dirid;
238 deh[0].deh_objectid = objid;
239 deh[0].deh_state = 0; /* Endian safe if 0 */
240 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
241 mark_de_visible(&(deh[0]));
242
243 /* direntry header of ".." */
244 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
245 /* key of ".." for the root directory */
246 /* these two are from the inode, and are are LE */
247 deh[1].deh_dir_id = par_dirid;
248 deh[1].deh_objectid = par_objid;
249 deh[1].deh_state = 0; /* Endian safe if 0 */
250 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
251 mark_de_visible(&(deh[1]));
252
253 /* copy ".." and "." */
254 memcpy(body + deh_location(&(deh[0])), ".", 1);
255 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/* compose directory item containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700259void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
260 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264 memset(body, 0, EMPTY_DIR_SIZE);
265 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267 /* direntry header of "." */
268 put_deh_offset(&(deh[0]), DOT_OFFSET);
269 /* these two are from make_le_item_head, and are are LE */
270 deh[0].deh_dir_id = dirid;
271 deh[0].deh_objectid = objid;
272 deh[0].deh_state = 0; /* Endian safe if 0 */
273 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
274 mark_de_visible(&(deh[0]));
275
276 /* direntry header of ".." */
277 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
278 /* key of ".." for the root directory */
279 /* these two are from the inode, and are are LE */
280 deh[1].deh_dir_id = par_dirid;
281 deh[1].deh_objectid = par_objid;
282 deh[1].deh_state = 0; /* Endian safe if 0 */
283 put_deh_location(&(deh[1]),
284 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
285 mark_de_visible(&(deh[1]));
286
287 /* copy ".." and "." */
288 memcpy(body + deh_location(&(deh[0])), ".", 1);
289 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}