staging: usbip: vhci_sysfs.c: check return value of sscanf
[linux-2.6-block.git] / fs / reiserfs / dir.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
1da177e4
LT
5#include <linux/string.h>
6#include <linux/errno.h>
7#include <linux/fs.h>
f466c6fd 8#include "reiserfs.h"
1da177e4 9#include <linux/stat.h>
1da177e4 10#include <linux/buffer_head.h>
5a0e3ad6 11#include <linux/slab.h>
1da177e4
LT
12#include <asm/uaccess.h>
13
5a1b6391 14extern const struct reiserfs_key MIN_KEY;
1da177e4 15
4acf381e 16static int reiserfs_readdir(struct file *, struct dir_context *);
02c24a82
JB
17static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
18 int datasync);
1da177e4 19
4b6f5d20 20const struct file_operations reiserfs_dir_operations = {
ca572727 21 .llseek = generic_file_llseek,
bd4c625c 22 .read = generic_read_dir,
4acf381e 23 .iterate = reiserfs_readdir,
bd4c625c 24 .fsync = reiserfs_dir_fsync,
205cb37b 25 .unlocked_ioctl = reiserfs_ioctl,
52b499c4
DH
26#ifdef CONFIG_COMPAT
27 .compat_ioctl = reiserfs_compat_ioctl,
28#endif
1da177e4
LT
29};
30
02c24a82
JB
31static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
32 int datasync)
bd4c625c 33{
7ea80859 34 struct inode *inode = filp->f_mapping->host;
bd4c625c 35 int err;
02c24a82
JB
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);
bd4c625c
LT
42 reiserfs_write_lock(inode->i_sb);
43 err = reiserfs_commit_for_inode(inode);
44 reiserfs_write_unlock(inode->i_sb);
02c24a82 45 mutex_unlock(&inode->i_mutex);
bd4c625c
LT
46 if (err < 0)
47 return err;
48 return 0;
1da177e4
LT
49}
50
1da177e4
LT
51#define store_ih(where,what) copy_item_head (where, what)
52
99ce4169 53static inline bool is_privroot_deh(struct inode *dir, struct reiserfs_de_head *deh)
677c9b2e 54{
99ce4169
AV
55 struct dentry *privroot = REISERFS_SB(dir->i_sb)->priv_root;
56 return (privroot->d_inode &&
73422811 57 deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
677c9b2e
JM
58}
59
cd62cdae 60int reiserfs_readdir_inode(struct inode *inode, struct dir_context *ctx)
1da177e4 61{
bd4c625c
LT
62 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
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]; /* avoid kmalloc if we can */
72 struct reiserfs_dir_entry de;
73 int ret = 0;
278f6679 74 int depth;
bd4c625c
LT
75
76 reiserfs_write_lock(inode->i_sb);
77
78 reiserfs_check_lock_depth(inode->i_sb, "readdir");
79
80 /* form key for search the next directory entry using f_pos field of
81 file structure */
4acf381e 82 make_cpu_key(&pos_key, inode, ctx->pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
bd4c625c
LT
83 next_pos = cpu_key_k_offset(&pos_key);
84
bd4c625c
LT
85 path_to_entry.reada = PATH_READA;
86 while (1) {
87 research:
88 /* search the directory item, containing entry with specified key */
89 search_res =
90 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
91 &de);
92 if (search_res == IO_ERROR) {
93 // FIXME: we could just skip part of directory which could
94 // not be read
95 ret = -EIO;
1da177e4 96 goto out;
1da177e4 97 }
bd4c625c
LT
98 entry_num = de.de_entry_num;
99 bh = de.de_bh;
100 item_num = de.de_item_num;
101 ih = de.de_ih;
102 store_ih(&tmp_ih, ih);
103
104 /* we must have found item, that is item of this directory, */
105 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
106 "vs-9000: found item %h does not match to dir we readdir %K",
107 ih, &pos_key);
108 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
109 "vs-9005 item_num == %d, item amount == %d",
110 item_num, B_NR_ITEMS(bh));
111
112 /* and entry must be not more than number of entries in the item */
113 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
114 "vs-9010: entry number is too big %d (%d)",
115 entry_num, I_ENTRY_COUNT(ih));
116
117 if (search_res == POSITION_FOUND
118 || entry_num < I_ENTRY_COUNT(ih)) {
119 /* go through all entries in the directory item beginning from the entry, that has been found */
120 struct reiserfs_de_head *deh =
121 B_I_DEH(bh, ih) + entry_num;
122
123 for (; entry_num < I_ENTRY_COUNT(ih);
124 entry_num++, deh++) {
125 int d_reclen;
126 char *d_name;
bd4c625c
LT
127 ino_t d_ino;
128
129 if (!de_visible(deh))
130 /* it is hidden entry */
131 continue;
132 d_reclen = entry_length(bh, ih, entry_num);
133 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
80eb68d2
LW
134
135 if (d_reclen <= 0 ||
136 d_name + d_reclen > bh->b_data + bh->b_size) {
137 /* There is corrupted data in entry,
138 * We'd better stop here */
139 pathrelse(&path_to_entry);
140 ret = -EIO;
141 goto out;
142 }
143
bd4c625c
LT
144 if (!d_name[d_reclen - 1])
145 d_reclen = strlen(d_name);
146
147 if (d_reclen >
148 REISERFS_MAX_NAME(inode->i_sb->
149 s_blocksize)) {
150 /* too big to send back to VFS */
151 continue;
152 }
153
154 /* Ignore the .reiserfs_priv entry */
99ce4169 155 if (is_privroot_deh(inode, deh))
bd4c625c 156 continue;
bd4c625c 157
4acf381e 158 ctx->pos = deh_offset(deh);
bd4c625c
LT
159 d_ino = deh_objectid(deh);
160 if (d_reclen <= 32) {
161 local_buf = small_buf;
162 } else {
d739b42b
PE
163 local_buf = kmalloc(d_reclen,
164 GFP_NOFS);
bd4c625c
LT
165 if (!local_buf) {
166 pathrelse(&path_to_entry);
167 ret = -ENOMEM;
168 goto out;
169 }
170 if (item_moved(&tmp_ih, &path_to_entry)) {
d739b42b 171 kfree(local_buf);
bd4c625c
LT
172 goto research;
173 }
174 }
175 // Note, that we copy name to user space via temporary
176 // buffer (local_buf) because filldir will block if
177 // user space buffer is swapped out. At that time
178 // entry can move to somewhere else
179 memcpy(local_buf, d_name, d_reclen);
8ebc4232
FW
180
181 /*
182 * Since filldir might sleep, we can release
183 * the write lock here for other waiters
184 */
278f6679 185 depth = reiserfs_write_unlock_nested(inode->i_sb);
4acf381e
AV
186 if (!dir_emit
187 (ctx, local_buf, d_reclen, d_ino,
188 DT_UNKNOWN)) {
278f6679 189 reiserfs_write_lock_nested(inode->i_sb, depth);
bd4c625c 190 if (local_buf != small_buf) {
d739b42b 191 kfree(local_buf);
bd4c625c
LT
192 }
193 goto end;
194 }
278f6679 195 reiserfs_write_lock_nested(inode->i_sb, depth);
bd4c625c 196 if (local_buf != small_buf) {
d739b42b 197 kfree(local_buf);
bd4c625c
LT
198 }
199 // next entry should be looked for with such offset
200 next_pos = deh_offset(deh) + 1;
201
202 if (item_moved(&tmp_ih, &path_to_entry)) {
0bdc7acb
JM
203 set_cpu_key_k_offset(&pos_key,
204 next_pos);
bd4c625c
LT
205 goto research;
206 }
207 } /* for */
1da177e4
LT
208 }
209
bd4c625c
LT
210 if (item_num != B_NR_ITEMS(bh) - 1)
211 // end of directory has been reached
212 goto end;
213
214 /* item we went through is last item of node. Using right
215 delimiting key check is it directory end */
216 rkey = get_rkey(&path_to_entry, inode->i_sb);
217 if (!comp_le_keys(rkey, &MIN_KEY)) {
218 /* set pos_key to key, that is the smallest and greater
219 that key of the last entry in the item */
220 set_cpu_key_k_offset(&pos_key, next_pos);
221 continue;
222 }
1da177e4 223
bd4c625c
LT
224 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
225 // end of directory has been reached
226 goto end;
1da177e4 227 }
bd4c625c
LT
228
229 /* directory continues in the right neighboring block */
230 set_cpu_key_k_offset(&pos_key,
231 le_key_k_offset(KEY_FORMAT_3_5, rkey));
232
233 } /* while */
234
a41f1a47 235end:
4acf381e 236 ctx->pos = next_pos;
bd4c625c
LT
237 pathrelse(&path_to_entry);
238 reiserfs_check_path(&path_to_entry);
a41f1a47 239out:
bd4c625c
LT
240 reiserfs_write_unlock(inode->i_sb);
241 return ret;
1da177e4
LT
242}
243
4acf381e 244static int reiserfs_readdir(struct file *file, struct dir_context *ctx)
a41f1a47 245{
cd62cdae 246 return reiserfs_readdir_inode(file_inode(file), ctx);
a41f1a47
JM
247}
248
1da177e4
LT
249/* compose directory item containing "." and ".." entries (entries are
250 not aligned to 4 byte boundary) */
251/* the last four params are LE */
bd4c625c
LT
252void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
253 __le32 par_dirid, __le32 par_objid)
1da177e4 254{
bd4c625c
LT
255 struct reiserfs_de_head *deh;
256
257 memset(body, 0, EMPTY_DIR_SIZE_V1);
258 deh = (struct reiserfs_de_head *)body;
259
260 /* direntry header of "." */
261 put_deh_offset(&(deh[0]), DOT_OFFSET);
262 /* these two are from make_le_item_head, and are are LE */
263 deh[0].deh_dir_id = dirid;
264 deh[0].deh_objectid = objid;
265 deh[0].deh_state = 0; /* Endian safe if 0 */
266 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
267 mark_de_visible(&(deh[0]));
268
269 /* direntry header of ".." */
270 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
271 /* key of ".." for the root directory */
272 /* these two are from the inode, and are are LE */
273 deh[1].deh_dir_id = par_dirid;
274 deh[1].deh_objectid = par_objid;
275 deh[1].deh_state = 0; /* Endian safe if 0 */
276 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
277 mark_de_visible(&(deh[1]));
278
279 /* copy ".." and "." */
280 memcpy(body + deh_location(&(deh[0])), ".", 1);
281 memcpy(body + deh_location(&(deh[1])), "..", 2);
1da177e4
LT
282}
283
284/* compose directory item containing "." and ".." entries */
bd4c625c
LT
285void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
286 __le32 par_dirid, __le32 par_objid)
1da177e4 287{
bd4c625c
LT
288 struct reiserfs_de_head *deh;
289
290 memset(body, 0, EMPTY_DIR_SIZE);
291 deh = (struct reiserfs_de_head *)body;
292
293 /* direntry header of "." */
294 put_deh_offset(&(deh[0]), DOT_OFFSET);
295 /* these two are from make_le_item_head, and are are LE */
296 deh[0].deh_dir_id = dirid;
297 deh[0].deh_objectid = objid;
298 deh[0].deh_state = 0; /* Endian safe if 0 */
299 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
300 mark_de_visible(&(deh[0]));
301
302 /* direntry header of ".." */
303 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
304 /* key of ".." for the root directory */
305 /* these two are from the inode, and are are LE */
306 deh[1].deh_dir_id = par_dirid;
307 deh[1].deh_objectid = par_objid;
308 deh[1].deh_state = 0; /* Endian safe if 0 */
309 put_deh_location(&(deh[1]),
310 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
311 mark_de_visible(&(deh[1]));
312
313 /* copy ".." and "." */
314 memcpy(body + deh_location(&(deh[0])), ".", 1);
315 memcpy(body + deh_location(&(deh[1])), "..", 2);
1da177e4 316}