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