blob: 284f7852de8b00e53211e40822d935acef0d9a67 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 *
4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
5 *
6 * Trivial Changes:
7 * Rights granted to Hans Reiser to redistribute under other terms providing
8 * he accepts all liability including but not limited to patent, fitness
9 * for purpose, and direct or indirect claims arising from failure to perform.
10 *
11 * NO WARRANTY
12 */
13
14#include <linux/config.h>
15#include <linux/time.h>
16#include <linux/bitops.h>
17#include <linux/reiserfs_fs.h>
18#include <linux/reiserfs_acl.h>
19#include <linux/reiserfs_xattr.h>
20#include <linux/smp_lock.h>
21#include <linux/quotaops.h>
22
23#define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { i->i_nlink++; if (i->i_nlink >= REISERFS_LINK_MAX) i->i_nlink=1; }
24#define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) i->i_nlink--;
25
26// directory item contains array of entry headers. This performs
27// binary search through that array
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070028static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070030 struct item_head *ih = de->de_ih;
31 struct reiserfs_de_head *deh = de->de_deh;
32 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070034 lbound = 0;
35 rbound = I_ENTRY_COUNT(ih) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070037 for (j = (rbound + lbound) / 2; lbound <= rbound;
38 j = (rbound + lbound) / 2) {
39 if (off < deh_offset(deh + j)) {
40 rbound = j - 1;
41 continue;
42 }
43 if (off > deh_offset(deh + j)) {
44 lbound = j + 1;
45 continue;
46 }
47 // this is not name found, but matched third key component
48 de->de_entry_num = j;
49 return NAME_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 de->de_entry_num = lbound;
53 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056// comment? maybe something like set de to point to what the path points to?
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057static inline void set_de_item_location(struct reiserfs_dir_entry *de,
58 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 de->de_bh = get_last_bh(path);
61 de->de_ih = get_ih(path);
62 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
63 de->de_item_num = PATH_LAST_POSITION(path);
64}
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66// de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070069 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 if (de->de_entry_num >= ih_entry_count(de->de_ih))
72 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070074 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
75 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
76 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh);
77 if (de->de_name[de->de_namelen - 1] == 0)
78 de->de_namelen = strlen(de->de_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081// what entry points to
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082static inline void set_de_object_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 if (de->de_entry_num >= ih_entry_count(de->de_ih))
85 BUG();
86 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num]));
87 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070094 if (de->de_entry_num >= ih_entry_count(de->de_ih))
95 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 /* store key of the found entry */
98 de->de_entry_key.version = KEY_FORMAT_3_5;
99 de->de_entry_key.on_disk_key.k_dir_id =
100 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
101 de->de_entry_key.on_disk_key.k_objectid =
102 le32_to_cpu(de->de_ih->ih_key.k_objectid);
103 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh));
104 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* We assign a key to each directory item, and place multiple entries
108in a single directory item. A directory item has a key equal to the
109key of the first directory entry in it.
110
111This function first calls search_by_key, then, if item whose first
112entry matches is not found it looks for the entry inside directory
113item found by search_by_key. Fills the path to the entry, and to the
114entry position in the item
115
116*/
117
118/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700119int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
120 struct path *path, struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700122 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700124 retval = search_item(sb, key, path);
125 switch (retval) {
126 case ITEM_NOT_FOUND:
127 if (!PATH_LAST_POSITION(path)) {
128 reiserfs_warning(sb,
129 "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
130 pathrelse(path);
131 return IO_ERROR;
132 }
133 PATH_LAST_POSITION(path)--;
134
135 case ITEM_FOUND:
136 break;
137
138 case IO_ERROR:
139 return retval;
140
141 default:
142 pathrelse(path);
143 reiserfs_warning(sb,
144 "vs-7002: search_by_entry_key: no path to here");
145 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148 set_de_item_location(de, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 if (!is_direntry_le_ih(de->de_ih) ||
152 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
153 print_block(de->de_bh, 0, -1, -1);
154 reiserfs_panic(sb,
155 "vs-7005: search_by_entry_key: found item %h is not directory item or "
156 "does not belong to the same directory as key %K",
157 de->de_ih, key);
158 }
159#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 /* binary search in directory item by third componen t of the
162 key. sets de->de_entry_num of de */
163 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
164 path->pos_in_item = de->de_entry_num;
165 if (retval != NAME_NOT_FOUND) {
166 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
167 set_de_name_and_namelen(de);
168 set_de_object_key(de);
169 }
170 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
174
175/* The third component is hashed, and you can choose from more than
176 one hash function. Per directory hashes are not yet implemented
177 but are thought about. This function should be moved to hashes.c
178 Jedi, please do so. -Hans */
179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180static __u32 get_third_component(struct super_block *s,
181 const char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183 __u32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 if (!len || (len == 1 && name[0] == '.'))
186 return DOT_OFFSET;
187 if (len == 2 && name[0] == '.' && name[1] == '.')
188 return DOT_DOT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 res = REISERFS_SB(s)->s_hash_function(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 // take bits from 7-th to 30-th including both bounds
193 res = GET_HASH_VALUE(res);
194 if (res == 0)
195 // needed to have no names before "." and ".." those have hash
196 // value == 0 and generation conters 1 and 2 accordingly
197 res = 128;
198 return res + MAX_GENERATION_NUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201static int reiserfs_match(struct reiserfs_dir_entry *de,
202 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 int retval = NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206 if ((namelen == de->de_namelen) &&
207 !memcmp(de->de_name, name, de->de_namelen))
208 retval =
209 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
210 NAME_FOUND_INVISIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
216
217 /* used when hash collisions exist */
218
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219static int linear_search_in_dir_item(struct cpu_key *key,
220 struct reiserfs_dir_entry *de,
221 const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 struct reiserfs_de_head *deh = de->de_deh;
224 int retval;
225 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227 i = de->de_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 if (i == I_ENTRY_COUNT(de->de_ih) ||
230 GET_HASH_VALUE(deh_offset(deh + i)) !=
231 GET_HASH_VALUE(cpu_key_k_offset(key))) {
232 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
236 "vs-7010: array of entry headers not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700238 deh += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 for (; i >= 0; i--, deh--) {
241 if (GET_HASH_VALUE(deh_offset(deh)) !=
242 GET_HASH_VALUE(cpu_key_k_offset(key))) {
243 // hash value does not match, no need to check whole name
244 return NAME_NOT_FOUND;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247 /* mark, that this generation number is used */
248 if (de->de_gen_number_bit_string)
249 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500250 de->de_gen_number_bit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 // calculate pointer to name and namelen
253 de->de_entry_num = i;
254 set_de_name_and_namelen(de);
255
256 if ((retval =
257 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
258 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
259
260 // key of pointed object
261 set_de_object_key(de);
262
263 store_de_entry_key(de);
264
265 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
266 return retval;
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
271 /* we have reached left most entry in the node. In common we
272 have to go to the left neighbor, but if generation counter
273 is 0 already, we know for sure, that there is no name with
274 the same hash value */
275 // FIXME: this work correctly only because hash value can not
276 // be 0. Btw, in case of Yura's hash it is probably possible,
277 // so, this is a bug
278 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 RFALSE(de->de_item_num,
281 "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283 return GOTO_PREVIOUS_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286// may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
287// FIXME: should add something like IOERROR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
289 struct path *path_to_entry,
290 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 struct cpu_key key_to_search;
293 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
296 return NAME_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 /* we will search for this key in the tree */
299 make_cpu_key(&key_to_search, dir,
300 get_third_component(dir->i_sb, name, namelen),
301 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700303 while (1) {
304 retval =
305 search_by_entry_key(dir->i_sb, &key_to_search,
306 path_to_entry, de);
307 if (retval == IO_ERROR) {
308 reiserfs_warning(dir->i_sb, "zam-7001: io error in %s",
309 __FUNCTION__);
310 return IO_ERROR;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 /* compare names for all entries having given hash value */
314 retval =
315 linear_search_in_dir_item(&key_to_search, de, name,
316 namelen);
317 if (retval != GOTO_PREVIOUS_ITEM) {
318 /* there is no need to scan directory anymore. Given entry found or does not exist */
319 path_to_entry->pos_in_item = de->de_entry_num;
320 return retval;
321 }
322
323 /* there is left neighboring item of this directory and given entry can be there */
324 set_cpu_key_k_offset(&key_to_search,
325 le_ih_k_offset(de->de_ih) - 1);
326 pathrelse(path_to_entry);
327
328 } /* while (1) */
329}
330
331static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
332 struct nameidata *nd)
333{
334 int retval;
335 struct inode *inode = NULL;
336 struct reiserfs_dir_entry de;
337 INITIALIZE_PATH(path_to_entry);
338
339 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
340 return ERR_PTR(-ENAMETOOLONG);
341
342 reiserfs_write_lock(dir->i_sb);
343 de.de_gen_number_bit_string = NULL;
344 retval =
345 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
346 &path_to_entry, &de);
347 pathrelse(&path_to_entry);
348 if (retval == NAME_FOUND) {
349 /* Hide the .reiserfs_priv directory */
350 if (reiserfs_xattrs(dir->i_sb) &&
351 !old_format_only(dir->i_sb) &&
352 REISERFS_SB(dir->i_sb)->priv_root &&
353 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
354 de.de_objectid ==
355 le32_to_cpu(INODE_PKEY
356 (REISERFS_SB(dir->i_sb)->priv_root->d_inode)->
357 k_objectid)) {
358 reiserfs_write_unlock(dir->i_sb);
359 return ERR_PTR(-EACCES);
360 }
361
362 inode =
363 reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
364 if (!inode || IS_ERR(inode)) {
365 reiserfs_write_unlock(dir->i_sb);
366 return ERR_PTR(-EACCES);
367 }
368
369 /* Propogate the priv_object flag so we know we're in the priv tree */
370 if (is_reiserfs_priv_object(dir))
371 reiserfs_mark_inode_private(inode);
372 }
373 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (retval == IO_ERROR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Pekka Enbergd9975d62006-01-14 13:21:09 -0800378 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382** looks up the dentry of the parent directory for child.
383** taken from ext2_get_parent
384*/
385struct dentry *reiserfs_get_parent(struct dentry *child)
386{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 int retval;
388 struct inode *inode = NULL;
389 struct reiserfs_dir_entry de;
390 INITIALIZE_PATH(path_to_entry);
391 struct dentry *parent;
392 struct inode *dir = child->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 if (dir->i_nlink == 0) {
395 return ERR_PTR(-ENOENT);
396 }
397 de.de_gen_number_bit_string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 reiserfs_write_lock(dir->i_sb);
400 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
401 pathrelse(&path_to_entry);
402 if (retval != NAME_FOUND) {
403 reiserfs_write_unlock(dir->i_sb);
404 return ERR_PTR(-ENOENT);
405 }
406 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 reiserfs_write_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 if (!inode || IS_ERR(inode)) {
410 return ERR_PTR(-EACCES);
411 }
412 parent = d_alloc_anon(inode);
413 if (!parent) {
414 iput(inode);
415 parent = ERR_PTR(-ENOMEM);
416 }
417 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* add entry to the directory (entry can be hidden).
421
422insert definition of when hidden directories are used here -Hans
423
424 Does not mark dir inode dirty, do it after successesfull call to it */
425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
427 struct inode *dir, const char *name, int namelen,
428 struct inode *inode, int visible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430 struct cpu_key entry_key;
431 struct reiserfs_de_head *deh;
432 INITIALIZE_PATH(path);
433 struct reiserfs_dir_entry de;
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500434 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700435 int gen_number;
436 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
437 if we create file with short name */
438 char *buffer;
439 int buflen, paste_size;
440 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700442 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 /* cannot allow items to be added into a busy deleted directory */
445 if (!namelen)
446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
449 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451 /* each entry has unique key. compose it */
452 make_cpu_key(&entry_key, dir,
453 get_third_component(dir->i_sb, name, namelen),
454 TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 /* get memory for composing the entry */
457 buflen = DEH_SIZE + ROUND_UP(namelen);
458 if (buflen > sizeof(small_buf)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800459 buffer = kmalloc(buflen, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 if (buffer == 0)
461 return -ENOMEM;
462 } else
463 buffer = small_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465 paste_size =
466 (get_inode_sd_version(dir) ==
467 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700469 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
470 deh = (struct reiserfs_de_head *)buffer;
471 deh->deh_location = 0; /* JDM Endian safe if 0 */
472 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
473 deh->deh_state = 0; /* JDM Endian safe if 0 */
474 /* put key (ino analog) to de */
475 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
476 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 /* copy name */
479 memcpy((char *)(deh + 1), name, namelen);
480 /* padd by 0s to the 4 byte boundary */
481 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
484 mark_de_without_sd(deh);
485 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 /* find the proper place for the new entry */
488 memset(bit_string, 0, sizeof(bit_string));
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500489 de.de_gen_number_bit_string = bit_string;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
491 if (retval != NAME_NOT_FOUND) {
492 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800493 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700494 pathrelse(&path);
495
496 if (retval == IO_ERROR) {
497 return -EIO;
498 }
499
500 if (retval != NAME_FOUND) {
501 reiserfs_warning(dir->i_sb,
502 "zam-7002:%s: \"reiserfs_find_entry\" "
503 "has returned unexpected value (%d)",
504 __FUNCTION__, retval);
505 }
506
507 return -EEXIST;
508 }
509
510 gen_number =
Jeff Mahoney3af1efe2006-03-02 13:25:26 -0500511 find_first_zero_bit(bit_string,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 MAX_GENERATION_NUMBER + 1);
513 if (gen_number > MAX_GENERATION_NUMBER) {
514 /* there is no free generation number */
515 reiserfs_warning(dir->i_sb,
516 "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
517 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800518 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700519 pathrelse(&path);
520 return -EBUSY;
521 }
522 /* adjust offset of directory enrty */
523 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
524 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
525
526 /* update max-hash-collisions counter in reiserfs_sb_info */
527 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
528
529 if (gen_number != 0) { /* we need to re-search for the insertion point */
530 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
531 NAME_NOT_FOUND) {
532 reiserfs_warning(dir->i_sb,
533 "vs-7032: reiserfs_add_entry: "
534 "entry with this key (%K) already exists",
535 &entry_key);
536
537 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800538 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539 pathrelse(&path);
540 return -EBUSY;
541 }
542 }
543
544 /* perform the insertion of the entry that we have prepared */
545 retval =
546 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
547 paste_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (buffer != small_buf)
Pekka Enbergd739b422006-02-01 03:06:43 -0800549 kfree(buffer);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700550 if (retval) {
551 reiserfs_check_path(&path);
552 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 dir->i_size += paste_size;
556 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
557 if (!S_ISDIR(inode->i_mode) && visible)
558 // reiserfs_mkdir or reiserfs_rename will do that by itself
559 reiserfs_update_sd(th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561 reiserfs_check_path(&path);
562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/* quota utility function, call if you've had to abort after calling
566** new_inode_init, and have not called reiserfs_new_inode yet.
567** This should only be called on inodes that do not have stat data
568** inserted into the tree yet.
569*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570static int drop_new_inode(struct inode *inode)
571{
572 DQUOT_DROP(inode);
573 make_bad_inode(inode);
574 inode->i_flags |= S_NOQUOTA;
575 iput(inode);
576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/* utility function that does setup for reiserfs_new_inode.
580** DQUOT_INIT needs lots of credits so it's better to have it
581** outside of a transaction, so we had to pull some bits of
582** reiserfs_new_inode out into this func.
583*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700584static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700587 /* the quota init calls have to know who to charge the quota to, so
588 ** we have to set uid and gid here
589 */
590 inode->i_uid = current->fsuid;
591 inode->i_mode = mode;
Jan Kara1b0a74d2005-08-13 13:15:34 +0200592 /* Make inode invalid - just in case we are going to drop it before
593 * the initialization happens */
594 INODE_PKEY(inode)->k_objectid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 if (dir->i_mode & S_ISGID) {
597 inode->i_gid = dir->i_gid;
598 if (S_ISDIR(mode))
599 inode->i_mode |= S_ISGID;
600 } else {
601 inode->i_gid = current->fsgid;
602 }
603 DQUOT_INIT(inode);
604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
608 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700610 int retval;
611 struct inode *inode;
612 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
613 int jbegin_count =
614 JOURNAL_PER_BALANCE_CNT * 2 +
615 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
616 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
617 struct reiserfs_transaction_handle th;
618 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700620 if (!(inode = new_inode(dir->i_sb))) {
621 return -ENOMEM;
622 }
623 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700629 if (locked)
630 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700632 retval = journal_begin(&th, dir->i_sb, jbegin_count);
633 if (retval) {
634 drop_new_inode(inode);
635 goto out_failed;
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 retval =
639 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
640 inode);
641 if (retval)
642 goto out_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700644 if (locked) {
645 reiserfs_write_unlock_xattrs(dir->i_sb);
646 locked = 0;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 inode->i_op = &reiserfs_file_inode_operations;
650 inode->i_fop = &reiserfs_file_operations;
651 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653 retval =
654 reiserfs_add_entry(&th, dir, dentry->d_name.name,
655 dentry->d_name.len, inode, 1 /*visible */ );
656 if (retval) {
657 int err;
658 inode->i_nlink--;
659 reiserfs_update_sd(&th, inode);
660 err = journal_end(&th, dir->i_sb, jbegin_count);
661 if (err)
662 retval = err;
663 iput(inode);
664 goto out_failed;
665 }
666 reiserfs_update_inode_transaction(inode);
667 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 d_instantiate(dentry, inode);
670 retval = journal_end(&th, dir->i_sb, jbegin_count);
671
672 out_failed:
673 if (locked)
674 reiserfs_write_unlock_xattrs(dir->i_sb);
675 reiserfs_write_unlock(dir->i_sb);
676 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
680 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700682 int retval;
683 struct inode *inode;
684 struct reiserfs_transaction_handle th;
685 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
686 int jbegin_count =
687 JOURNAL_PER_BALANCE_CNT * 3 +
688 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
689 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
690 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700692 if (!new_valid_dev(rdev))
693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695 if (!(inode = new_inode(dir->i_sb))) {
696 return -ENOMEM;
697 }
698 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700702 reiserfs_write_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700704 if (locked)
705 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700707 retval = journal_begin(&th, dir->i_sb, jbegin_count);
708 if (retval) {
709 drop_new_inode(inode);
710 goto out_failed;
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700713 retval =
714 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
715 inode);
716 if (retval) {
717 goto out_failed;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 if (locked) {
721 reiserfs_write_unlock_xattrs(dir->i_sb);
722 locked = 0;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700725 inode->i_op = &reiserfs_special_inode_operations;
726 init_special_inode(inode, inode->i_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 //FIXME: needed for block and char devices only
729 reiserfs_update_sd(&th, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 reiserfs_update_inode_transaction(inode);
732 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 retval =
735 reiserfs_add_entry(&th, dir, dentry->d_name.name,
736 dentry->d_name.len, inode, 1 /*visible */ );
737 if (retval) {
738 int err;
739 inode->i_nlink--;
740 reiserfs_update_sd(&th, inode);
741 err = journal_end(&th, dir->i_sb, jbegin_count);
742 if (err)
743 retval = err;
744 iput(inode);
745 goto out_failed;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700748 d_instantiate(dentry, inode);
749 retval = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751 out_failed:
752 if (locked)
753 reiserfs_write_unlock_xattrs(dir->i_sb);
754 reiserfs_write_unlock(dir->i_sb);
755 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700758static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760 int retval;
761 struct inode *inode;
762 struct reiserfs_transaction_handle th;
763 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
764 int jbegin_count =
765 JOURNAL_PER_BALANCE_CNT * 3 +
766 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
767 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
768 int locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700771 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
772 REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774 mode = S_IFDIR | mode;
775 if (!(inode = new_inode(dir->i_sb))) {
776 return -ENOMEM;
777 }
778 new_inode_init(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700780 locked = reiserfs_cache_default_acl(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700782 reiserfs_write_lock(dir->i_sb);
783 if (locked)
784 reiserfs_write_lock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700786 retval = journal_begin(&th, dir->i_sb, jbegin_count);
787 if (retval) {
788 drop_new_inode(inode);
789 goto out_failed;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700792 /* inc the link count now, so another writer doesn't overflow it while
793 ** we sleep later on.
794 */
795 INC_DIR_INODE_NLINK(dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700797 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
798 old_format_only(dir->i_sb) ?
799 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
800 dentry, inode);
801 if (retval) {
802 dir->i_nlink--;
803 goto out_failed;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 if (locked) {
807 reiserfs_write_unlock_xattrs(dir->i_sb);
808 locked = 0;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700811 reiserfs_update_inode_transaction(inode);
812 reiserfs_update_inode_transaction(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814 inode->i_op = &reiserfs_dir_inode_operations;
815 inode->i_fop = &reiserfs_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700817 // note, _this_ add_entry will not update dir's stat data
818 retval =
819 reiserfs_add_entry(&th, dir, dentry->d_name.name,
820 dentry->d_name.len, inode, 1 /*visible */ );
821 if (retval) {
822 int err;
823 inode->i_nlink = 0;
824 DEC_DIR_INODE_NLINK(dir);
825 reiserfs_update_sd(&th, inode);
826 err = journal_end(&th, dir->i_sb, jbegin_count);
827 if (err)
828 retval = err;
829 iput(inode);
830 goto out_failed;
831 }
832 // the above add_entry did not update dir's stat data
833 reiserfs_update_sd(&th, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700835 d_instantiate(dentry, inode);
836 retval = journal_end(&th, dir->i_sb, jbegin_count);
837 out_failed:
838 if (locked)
839 reiserfs_write_unlock_xattrs(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 reiserfs_write_unlock(dir->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700841 return retval;
842}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844static inline int reiserfs_empty_dir(struct inode *inode)
845{
846 /* we can cheat because an old format dir cannot have
847 ** EMPTY_DIR_SIZE, and a new format dir cannot have
848 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
849 ** regardless of disk format version, the directory is empty.
850 */
851 if (inode->i_size != EMPTY_DIR_SIZE &&
852 inode->i_size != EMPTY_DIR_SIZE_V1) {
853 return 0;
854 }
855 return 1;
856}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700858static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
859{
860 int retval, err;
861 struct inode *inode;
862 struct reiserfs_transaction_handle th;
863 int jbegin_count;
864 INITIALIZE_PATH(path);
865 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700867 /* we will be doing 2 balancings and update 2 stat data, we change quotas
868 * of the owner of the directory and of the owner of the parent directory.
869 * The quota structure is possibly deleted only on last iput => outside
870 * of this transaction */
871 jbegin_count =
872 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
873 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700875 reiserfs_write_lock(dir->i_sb);
876 retval = journal_begin(&th, dir->i_sb, jbegin_count);
877 if (retval)
878 goto out_rmdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880 de.de_gen_number_bit_string = NULL;
881 if ((retval =
882 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
883 &path, &de)) == NAME_NOT_FOUND) {
884 retval = -ENOENT;
885 goto end_rmdir;
886 } else if (retval == IO_ERROR) {
887 retval = -EIO;
888 goto end_rmdir;
889 }
890
891 inode = dentry->d_inode;
892
893 reiserfs_update_inode_transaction(inode);
894 reiserfs_update_inode_transaction(dir);
895
896 if (de.de_objectid != inode->i_ino) {
897 // FIXME: compare key of an object and a key found in the
898 // entry
899 retval = -EIO;
900 goto end_rmdir;
901 }
902 if (!reiserfs_empty_dir(inode)) {
903 retval = -ENOTEMPTY;
904 goto end_rmdir;
905 }
906
907 /* cut entry from dir directory */
908 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
909 0 /*new file size - not used here */ );
910 if (retval < 0)
911 goto end_rmdir;
912
913 if (inode->i_nlink != 2 && inode->i_nlink != 1)
914 reiserfs_warning(inode->i_sb, "%s: empty directory has nlink "
915 "!= 2 (%d)", __FUNCTION__, inode->i_nlink);
916
917 inode->i_nlink = 0;
918 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
919 reiserfs_update_sd(&th, inode);
920
921 DEC_DIR_INODE_NLINK(dir)
922 dir->i_size -= (DEH_SIZE + de.de_entrylen);
923 reiserfs_update_sd(&th, dir);
924
925 /* prevent empty directory from getting lost */
926 add_save_link(&th, inode, 0 /* not truncate */ );
927
928 retval = journal_end(&th, dir->i_sb, jbegin_count);
929 reiserfs_check_path(&path);
930 out_rmdir:
931 reiserfs_write_unlock(dir->i_sb);
932 return retval;
933
934 end_rmdir:
935 /* we must release path, because we did not call
936 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
937 release path if operation was not complete */
938 pathrelse(&path);
939 err = journal_end(&th, dir->i_sb, jbegin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 reiserfs_write_unlock(dir->i_sb);
941 return err ? err : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
945{
946 int retval, err;
947 struct inode *inode;
948 struct reiserfs_dir_entry de;
949 INITIALIZE_PATH(path);
950 struct reiserfs_transaction_handle th;
951 int jbegin_count;
952 unsigned long savelink;
953
954 inode = dentry->d_inode;
955
956 /* in this transaction we can be doing at max two balancings and update
957 * two stat datas, we change quotas of the owner of the directory and of
958 * the owner of the parent directory. The quota structure is possibly
959 * deleted only on iput => outside of this transaction */
960 jbegin_count =
961 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
962 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
963
964 reiserfs_write_lock(dir->i_sb);
965 retval = journal_begin(&th, dir->i_sb, jbegin_count);
966 if (retval)
967 goto out_unlink;
968
969 de.de_gen_number_bit_string = NULL;
970 if ((retval =
971 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
972 &path, &de)) == NAME_NOT_FOUND) {
973 retval = -ENOENT;
974 goto end_unlink;
975 } else if (retval == IO_ERROR) {
976 retval = -EIO;
977 goto end_unlink;
978 }
979
980 reiserfs_update_inode_transaction(inode);
981 reiserfs_update_inode_transaction(dir);
982
983 if (de.de_objectid != inode->i_ino) {
984 // FIXME: compare key of an object and a key found in the
985 // entry
986 retval = -EIO;
987 goto end_unlink;
988 }
989
990 if (!inode->i_nlink) {
991 reiserfs_warning(inode->i_sb, "%s: deleting nonexistent file "
992 "(%s:%lu), %d", __FUNCTION__,
993 reiserfs_bdevname(inode->i_sb), inode->i_ino,
994 inode->i_nlink);
995 inode->i_nlink = 1;
996 }
997
998 inode->i_nlink--;
999
1000 /*
1001 * we schedule before doing the add_save_link call, save the link
1002 * count so we don't race
1003 */
1004 savelink = inode->i_nlink;
1005
1006 retval =
1007 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
1008 0);
1009 if (retval < 0) {
1010 inode->i_nlink++;
1011 goto end_unlink;
1012 }
1013 inode->i_ctime = CURRENT_TIME_SEC;
1014 reiserfs_update_sd(&th, inode);
1015
1016 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1017 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
1018 reiserfs_update_sd(&th, dir);
1019
1020 if (!savelink)
1021 /* prevent file from getting lost */
1022 add_save_link(&th, inode, 0 /* not truncate */ );
1023
1024 retval = journal_end(&th, dir->i_sb, jbegin_count);
1025 reiserfs_check_path(&path);
1026 reiserfs_write_unlock(dir->i_sb);
1027 return retval;
1028
1029 end_unlink:
1030 pathrelse(&path);
1031 err = journal_end(&th, dir->i_sb, jbegin_count);
1032 reiserfs_check_path(&path);
1033 if (err)
1034 retval = err;
1035 out_unlink:
1036 reiserfs_write_unlock(dir->i_sb);
1037 return retval;
1038}
1039
1040static int reiserfs_symlink(struct inode *parent_dir,
1041 struct dentry *dentry, const char *symname)
1042{
1043 int retval;
1044 struct inode *inode;
1045 char *name;
1046 int item_len;
1047 struct reiserfs_transaction_handle th;
1048 int mode = S_IFLNK | S_IRWXUGO;
1049 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1050 int jbegin_count =
1051 JOURNAL_PER_BALANCE_CNT * 3 +
1052 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1053 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1054
1055 if (!(inode = new_inode(parent_dir->i_sb))) {
1056 return -ENOMEM;
1057 }
1058 new_inode_init(inode, parent_dir, mode);
1059
1060 reiserfs_write_lock(parent_dir->i_sb);
1061 item_len = ROUND_UP(strlen(symname));
1062 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1063 retval = -ENAMETOOLONG;
1064 drop_new_inode(inode);
1065 goto out_failed;
1066 }
1067
Pekka Enbergd739b422006-02-01 03:06:43 -08001068 name = kmalloc(item_len, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001069 if (!name) {
1070 drop_new_inode(inode);
1071 retval = -ENOMEM;
1072 goto out_failed;
1073 }
1074 memcpy(name, symname, strlen(symname));
1075 padd_item(name, item_len, strlen(symname));
1076
1077 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1078
1079 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1080 if (retval) {
1081 drop_new_inode(inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001082 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001083 goto out_failed;
1084 }
1085
1086 retval =
1087 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1088 dentry, inode);
Pekka Enbergd739b422006-02-01 03:06:43 -08001089 kfree(name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001090 if (retval) { /* reiserfs_new_inode iputs for us */
1091 goto out_failed;
1092 }
1093
1094 reiserfs_update_inode_transaction(inode);
1095 reiserfs_update_inode_transaction(parent_dir);
1096
1097 inode->i_op = &reiserfs_symlink_inode_operations;
1098 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1099
1100 // must be sure this inode is written with this transaction
1101 //
1102 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1103
1104 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1105 dentry->d_name.len, inode, 1 /*visible */ );
1106 if (retval) {
1107 int err;
1108 inode->i_nlink--;
1109 reiserfs_update_sd(&th, inode);
1110 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1111 if (err)
1112 retval = err;
1113 iput(inode);
1114 goto out_failed;
1115 }
1116
1117 d_instantiate(dentry, inode);
1118 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1119 out_failed:
1120 reiserfs_write_unlock(parent_dir->i_sb);
1121 return retval;
1122}
1123
1124static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1125 struct dentry *dentry)
1126{
1127 int retval;
1128 struct inode *inode = old_dentry->d_inode;
1129 struct reiserfs_transaction_handle th;
1130 /* We need blocks for transaction + update of quotas for the owners of the directory */
1131 int jbegin_count =
1132 JOURNAL_PER_BALANCE_CNT * 3 +
1133 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1134
1135 reiserfs_write_lock(dir->i_sb);
1136 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1137 //FIXME: sd_nlink is 32 bit for new files
1138 reiserfs_write_unlock(dir->i_sb);
1139 return -EMLINK;
1140 }
1141 if (inode->i_nlink == 0) {
1142 reiserfs_write_unlock(dir->i_sb);
1143 return -ENOENT;
1144 }
1145
1146 /* inc before scheduling so reiserfs_unlink knows we are here */
1147 inode->i_nlink++;
1148
1149 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1150 if (retval) {
1151 inode->i_nlink--;
1152 reiserfs_write_unlock(dir->i_sb);
1153 return retval;
1154 }
1155
1156 /* create new entry */
1157 retval =
1158 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1159 dentry->d_name.len, inode, 1 /*visible */ );
1160
1161 reiserfs_update_inode_transaction(inode);
1162 reiserfs_update_inode_transaction(dir);
1163
1164 if (retval) {
1165 int err;
1166 inode->i_nlink--;
1167 err = journal_end(&th, dir->i_sb, jbegin_count);
1168 reiserfs_write_unlock(dir->i_sb);
1169 return err ? err : retval;
1170 }
1171
1172 inode->i_ctime = CURRENT_TIME_SEC;
1173 reiserfs_update_sd(&th, inode);
1174
1175 atomic_inc(&inode->i_count);
1176 d_instantiate(dentry, inode);
1177 retval = journal_end(&th, dir->i_sb, jbegin_count);
1178 reiserfs_write_unlock(dir->i_sb);
1179 return retval;
1180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182// de contains information pointing to an entry which
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183static int de_still_valid(const char *name, int len,
1184 struct reiserfs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186 struct reiserfs_dir_entry tmp = *de;
1187
1188 // recalculate pointer to name and name length
1189 set_de_name_and_namelen(&tmp);
1190 // FIXME: could check more
1191 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1192 return 0;
1193 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196static int entry_points_to_object(const char *name, int len,
1197 struct reiserfs_dir_entry *de,
1198 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001200 if (!de_still_valid(name, len, de))
1201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001203 if (inode) {
1204 if (!de_visible(de->de_deh + de->de_entry_num))
1205 reiserfs_panic(NULL,
1206 "vs-7042: entry_points_to_object: entry must be visible");
1207 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001210 /* this must be added hidden entry */
1211 if (de_visible(de->de_deh + de->de_entry_num))
1212 reiserfs_panic(NULL,
1213 "vs-7043: entry_points_to_object: entry must be visible");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/* sets key of objectid the entry has to point to */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1220 struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 /* JDM These operations are endian safe - both are le */
1223 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1224 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/*
1228 * process, that is going to call fix_nodes/do_balance must hold only
1229 * one path. If it holds 2 or more, it can get into endless waiting in
1230 * get_empty_nodes or its clones
1231 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001232static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1233 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001235 int retval;
1236 INITIALIZE_PATH(old_entry_path);
1237 INITIALIZE_PATH(new_entry_path);
1238 INITIALIZE_PATH(dot_dot_entry_path);
1239 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1240 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1241 struct inode *old_inode, *new_dentry_inode;
1242 struct reiserfs_transaction_handle th;
1243 int jbegin_count;
1244 umode_t old_inode_mode;
1245 unsigned long savelink = 1;
1246 struct timespec ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001248 /* three balancings: (1) old name removal, (2) new name insertion
1249 and (3) maybe "save" link insertion
1250 stat data updates: (1) old directory,
1251 (2) new directory and (3) maybe old object stat data (when it is
1252 directory) and (4) maybe stat data of object to which new entry
1253 pointed initially and (5) maybe block containing ".." of
1254 renamed directory
1255 quota updates: two parent directories */
1256 jbegin_count =
1257 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1258 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001260 old_inode = old_dentry->d_inode;
1261 new_dentry_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001263 // make sure, that oldname still exists and points to an object we
1264 // are going to rename
1265 old_de.de_gen_number_bit_string = NULL;
1266 reiserfs_write_lock(old_dir->i_sb);
1267 retval =
1268 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1269 old_dentry->d_name.len, &old_entry_path,
1270 &old_de);
1271 pathrelse(&old_entry_path);
1272 if (retval == IO_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 reiserfs_write_unlock(old_dir->i_sb);
1274 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001276
1277 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1278 reiserfs_write_unlock(old_dir->i_sb);
1279 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001281
1282 old_inode_mode = old_inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (S_ISDIR(old_inode_mode)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001284 // make sure, that directory being renamed has correct ".."
1285 // and that its new parent directory has not too many links
1286 // already
1287
1288 if (new_dentry_inode) {
1289 if (!reiserfs_empty_dir(new_dentry_inode)) {
1290 reiserfs_write_unlock(old_dir->i_sb);
1291 return -ENOTEMPTY;
1292 }
1293 }
1294
1295 /* directory is renamed, its parent directory will be changed,
1296 ** so find ".." entry
1297 */
1298 dot_dot_de.de_gen_number_bit_string = NULL;
1299 retval =
1300 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1301 &dot_dot_de);
1302 pathrelse(&dot_dot_entry_path);
1303 if (retval != NAME_FOUND) {
1304 reiserfs_write_unlock(old_dir->i_sb);
1305 return -EIO;
1306 }
1307
1308 /* inode number of .. must equal old_dir->i_ino */
1309 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1310 reiserfs_write_unlock(old_dir->i_sb);
1311 return -EIO;
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1316 if (retval) {
1317 reiserfs_write_unlock(old_dir->i_sb);
1318 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001321 /* add new entry (or find the existing one) */
1322 retval =
1323 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1324 new_dentry->d_name.len, old_inode, 0);
1325 if (retval == -EEXIST) {
1326 if (!new_dentry_inode) {
1327 reiserfs_panic(old_dir->i_sb,
1328 "vs-7050: new entry is found, new inode == 0\n");
1329 }
1330 } else if (retval) {
1331 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1332 reiserfs_write_unlock(old_dir->i_sb);
1333 return err ? err : retval;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336 reiserfs_update_inode_transaction(old_dir);
1337 reiserfs_update_inode_transaction(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 /* this makes it so an fsync on an open fd for the old name will
1340 ** commit the rename operation
1341 */
1342 reiserfs_update_inode_transaction(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001344 if (new_dentry_inode)
1345 reiserfs_update_inode_transaction(new_dentry_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001347 while (1) {
1348 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1349 if ((retval =
1350 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1351 &old_entry_path,
1352 &old_de)) != NAME_FOUND) {
1353 pathrelse(&old_entry_path);
1354 journal_end(&th, old_dir->i_sb, jbegin_count);
1355 reiserfs_write_unlock(old_dir->i_sb);
1356 return -EIO;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001359 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001361 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1362
1363 // look for new name by reiserfs_find_entry
1364 new_de.de_gen_number_bit_string = NULL;
1365 retval =
1366 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1367 new_dentry->d_name.len, &new_entry_path,
1368 &new_de);
1369 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1370 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1371 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1372 pathrelse(&new_entry_path);
1373 pathrelse(&old_entry_path);
1374 journal_end(&th, old_dir->i_sb, jbegin_count);
1375 reiserfs_write_unlock(old_dir->i_sb);
1376 return -EIO;
1377 }
1378
1379 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1380
1381 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1382
1383 if (S_ISDIR(old_inode->i_mode)) {
1384 if ((retval =
1385 search_by_entry_key(new_dir->i_sb,
1386 &dot_dot_de.de_entry_key,
1387 &dot_dot_entry_path,
1388 &dot_dot_de)) != NAME_FOUND) {
1389 pathrelse(&dot_dot_entry_path);
1390 pathrelse(&new_entry_path);
1391 pathrelse(&old_entry_path);
1392 journal_end(&th, old_dir->i_sb, jbegin_count);
1393 reiserfs_write_unlock(old_dir->i_sb);
1394 return -EIO;
1395 }
1396 copy_item_head(&dot_dot_ih,
1397 get_ih(&dot_dot_entry_path));
1398 // node containing ".." gets into transaction
1399 reiserfs_prepare_for_journal(old_inode->i_sb,
1400 dot_dot_de.de_bh, 1);
1401 }
1402 /* we should check seals here, not do
1403 this stuff, yes? Then, having
1404 gathered everything into RAM we
1405 should lock the buffers, yes? -Hans */
1406 /* probably. our rename needs to hold more
1407 ** than one path at once. The seals would
1408 ** have to be written to deal with multi-path
1409 ** issues -chris
1410 */
1411 /* sanity checking before doing the rename - avoid races many
1412 ** of the above checks could have scheduled. We have to be
1413 ** sure our items haven't been shifted by another process.
1414 */
1415 if (item_moved(&new_entry_ih, &new_entry_path) ||
1416 !entry_points_to_object(new_dentry->d_name.name,
1417 new_dentry->d_name.len,
1418 &new_de, new_dentry_inode) ||
1419 item_moved(&old_entry_ih, &old_entry_path) ||
1420 !entry_points_to_object(old_dentry->d_name.name,
1421 old_dentry->d_name.len,
1422 &old_de, old_inode)) {
1423 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1424 new_de.de_bh);
1425 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1426 old_de.de_bh);
1427 if (S_ISDIR(old_inode_mode))
1428 reiserfs_restore_prepared_buffer(old_inode->
1429 i_sb,
1430 dot_dot_de.
1431 de_bh);
1432 continue;
1433 }
1434 if (S_ISDIR(old_inode_mode)) {
1435 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1436 !entry_points_to_object("..", 2, &dot_dot_de,
1437 old_dir)) {
1438 reiserfs_restore_prepared_buffer(old_inode->
1439 i_sb,
1440 old_de.de_bh);
1441 reiserfs_restore_prepared_buffer(old_inode->
1442 i_sb,
1443 new_de.de_bh);
1444 reiserfs_restore_prepared_buffer(old_inode->
1445 i_sb,
1446 dot_dot_de.
1447 de_bh);
1448 continue;
1449 }
1450 }
1451
1452 RFALSE(S_ISDIR(old_inode_mode) &&
1453 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1454
1455 break;
1456 }
1457
1458 /* ok, all the changes can be done in one fell swoop when we
1459 have claimed all the buffers needed. */
1460
1461 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1462 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1463 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1464
1465 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1466 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1467 ctime = CURRENT_TIME_SEC;
1468 old_dir->i_ctime = old_dir->i_mtime = ctime;
1469 new_dir->i_ctime = new_dir->i_mtime = ctime;
1470 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1471 renamed object */
1472 old_inode->i_ctime = ctime;
1473
1474 if (new_dentry_inode) {
1475 // adjust link number of the victim
1476 if (S_ISDIR(new_dentry_inode->i_mode)) {
1477 new_dentry_inode->i_nlink = 0;
1478 } else {
1479 new_dentry_inode->i_nlink--;
1480 }
1481 new_dentry_inode->i_ctime = ctime;
1482 savelink = new_dentry_inode->i_nlink;
1483 }
1484
1485 if (S_ISDIR(old_inode_mode)) {
1486 // adjust ".." of renamed directory
1487 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1488 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1489
1490 if (!new_dentry_inode)
1491 /* there (in new_dir) was no directory, so it got new link
1492 (".." of renamed directory) */
1493 INC_DIR_INODE_NLINK(new_dir);
1494
1495 /* old directory lost one link - ".. " of renamed directory */
1496 DEC_DIR_INODE_NLINK(old_dir);
1497 }
1498 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1499 pathrelse(&new_entry_path);
1500 pathrelse(&dot_dot_entry_path);
1501
1502 // FIXME: this reiserfs_cut_from_item's return value may screw up
1503 // anybody, but it will panic if will not be able to find the
1504 // entry. This needs one more clean up
1505 if (reiserfs_cut_from_item
1506 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1507 0) < 0)
1508 reiserfs_warning(old_dir->i_sb,
1509 "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1510
1511 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1512
1513 reiserfs_update_sd(&th, old_dir);
1514 reiserfs_update_sd(&th, new_dir);
1515 reiserfs_update_sd(&th, old_inode);
1516
1517 if (new_dentry_inode) {
1518 if (savelink == 0)
1519 add_save_link(&th, new_dentry_inode,
1520 0 /* not truncate */ );
1521 reiserfs_update_sd(&th, new_dentry_inode);
1522 }
1523
1524 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1525 reiserfs_write_unlock(old_dir->i_sb);
1526 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529/*
1530 * directories can handle most operations...
1531 */
1532struct inode_operations reiserfs_dir_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001533 //&reiserfs_dir_operations, /* default_file_ops */
1534 .create = reiserfs_create,
1535 .lookup = reiserfs_lookup,
1536 .link = reiserfs_link,
1537 .unlink = reiserfs_unlink,
1538 .symlink = reiserfs_symlink,
1539 .mkdir = reiserfs_mkdir,
1540 .rmdir = reiserfs_rmdir,
1541 .mknod = reiserfs_mknod,
1542 .rename = reiserfs_rename,
1543 .setattr = reiserfs_setattr,
1544 .setxattr = reiserfs_setxattr,
1545 .getxattr = reiserfs_getxattr,
1546 .listxattr = reiserfs_listxattr,
1547 .removexattr = reiserfs_removexattr,
1548 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549};
1550
1551/*
1552 * symlink operations.. same as page_symlink_inode_operations, with xattr
1553 * stuff added
1554 */
1555struct inode_operations reiserfs_symlink_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001556 .readlink = generic_readlink,
1557 .follow_link = page_follow_link_light,
1558 .put_link = page_put_link,
1559 .setattr = reiserfs_setattr,
1560 .setxattr = reiserfs_setxattr,
1561 .getxattr = reiserfs_getxattr,
1562 .listxattr = reiserfs_listxattr,
1563 .removexattr = reiserfs_removexattr,
1564 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566};
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/*
1569 * special file operations.. just xattr/acl stuff
1570 */
1571struct inode_operations reiserfs_special_inode_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001572 .setattr = reiserfs_setattr,
1573 .setxattr = reiserfs_setxattr,
1574 .getxattr = reiserfs_getxattr,
1575 .listxattr = reiserfs_listxattr,
1576 .removexattr = reiserfs_removexattr,
1577 .permission = reiserfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579};