blob: cba70165e9c566719dc585f5fbb76c4d2b1f4915 [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>
Al Virof466c6f2012-03-17 01:16:43 -04008#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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
Al Viro4acf3812013-05-17 22:42:17 -040016static int reiserfs_readdir(struct file *, struct dir_context *);
Josef Bacik02c24a82011-07-16 20:44:56 -040017static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
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 = {
jan Blunckca572722010-05-26 14:44:53 -070021 .llseek = generic_file_llseek,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070022 .read = generic_read_dir,
Al Viro4acf3812013-05-17 22:42:17 -040023 .iterate = reiserfs_readdir,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070024 .fsync = reiserfs_dir_fsync,
Frederic Weisbecker205cb372009-10-14 23:22:17 +020025 .unlocked_ioctl = reiserfs_ioctl,
David Howells52b499c2006-08-29 19:06:18 +010026#ifdef CONFIG_COMPAT
27 .compat_ioctl = reiserfs_compat_ioctl,
28#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
Josef Bacik02c24a82011-07-16 20:44:56 -040031static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
32 int datasync)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070033{
Christoph Hellwig7ea80852010-05-26 17:53:25 +020034 struct inode *inode = filp->f_mapping->host;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070035 int err;
Josef Bacik02c24a82011-07-16 20:44:56 -040036
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);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070042 reiserfs_write_lock(inode->i_sb);
43 err = reiserfs_commit_for_inode(inode);
44 reiserfs_write_unlock(inode->i_sb);
Josef Bacik02c24a82011-07-16 20:44:56 -040045 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046 if (err < 0)
47 return err;
48 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define store_ih(where,what) copy_item_head (where, what)
52
Jeff Mahoney677c9b22009-05-05 15:30:17 -040053static inline bool is_privroot_deh(struct dentry *dir,
54 struct reiserfs_de_head *deh)
55{
Jeff Mahoney677c9b22009-05-05 15:30:17 -040056 struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
Jeff Mahoney73422812009-05-10 16:05:39 -040057 return (dir == dir->d_parent && privroot->d_inode &&
58 deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
Jeff Mahoney677c9b22009-05-05 15:30:17 -040059}
60
Al Viro4acf3812013-05-17 22:42:17 -040061int reiserfs_readdir_dentry(struct dentry *dentry, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Jeff Mahoneya41f1a42009-03-30 14:02:40 -040063 struct inode *inode = dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
65 INITIALIZE_PATH(path_to_entry);
66 struct buffer_head *bh;
67 int item_num, entry_num;
68 const struct reiserfs_key *rkey;
69 struct item_head *ih, tmp_ih;
70 int search_res;
71 char *local_buf;
72 loff_t next_pos;
73 char small_buf[32]; /* avoid kmalloc if we can */
74 struct reiserfs_dir_entry de;
75 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079 reiserfs_check_lock_depth(inode->i_sb, "readdir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 /* form key for search the next directory entry using f_pos field of
82 file structure */
Al Viro4acf3812013-05-17 22:42:17 -040083 make_cpu_key(&pos_key, inode, ctx->pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 next_pos = cpu_key_k_offset(&pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086 path_to_entry.reada = PATH_READA;
87 while (1) {
88 research:
89 /* search the directory item, containing entry with specified key */
90 search_res =
91 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
92 &de);
93 if (search_res == IO_ERROR) {
94 // FIXME: we could just skip part of directory which could
95 // not be read
96 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099 entry_num = de.de_entry_num;
100 bh = de.de_bh;
101 item_num = de.de_item_num;
102 ih = de.de_ih;
103 store_ih(&tmp_ih, ih);
104
105 /* we must have found item, that is item of this directory, */
106 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
107 "vs-9000: found item %h does not match to dir we readdir %K",
108 ih, &pos_key);
109 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
110 "vs-9005 item_num == %d, item amount == %d",
111 item_num, B_NR_ITEMS(bh));
112
113 /* and entry must be not more than number of entries in the item */
114 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
115 "vs-9010: entry number is too big %d (%d)",
116 entry_num, I_ENTRY_COUNT(ih));
117
118 if (search_res == POSITION_FOUND
119 || entry_num < I_ENTRY_COUNT(ih)) {
120 /* go through all entries in the directory item beginning from the entry, that has been found */
121 struct reiserfs_de_head *deh =
122 B_I_DEH(bh, ih) + entry_num;
123
124 for (; entry_num < I_ENTRY_COUNT(ih);
125 entry_num++, deh++) {
126 int d_reclen;
127 char *d_name;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 ino_t d_ino;
129
130 if (!de_visible(deh))
131 /* it is hidden entry */
132 continue;
133 d_reclen = entry_length(bh, ih, entry_num);
134 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
Lepton Wu80eb68d2007-10-16 23:29:50 -0700135
136 if (d_reclen <= 0 ||
137 d_name + d_reclen > bh->b_data + bh->b_size) {
138 /* There is corrupted data in entry,
139 * We'd better stop here */
140 pathrelse(&path_to_entry);
141 ret = -EIO;
142 goto out;
143 }
144
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 if (!d_name[d_reclen - 1])
146 d_reclen = strlen(d_name);
147
148 if (d_reclen >
149 REISERFS_MAX_NAME(inode->i_sb->
150 s_blocksize)) {
151 /* too big to send back to VFS */
152 continue;
153 }
154
155 /* Ignore the .reiserfs_priv entry */
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400156 if (is_privroot_deh(dentry, deh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 continue;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158
Al Viro4acf3812013-05-17 22:42:17 -0400159 ctx->pos = deh_offset(deh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700160 d_ino = deh_objectid(deh);
161 if (d_reclen <= 32) {
162 local_buf = small_buf;
163 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800164 local_buf = kmalloc(d_reclen,
165 GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166 if (!local_buf) {
167 pathrelse(&path_to_entry);
168 ret = -ENOMEM;
169 goto out;
170 }
171 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800172 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 goto research;
174 }
175 }
176 // Note, that we copy name to user space via temporary
177 // buffer (local_buf) because filldir will block if
178 // user space buffer is swapped out. At that time
179 // entry can move to somewhere else
180 memcpy(local_buf, d_name, d_reclen);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200181
182 /*
183 * Since filldir might sleep, we can release
184 * the write lock here for other waiters
185 */
186 reiserfs_write_unlock(inode->i_sb);
Al Viro4acf3812013-05-17 22:42:17 -0400187 if (!dir_emit
188 (ctx, local_buf, d_reclen, d_ino,
189 DT_UNKNOWN)) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200190 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800192 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700193 }
194 goto end;
195 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200196 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800198 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 }
200 // next entry should be looked for with such offset
201 next_pos = deh_offset(deh) + 1;
202
203 if (item_moved(&tmp_ih, &path_to_entry)) {
Jeff Mahoney0bdc7ac2013-05-31 15:07:52 -0400204 set_cpu_key_k_offset(&pos_key,
205 next_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206 goto research;
207 }
208 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700211 if (item_num != B_NR_ITEMS(bh) - 1)
212 // end of directory has been reached
213 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 /* item we went through is last item of node. Using right
216 delimiting key check is it directory end */
217 rkey = get_rkey(&path_to_entry, inode->i_sb);
218 if (!comp_le_keys(rkey, &MIN_KEY)) {
219 /* set pos_key to key, that is the smallest and greater
220 that key of the last entry in the item */
221 set_cpu_key_k_offset(&pos_key, next_pos);
222 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
226 // end of directory has been reached
227 goto end;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230 /* directory continues in the right neighboring block */
231 set_cpu_key_k_offset(&pos_key,
232 le_key_k_offset(KEY_FORMAT_3_5, rkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400236end:
Al Viro4acf3812013-05-17 22:42:17 -0400237 ctx->pos = next_pos;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700238 pathrelse(&path_to_entry);
239 reiserfs_check_path(&path_to_entry);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400240out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 reiserfs_write_unlock(inode->i_sb);
242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Al Viro4acf3812013-05-17 22:42:17 -0400245static int reiserfs_readdir(struct file *file, struct dir_context *ctx)
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400246{
Al Viro4acf3812013-05-17 22:42:17 -0400247 return reiserfs_readdir_dentry(file->f_path.dentry, ctx);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/* compose directory item containing "." and ".." entries (entries are
251 not aligned to 4 byte boundary) */
252/* the last four params are LE */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
254 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700256 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700258 memset(body, 0, EMPTY_DIR_SIZE_V1);
259 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 /* direntry header of "." */
262 put_deh_offset(&(deh[0]), DOT_OFFSET);
263 /* these two are from make_le_item_head, and are are LE */
264 deh[0].deh_dir_id = dirid;
265 deh[0].deh_objectid = objid;
266 deh[0].deh_state = 0; /* Endian safe if 0 */
267 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
268 mark_de_visible(&(deh[0]));
269
270 /* direntry header of ".." */
271 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
272 /* key of ".." for the root directory */
273 /* these two are from the inode, and are are LE */
274 deh[1].deh_dir_id = par_dirid;
275 deh[1].deh_objectid = par_objid;
276 deh[1].deh_state = 0; /* Endian safe if 0 */
277 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
278 mark_de_visible(&(deh[1]));
279
280 /* copy ".." and "." */
281 memcpy(body + deh_location(&(deh[0])), ".", 1);
282 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/* compose directory item containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700286void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
287 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700289 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 memset(body, 0, EMPTY_DIR_SIZE);
292 deh = (struct reiserfs_de_head *)body;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 /* direntry header of "." */
295 put_deh_offset(&(deh[0]), DOT_OFFSET);
296 /* these two are from make_le_item_head, and are are LE */
297 deh[0].deh_dir_id = dirid;
298 deh[0].deh_objectid = objid;
299 deh[0].deh_state = 0; /* Endian safe if 0 */
300 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
301 mark_de_visible(&(deh[0]));
302
303 /* direntry header of ".." */
304 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
305 /* key of ".." for the root directory */
306 /* these two are from the inode, and are are LE */
307 deh[1].deh_dir_id = par_dirid;
308 deh[1].deh_objectid = par_objid;
309 deh[1].deh_state = 0; /* Endian safe if 0 */
310 put_deh_location(&(deh[1]),
311 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
312 mark_de_visible(&(deh[1]));
313
314 /* copy ".." and "." */
315 memcpy(body + deh_location(&(deh[0])), ".", 1);
316 memcpy(body + deh_location(&(deh[1])), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}