1
2
3
4
5#include <linux/string.h>
6#include <linux/errno.h>
7#include <linux/fs.h>
8#include "reiserfs.h"
9#include <linux/stat.h>
10#include <linux/buffer_head.h>
11#include <linux/slab.h>
12#include <asm/uaccess.h>
13
14extern const struct reiserfs_key MIN_KEY;
15
16static int reiserfs_readdir(struct file *, struct dir_context *);
17static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
18 int datasync);
19
20const struct file_operations reiserfs_dir_operations = {
21 .llseek = generic_file_llseek,
22 .read = generic_read_dir,
23 .iterate = reiserfs_readdir,
24 .fsync = reiserfs_dir_fsync,
25 .unlocked_ioctl = reiserfs_ioctl,
26#ifdef CONFIG_COMPAT
27 .compat_ioctl = reiserfs_compat_ioctl,
28#endif
29};
30
31static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
32 int datasync)
33{
34 struct inode *inode = filp->f_mapping->host;
35 int err;
36
37 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
38 if (err)
39 return err;
40
41 mutex_lock(&inode->i_mutex);
42 reiserfs_write_lock(inode->i_sb);
43 err = reiserfs_commit_for_inode(inode);
44 reiserfs_write_unlock(inode->i_sb);
45 mutex_unlock(&inode->i_mutex);
46 if (err < 0)
47 return err;
48 return 0;
49}
50
51#define store_ih(where,what) copy_item_head (where, what)
52
53static inline bool is_privroot_deh(struct inode *dir, struct reiserfs_de_head *deh)
54{
55 struct dentry *privroot = REISERFS_SB(dir->i_sb)->priv_root;
56 return (privroot->d_inode &&
57 deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
58}
59
60int reiserfs_readdir_inode(struct inode *inode, struct dir_context *ctx)
61{
62 struct cpu_key pos_key;
63 INITIALIZE_PATH(path_to_entry);
64 struct buffer_head *bh;
65 int item_num, entry_num;
66 const struct reiserfs_key *rkey;
67 struct item_head *ih, tmp_ih;
68 int search_res;
69 char *local_buf;
70 loff_t next_pos;
71 char small_buf[32];
72 struct reiserfs_dir_entry de;
73 int ret = 0;
74
75 reiserfs_write_lock(inode->i_sb);
76
77 reiserfs_check_lock_depth(inode->i_sb, "readdir");
78
79
80
81 make_cpu_key(&pos_key, inode, ctx->pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
82 next_pos = cpu_key_k_offset(&pos_key);
83
84 path_to_entry.reada = PATH_READA;
85 while (1) {
86 research:
87
88 search_res =
89 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
90 &de);
91 if (search_res == IO_ERROR) {
92
93
94 ret = -EIO;
95 goto out;
96 }
97 entry_num = de.de_entry_num;
98 bh = de.de_bh;
99 item_num = de.de_item_num;
100 ih = de.de_ih;
101 store_ih(&tmp_ih, ih);
102
103
104 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
105 "vs-9000: found item %h does not match to dir we readdir %K",
106 ih, &pos_key);
107 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
108 "vs-9005 item_num == %d, item amount == %d",
109 item_num, B_NR_ITEMS(bh));
110
111
112 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
113 "vs-9010: entry number is too big %d (%d)",
114 entry_num, I_ENTRY_COUNT(ih));
115
116 if (search_res == POSITION_FOUND
117 || entry_num < I_ENTRY_COUNT(ih)) {
118
119 struct reiserfs_de_head *deh =
120 B_I_DEH(bh, ih) + entry_num;
121
122 for (; entry_num < I_ENTRY_COUNT(ih);
123 entry_num++, deh++) {
124 int d_reclen;
125 char *d_name;
126 ino_t d_ino;
127
128 if (!de_visible(deh))
129
130 continue;
131 d_reclen = entry_length(bh, ih, entry_num);
132 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
133
134 if (d_reclen <= 0 ||
135 d_name + d_reclen > bh->b_data + bh->b_size) {
136
137
138 pathrelse(&path_to_entry);
139 ret = -EIO;
140 goto out;
141 }
142
143 if (!d_name[d_reclen - 1])
144 d_reclen = strlen(d_name);
145
146 if (d_reclen >
147 REISERFS_MAX_NAME(inode->i_sb->
148 s_blocksize)) {
149
150 continue;
151 }
152
153
154 if (is_privroot_deh(inode, deh))
155 continue;
156
157 ctx->pos = deh_offset(deh);
158 d_ino = deh_objectid(deh);
159 if (d_reclen <= 32) {
160 local_buf = small_buf;
161 } else {
162 local_buf = kmalloc(d_reclen,
163 GFP_NOFS);
164 if (!local_buf) {
165 pathrelse(&path_to_entry);
166 ret = -ENOMEM;
167 goto out;
168 }
169 if (item_moved(&tmp_ih, &path_to_entry)) {
170 kfree(local_buf);
171 goto research;
172 }
173 }
174
175
176
177
178 memcpy(local_buf, d_name, d_reclen);
179
180
181
182
183
184 reiserfs_write_unlock(inode->i_sb);
185 if (!dir_emit
186 (ctx, local_buf, d_reclen, d_ino,
187 DT_UNKNOWN)) {
188 reiserfs_write_lock(inode->i_sb);
189 if (local_buf != small_buf) {
190 kfree(local_buf);
191 }
192 goto end;
193 }
194 reiserfs_write_lock(inode->i_sb);
195 if (local_buf != small_buf) {
196 kfree(local_buf);
197 }
198
199 next_pos = deh_offset(deh) + 1;
200
201 if (item_moved(&tmp_ih, &path_to_entry)) {
202 set_cpu_key_k_offset(&pos_key,
203 next_pos);
204 goto research;
205 }
206 }
207 }
208
209 if (item_num != B_NR_ITEMS(bh) - 1)
210
211 goto end;
212
213
214
215 rkey = get_rkey(&path_to_entry, inode->i_sb);
216 if (!comp_le_keys(rkey, &MIN_KEY)) {
217
218
219 set_cpu_key_k_offset(&pos_key, next_pos);
220 continue;
221 }
222
223 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
224
225 goto end;
226 }
227
228
229 set_cpu_key_k_offset(&pos_key,
230 le_key_k_offset(KEY_FORMAT_3_5, rkey));
231
232 }
233
234end:
235 ctx->pos = next_pos;
236 pathrelse(&path_to_entry);
237 reiserfs_check_path(&path_to_entry);
238out:
239 reiserfs_write_unlock(inode->i_sb);
240 return ret;
241}
242
243static int reiserfs_readdir(struct file *file, struct dir_context *ctx)
244{
245 return reiserfs_readdir_inode(file_inode(file), ctx);
246}
247
248
249
250
251void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
252 __le32 par_dirid, __le32 par_objid)
253{
254 struct reiserfs_de_head *deh;
255
256 memset(body, 0, EMPTY_DIR_SIZE_V1);
257 deh = (struct reiserfs_de_head *)body;
258
259
260 put_deh_offset(&(deh[0]), DOT_OFFSET);
261
262 deh[0].deh_dir_id = dirid;
263 deh[0].deh_objectid = objid;
264 deh[0].deh_state = 0;
265 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
266 mark_de_visible(&(deh[0]));
267
268
269 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
270
271
272 deh[1].deh_dir_id = par_dirid;
273 deh[1].deh_objectid = par_objid;
274 deh[1].deh_state = 0;
275 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
276 mark_de_visible(&(deh[1]));
277
278
279 memcpy(body + deh_location(&(deh[0])), ".", 1);
280 memcpy(body + deh_location(&(deh[1])), "..", 2);
281}
282
283
284void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
285 __le32 par_dirid, __le32 par_objid)
286{
287 struct reiserfs_de_head *deh;
288
289 memset(body, 0, EMPTY_DIR_SIZE);
290 deh = (struct reiserfs_de_head *)body;
291
292
293 put_deh_offset(&(deh[0]), DOT_OFFSET);
294
295 deh[0].deh_dir_id = dirid;
296 deh[0].deh_objectid = objid;
297 deh[0].deh_state = 0;
298 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
299 mark_de_visible(&(deh[0]));
300
301
302 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
303
304
305 deh[1].deh_dir_id = par_dirid;
306 deh[1].deh_objectid = par_objid;
307 deh[1].deh_state = 0;
308 put_deh_location(&(deh[1]),
309 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
310 mark_de_visible(&(deh[1]));
311
312
313 memcpy(body + deh_location(&(deh[0])), ".", 1);
314 memcpy(body + deh_location(&(deh[1])), "..", 2);
315}
316