blob: d71ac657928931e114ea0798342e501f27558a79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
5#include <linux/config.h>
6#include <linux/string.h>
7#include <linux/errno.h>
8#include <linux/fs.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>
14
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070015extern struct reiserfs_key MIN_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070017static int reiserfs_readdir(struct file *, void *, filldir_t);
18static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
19 int datasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct file_operations reiserfs_dir_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070022 .read = generic_read_dir,
23 .readdir = reiserfs_readdir,
24 .fsync = reiserfs_dir_fsync,
25 .ioctl = reiserfs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070028static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
29 int datasync)
30{
31 struct inode *inode = dentry->d_inode;
32 int err;
33 reiserfs_write_lock(inode->i_sb);
34 err = reiserfs_commit_for_inode(inode);
35 reiserfs_write_unlock(inode->i_sb);
36 if (err < 0)
37 return err;
38 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define store_ih(where,what) copy_item_head (where, what)
42
43//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046 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;
53 int search_res;
54 char *local_buf;
55 loff_t next_pos;
56 char small_buf[32]; /* avoid kmalloc if we can */
57 struct reiserfs_dir_entry de;
58 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062 reiserfs_check_lock_depth(inode->i_sb, "readdir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 /* form key for search the next directory entry using f_pos field of
65 file structure */
66 make_cpu_key(&pos_key, inode,
67 (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET, TYPE_DIRENTRY,
68 3);
69 next_pos = cpu_key_k_offset(&pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 /* reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos); */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 path_to_entry.reada = PATH_READA;
74 while (1) {
75 research:
76 /* search the directory item, containing entry with specified key */
77 search_res =
78 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
79 &de);
80 if (search_res == IO_ERROR) {
81 // FIXME: we could just skip part of directory which could
82 // not be read
83 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086 entry_num = de.de_entry_num;
87 bh = de.de_bh;
88 item_num = de.de_item_num;
89 ih = de.de_ih;
90 store_ih(&tmp_ih, ih);
91
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",
95 ih, &pos_key);
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));
99
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));
104
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;
110
111 for (; entry_num < I_ENTRY_COUNT(ih);
112 entry_num++, deh++) {
113 int d_reclen;
114 char *d_name;
115 off_t d_off;
116 ino_t d_ino;
117
118 if (!de_visible(deh))
119 /* it is hidden entry */
120 continue;
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);
125
126 if (d_reclen >
127 REISERFS_MAX_NAME(inode->i_sb->
128 s_blocksize)) {
129 /* too big to send back to VFS */
130 continue;
131 }
132
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)->
143 k_objectid)) {
144 continue;
145 }
146
147 d_off = deh_offset(deh);
148 filp->f_pos = d_off;
149 d_ino = deh_objectid(deh);
150 if (d_reclen <= 32) {
151 local_buf = small_buf;
152 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800153 local_buf = kmalloc(d_reclen,
154 GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 if (!local_buf) {
156 pathrelse(&path_to_entry);
157 ret = -ENOMEM;
158 goto out;
159 }
160 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800161 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 goto research;
163 }
164 }
165 // Note, that we copy name to user space via temporary
166 // buffer (local_buf) because filldir will block if
167 // user space buffer is swapped out. At that time
168 // entry can move to somewhere else
169 memcpy(local_buf, d_name, d_reclen);
170 if (filldir
171 (dirent, local_buf, d_reclen, d_off, d_ino,
172 DT_UNKNOWN) < 0) {
173 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800174 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700175 }
176 goto end;
177 }
178 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800179 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 }
181 // next entry should be looked for with such offset
182 next_pos = deh_offset(deh) + 1;
183
184 if (item_moved(&tmp_ih, &path_to_entry)) {
185 goto research;
186 }
187 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 if (item_num != B_NR_ITEMS(bh) - 1)
191 // end of directory has been reached
192 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 /* item we went through is last item of node. Using right
195 delimiting key check is it directory end */
196 rkey = get_rkey(&path_to_entry, inode->i_sb);
197 if (!comp_le_keys(rkey, &MIN_KEY)) {
198 /* set pos_key to key, that is the smallest and greater
199 that key of the last entry in the item */
200 set_cpu_key_k_offset(&pos_key, next_pos);
201 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
205 // end of directory has been reached
206 goto end;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700209 /* directory continues in the right neighboring block */
210 set_cpu_key_k_offset(&pos_key,
211 le_key_k_offset(KEY_FORMAT_3_5, rkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700213 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 end:
216 filp->f_pos = next_pos;
217 pathrelse(&path_to_entry);
218 reiserfs_check_path(&path_to_entry);
219 out:
220 reiserfs_write_unlock(inode->i_sb);
221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/* compose directory item containing "." and ".." entries (entries are
225 not aligned to 4 byte boundary) */
226/* the last four params are LE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
228 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 memset(body, 0, EMPTY_DIR_SIZE_V1);
233 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 /* direntry header of "." */
236 put_deh_offset(&(deh[0]), DOT_OFFSET);
237 /* these two are from make_le_item_head, and are are LE */
238 deh[0].deh_dir_id = dirid;
239 deh[0].deh_objectid = objid;
240 deh[0].deh_state = 0; /* Endian safe if 0 */
241 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
242 mark_de_visible(&(deh[0]));
243
244 /* direntry header of ".." */
245 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
246 /* key of ".." for the root directory */
247 /* these two are from the inode, and are are LE */
248 deh[1].deh_dir_id = par_dirid;
249 deh[1].deh_objectid = par_objid;
250 deh[1].deh_state = 0; /* Endian safe if 0 */
251 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
252 mark_de_visible(&(deh[1]));
253
254 /* copy ".." and "." */
255 memcpy(body + deh_location(&(deh[0])), ".", 1);
256 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/* compose directory item containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
261 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700265 memset(body, 0, EMPTY_DIR_SIZE);
266 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700268 /* direntry header of "." */
269 put_deh_offset(&(deh[0]), DOT_OFFSET);
270 /* these two are from make_le_item_head, and are are LE */
271 deh[0].deh_dir_id = dirid;
272 deh[0].deh_objectid = objid;
273 deh[0].deh_state = 0; /* Endian safe if 0 */
274 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
275 mark_de_visible(&(deh[0]));
276
277 /* direntry header of ".." */
278 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
279 /* key of ".." for the root directory */
280 /* these two are from the inode, and are are LE */
281 deh[1].deh_dir_id = par_dirid;
282 deh[1].deh_objectid = par_objid;
283 deh[1].deh_state = 0; /* Endian safe if 0 */
284 put_deh_location(&(deh[1]),
285 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
286 mark_de_visible(&(deh[1]));
287
288 /* copy ".." and "." */
289 memcpy(body + deh_location(&(deh[0])), ".", 1);
290 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}