blob: 07930449a9583ebee29e5781c3e4dbf9f609aa6f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/buffer_head.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/uaccess.h>
13
David Woodhouse5a1b6392007-05-23 13:57:52 -070014extern const 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,
Frederic Weisbecker205cb372009-10-14 23:22:17 +020024 .unlocked_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
Jeff Mahoney677c9b22009-05-05 15:30:17 -040045static inline bool is_privroot_deh(struct dentry *dir,
46 struct reiserfs_de_head *deh)
47{
Jeff Mahoney677c9b22009-05-05 15:30:17 -040048 struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
Jeff Mahoney73422812009-05-10 16:05:39 -040049 return (dir == dir->d_parent && privroot->d_inode &&
50 deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
Jeff Mahoney677c9b22009-05-05 15:30:17 -040051}
52
Jeff Mahoneya41f1a42009-03-30 14:02:40 -040053int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
54 filldir_t filldir, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jeff Mahoneya41f1a42009-03-30 14:02:40 -040056 struct inode *inode = dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
58 INITIALIZE_PATH(path_to_entry);
59 struct buffer_head *bh;
60 int item_num, entry_num;
61 const struct reiserfs_key *rkey;
62 struct item_head *ih, tmp_ih;
63 int search_res;
64 char *local_buf;
65 loff_t next_pos;
66 char small_buf[32]; /* avoid kmalloc if we can */
67 struct reiserfs_dir_entry de;
68 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072 reiserfs_check_lock_depth(inode->i_sb, "readdir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070074 /* form key for search the next directory entry using f_pos field of
75 file structure */
Jeff Mahoneya41f1a42009-03-30 14:02:40 -040076 make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077 next_pos = cpu_key_k_offset(&pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079 path_to_entry.reada = PATH_READA;
80 while (1) {
81 research:
82 /* search the directory item, containing entry with specified key */
83 search_res =
84 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
85 &de);
86 if (search_res == IO_ERROR) {
87 // FIXME: we could just skip part of directory which could
88 // not be read
89 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092 entry_num = de.de_entry_num;
93 bh = de.de_bh;
94 item_num = de.de_item_num;
95 ih = de.de_ih;
96 store_ih(&tmp_ih, ih);
97
98 /* we must have found item, that is item of this directory, */
99 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
100 "vs-9000: found item %h does not match to dir we readdir %K",
101 ih, &pos_key);
102 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
103 "vs-9005 item_num == %d, item amount == %d",
104 item_num, B_NR_ITEMS(bh));
105
106 /* and entry must be not more than number of entries in the item */
107 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
108 "vs-9010: entry number is too big %d (%d)",
109 entry_num, I_ENTRY_COUNT(ih));
110
111 if (search_res == POSITION_FOUND
112 || entry_num < I_ENTRY_COUNT(ih)) {
113 /* go through all entries in the directory item beginning from the entry, that has been found */
114 struct reiserfs_de_head *deh =
115 B_I_DEH(bh, ih) + entry_num;
116
117 for (; entry_num < I_ENTRY_COUNT(ih);
118 entry_num++, deh++) {
119 int d_reclen;
120 char *d_name;
121 off_t d_off;
122 ino_t d_ino;
123
124 if (!de_visible(deh))
125 /* it is hidden entry */
126 continue;
127 d_reclen = entry_length(bh, ih, entry_num);
128 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
Lepton Wu80eb68d2007-10-16 23:29:50 -0700129
130 if (d_reclen <= 0 ||
131 d_name + d_reclen > bh->b_data + bh->b_size) {
132 /* There is corrupted data in entry,
133 * We'd better stop here */
134 pathrelse(&path_to_entry);
135 ret = -EIO;
136 goto out;
137 }
138
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 if (!d_name[d_reclen - 1])
140 d_reclen = strlen(d_name);
141
142 if (d_reclen >
143 REISERFS_MAX_NAME(inode->i_sb->
144 s_blocksize)) {
145 /* too big to send back to VFS */
146 continue;
147 }
148
149 /* Ignore the .reiserfs_priv entry */
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400150 if (is_privroot_deh(dentry, deh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 continue;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152
153 d_off = deh_offset(deh);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400154 *pos = d_off;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 d_ino = deh_objectid(deh);
156 if (d_reclen <= 32) {
157 local_buf = small_buf;
158 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800159 local_buf = kmalloc(d_reclen,
160 GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 if (!local_buf) {
162 pathrelse(&path_to_entry);
163 ret = -ENOMEM;
164 goto out;
165 }
166 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800167 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 goto research;
169 }
170 }
171 // Note, that we copy name to user space via temporary
172 // buffer (local_buf) because filldir will block if
173 // user space buffer is swapped out. At that time
174 // entry can move to somewhere else
175 memcpy(local_buf, d_name, d_reclen);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200176
177 /*
178 * Since filldir might sleep, we can release
179 * the write lock here for other waiters
180 */
181 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700182 if (filldir
183 (dirent, local_buf, d_reclen, d_off, d_ino,
184 DT_UNKNOWN) < 0) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200185 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800187 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 }
189 goto end;
190 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200191 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800193 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 }
195 // next entry should be looked for with such offset
196 next_pos = deh_offset(deh) + 1;
197
198 if (item_moved(&tmp_ih, &path_to_entry)) {
199 goto research;
200 }
201 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 if (item_num != B_NR_ITEMS(bh) - 1)
205 // end of directory has been reached
206 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 /* item we went through is last item of node. Using right
209 delimiting key check is it directory end */
210 rkey = get_rkey(&path_to_entry, inode->i_sb);
211 if (!comp_le_keys(rkey, &MIN_KEY)) {
212 /* set pos_key to key, that is the smallest and greater
213 that key of the last entry in the item */
214 set_cpu_key_k_offset(&pos_key, next_pos);
215 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
219 // end of directory has been reached
220 goto end;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 /* directory continues in the right neighboring block */
224 set_cpu_key_k_offset(&pos_key,
225 le_key_k_offset(KEY_FORMAT_3_5, rkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400229end:
230 *pos = next_pos;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 pathrelse(&path_to_entry);
232 reiserfs_check_path(&path_to_entry);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400233out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 reiserfs_write_unlock(inode->i_sb);
235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400238static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
239{
240 struct dentry *dentry = file->f_path.dentry;
241 return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/* compose directory item containing "." and ".." entries (entries are
245 not aligned to 4 byte boundary) */
246/* the last four params are LE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
248 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700250 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 memset(body, 0, EMPTY_DIR_SIZE_V1);
253 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 /* direntry header of "." */
256 put_deh_offset(&(deh[0]), DOT_OFFSET);
257 /* these two are from make_le_item_head, and are are LE */
258 deh[0].deh_dir_id = dirid;
259 deh[0].deh_objectid = objid;
260 deh[0].deh_state = 0; /* Endian safe if 0 */
261 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
262 mark_de_visible(&(deh[0]));
263
264 /* direntry header of ".." */
265 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
266 /* key of ".." for the root directory */
267 /* these two are from the inode, and are are LE */
268 deh[1].deh_dir_id = par_dirid;
269 deh[1].deh_objectid = par_objid;
270 deh[1].deh_state = 0; /* Endian safe if 0 */
271 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
272 mark_de_visible(&(deh[1]));
273
274 /* copy ".." and "." */
275 memcpy(body + deh_location(&(deh[0])), ".", 1);
276 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/* compose directory item containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
281 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700285 memset(body, 0, EMPTY_DIR_SIZE);
286 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 /* direntry header of "." */
289 put_deh_offset(&(deh[0]), DOT_OFFSET);
290 /* these two are from make_le_item_head, and are are LE */
291 deh[0].deh_dir_id = dirid;
292 deh[0].deh_objectid = objid;
293 deh[0].deh_state = 0; /* Endian safe if 0 */
294 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
295 mark_de_visible(&(deh[0]));
296
297 /* direntry header of ".." */
298 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
299 /* key of ".." for the root directory */
300 /* these two are from the inode, and are are LE */
301 deh[1].deh_dir_id = par_dirid;
302 deh[1].deh_objectid = par_objid;
303 deh[1].deh_state = 0; /* Endian safe if 0 */
304 put_deh_location(&(deh[1]),
305 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
306 mark_de_visible(&(deh[1]));
307
308 /* copy ".." and "." */
309 memcpy(body + deh_location(&(deh[0])), ".", 1);
310 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}