Blame view

fs/reiserfs/dir.c 9.05 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
  #include <linux/string.h>
  #include <linux/errno.h>
  #include <linux/fs.h>
  #include <linux/reiserfs_fs.h>
  #include <linux/stat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
  #include <linux/buffer_head.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
10
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
  #include <asm/uaccess.h>
5a1b63914   David Woodhouse   Missing 'const' f...
12
  extern const struct reiserfs_key MIN_KEY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13

bd4c625c0   Linus Torvalds   reiserfs: run scr...
14
  static int reiserfs_readdir(struct file *, void *, filldir_t);
02c24a821   Josef Bacik   fs: push i_mutex ...
15
16
  static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
  			      int datasync);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17

4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
18
  const struct file_operations reiserfs_dir_operations = {
ca572727d   jan Blunck   fs/: do not fallb...
19
  	.llseek = generic_file_llseek,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
20
21
22
  	.read = generic_read_dir,
  	.readdir = reiserfs_readdir,
  	.fsync = reiserfs_dir_fsync,
205cb37b8   Frederic Weisbecker   kill-the-bkl/reis...
23
  	.unlocked_ioctl = reiserfs_ioctl,
52b499c43   David Howells   [PATCH] BLOCK: Mo...
24
25
26
  #ifdef CONFIG_COMPAT
  	.compat_ioctl = reiserfs_compat_ioctl,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  };
02c24a821   Josef Bacik   fs: push i_mutex ...
28
29
  static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
  			      int datasync)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
