blob: 0a7dc941aaf4dc7190b4ac3864765f6688b11683 [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>
Fabian Frederick17093992014-08-08 14:21:12 -070012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
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
Al Viro99ce4162013-05-17 22:45:29 -040053static inline bool is_privroot_deh(struct inode *dir, struct reiserfs_de_head *deh)
Jeff Mahoney677c9b22009-05-05 15:30:17 -040054{
Al Viro99ce4162013-05-17 22:45:29 -040055 struct dentry *privroot = REISERFS_SB(dir->i_sb)->priv_root;
56 return (privroot->d_inode &&
Jeff Mahoney73422812009-05-10 16:05:39 -040057 deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
Jeff Mahoney677c9b22009-05-05 15:30:17 -040058}
59
Al Virocd62cda2013-05-17 22:58:58 -040060int reiserfs_readdir_inode(struct inode *inode, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jeff Mahoney098297b2014-04-23 10:00:36 -040062
63 /* key of current position in the directory (key of directory entry) */
64 struct cpu_key pos_key;
65
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066 INITIALIZE_PATH(path_to_entry);
67 struct buffer_head *bh;
68 int item_num, entry_num;
69 const struct reiserfs_key *rkey;
70 struct item_head *ih, tmp_ih;
71 int search_res;
72 char *local_buf;
73 loff_t next_pos;
74 char small_buf[32]; /* avoid kmalloc if we can */
75 struct reiserfs_dir_entry de;
76 int ret = 0;
Jeff Mahoney278f6672013-08-08 17:34:46 -040077 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 reiserfs_check_lock_depth(inode->i_sb, "readdir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jeff Mahoney098297b2014-04-23 10:00:36 -040083 /*
84 * form key for search the next directory entry using
85 * f_pos field of file structure
86 */
Al Viro4acf3812013-05-17 22:42:17 -040087 make_cpu_key(&pos_key, inode, ctx->pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 next_pos = cpu_key_k_offset(&pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090 path_to_entry.reada = PATH_READA;
91 while (1) {
Jeff Mahoneycf776a72014-04-23 10:00:41 -040092research:
Jeff Mahoney098297b2014-04-23 10:00:36 -040093 /*
94 * search the directory item, containing entry with
95 * specified key
96 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 search_res =
98 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
99 &de);
100 if (search_res == IO_ERROR) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400101 /*
102 * FIXME: we could just skip part of directory
103 * which could not be read
104 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 entry_num = de.de_entry_num;
109 bh = de.de_bh;
110 item_num = de.de_item_num;
111 ih = de.de_ih;
112 store_ih(&tmp_ih, ih);
113
114 /* we must have found item, that is item of this directory, */
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400115 RFALSE(COMP_SHORT_KEYS(&ih->ih_key, &pos_key),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700116 "vs-9000: found item %h does not match to dir we readdir %K",
117 ih, &pos_key);
118 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
119 "vs-9005 item_num == %d, item amount == %d",
120 item_num, B_NR_ITEMS(bh));
121
Jeff Mahoney098297b2014-04-23 10:00:36 -0400122 /*
123 * and entry must be not more than number of entries
124 * in the item
125 */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400126 RFALSE(ih_entry_count(ih) < entry_num,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 "vs-9010: entry number is too big %d (%d)",
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400128 entry_num, ih_entry_count(ih));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700129
Jeff Mahoney098297b2014-04-23 10:00:36 -0400130 /*
131 * go through all entries in the directory item beginning
132 * from the entry, that has been found
133 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134 if (search_res == POSITION_FOUND
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400135 || entry_num < ih_entry_count(ih)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 struct reiserfs_de_head *deh =
137 B_I_DEH(bh, ih) + entry_num;
138
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400139 for (; entry_num < ih_entry_count(ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700140 entry_num++, deh++) {
141 int d_reclen;
142 char *d_name;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 ino_t d_ino;
Jeff Mahoney01d88852014-04-02 14:40:26 -0400144 loff_t cur_pos = deh_offset(deh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145
Jeff Mahoney098297b2014-04-23 10:00:36 -0400146 /* it is hidden entry */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 if (!de_visible(deh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148 continue;
149 d_reclen = entry_length(bh, ih, entry_num);
150 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
Lepton Wu80eb68d2007-10-16 23:29:50 -0700151
152 if (d_reclen <= 0 ||
153 d_name + d_reclen > bh->b_data + bh->b_size) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400154 /*
155 * There is corrupted data in entry,
156 * We'd better stop here
157 */
Lepton Wu80eb68d2007-10-16 23:29:50 -0700158 pathrelse(&path_to_entry);
159 ret = -EIO;
160 goto out;
161 }
162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700163 if (!d_name[d_reclen - 1])
164 d_reclen = strlen(d_name);
165
Jeff Mahoney098297b2014-04-23 10:00:36 -0400166 /* too big to send back to VFS */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167 if (d_reclen >
168 REISERFS_MAX_NAME(inode->i_sb->
169 s_blocksize)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 continue;
171 }
172
173 /* Ignore the .reiserfs_priv entry */
Al Viro99ce4162013-05-17 22:45:29 -0400174 if (is_privroot_deh(inode, deh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700175 continue;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176
Al Viro4acf3812013-05-17 22:42:17 -0400177 ctx->pos = deh_offset(deh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 d_ino = deh_objectid(deh);
179 if (d_reclen <= 32) {
180 local_buf = small_buf;
181 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800182 local_buf = kmalloc(d_reclen,
183 GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 if (!local_buf) {
185 pathrelse(&path_to_entry);
186 ret = -ENOMEM;
187 goto out;
188 }
189 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800190 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 goto research;
192 }
193 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400194
195 /*
196 * Note, that we copy name to user space via
197 * temporary buffer (local_buf) because
198 * filldir will block if user space buffer is
199 * swapped out. At that time entry can move to
200 * somewhere else
201 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700202 memcpy(local_buf, d_name, d_reclen);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200203
204 /*
205 * Since filldir might sleep, we can release
206 * the write lock here for other waiters
207 */
Jeff Mahoney278f6672013-08-08 17:34:46 -0400208 depth = reiserfs_write_unlock_nested(inode->i_sb);
Al Viro4acf3812013-05-17 22:42:17 -0400209 if (!dir_emit
210 (ctx, local_buf, d_reclen, d_ino,
211 DT_UNKNOWN)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -0400212 reiserfs_write_lock_nested(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700213 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800214 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 }
216 goto end;
217 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400218 reiserfs_write_lock_nested(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800220 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700221 }
Jeff Mahoney01d88852014-04-02 14:40:26 -0400222
223 /* deh_offset(deh) may be invalid now. */
224 next_pos = cur_pos + 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225
226 if (item_moved(&tmp_ih, &path_to_entry)) {
Jeff Mahoney0bdc7ac2013-05-31 15:07:52 -0400227 set_cpu_key_k_offset(&pos_key,
228 next_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 goto research;
230 }
231 } /* for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Jeff Mahoney098297b2014-04-23 10:00:36 -0400234 /* end of directory has been reached */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 if (item_num != B_NR_ITEMS(bh) - 1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700236 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jeff Mahoney098297b2014-04-23 10:00:36 -0400238 /*
239 * item we went through is last item of node. Using right
240 * delimiting key check is it directory end
241 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242 rkey = get_rkey(&path_to_entry, inode->i_sb);
243 if (!comp_le_keys(rkey, &MIN_KEY)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400244 /*
245 * set pos_key to key, that is the smallest and greater
246 * that key of the last entry in the item
247 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 set_cpu_key_k_offset(&pos_key, next_pos);
249 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jeff Mahoney098297b2014-04-23 10:00:36 -0400252 /* end of directory has been reached */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 goto end;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700257 /* directory continues in the right neighboring block */
258 set_cpu_key_k_offset(&pos_key,
259 le_key_k_offset(KEY_FORMAT_3_5, rkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400263end:
Al Viro4acf3812013-05-17 22:42:17 -0400264 ctx->pos = next_pos;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700265 pathrelse(&path_to_entry);
266 reiserfs_check_path(&path_to_entry);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400267out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700268 reiserfs_write_unlock(inode->i_sb);
269 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Al Viro4acf3812013-05-17 22:42:17 -0400272static int reiserfs_readdir(struct file *file, struct dir_context *ctx)
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400273{
Al Virocd62cda2013-05-17 22:58:58 -0400274 return reiserfs_readdir_inode(file_inode(file), ctx);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400275}
276
Jeff Mahoney098297b2014-04-23 10:00:36 -0400277/*
278 * compose directory item containing "." and ".." entries (entries are
279 * not aligned to 4 byte boundary)
280 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
282 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Jeff Mahoney16da1672014-04-23 10:00:43 -0400284 struct reiserfs_de_head *dot, *dotdot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700286 memset(body, 0, EMPTY_DIR_SIZE_V1);
Jeff Mahoney16da1672014-04-23 10:00:43 -0400287 dot = (struct reiserfs_de_head *)body;
288 dotdot = dot + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 /* direntry header of "." */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400291 put_deh_offset(dot, DOT_OFFSET);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 /* these two are from make_le_item_head, and are are LE */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400293 dot->deh_dir_id = dirid;
294 dot->deh_objectid = objid;
295 dot->deh_state = 0; /* Endian safe if 0 */
296 put_deh_location(dot, EMPTY_DIR_SIZE_V1 - strlen("."));
297 mark_de_visible(dot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298
299 /* direntry header of ".." */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400300 put_deh_offset(dotdot, DOT_DOT_OFFSET);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301 /* key of ".." for the root directory */
302 /* these two are from the inode, and are are LE */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400303 dotdot->deh_dir_id = par_dirid;
304 dotdot->deh_objectid = par_objid;
305 dotdot->deh_state = 0; /* Endian safe if 0 */
306 put_deh_location(dotdot, deh_location(dot) - strlen(".."));
307 mark_de_visible(dotdot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308
309 /* copy ".." and "." */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400310 memcpy(body + deh_location(dot), ".", 1);
311 memcpy(body + deh_location(dotdot), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/* compose directory item containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700315void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
316 __le32 par_dirid, __le32 par_objid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Jeff Mahoney16da1672014-04-23 10:00:43 -0400318 struct reiserfs_de_head *dot, *dotdot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320 memset(body, 0, EMPTY_DIR_SIZE);
Jeff Mahoney16da1672014-04-23 10:00:43 -0400321 dot = (struct reiserfs_de_head *)body;
322 dotdot = dot + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700324 /* direntry header of "." */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400325 put_deh_offset(dot, DOT_OFFSET);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700326 /* these two are from make_le_item_head, and are are LE */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400327 dot->deh_dir_id = dirid;
328 dot->deh_objectid = objid;
329 dot->deh_state = 0; /* Endian safe if 0 */
330 put_deh_location(dot, EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
331 mark_de_visible(dot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700332
333 /* direntry header of ".." */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400334 put_deh_offset(dotdot, DOT_DOT_OFFSET);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 /* key of ".." for the root directory */
336 /* these two are from the inode, and are are LE */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400337 dotdot->deh_dir_id = par_dirid;
338 dotdot->deh_objectid = par_objid;
339 dotdot->deh_state = 0; /* Endian safe if 0 */
340 put_deh_location(dotdot, deh_location(dot) - ROUND_UP(strlen("..")));
341 mark_de_visible(dotdot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342
343 /* copy ".." and "." */
Jeff Mahoney16da1672014-04-23 10:00:43 -0400344 memcpy(body + deh_location(dot), ".", 1);
345 memcpy(body + deh_location(dotdot), "..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}