30
  {
7ea808591   Christoph Hellwig   drop unused dentr...
31
  	struct inode *inode = filp->f_mapping->host;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
32
  	int err;
02c24a821   Josef Bacik   fs: push i_mutex ...
33
34
35
36
37
38
  
  	err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  	if (err)
  		return err;
  
  	mutex_lock(&inode->i_mutex);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
39
40
41
  	reiserfs_write_lock(inode->i_sb);
  	err = reiserfs_commit_for_inode(inode);
  	reiserfs_write_unlock(inode->i_sb);
02c24a821   Josef Bacik   fs: push i_mutex ...
42
  	mutex_unlock(&inode->i_mutex);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
43
44
45
  	if (err < 0)
  		return err;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  #define store_ih(where,what) copy_item_head (where, what)
677c9b2e3   Jeff Mahoney   reiserfs: remove ...
48
49
50
  static inline bool is_privroot_deh(struct dentry *dir,
  				   struct reiserfs_de_head *deh)
  {
677c9b2e3   Jeff Mahoney   reiserfs: remove ...
51
  	struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
73422811d   Jeff Mahoney   reiserfs: allow e...
52
53
  	return (dir == dir->d_parent && privroot->d_inode &&
  	        deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
677c9b2e3   Jeff Mahoney   reiserfs: remove ...
54
  }
a41f1a471   Jeff Mahoney   reiserfs: use gen...
55
56
  int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
  			   filldir_t filldir, loff_t *pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
58
  	struct inode *inode = dentry->d_inode;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  	struct cpu_key pos_key;	/* key of current position in the directory (key of directory entry) */
  	INITIALIZE_PATH(path_to_entry);
  	struct buffer_head *bh;
  	int item_num, entry_num;
  	const struct reiserfs_key *rkey;
  	struct item_head *ih, tmp_ih;
  	int search_res;
  	char *local_buf;
  	loff_t next_pos;
  	char small_buf[32];	/* avoid kmalloc if we can */
  	struct reiserfs_dir_entry de;
  	int ret = 0;
  
  	reiserfs_write_lock(inode->i_sb);
  
  	reiserfs_check_lock_depth(inode->i_sb, "readdir");
  
  	/* form key for search the next directory entry using f_pos field of
  	   file structure */
a41f1a471   Jeff Mahoney   reiserfs: use gen...
78
  	make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
79
  	next_pos = cpu_key_k_offset(&pos_key);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
80
81
82
83
84
85
86
87
88
89
90
  	path_to_entry.reada = PATH_READA;
  	while (1) {
  	      research:
  		/* search the directory item, containing entry with specified key */
  		search_res =
  		    search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  					&de);
  		if (search_res == IO_ERROR) {
  			// FIXME: we could just skip part of directory which could
  			// not be read
  			ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  		}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  		entry_num = de.de_entry_num;
  		bh = de.de_bh;
  		item_num = de.de_item_num;
  		ih = de.de_ih;
  		store_ih(&tmp_ih, ih);
  
  		/* we must have found item, that is item of this directory, */
  		RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  		       "vs-9000: found item %h does not match to dir we readdir %K",
  		       ih, &pos_key);
  		RFALSE(item_num > B_NR_ITEMS(bh) - 1,
  		       "vs-9005 item_num == %d, item amount == %d",
  		       item_num, B_NR_ITEMS(bh));
  
  		/* and entry must be not more than number of entries in the item */
  		RFALSE(I_ENTRY_COUNT(ih) < entry_num,
  		       "vs-9010: entry number is too big %d (%d)",
  		       entry_num, I_ENTRY_COUNT(ih));
  
  		if (search_res == POSITION_FOUND
  		    || entry_num < I_ENTRY_COUNT(ih)) {
  			/* go through all entries in the directory item beginning from the entry, that has been found */
  			struct reiserfs_de_head *deh =
  			    B_I_DEH(bh, ih) + entry_num;
  
  			for (; entry_num < I_ENTRY_COUNT(ih);
  			     entry_num++, deh++) {
  				int d_reclen;
  				char *d_name;
  				off_t d_off;
  				ino_t d_ino;
  
  				if (!de_visible(deh))
  					/* it is hidden entry */
  					continue;
  				d_reclen = entry_length(bh, ih, entry_num);
  				d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
80eb68d23   Lepton Wu   reiserfs: fix ker...
130
131
132
133
134
135
136
137
138
  
  				if (d_reclen <= 0 ||
  				    d_name + d_reclen > bh->b_data + bh->b_size) {
  					/* There is corrupted data in entry,
  					 * We'd better stop here */
  					pathrelse(&path_to_entry);
  					ret = -EIO;
  					goto out;
  				}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
139
140
141
142
143
144
145
146
147
148
149
  				if (!d_name[d_reclen - 1])
  					d_reclen = strlen(d_name);
  
  				if (d_reclen >
  				    REISERFS_MAX_NAME(inode->i_sb->
  						      s_blocksize)) {
  					/* too big to send back to VFS */
  					continue;
  				}
  
  				/* Ignore the .reiserfs_priv entry */
677c9b2e3   Jeff Mahoney   reiserfs: remove ...
150
  				if (is_privroot_deh(dentry, deh))
bd4c625c0   Linus Torvalds   reiserfs: run scr...
151
  					continue;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
152
153
  
  				d_off = deh_offset(deh);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
154
  				*pos = d_off;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
155
156
157
158
  				d_ino = deh_objectid(deh);
  				if (d_reclen <= 32) {
  					local_buf = small_buf;
  				} else {
d739b42b8   Pekka Enberg   [PATCH] reiserfs:...
159
160
  					local_buf = kmalloc(d_reclen,
  							    GFP_NOFS);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
161
162
163
164
165
166
  					if (!local_buf) {
  						pathrelse(&path_to_entry);
  						ret = -ENOMEM;
  						goto out;
  					}
  					if (item_moved(&tmp_ih, &path_to_entry)) {
d739b42b8   Pekka Enberg   [PATCH] reiserfs:...
167
  						kfree(local_buf);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
168
169
170
171
172
173
174
175
  						goto research;
  					}
  				}
  				// Note, that we copy name to user space via temporary
  				// buffer (local_buf) because filldir will block if
  				// user space buffer is swapped out. At that time
  				// entry can move to somewhere else
  				memcpy(local_buf, d_name, d_reclen);
8ebc42323   Frederic Weisbecker   reiserfs: kill-th...
176
177
178
179
180
181
  
  				/*
  				 * Since filldir might sleep, we can release
  				 * the write lock here for other waiters
  				 */
  				reiserfs_write_unlock(inode->i_sb);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
182
183
184
  				if (filldir
  				    (dirent, local_buf, d_reclen, d_off, d_ino,
  				     DT_UNKNOWN) < 0) {
8ebc42323   Frederic Weisbecker   reiserfs: kill-th...
185
  					reiserfs_write_lock(inode->i_sb);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
186
  					if (local_buf != small_buf) {
d739b42b8   Pekka Enberg   [PATCH] reiserfs:...
187
  						kfree(local_buf);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
188
189
190
  					}
  					goto end;
  				}
8ebc42323   Frederic Weisbecker   reiserfs: kill-th...
191
  				reiserfs_write_lock(inode->i_sb);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
192
  				if (local_buf != small_buf) {
d739b42b8   Pekka Enberg   [PATCH] reiserfs:...
193
  					kfree(local_buf);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
194
195
196
197
198
199
200
201
  				}
  				// next entry should be looked for with such offset
  				next_pos = deh_offset(deh) + 1;
  
  				if (item_moved(&tmp_ih, &path_to_entry)) {
  					goto research;
  				}
  			}	/* for */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
  		}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
203
204
205
206
207
208
209
210
211
212
213
214
215
  		if (item_num != B_NR_ITEMS(bh) - 1)
  			// end of directory has been reached
  			goto end;
  
  		/* item we went through is last item of node. Using right
  		   delimiting key check is it directory end */
  		rkey = get_rkey(&path_to_entry, inode->i_sb);
  		if (!comp_le_keys(rkey, &MIN_KEY)) {
  			/* set pos_key to key, that is the smallest and greater
  			   that key of the last entry in the item */
  			set_cpu_key_k_offset(&pos_key, next_pos);
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216

bd4c625c0   Linus Torvalds   reiserfs: run scr...
217
218
219
  		if (COMP_SHORT_KEYS(rkey, &pos_key)) {
  			// end of directory has been reached
  			goto end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  		}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
221
222
223
224
225
226
  
  		/* directory continues in the right neighboring block */
  		set_cpu_key_k_offset(&pos_key,
  				     le_key_k_offset(KEY_FORMAT_3_5, rkey));
  
  	}			/* while */
a41f1a471   Jeff Mahoney   reiserfs: use gen...
227
228
  end:
  	*pos = next_pos;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
229
230
  	pathrelse(&path_to_entry);
  	reiserfs_check_path(&path_to_entry);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
231
  out:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
232
233
  	reiserfs_write_unlock(inode->i_sb);
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  }
a41f1a471   Jeff Mahoney   reiserfs: use gen...
235
236
237
238
239
  static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  {
  	struct dentry *dentry = file->f_path.dentry;
  	return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
  /* compose directory item containing "." and ".." entries (entries are
     not aligned to 4 byte boundary) */
  /* the last four params are LE */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
243
244
  void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
  			    __le32 par_dirid, __le32 par_objid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  	struct reiserfs_de_head *deh;
  
  	memset(body, 0, EMPTY_DIR_SIZE_V1);
  	deh = (struct reiserfs_de_head *)body;
  
  	/* direntry header of "." */
  	put_deh_offset(&(deh[0]), DOT_OFFSET);
  	/* these two are from make_le_item_head, and are are LE */
  	deh[0].deh_dir_id = dirid;
  	deh[0].deh_objectid = objid;
  	deh[0].deh_state = 0;	/* Endian safe if 0 */
  	put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
  	mark_de_visible(&(deh[0]));
  
  	/* direntry header of ".." */
  	put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
  	/* key of ".." for the root directory */
  	/* these two are from the inode, and are are LE */
  	deh[1].deh_dir_id = par_dirid;
  	deh[1].deh_objectid = par_objid;
  	deh[1].deh_state = 0;	/* Endian safe if 0 */
  	put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
  	mark_de_visible(&(deh[1]));
  
  	/* copy ".." and "." */
  	memcpy(body + deh_location(&(deh[0])), ".", 1);
  	memcpy(body + deh_location(&(deh[1])), "..", 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
275
  }
  
  /* compose directory item containing "." and ".." entries */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
276
277
  void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
  			 __le32 par_dirid, __le32 par_objid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  	struct reiserfs_de_head *deh;
  
  	memset(body, 0, EMPTY_DIR_SIZE);
  	deh = (struct reiserfs_de_head *)body;
  
  	/* direntry header of "." */
  	put_deh_offset(&(deh[0]), DOT_OFFSET);
  	/* these two are from make_le_item_head, and are are LE */
  	deh[0].deh_dir_id = dirid;
  	deh[0].deh_objectid = objid;
  	deh[0].deh_state = 0;	/* Endian safe if 0 */
  	put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
  	mark_de_visible(&(deh[0]));
  
  	/* direntry header of ".." */
  	put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
  	/* key of ".." for the root directory */
  	/* these two are from the inode, and are are LE */
  	deh[1].deh_dir_id = par_dirid;
  	deh[1].deh_objectid = par_objid;
  	deh[1].deh_state = 0;	/* Endian safe if 0 */
  	put_deh_location(&(deh[1]),
  			 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
  	mark_de_visible(&(deh[1]));
  
  	/* copy ".." and "." */
  	memcpy(body + deh_location(&(deh[0])), ".", 1);
  	memcpy(body + deh_location(&(deh[1])), "..", 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
  }